我有这个查询从表中选择不同的TCName
值:
SELECT DISTINCT(TCName)
FROM [dbo].[TCDetails]
这是另一个将数据汇总到表中的查询:
SELECT
sum(BS.BLDOS) as BLDOS,
sum(BS.CollectedAmount) as CollectedAmount
FROM [Customer] C
INNER JOIN [dbo].[BillingStatus] BS
ON BS.CustomerID = C.CustomerID
INNER JOIN [dbo].[TCDetails] TC
ON TC.CustomerID = BS.CustomerID
我想编写一个查询,以便能够将所有TC的数据总和打印到单个表
中我试试这个,但它不起作用
SELECT
sum(BS.BLDOS) as BLDOS,
sum(BS.CollectedAmount) as CollectedAmount
FROM [Customer] C
INNER JOIN [dbo].[BillingStatus] BS
ON BS.CustomerID = C.CustomerID
INNER JOIN [dbo].[TCDetails] TC
ON TC.CustomerID = BS.CustomerID
WHERE TCName in
(
Select distinct
(TCName)
FROM [dbo].[TCDetails]
)
我想打印它像
TCName | sum(BS.BLDOS) | sum(BS.CollectedAmount)
xyz | 23456 | 6755
tyu | 34556 | 567898
bnv | 21467 | 345
答案 0 :(得分:1)
如果您想在输出中添加多行,则需要group by
SELECT TCName, sum(BS.BLDOS) as BLDOS, sum(BS.CollectedAmount) as CollectedAmount
FROM [Customer] C INNER JOIN
[dbo].[BillingStatus] BS
ON BS.CustomerID = C.CustomerID INNER JOIN
[dbo].[TCDetails] TC
ON TC.CustomerID = BS.CustomerID
WHERE TCName in (Select distinct(TCName) FROM [dbo].[TCDetails])
GROUP BY TCName;