Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
rohit04413
Yak Posting Veteran
72 Posts |
Posted - 2011-03-15 : 05:57:19
|
I have 2 tables and sample data is as follows:1.) tblPO:pono = 1001firstsr=5000lastsr=5050 qty = 51 pono = 1002firstsr = 5100lastsr = 5200qty = 101pono = 1003firstsr = 5201lastsr = 5250qty = 502.) tblAS (sr always belongs to anyone of the above PO and falls between firstsr and lastsr)sr = 5000run = 1result= failsr = 5000run = 2 result = passsr = 5001run = 1result = passsr=5110run = 1result = passsr = 5112run = 1result = failsr = 5112run = 2result = pass-------------------------Now i want a query which should display the following result:First column should be pono.Second column should be qty corresponding to pono .Third column should be count of serial where result = pass with run = max for that serial.Fourth Column should be count of serial where result = fail with run = max for that serialFifth column should be the pass percentage for each pono.e.g. For above data the result should be as following:pono, qty, passed, failed, %age1001, 51, 2, 0, 2/511002, 101, 1, 1, 1/101Please Help |
|
theboyholty
Posting Yak Master
226 Posts |
Posted - 2011-03-16 : 12:29:00
|
[code]--CREATE TABLE tblPO (--pono int,--firstsr INT,--lastsr INT,--qty INT)--INSERT INTO tblPO (pono,firstsr,lastsr,qty) --SELECT 1001,5000,5050,51 UNION ALL--SELECT 1002,5100,5200,101 UNION ALL--SELECT 1003,5201,5250,50 UNION ALL--CREATE TABLE tblAS (--sr INT,--run INT,--result VARCHAR(5))--INSERT INTO tblAS (sr,run,result)--SELECT 5000,1,'fail' UNION ALL--SELECT 5000,2,'pass' UNION ALL--SELECT 5001,1,'pass' UNION ALL--SELECT 5110,1,'pass' UNION ALL--SELECT 5112,1,'fail' UNION ALL--SELECT 5112,2,'pass'--SELECT * FROM tblPO--SELECT * FROM tblASSELECT pono ,qty ,SUM(CASE WHEN result = 'pass' THEN 1 ELSE 0 END) AS Pass ,SUM(CASE WHEN result = 'fail' THEN 1 ELSE 0 END) AS Fail ,(100/COUNT(*)) * SUM(CASE WHEN result = 'pass' THEN 1 ELSE 0 END) as PercFROM tblPO pLEFT JOIN tblAS a ON a.sr>=p.FirstSR AND a.sr<=p.LastSRGROUP BY pono,qtyORDER BY pono,qty[/code]Hi. First of all, my results don't quite match yours. That may be because of the definition of 'between'. I've done it inclusive but have just realised that this may cause some replication. You can ammend the query to fix this, its the >= and <= on the join that does it.I've included all my create tables code, but its just the bit at the bottom you'd need.Also, you're example of what you expect in your %age column 2/51 is a little odd I think. I've put in an actual percentage but again, this is easily fixable.Have a look and let us know what you think.---------------------------------------------------------------------------------http://www.mannyroadend.co.uk The official unofficial website of Bury Football Club |
 |
|
rohit04413
Yak Posting Veteran
72 Posts |
Posted - 2011-03-24 : 00:45:01
|
Thanks for your reply but i don't need to create table. I am facing problem only while generating report from the existing data. I want only those records for which "run" is maximum. please think again on this. As shown in the sample data provided by me.for sr=5112, there are 2 records in tblas. from these 2 records, i want to choose only one with higher value of "run"quote: Originally posted by theboyholty
--CREATE TABLE tblPO (--pono int,--firstsr INT,--lastsr INT,--qty INT)--INSERT INTO tblPO (pono,firstsr,lastsr,qty) --SELECT 1001,5000,5050,51 UNION ALL--SELECT 1002,5100,5200,101 UNION ALL--SELECT 1003,5201,5250,50 UNION ALL--CREATE TABLE tblAS (--sr INT,--run INT,--result VARCHAR(5))--INSERT INTO tblAS (sr,run,result)--SELECT 5000,1,'fail' UNION ALL--SELECT 5000,2,'pass' UNION ALL--SELECT 5001,1,'pass' UNION ALL--SELECT 5110,1,'pass' UNION ALL--SELECT 5112,1,'fail' UNION ALL--SELECT 5112,2,'pass'--SELECT * FROM tblPO--SELECT * FROM tblASSELECT pono ,qty ,SUM(CASE WHEN result = 'pass' THEN 1 ELSE 0 END) AS Pass ,SUM(CASE WHEN result = 'fail' THEN 1 ELSE 0 END) AS Fail ,(100/COUNT(*)) * SUM(CASE WHEN result = 'pass' THEN 1 ELSE 0 END) as PercFROM tblPO pLEFT JOIN tblAS a ON a.sr>=p.FirstSR AND a.sr<=p.LastSRGROUP BY pono,qtyORDER BY pono,qty Hi. First of all, my results don't quite match yours. That may be because of the definition of 'between'. I've done it inclusive but have just realised that this may cause some replication. You can ammend the query to fix this, its the >= and <= on the join that does it.I've included all my create tables code, but its just the bit at the bottom you'd need.Also, you're example of what you expect in your %age column 2/51 is a little odd I think. I've put in an actual percentage but again, this is easily fixable.Have a look and let us know what you think.---------------------------------------------------------------------------------http://www.mannyroadend.co.uk The official unofficial website of Bury Football Club
|
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-03-24 : 00:58:19
|
quote: Originally posted by rohit04413 Thanks for your reply but i don't need to create table. I am facing problem only while generating report from the existing data. I want only those records for which "run" is maximum. please think again on this.
quote: Originally posted by theboyholtyHi. First of all, my results don't quite match yours. That may be because of the definition of 'between'. I've done it inclusive but have just realised that this may cause some replication. You can ammend the query to fix this, its the >= and <= on the join that does it.I've included all my create tables code, but its just the bit at the bottom you'd need.
KH[spoiler]Time is always against us[/spoiler] |
 |
|
rohit04413
Yak Posting Veteran
72 Posts |
Posted - 2011-03-24 : 01:11:37
|
But the provided query doesn't mention "run" anywhere.As i have specified earlier that where "sr" has same value in multiple records, only those "result" should be counted where "run" is maximum. Please help !!!quote: Originally posted by khtan
quote: Originally posted by rohit04413 Thanks for your reply but i don't need to create table. I am facing problem only while generating report from the existing data. I want only those records for which "run" is maximum. please think again on this.
quote: Originally posted by theboyholtyHi. First of all, my results don't quite match yours. That may be because of the definition of 'between'. I've done it inclusive but have just realised that this may cause some replication. You can ammend the query to fix this, its the >= and <= on the join that does it.I've included all my create tables code, but its just the bit at the bottom you'd need.
KH[spoiler]Time is always against us[/spoiler] |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-03-24 : 01:28:52
|
starting from the SELECT . . . KH[spoiler]Time is always against us[/spoiler] |
 |
|
rohit04413
Yak Posting Veteran
72 Posts |
Posted - 2011-03-24 : 02:06:44
|
Dear Sir,let me take an example, since there are 2 records with sr=5112,which of the records the given query will consider? result=fail or result=pass?thats why i am insisting that i want only those results where run is max.quote: Originally posted by khtan starting from the SELECT . . . KH[spoiler]Time is always against us[/spoiler]
|
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-03-24 : 02:31:20
|
[code]; with tblASmax as( select sr, run, result from ( select *, row_no = row_number() over (partition by sr order by run desc) from tblAS ) m where m.row_no = 1)select po.pono, po.qty, passed = count(case when result = 'pass' then result end), failed = count(case when result = 'fail' then result end), [%age] = count(case when result = 'pass' then result end) * 100.0 / po.qtyfrom tblPO po inner join tblASmax rs on po.firstsr <= rs.sr and po.lastsr >= rs.srgroup by po.pono, po.qty[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
rohit04413
Yak Posting Veteran
72 Posts |
Posted - 2011-03-24 : 03:59:26
|
Recursive common table expression 'tblAS' does not contain a top-level UNION ALL operator.I am new to sql, plz see if there is any syntax error...quote: Originally posted by khtan
; with tblASmax as( select sr, run, result from ( select *, row_no = row_number() over (partition by sr order by run desc) from tblAS ) m where m.row_no = 1)select po.pono, po.qty, passed = count(case when result = 'pass' then result end), failed = count(case when result = 'fail' then result end), [%age] = count(case when result = 'pass' then result end) * 100.0 / po.qtyfrom tblPO po inner join tblASmax rs on po.firstsr <= rs.sr and po.lastsr >= rs.srgroup by po.pono, po.qty KH[spoiler]Time is always against us[/spoiler]
|
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-03-24 : 05:12:42
|
post the exact query that you used KH[spoiler]Time is always against us[/spoiler] |
 |
|
rohit04413
Yak Posting Veteran
72 Posts |
Posted - 2011-03-24 : 06:52:09
|
I am using following query:; with tblAS as( select serialnumber, runno, result from (select *, row_no = row_number() over (partition by serialnumber order by runno desc) from tblAS) m where m.row_no = 1)select tblpo.productiono, tblpo.qty, passed = count(case when result = 'pass' then result end), failed = count(case when result = 'fail' then result end), [%age] = count(case when result = 'pass' then result end) * 100.0 / po.qtyfrom tblPO po inner join tblAS rs on po.firstsr <= rs.sr and po.lastsr >= rs.srgroup by po.pono, po.qtyquote: Originally posted by khtan post the exact query that you used KH[spoiler]Time is always against us[/spoiler]
|
 |
|
rohit04413
Yak Posting Veteran
72 Posts |
Posted - 2011-03-24 : 07:03:56
|
Is it right? quote: Originally posted by khtan
; with tblASmax as( select sr, run, result from ( select *, row_no = row_number() over (partition by sr order by run desc) from tblAS ) m where m.row_no = 1)select po.pono, po.qty, passed = count(case when result = 'pass' then result end), failed = count(case when result = 'fail' then result end), [%age] = count(case when result = 'pass' then result end) * 100.0 / po.qtyfrom tblPO po inner join tblASmax rs on po.firstsr <= rs.sr and po.lastsr >= rs.srgroup by po.pono, po.qty KH[spoiler]Time is always against us[/spoiler]
|
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-03-24 : 07:25:10
|
yes. Use the query i posted. KH[spoiler]Time is always against us[/spoiler] |
 |
|
rohit04413
Yak Posting Veteran
72 Posts |
Posted - 2011-03-28 : 01:04:42
|
Hello Sir,I am not getting the desired results from this query.Will you please help me understanding the highlighted code below.quote: Originally posted by rohit04413 Is it right? quote: Originally posted by khtan
; with tblASmax as( select sr, run, result from ( select *, row_no = row_number() over (partition by sr order by run desc) from tblAS ) m where m.row_no = 1)select po.pono, po.qty, passed = count(case when result = 'pass' then result end), failed = count(case when result = 'fail' then result end), [%age] = count(case when result = 'pass' then result end) * 100.0 / po.qtyfrom tblPO po inner join tblASmax rs on po.firstsr <= rs.sr and po.lastsr >= rs.srgroup by po.pono, po.qty KH[spoiler]Time is always against us[/spoiler]
|
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-03-28 : 02:31:45
|
tblASmax is the Common Table Expressions. It gives the latest result (pass or fail) based on run no for each sr.the row_number() will return a sequence no for each sr starting from the highest run as 1 and increment there after. So filter for row_no = 1 will give you the latest run no for each srbased on the sample data provided, the result of the query i posted pono qty passed failed %age ----------- ----------- ----------- ----------- ---------------------------- 1001 51 2 0 3.9215686274501002 101 2 0 1.980198019801(2 row(s) affected) KH[spoiler]Time is always against us[/spoiler] |
 |
|
rohit04413
Yak Posting Veteran
72 Posts |
Posted - 2011-03-28 : 03:30:44
|
Thank You so much.A little more help please.I am adding 1 more parameter into the following query "process".I want results in grouping of process, first process='A' and process='B'. results should be in the same output with different column as process.quote: with tblASmax as( select sr, run, result from ( select *, row_no = row_number() over (partition by sr order by run desc) from tblAS where process = 'fpt' ) m where m.row_no = 1)select po.pono, po.qty, passed = count(case when result = 'pass' then result end), failed = count(case when result = 'fail' then result end), [%age] = count(case when result = 'pass' then result end) * 100.0 / po.qtyfrom tblPO po inner join tblASmax rs on po.firstsr <= rs.sr and po.lastsr >= rs.srgroup by po.pono, po.qty
|
 |
|
rohit04413
Yak Posting Veteran
72 Posts |
Posted - 2011-03-28 : 03:32:40
|
For example:result should be like:quote: Originally posted by khtan tblASmax is the Common Table Expressions. It gives the latest result (pass or fail) based on run no for each sr.the row_number() will return a sequence no for each sr starting from the highest run as 1 and increment there after. So filter for row_no = 1 will give you the latest run no for each srbased on the sample data provided, the result of the query i posted pono qty passed-A passed-B failed %age ----------- ----------- ----------- ----------- ---------------------------- 1001 51 2 0 3.9215686274501002 101 2 0 1.980198019801(2 row(s) affected) KH[spoiler]Time is always against us[/spoiler]
|
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-03-28 : 03:56:40
|
just add another condition to the case when . .passed_A = count(case when process = 'Process A' and result = 'pass' then result end),passed_B = count(case when process = 'Process B' and result = 'pass' then result end), KH[spoiler]Time is always against us[/spoiler] |
 |
|
rohit04413
Yak Posting Veteran
72 Posts |
Posted - 2011-03-28 : 04:22:03
|
I think there is something wrong if i do like this.because records are already selected by ( select sr, run, result from ( select *, row_no = row_number() over (partition by sr order by run desc) from tblAS ) m where m.row_no = 1)Now the above statement will output data with run =Max irrespective of the process. This data will then be used by the following query to output the final result.select po.pono, po.qty, passed = count(case when result = 'pass' then result end), failed = count(case when result = 'fail' then result end), [%age] = count(case when result = 'pass' then result end) * 100.0 / po.qtyfrom tblPO po inner join tblASmax rs on po.firstsr <= rs.sr and po.lastsr >= rs.srgroup by po.pono, po.qtyWhat i am trying to say is that condition for process should be applied in the first query itself where it extracts the Max run from the database.quote: Originally posted by khtan just add another condition to the case when . .passed_A = count(case when process = 'Process A' and result = 'pass' then result end),passed_B = count(case when process = 'Process B' and result = 'pass' then result end), KH[spoiler]Time is always against us[/spoiler]
|
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-03-28 : 04:37:30
|
yes. you have to include the process column in CTE tblASmax KH[spoiler]Time is always against us[/spoiler] |
 |
|
rohit04413
Yak Posting Veteran
72 Posts |
Posted - 2011-03-28 : 04:59:25
|
OK.This is what i was asking earlier that how to add 2 conditions, process='A' and another process = 'B' simultaneously in tblAsmax so that results are displayed together.quote: Originally posted by khtan yes. you have to include the process column in CTE tblASmax KH[spoiler]Time is always against us[/spoiler]
|
 |
|
Next Page
|
|
|
|
|