SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Return 4 rows, not 11 rows.
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

ugh3012
Yak Posting Veteran

56 Posts

Posted - 07/25/2013 :  13:11:34  Show Profile  Reply with Quote
I have two tables. Table A has 4 rows. Table C has 11 rows and I only need one field from table C.


My query looks something like this like this. 
SELECT 
A.field1
,A.field2
,C.field24
FROM Table A
inner join Table C
on A.field1 = C.field3
and C.field.StartDate Between A.EffDate and ISnull(A.TermDate , CAST('12/31/2078 23:59:59' as Datetime))
Where C.field24 not in (‘531’, …)


It returns 11 rows as there are 11 rows in Table C. I need the query to ignore all other rows and only get infomation from field24 as they are the same and put them into 4 rows. I also need the query not to include any rows from table A based on this “Where C.field24 not in (‘531’, …).”

James K
Flowing Fount of Yak Knowledge

3326 Posts

Posted - 07/25/2013 :  13:20:21  Show Profile  Reply with Quote
Try something like this. This would give you one row per combination of field1 and field2, but whether the value of field24 that you get is the right one or not - I don't know
SELECT 
A.field1
,A.field2
,MAX(C.field24) AS field24
FROM Table A
inner join Table C
on A.field1 = C.field3
and C.field.StartDate Between A.EffDate and ISnull(A.TermDate , CAST('12/31/2078 23:59:59' as Datetime))
Where C.field24 not in ('531', ...)
GROUP BY 
	A.field1
	,A.field2
Go to Top of Page

ugh3012
Yak Posting Veteran

56 Posts

Posted - 07/25/2013 :  13:27:30  Show Profile  Reply with Quote
No joy :(
Go to Top of Page

djj55
Constraint Violating Yak Guru

USA
283 Posts

Posted - 07/25/2013 :  14:48:27  Show Profile  Reply with Quote
Not knowing the values of field24 have you tried DISTINCT?

djj
Go to Top of Page
  Previous Topic Topic Next Topic  
 New 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.12 seconds. Powered By: Snitz Forums 2000