我有一张桌子,里面有6列。我必须确保这些列中的5个没有任何重复。所以我使用了声明:
SELECT SUB_ACCT_NO_PAJ, CustomerType, POST_DTE_PAJ, IA_DateYear, ADJ_RSN_PAJ, count(*) AS [aCount]
INTO TempTable1
FROM All_Adjustments
GROUP BY SUB_ACCT_NO_PAJ, CustomerType, POST_DTE_PAJ, IA_DateYear, ADJ_RSN_PAJ,
我的问题是需要为这些行中的每一行合计第六列POST_AMT_PAJ。因此,如果任何行在其他字段中都是完全重复的,那么我需要从它们中获取每个POST_AMT_PAJ并将它们总计为新表。
任何帮助表示赞赏!谢谢!
所有调整:
SUB_ACCT | Customer Type | POST_DTE | Dateyear | POST_AMT
------------------------------------------------------------------------
1 | R | July 3 | 2010 | 5
1 | R | July 3 | 2010 | 8
2 | L | June 2 | 2008 | 14
2 | R | June 2 | 2009 | 12
会去......
SUB_ACCT | Customer Type | POST_DTE | Dateyear | POST_AMT
------------------------------------------------------------------------
1 | R | July 3 | 2010 | 13
2 | L | June 2 | 2008 | 14
2 | R | June 2 | 2009 | 12
答案 0 :(得分:1)
要获取列的总数,请使用SUM:
SELECT
SUB_ACCT_NO_PAJ,
CustomerType,
POST_DTE_PAJ,
IA_DateYear,
ADJ_RSN_PAJ,
SUM(POST_AMT_PAJ) AS POST_AMT_PAJ_total
COUNT(*) AS [aCount]
INTO TempTable1
FROM All_Adjustments
GROUP BY
SUB_ACCT_NO_PAJ,
CustomerType,
POST_DTE_PAJ,
IA_DateYear,
ADJ_RSN_PAJ
答案 1 :(得分:0)
试试这个:
SELECT
SUB_ACCT_NO_PAJ,
CustomerType, POST_DTE_PAJ,
IA_DateYear,
Sum(ADJ_RSN_PAJ) as 'Total_Adj_Rsn_Paj',
count(*) AS [aCount]
INTO TempTable1
FROM All_Adjustments
GROUP BY SUB_ACCT_NO_PAJ, CustomerType, POST_DTE_PAJ, IA_DateYear
由于ADJ_RSN_PAJ
将在聚合函数中,因此不应导致Group By
子句爆炸。
注意:这完全来自内存,所以你可能需要做一些tweeking(我实际上没有SQL Server可以使用 - 甚至用于语法检查......)