Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My table is look like this:

Sub_Package_ID       Package_ID           Destination_ID       Sub_Destionation_ID
-------------------- -------------------- -------------------- --------------------
1                    1                    1                    5
2                    1                    2                    8
3                    1                    2                    9
4                    2                    1                    6
5                    2                    2                    7
6                    3                    1                    5
7                    3                    2                    7
8                    4                    1                    5
9                    4                    2                    8
10                   4                    2                    9
11                   4                    2                    7


I want to write a query in that way if i enter (6,7) as Sub_Destionation_ID query will return (2) as Package_ID, if i enter (5,7) as Sub_Destionation_ID query will return (3) as Package_ID

if there is no such Package_ID exists as per user input such as
if i enter (6,9) as Sub_Destionation_ID query will return nothing as Package_ID,
if i enter (8,5) as Sub_Destionation_ID query will return nothing as Package_ID,
if i enter (8,9) as Sub_Destionation_ID query will return nothing as Package_ID,
if i enter (8,7) as Sub_Destionation_ID query will return nothing as Package_ID.


Hope i can explain my question; if there is any query please ask.

Please help me to solve this query.
Thanks in advance.
Posted
Updated 15-Feb-13 17:50pm
v4
Comments
Karthik Harve 15-Feb-13 23:20pm    
Are you expecting only one row as result ?
Karthik Harve 15-Feb-13 23:20pm    
does top clause would not help you ?
sahabiswarup 15-Feb-13 23:23pm    
if the input value is present in the record then query only return record else not. I've not try top clause; can you please give me one example?

1 solution

SQL
SELECT DISTINCT (Package_ID)FROM [Sub_Package]
WHERE Sub_Package.Package_ID In (select Package_ID from Sub_Package where Sub_Destionation_ID = '6')
    And Sub_Package.Package_ID In (select Package_ID from Sub_Package where Sub_Destionation_ID = '7')
    And Sub_Package.Package_ID In (select Package_ID from Sub_Package where Sub_Destionation_ID = '9')
 
Share this answer
 
Comments
sahabiswarup 15-Feb-13 23:56pm    
I've tried this above code but one problem is there:

If i enter Sub_Destionation_ID ="5,8,9" query returns two record Package_ID ="1,4"

But i only want the single record ie Package_ID ="1" because only Package_ID ="1" has three Sub_Destionation_ID ="5,8,9".
Package_ID ="4" has also Sub_Destionation_ID ="5,8,9" but it has another one Sub_Destionation_ID="7".

So can anyone please tell how to get the exact output?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900