| Author |
Topic |
|
gagani
Posting Yak Master
112 Posts |
Posted - 2011-12-07 : 08:27:20
|
| table1:orderid value result101 60 passedtable2:orderid value result101 60 passed101 0 nonetable1 and table2 has same number of columns. table1 has complete data. table2 has less number of rows compared to table1. but i want to extract the rows irrespective of data in table2 or not for orderids. so i did left join. the problem is there are some duplicate orderids in table2. my requirement is i have to join two tables and if there are duplicates for any particular orderid in table2, then i should take the value when result='passed'.table1 should not be affected. result='passed' should be applied only for table2 when there are duplicates in tabl2 for a particular orderid |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-07 : 08:32:12
|
do you mean this?update t2set t2.value=t1.valuefrom table2 t2inner join table1 t1on t1.orderid = t2.orderidand t2.result = 'passed' ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
gagani
Posting Yak Master
112 Posts |
Posted - 2011-12-07 : 09:03:48
|
| consider table1:orderid value result101 60 passed102 40 none 103 80 nonetable2:orderid value result101 60 passed101 0 none102 30 passed102 0 noneI performed this query:select t1.orderid, t1.value, t2.value from table1 t1 left outer join table2 t2on t1.orderid = t2.orderidthe output is like this:t1.orderid t1.value t2.value101 60 60101 60 0102 40 30102 40 0103 80 0but i want the output like this:t1.orderid t1.value t2.value101 60 60102 40 30103 80 null |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-07 : 09:05:09
|
| [code]select t1.orderid, t1.value, t2.value from table1 t1 left outer join table2 t2on t1.orderid = t2.orderidand t2.result='passed'[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
gagani
Posting Yak Master
112 Posts |
Posted - 2011-12-07 : 09:45:20
|
| that is missing out the row103 80 nullof table1.I want every row of table1 irrespective of any status. and if there are duplicates in table2 for a particular orderid, then it should consider the one with status= passed for that particular orderid in table2.if there are no duplicates in table2, then it should give the value with whatever statusconsider table1:orderid value result101 60 passed102 40 none 103 80 none104 40 nonetable2:orderid value result101 60 passed101 0 none102 30 passed102 0 none103 55 nonethe output should be101 60 60102 40 30103 80 null104 40 nonefrom the above, the duplicates are there for orderids 101 and 102 only. table2 does not have orderid 104, but it should come in the output as table1 has it. and for orderid 103, there is no duplicate in table2, so it should give the value of it. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-07 : 10:00:40
|
its working fine for me. you're probably not using it as suggestedsee belowdeclare @table1 table(orderid int,value int,result varchar(100))insert @table1select 101, 60, 'passed' union allselect 102, 40 ,'none' union allselect 103, 80, 'none' union allselect 104, 40, 'none'declare @table2 table(orderid int,value int,result varchar(100))insert @table2select 101, 60, 'passed' union allselect 101, 0, 'none' union allselect 102, 30, 'passed'union allselect 102, 0, 'none' union allselect 103, 55, 'none'select t1.orderid, t1.value, t2.value from @table1 t1 left outer join @table2 t2on t1.orderid = t2.orderidand t2.result='passed'output-----------------------------------------orderid value value101 60 60102 40 30103 80 NULL104 40 NULL ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
gagani
Posting Yak Master
112 Posts |
Posted - 2011-12-07 : 11:10:25
|
| sorry, it's my fault. I didn't give the output what i wanted correctly.could you please consider the given tables again,the output should be101 60 60102 40 30103 80 55104 40 nullfrom the above, for the orderid 103 it is taking the value from table2. the condition should apply only when there are duplicates in table2. if there are no duplicates in table2, it should retrieve the value from table2 irrespective of the status for the given orderid. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-07 : 11:17:35
|
| [code]select t1.orderid, t1.value, COALESCE(MAX(CASE WHEN result='passed' THEN t2.value END),MAX(CASE WHEN result<>'passed' THEN t2.value END)) AS t2valuefrom @table1 t1 left outer join @table2 t2on t1.orderid = t2.orderidgroup by t1.orderid, t1.value[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
gagani
Posting Yak Master
112 Posts |
Posted - 2011-12-08 : 08:10:38
|
| thank you for your query. I am trying to perform the same query for the condition datetimecolumn = max(datetimecolumn) instead of result='passed'. But i am confused as it would lead to one max function inside the case and the other outside. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-08 : 08:49:42
|
| you cant use one max function inside other. which max(datetime) you're trying to look for?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Jeffreys
Starting Member
45 Posts |
Posted - 2011-12-08 : 09:47:11
|
| If I understand your question correctly, this may work. It seems you want to take the union of two tables. If duplicate orderid's exist you want to take the orderid with a 'passed' result. This should work assuming the result can be either 'passed' or 'failed'. If the failed value for result is a null, change the sort order to asc. SELECT orderId ,Value ,ResultFROM ( select RowN = ROW_NUMBER() OVER (PARTITION BY orderID ORDER BY Result desc) ,orderId ,Value ,Result FROM ( SELECT orderId ,Value ,Result FROM table1 t1 UNION ALL SELECT orderId ,Value ,Result FROM table1 t2 )ivw)ivw2WHERE RowN = 1 |
 |
|
|
gagani
Posting Yak Master
112 Posts |
Posted - 2011-12-09 : 06:00:41
|
| If there are duplicates in second table, then it should take the value when datetime column is maximum.Earlier we considered the condition result=passed , user do not want that condition now. he only wants based on the datetime column is maximum.consider table1:orderid value datetimecolumn101 60 23/11/2011102 40 24/11/2011103 80 25/11/2011104 40 25/11/2011table2:orderid value result101 60 23/11/2011101 0 22/11/2011102 30 26/11/2011102 0 25/11/2011103 55 23/11/2011the output should be101 60 60102 40 30103 80 55104 40 nullthe duplicates in second table are filtered based on maximum of datetime column. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-09 : 06:11:10
|
ok. see belowSELECT t1.orderid,t1.value,t4.valueFROM table1 t1LEFT JOIN (SELECT t2.* FROM table2 t2 INNER JOIN (SELECT orderid,MAX(result) AS MaxDate FROM table2 GROUP BY orderid)t3 ON t3.orderid = t2.orderid AND t3.MaxDate = t2.result )t4ON t4.orderid = t1.orderid ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|