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
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Sql Help

Author  Topic 

vjs2445
Starting Member

16 Posts

Posted - 2014-01-16 : 15:56:36
I need some help in writing SQL.

Following is the table as well result I am trying to get:

Col1 Col2
A N
A M
B N
C M
D N
D M

Result
A N
A M
D N
D M

I am looking for the rows which has both N and M value.

Thanks in advance

nagino
Yak Posting Veteran

75 Posts

Posted - 2014-01-16 : 19:05:05
If Col2 have only 'N' or 'M', How dose that look?

----------------------------------------------
--#TBL mean 'the table'
SELECT
Col1,
Col2
FROM #TBL
WHERE EXISTS (
SELECT *
FROM #TBL FILTER
WHERE #TBL.Col1 = FILTER.Col1
AND #TBL.Col2 != FILTER.Col2)


-------------------------------------
From Japan
Sorry, my English ability is limited.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2014-01-17 : 02:14:31
quote:
Originally posted by vjs2445

I need some help in writing SQL.

Following is the table as well result I am trying to get:

Col1 Col2
A N
A M
B N
C M
D N
D M

Result
A N
A M
D N
D M

I am looking for the rows which has both N and M value.

Thanks in advance



SELECT Col1,Col2
FROM
(
SELECT MAX(Col2) OVER (PARTITION BY Col1) AS MaxCol2,
MIN(Col2) OVER (PARTITION BY Col1) AS MinCol2,*
FROM Table
WHERE Col2 IN ('M','N')
)t
WHERE MaxCol2 <> MinCol2


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

vjs2445
Starting Member

16 Posts

Posted - 2014-01-17 : 10:28:38
Thanks for the help.

Regards,
Go to Top of Page
   

- Advertisement -