| Author |
Topic |
|
saranchandru
Starting Member
4 Posts |
Posted - 2011-05-13 : 03:32:03
|
| Dear Members, The table structure as followsNAME gcode rcodename1 CO 45name1 CO 16name1 PR 1name1 PR 2name2 CO 45name2 CO 16name2 PR 1name3 CO 45name3 CO 16select distinct [NAME]from table1 t1 where( (t1.gcode='CO' and t1.rcode='45') OR (t1.gcode='CO' and t1.rcode='16'))the above query will give the resultname1name2name3bcos all names are having CO-45, CO-16If you see name 3 has exactly 2 rows in table2 with CO-45 and CO-16How to write a SQL to get the result as ONLY name3that means I want the names which are having exaclty CO-45 and CO-16Thanks & RegardsSaravanan Chandru |
|
|
mrashish77
Starting Member
4 Posts |
Posted - 2011-05-13 : 03:57:37
|
| you can add one condition asselect distinct [NAME]from table1 t1 where( (t1.gcode='CO' and t1.rcode='45') OR (t1.gcode='CO' and t1.rcode='16'))and NAME Not in ( select distinct [NAME] from table1 t2 where (t2.gcode<>'CO' and t2.rcode<>'45') OR (t2.gcode<>'CO' and t2.rcode<>'16')) |
 |
|
|
mrashish77
Starting Member
4 Posts |
Posted - 2011-05-13 : 03:59:43
|
you can even igonre your where condition |
 |
|
|
saranchandru
Starting Member
4 Posts |
Posted - 2011-05-13 : 04:21:55
|
| Thanks for your immediate reply. Problem is NOT IN and <> is taking too long. Is there any way write the query?Thanks & RegardsSaravanan Chandru |
 |
|
|
mrashish77
Starting Member
4 Posts |
Posted - 2011-05-13 : 05:39:51
|
| Also check below queryselect [NAME],Flagfrom( select [NAME], sum(case when (t1.gcode='CO' and t1.rcode='45') OR (t1.gcode='CO' and t1.rcode='16') then 100 else 1 end) as Flag from table1 t1 group by Name) awhere Flag % 100 = 0 |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2011-05-13 : 06:05:33
|
[code]-- From Peso I have learned this:declare @Sample table(Name varchar(255),GCode varchar(255),RCode varchar(255))insert @Sample(Name, GCode, RCode)select 'name1', 'CO', '45' union allselect 'name1', 'CO', '16' union allselect 'name1', 'PR', '1' union allselect 'name1', 'PR', '2' union allselect 'name2', 'CO', '45' union allselect 'name2', 'CO', '16' union allselect 'name2', 'PR', '1' union allselect 'name3', 'CO', '45' union allselect 'name3', 'CO', '16'select [Name]from @Sample t1 --where--( (t1.gcode='CO' and t1.rcode='45') OR (t1.gcode='CO' and t1.rcode='16'))group by [Name]having min(case when (t1.gcode='CO' and t1.rcode='45') OR (t1.gcode='CO' and t1.rcode='16') then 1 else 0 end)=1[/code] No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
saranchandru
Starting Member
4 Posts |
Posted - 2011-05-13 : 07:59:14
|
| Great Idea's. Thanks. Its working fine for the listed case. But If I add one more row name4, CO, 45That is also coming in the result set. At that time also I expect the result set to be "name3" only. Can you guide me please. Thanks in advance.Thanks & RegardsSaravanan Chandru |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2011-05-13 : 08:11:25
|
Should be this:select [Name]from @Sample t1 group by [Name]having sum(case when (t1.gcode='CO' and t1.rcode='45') then 1 when (t1.gcode='CO' and t1.rcode='16') then 1 else -1 end)=2 No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
saranchandru
Starting Member
4 Posts |
Posted - 2011-05-13 : 08:27:41
|
| Thank you so much. Your query is working fine. Thanks you friends.Thanks & RegardsSaravanan Chandru |
 |
|
|
|