我有多个select语句,我想以某种方式组合成一个结果集。我希望每个子查询都是一个列。有5个语句引用了总共3个表。他们都有一个日期字段,我想加入他们。以下是我所拥有的一些代码的示例:
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]
输出栏将是DATE,OTHER DEPOSITS,SWEEP BALANCE,MONEY MARKET&amp; TIPS
此外,如果还有其他方法可以做到这一点,我欢迎所有建议。我对SQL很新。
答案 0 :(得分:0)
这样的事情就足够了:
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