我想要实现的是根据“iSellthruTxnDateKey”列中相应的DISTINCT值对“nTxnCount”列进行求和。
以更简单的方式讲述它。我试图找到列A的不同值,然后将列B中的值汇总。
| A | B |
--------------
| Dog | 1 |
| Cat | 5 |
| Horse | 10 |
| Horse | 10 |
| Dog | 1 |
| Snake | 5 |
将会发生什么......
| A | B |
--------------
| Dog | 1 |
| Cat | 5 |
| Horse | 10 |
| Snake | 5 |
结束结果.. 21 ,这是上面聚合值的总和
以下是我的测试查询:
SELECT ftKPI.sCompanyCode AS 'Country'
, dmCus.sFQOutletOnlineStore AS 'Outlet Online Store'
, dmCus.sDistributionChannel AS 'Distribution Channel'
, dmCus.sGlobalRetailSegmentName AS 'Global Retail Segment'
, dmCus.sCustomerCode AS 'Customer Code'
, dmCus.nsCustomerName AS 'Customer Name'
, SUM(DISTINCT ftKPI.nTxnCount)
FROM ftRetailKPI ftKPI
INNER JOIN dmCalendar dmCal (NOLOCK) on ftKPI.iSellthruTxnDateKey = dmCal.iCalendarKey
INNER JOIN dmProduct dmPRd (NOLOCK) on ftKPI.iProductKey = dmPrd.iProductKey
INNER JOIN dmCustomer dmCus (NOLOCK) on ftKPI.iCustomerKey = dmCus.iCustomerKey
INNER JOIN dmProductSalesArea dmPSA (NOLOCK) ON ftKPI.iProductSalesAreaKey = dmPSA.iProductSalesAreaKey
INNER JOIN ftExchangerate XRate (NOLOCK) on XRate.sYearMonth = '201506' AND ftKPI.sCompanyCode = XRate.sCompanyCode
WHERE (ftKPI.sCompanyCode IN ('LSA') OR dmCus.sCountryName IN ('Australia'))
AND dmPRd.sProductCategoryName IN ('Tops', 'Bottoms', 'Accessories')
AND dmCus.sDistributionChannel IN ('DI')
AND dmCus.sGlobalRetailSegmentName IN ('Co. Operated Comm')
AND dmCus.sFQOutletOnlineStore IN ('First Quality Store')
AND dmCal.sFiscalYYYY IN ('2015')
AND dmCal.sFiscalQQ IN ('Q1', 'Q2')
AND dmCal.sFiscalWeek IN ('Wk01')
AND dmCus.sCustomerCode IN ('0020017262')
GROUP BY ftKPI.sCompanyCode
, dmCus.sCountryName
--, dmPrd.sBrandName
, dmCus.sCustomerCode
, dmCus.nsCustomerName
, dmCus.nsCustomerNameLocal
, dmCus.sDistributionChannel
, dmCus.sGlobalRetailSegmentName
, dmCus.sRetailSegmentGroupName
, dmCus.sFQOutletOnlineStore
, dmCus.sCustomerCode
答案 0 :(得分:1)
好吧我想我知道你想要什么。 你必须这样做
select sum(colB) from
(Select distinct * from tableA) x
在这里进行演示,以便您更好地理解:FIDDLE DEMO
答案 1 :(得分:1)
您需要使用子查询
select sum(foo.b)
from (SELECT distinct on (a) a,b from temp) as foo