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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 HELP in query

Author  Topic 

viswaratha
Starting Member

3 Posts

Posted - 2012-03-28 : 11:17:30
Table T1
C1 C2 C3 C4 C5.............
--------------------------------------
A 1 N
B 1 Y
B 1 N
B 2 N

I have this above table.
For every entry with C3 = 'Y', there will be a C3 = 'N'.
I want to write a query which selects all the records ignoring the records with C3 = 'N' when there is an entry for 'Y'.

Expected RESULT:

C1 C2 C3 C4 C5.............
--------------------------------------
A 1 N
B 1 Y
B 2 N


Thanks,
Raja

viswaratha
Starting Member

3 Posts

Posted - 2012-03-28 : 11:18:18
Just want to add. I am using a DB2 database.

Thanks,
Raja
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-03-28 : 11:22:08
try http://www.dbforums.com/db2/

This site is on MS SQL Server


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

viswaratha
Starting Member

3 Posts

Posted - 2012-03-28 : 11:28:43
Thanks very much

Thanks,
Raja
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-03-28 : 11:53:01
Gotta run to a meeting..but here's a start



CREATE -- GLOBAL TEMPORARY
TABLE T1 (C1 CHAR(1), C2 CHAR(1), C3 CHAR(1))
;

INSERT INTO T1(C1, C2, C3)
SELECT 'A', '1', 'N' -- FROM SYSIBM.SYSDUMMY1
UNION ALL
SELECT 'B', '1', 'Y' -- FROM SYSIBM.SYSDUMMY1
UNION ALL
SELECT 'B', '1', 'N' -- FROM SYSIBM.SYSDUMMY1
UNION ALL
SELECT 'B', '2', 'N' -- FROM SYSIBM.SYSDUMMY1
;

SELECT * FROM T1
WHERE
SELECT C1, C2
FROM T1 o
WHERE EXISTS (SELECT * FROM T1 i1 WHERE i1.C1 = o.C1 AND i1.C2 = o.C2 AND i1.C3 = 'Y')
AND EXISTS (SELECT * FROM T1 i1 WHERE i1.C1 = o.C1 AND i1.C2 = o.C2 AND i1.C3 = 'N')
GROUP BY C1, C2
HAVING COUNT(*) > 1)
;




Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page
   

- Advertisement -