Please start any new threads on our new site at We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 where condition
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Constraint Violating Yak Guru

420 Posts

Posted - 09/18/2013 :  05:45:14  Show Profile  Reply with Quote

i have this:

id depart arrive type journeyNumber
123 car bus 4 1
123 bus van 4 2
456 car bus 2 1
999 bus van 2 1

type=4 meants connecting route, type=2 means none connecting route.
arrive=van are from US country.

i would like to pull out all which has connecting route and from US country.

i did this but it only come out as below:

select * from tableA inner join tableB on where type=4 and country=US

id depart arrive type journeyNumber
123 bus van 4 2

it suppose to come out as below:

id depart arrive type journeyNumber
123 car bus 4 1
123 bus van 4 2

how can i get as the outcome?

Premature Yak Congratulator

22864 Posts

Posted - 10/11/2013 :  07:41:16  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
Does it mean that you want to return all rows for IDs associated with the result?


Failing to plan is Planning to fail
Go to Top of Page

Yak Posting Veteran

56 Posts

Posted - 10/12/2013 :  21:00:07  Show Profile  Reply with Quote
Not Sure, what you want. Hope this helps

, count(*) over (partition by TableA.ID) as IDCount

FROM TableA tableA
inner join tableB on
tableA.type=4 and

) t
IDCount > 1

M. Ncube
Go to Top of Page

Very Important crosS Applying yaK Herder

52326 Posts

Posted - 10/13/2013 :  01:31:55  Show Profile  Reply with Quote
Maybe this?

select * 
from (select *,SUM(CASE WHEN type=4 and country='US' THEN 1 ELSE 0 END) OVER (PARTITION BY ID) AS Cnt 
      FROM tableA 
inner join tableB b 
WHERE a.Cnt > 0

SQL Server MVP
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.02 seconds. Powered By: Snitz Forums 2000