1

## SQL合計問題

rogeryao iT邦大師 1 級 ‧ 2020-06-16 12:52:56 檢舉

select DiscountPrice
,Sum(Cash) Cash
,Code
,StartDate
,EndDate
from (
....
) k
group by DiscountPrice
,Code
,StartDate
,EndDate
Lungiqa iT邦新手 5 級 ‧ 2020-06-16 14:27:48 檢舉
,[DiscountCode]
,[CategoryType]
,[DiscountType]
,[ConditionType]
,[DiscountPrice]
,[DiscountCount]
--------------------我是分隔線------------------
[Code]
,[Name]
,[DiscountType]
,[CategoryType]
,[DiscountClass]
,[AllowMutiple]
,[AutoDiscount]
,[Weighted]
,[LimitedTimes]
,[Mode]
,[StartDate]
,[EndDate]
,[Disable]
,[DiscountPhase]
,[NickName]
,[IsTemplate]

### 4 個回答

0
rogeryao
iT邦大師 1 級 ‧ 2020-06-16 15:14:45

``````SELECT A.CategoryType,A.DiscountType,A.Code,A.StartDate,A.EndDate,B.DiscountPrice,
SUM(B.DiscountPrice) AS TatalDiscountPrice
FROM [HQInformation].[dbo].[TradeDiscountDetailStatistics] AS B
LEFT JOIN [HQMasterFile].[dbo].[Discount] AS A
ON B.CategoryType=A.CategoryType AND B.DiscountType=A.DiscountType
GROUP BY A.CategoryType,A.DiscountType,A.Code,A.StartDate,A.EndDate,B.DiscountPrice
ORDER BY A.Code ASC
``````

DEMO

``````SELECT A.Code,A.StartDate,A.EndDate,B.DiscountPrice,
SUM(B.DiscountPrice) AS TatalDiscountPrice
FROM [HQInformation].[dbo].[TradeDiscountDetailStatistics] AS B
LEFT JOIN [HQMasterFile].[dbo].[Discount] AS A
GROUP BY A.Code,A.StartDate,A.EndDate,B.DiscountPrice
ORDER BY A.Code ASC
``````

DEMO

Lungiqa iT邦新手 5 級 ‧ 2020-06-16 17:16:37 檢舉

select
t.StoreCode as "門店代號"
,t.StoreName as "門店名稱"
,t.ProductName as "產品名稱"
,c.DiscountPrice as "折扣金額"
,d.Name as "活動名稱"
,d.StartDate as "活動開始"
,d.EndDate as "活動結束"
join HQMasterFile.dbo.Discount d
on c.DiscountCode = d.Code
order by StartDate ASC;

Lungiqa iT邦新手 5 級 ‧ 2020-06-16 17:18:50 檢舉

2
wrxue
iT邦研究生 3 級 ‧ 2020-06-16 11:07:16

GROUP裡只要Code就好了。

Lungiqa iT邦新手 5 級 ‧ 2020-06-16 12:08:00 檢舉

wrxue iT邦研究生 3 級 ‧ 2020-06-16 14:56:13 檢舉

Lungiqa iT邦新手 5 級 ‧ 2020-06-16 17:13:44 檢舉

DiscountCode它是存在的喔

0

iT邦大師 1 級 ‧ 2020-06-16 11:07:49

Lungiqa iT邦新手 5 級 ‧ 2020-06-16 12:10:09 檢舉

0