Please start any new threads on our new site at We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Sampling method & counters
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Junior Sqler
Starting Member

18 Posts

Posted - 03/07/2013 :  07:48:18  Show Profile  Reply with Quote
Hello !

I am trying to create a sampling procedure for invoice checking for a specific period of time.
Data: of different suppliers
2.Invoices for each of above suppliers

Target:For each supplier check every 2 invoices.
If find an invoice with discrepancy i check every invoice after it till I find 2 invoices in row ok.
Then restart sampling.

This invoice ok? Previous Invoice ok? status

no NULL Initial
no no Skip
Yes no sampling
no Yes 1st Check
no no 2nd Check
no no skip
no no sampling

How could I adjust my query if i want to sample every n invoices instead of every 2? (the rest will remain the same)

I understand that a counter for 'skip' status is needed but i'm not sure how i could do that.

My query (it works) is the following:

[ranking] int,
newrowNumb int,

declare @SuppliersCount as int
set @SuppliersCount = (select COUNT(distinct [bUSINESSPARTNERCODE]) from #q2)

declare @countsupplier int
SET @countsupplier=1

while @countsupplier<=@SuppliersCount

declare @RowsCount INT
set @RowsCount = (select MAX(newrownumb) from #q2 where BusinessPartnerCode = (select distinct businesspartnercode from #q2 where ranking = @countsupplier))

declare @countrow INT
SET @countrow=1
WHILE @countrow <= @RowsCount

insert into #temp(ranking, newrownumb, [status])
select A.ranking,A.newrowNumb,
case when a.newrowNumb = 1 THEN 'Sampling'
when a.newrowNumb=2 and a.PreviousDiscrepancies='yes' THEN '1st Check'
when a.newrowNumb=2 and a.PreviousDiscrepancies='no' THEN 'Skip'
when b.[status]= '1st Check' THEN '2nd Check'
when b.[status]='2nd Check' and a.PreviousDiscrepancies='yes' THEN '1st Check'
when b.[status]='2nd Check' and a.PreviousDiscrepancies='no' THEN 'skip'
when b.[status]='skip' THEN 'sampling'
when b.[status]='sampling' and a.PreviousDiscrepancies='no' THEN 'skip'
when b.[status]='sampling' and a.PreviousDiscrepancies='yes ' THEN '1st Check'
else 'unknown' end as status
from #q2 a
LEFT join #temp b on b.newrowNumb = a.newrowNumb - 1 and a.ranking=b.ranking
where a.ranking = @countsupplier and a.newrowNumb = @countrow

set @countrow=@countrow +1
SET @countsupplier= @countsupplier +1

Drop table #FinalMethod2
select q.*,t.[status]
into #FinalMethod2
from #q2 q
left join #temp t on t.ranking = q.ranking and t.newrowNumb = q.newrowNumb

Thank you in advance for your help!!

Junior Sqler
Starting Member

18 Posts

Posted - 03/08/2013 :  03:48:54  Show Profile  Reply with Quote
I guess that my question was complicated/not clear... :(
That is why I will be JUNIOR Sqler forever...
Go to Top of Page

Flowing Fount of Yak Knowledge

2242 Posts

Posted - 03/08/2013 :  04:01:46  Show Profile  Reply with Quote
Post as per guidelines in the link.. Then it is easy to provide solution

Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000