Domanda

I have multiple select statements that I would like to combine somehow into one result set. I would like each subquery to be a single column. There are 5 statements that reference a total of 3 tables. They all have a date field that I would like to join them on. Here is an example of some of the code I have:

SELECT sum([Account Current Balance]) AS 'Other Deposits'
FROM [FINANCE].[dbo].[BICDATAd]
WHERE [Account Type Code] < '100' AND [Account Type Code] Not like '024' and [Account Type Code] Not Like '031'
GROUP BY [Full Date]


SELECT sum([SumOfMARKET_VALUE]) AS 'Sweep Balance'
FROM [Finance].[dbo].[SQLFDIC]
GROUP BY [As Of Date]

SELECT sum([MARKET_VALUE]) AS 'Money Market + TIPS'
FROM [FINANCE].[dbo].[SQLFDICpledge]
WHERE (SEC_TYPE LIKE '10%' OR SEC_TYPE LIKE '30%') AND ACCOUNT_NUMBER = '1040004859'
GROUP BY [As of Date]

Output columns would be DATE, OTHER DEPOSITS, SWEEP BALANCE, MONEY MARKET & TIPS

Also, if there's some other way to go about doing this, I would welcome all suggestions. I'm fairly new to SQL.

È stato utile?

Soluzione

Something like this might suffice:

SELECT 
    coalesce(T1.[theDate], T2.[theDate], T3.[theDate]) as [Date],
    [Other Deposits],
    [Sweep Balance],
    [Money Market + TIPS]
from 
(
    SELECT sum([Account Current Balance]) AS 'Other Deposits', [Full Date] as theDate
    FROM [FINANCE].[dbo].[BICDATAd]
    WHERE [Account Type Code] < '100' AND [Account Type Code] Not like '024' and [Account Type Code] Not Like '031'
    GROUP BY [Full Date]
) T1
full outer join
(
    SELECT sum([SumOfMARKET_VALUE]) AS 'Sweep Balance', [As Of Date] as theDate
    FROM [Finance].[dbo].[SQLFDIC]
    GROUP BY [As Of Date]
) T2 on T1.theDate = T2.theDate
full outer join
(
    SELECT sum([MARKET_VALUE]) AS 'Money Market + TIPS', [As of Date] as theDate
    FROM [FINANCE].[dbo].[SQLFDICpledge]
    WHERE (SEC_TYPE LIKE '10%' OR SEC_TYPE LIKE '30%') AND ACCOUNT_NUMBER = '1040004859'
    GROUP BY [As of Date]
) T3 on T2.theDate = T3.theDate
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top