0

I have a table like below

CAccountID  CID   NetworkID
1               1      1
2               1      2
3               2      1
4               2      2
5               2      3
6               3      1
7               3      2
8               3      3
9               4      1
10              4      2

I need a query to select all CID having all 3 NetworkID(1,2,3) and don't need to display only 1 and 2 NetworkID.

Output should be like below,

CAccountID  CID  NetworkID
3                2     1
4                2     2
5                2     3
6                3     1
7                3     2
8                3     3
1
  • Please mention your sql server version.. Commented Aug 27, 2018 at 11:18

4 Answers 4

1

You can use GROUP BY with JOIN :

select t.*
from table t inner join
     ( select cid 
       from table
       where NetworkID in (1,2,3)
       group by cid
       having count(distinct NetworkID) = 3
     ) tt 
     on tt.cid  = t.cid; 
Sign up to request clarification or add additional context in comments.

1 Comment

Hi Sharma,Thank you for your reply. I need CID with having all 1,2,3 NetwordID.. Please check the output i expected
0

Try this:

select * from my_table t
where exists(select 1 from my_table
             where CID = t.CID and NetworkID in (1,2,3)
             group by CID
             having count(*) = 3)

2 Comments

Hi, Thank you for your reply. I need CID with having all 1,2,3 NetwordID.. Please check the output i expected
@user2986417 Did try my query? It should return your desired result.
0

Try this:

select * from <<tablename>> where cid in(select cid from <<tablename>> group by cid having count(*)=3).

Here the subquery will return you all thouse cid which have 3 rows in your table.

Or if you have more network ids then use of INTERSECT operator can be helpful:

select * from <<tablename>> where cid in (
   select cid from <<tablename>> where NetworkID=1
   INTERSECT 
   select cid from <<tablename>> where NetworkID=2
   INTERSECT
   select cid from <<tablename>> where NetworkID=3
);

INTERSECT operator basically returns all the rows common in the queries. Thus, your data unpredicatbility can be handled in this way

2 Comments

Don't forget that you probably should specify where NetworkID in (1,2,3) as data can be unpredictable.
where NetworkID in (1,2,3) may not help perfeclty. However, I liked your concern about data unpredicatbility, thus, have added another approach :)
0

Try xml path.

SELECT *
FROM Table_Name B
WHERE (SELECT [text()] = A.Network FROM Table_Name A WHERE A.CID = B.CID 
ORDER BY CID, CAAccount FOR XML PATH('')) = 123

CTE Demo:

; WITH CTE(CAAccount, CID, Network) AS
(
SELECT 1 , 1, 1 UNION ALL
SELECT 2 , 1, 2 UNION ALL
SELECT 3 , 2, 1 UNION ALL
SELECT 4 , 2, 2 UNION ALL
SELECT 5 , 2, 3 UNION ALL
SELECT 6 , 3, 1 UNION ALL
SELECT 7 , 3, 2 UNION ALL
SELECT 8 , 3, 3 UNION ALL
SELECT 9 , 4, 1 UNION ALL
SELECT 10, 4, 2
) SELECT *
FROM CTE B
WHERE (SELECT [text()] = A.Network FROM CTE A WHERE A.CID = B.CID ORDER BY CID, CAAccount FOR XML PATH('')) = 123

Output:

CAAccount   CID Network
3           2   1
4           2   2
5           2   3
6           3   1
7           3   2
8           3   3

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.