我是SQL 2012的新手,需要你的帮助。我已经设法将以下查询放在一起并且它有效,当我尝试按照第3个表中的类别类型进行分组时失败。不确定如何加入,但是遇到绑定错误。
所以基本上,2 where(select from)语句可以工作,但我不能得到c.category。 (表名:ELM_HO.dbo.BCX_ExclusionsJan2013)
请您提供指导。谢谢
SELECT c.category
,count (c.category)
,count(distinct b.[ACCOUNT NUMBER])
,sum([30 DAYS BAL]) as [30 DAYS]
,sum([30 DAYS VAT BAL]) as [30 DAYS VAT]
,sum([60 DAYS BAL]) as [60 DAYS]
,sum([60 DAYS VAT BAL])as [60 DAYS VAT]
,sum([90 DAYS BAL]) as [90 DAYS]
,sum([90 DAYS VAT BAL]) as [90 DAYS VAT]
,sum([REST]) as [REST]
,sum([VAT REST]) as [REST VAT]
,sum(b.[30 DAYS BAL]) + sum(b.[30 DAYS VAT BAL])+ sum(b.[60 DAYS BAL]) + sum(b.[60 DAYS VAT BAL]) + sum(b.[90 DAYS BAL]) + sum(b.[90 DAYS VAT BAL]) +sum(b.[REST]) + sum(b.[VAT REST]) as [TOTAL PAYMENTS]
FROM [ELM_BP348].[dbo].[BP348JUL2013] as b]
where b.[ACCOUNT NUMBER] in (SELECT [Account Number] FROM [TIMS].[dbo].[TIMS90-Claim_JUL2013])
and b.[ACCOUNT NUMBER] in (select [Account no] from ELM_HO.dbo.BCX_ExclusionsJan2013 as c)
group by c.category
答案 0 :(得分:0)
由于您没有提供任何数据,我无法对此进行测试,但我认为这可能是您想要的:
SELECT
c.category,
COUNT(c.category),
COUNT(DISTINCT b.[ACCOUNT NUMBER]),
SUM([30 DAYS BAL]) as [30 DAYS],
SUM([30 DAYS VAT BAL]) as [30 DAYS VAT],
SUM([60 DAYS BAL]) as [60 DAYS],
SUM([60 DAYS VAT BAL])as [60 DAYS VAT],
SUM([90 DAYS BAL]) as [90 DAYS],
SUM([90 DAYS VAT BAL]) as [90 DAYS VAT],
SUM([REST]) as [REST],
SUM([VAT REST]) as [REST VAT],
SUM(b.[30 DAYS BAL]) + SUM(b.[30 DAYS VAT BAL])+ SUM(b.[60 DAYS BAL]) + SUM(b.[60 DAYS VAT BAL]) + SUM(b.[90 DAYS BAL]) + SUM(b.[90 DAYS VAT BAL]) +SUM(b.[REST]) + SUM(b.[VAT REST]) as [TOTAL PAYMENTS]
FROM [ELM_BP348].[dbo].[BP348JUL2013] AS b
INNER JOIN ELM_HO.dbo.BCX_ExclusionsJan2013 AS c ON b.[ACCOUNT NUMBER] = c.[Account no]
WHERE b.[ACCOUNT NUMBER] in (SELECT [Account Number] FROM [TIMS].[dbo].[TIMS90-Claim_JUL2013])
GROUP BY c.category
您也可以将WHERE子句替换为另一个内连接。