SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Sampling method & counters
 New Topic  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: 1.group 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:

dROP TABLE #TEMP
CREATE TABLE #TEMP(
[ranking] int,
newrowNumb int,
[STATUS] VARCHAR(20)

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


declare @countsupplier int
SET @countsupplier=1

while @countsupplier<=@SuppliersCount
BEGIN

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
BEGIN

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
END
SET @countsupplier= @countsupplier +1
end


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

bandi
Flowing Fount of Yak Knowledge

India
2206 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
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


--
Chandu
Go to Top of Page
  Previous Topic Topic Next Topic  
 New 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.08 seconds. Powered By: Snitz Forums 2000