Single SQL Server Result Set from Query

Posted by JamesC on Stack Overflow See other posts from Stack Overflow or by JamesC
Published on 2010-06-01T17:14:58Z Indexed on 2010/06/01 17:23 UTC
Read the original article Hit count: 181

Filed under:
|
|

Hi

Please advise on how to merge two results in to one using SQL Server 2005.

I have the situation where an Account can have up to two Settlement Instructions and this have been modeled like so:

The slim-ed down schema:

Account
---------------------
Id
AccountName
PrimarySettlementId (nullable)
AlternateSettlementId (nullable)


SettlementInstruction
----------------------
Id
Name

The output I want is a single result set with a select statement something along the lines of this which will allow me to construct some java objects in my Spring row mapper:

select
  Account.Id as accountId, 
  Account.AccountName as accountName, 

  s1.Id as primarySettlementId, 
  s1.Name as primarySettlementName, 

  s2.Id as alternateSettlementId, 
  s2.Name as alternateSettlementName

I've tried various things but cannot find a way to get the result set merged in to one where the primary and alternate FK's are not null.

Finally I have searched the forum, but nothing quite seems to fit with what I need.

© Stack Overflow or respective owner

Related posts about sql

Related posts about sql-server