| | |
query help
Please support our MS SQL advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: May 2009
Posts: 1
Reputation:
Solved Threads: 0
I have the following data in a table named dtlstbl which has columns id and refid
100,1
100,2
101,1
102,2
103,1
103,2
I am fetching data from this table thus
select id from dtlstbl where refid in (1,2)
All the records are returned.
I want a SQL 2000 query that will return only the following result
100
103
ie. id's which have ref id both 1 and 2.
100,1
100,2
101,1
102,2
103,1
103,2
I am fetching data from this table thus
select id from dtlstbl where refid in (1,2)
All the records are returned.
I want a SQL 2000 query that will return only the following result
100
103
ie. id's which have ref id both 1 and 2.
•
•
Join Date: Dec 2007
Posts: 252
Reputation:
Solved Threads: 27
there may be another way but off the top of my head you can use "or"
actually i just did a quick search and found
sql Syntax (Toggle Plain Text)
SELECT id FROM dtlstbl WHERE refid = 1 OR refid = 2
sql Syntax (Toggle Plain Text)
SELECT id FROM dtlstbl WHERE (refid(1,2))
Last edited by ProfessorPC; May 29th, 2009 at 1:02 pm.
I think a self join is required:
sql Syntax (Toggle Plain Text)
SELECT DISTINCT d1.id FROM dtlstbl as d1 INNER JOIN dtlstbl as d2 on d1.id = d2.id AND d2.refid = 2 WHERE d1.refid = 1;
•
•
•
•
there may be another way but off the top of my head you can use "or"
sql Syntax (Toggle Plain Text)
SELECT id FROM dtlstbl WHERE refid = 1 OR refid = 2
Last edited by pty; May 29th, 2009 at 12:50 pm. Reason: Stray quote tag!
Note to self... pocket cup
SINCE YOUR REF ID IS FIXED SO NO NEED TO SELF JOIN IN THIS SCENARIO. JUST TAKE THE GROUPWISE COUNT =2. Note if refid duplicated for a specific id then below query will not work. In this scenario follow the 2nd query
GENERIC QUERY
MS SQL Syntax (Toggle Plain Text)
SELECT ID FROM dtlstbl GROUP BY ID HAVING COUNT(*)=2
GENERIC QUERY
MS SQL Syntax (Toggle Plain Text)
SELECT DISTINCT ID FROM( SELECT t1.ID ID,t1.REFID ref1,t2.refid ref2 FROM dtlstbl t1 LEFT JOIN dtlstbl t2 ON t1.id=t2.id) tbl WHERE tbl.ref1=1 AND tbl.ref2=2
Last edited by mail2saion; Jun 1st, 2009 at 3:09 am.
MARK AS SOLVED if its help you.
REGARDS
MCTS - Shawpnendu bikash maloroy
http://shawpnendu.blogspot.com
REGARDS
MCTS - Shawpnendu bikash maloroy
http://shawpnendu.blogspot.com
![]() |
Similar Threads
- Sql Query (VB.NET)
- Removing Query Strings (ASP.NET)
- Double MySQL Query (PHP)
- Dynamic Query (JSP)
- MySQL nested query / joined query conversion help (MySQL)
- problem with lengthy query (Java)
- Retreiving variables from a sql query into a form (PHP)
- Query Building (Database Design)
Other Threads in the MS SQL Forum
- Previous Thread: how to deal with two table
- Next Thread: Basic SQL Help
| Thread Tools | Search this Thread |





