我到处搜索,找不到所需的内容。我将数据集放入按天细分的两个临时表中。在选择的临时表之一中,我需要按周分组。这会引起问题,因为它破坏了我的百分比。我将不胜感激任何有关如何使以下查询通过选项与选定组一起执行的指导。
Select datepart(wk,ts.TotalDate) AS Date,
Tender_Type,
SUM(TransactionsbyType),
SUM(TotalDayTransactions),
ROUND(TransactionsbyType * 100.0 / TotalDayTransactions, 3),
ROUND(TenderAmountAuthorized * 100.0 / TotalDayAuthAmount, 3) AS 'PercentOfSalesByTenderType'
from #TotalSalesByOrderingSystem tso
join #TotalSales ts on ts.TotalDate = tso.TotalOrderingSysDate
group by datepart(wk,ts.TotalDate), Tender_Type
order by datepart(wk,ts.TotalDate), Tender_Type
执行查询时返回错误,指出用于获取百分比TransactionsbyType
和TotalDayTransactions
的列不在聚合中。
如果我将它们放在汇总中,数据将无法在数周内正确折叠。我以前在这里尝试过的解决方案均未成功:stackoverflow.com/questions/6207224/calculating-percentages-with-group-by-query
答案 0 :(得分:1)
如果要在同一时期内使用百分比,则只需在这些计算中使用相同的SUM
:
Select datepart(wk,ts.TotalDate) AS Date,
Tender_Type,
SUM(TransactionsbyType),
SUM(TotalDayTransactions),
ROUND(SUM(TransactionsbyType) * 100.0 / SUM(TotalDayTransactions), 3),
ROUND(SUM(TenderAmountAuthorized) * 100.0 / SUM(TotalDayAuthAmount), 3) AS 'PercentOfSalesByTenderType'
from #TotalSalesByOrderingSystem tso
join #TotalSales ts on ts.TotalDate = tso.TotalOrderingSysDate
group by datepart(wk,ts.TotalDate), Tender_Type
order by datepart(wk,ts.TotalDate), Tender_Type