#sql-server #tsql
#sql-сервер #tsql
Вопрос:
Если у вас есть таблица с SALES {Customer_ID, Price, SmallDateTime date}
. Как вы сообщаете обо всех продажах за месяц?
SELECT Sum(Price) As Total Sales FROM SALES Group By What Having What
Ответ №1:
SELECT YEAR(date) as SalesYear,
MONTH(date) as SalesMonth,
SUM(Price) AS TotalSales
FROM Sales
GROUP BY YEAR(date), MONTH(date)
ORDER BY YEAR(date), MONTH(date)
Ответ №2:
SELECT CONVERT(CHAR(7), SmallDateTime, 120) as Year_Month,
SUM(Price)
FROM Sales
GROUP BY CONVERT(CHAR(7), SmallDateTime, 120)
ORDER BY Year_Month
Ответ №3:
Другим решением является вычисление первого дня месяца
Select DateAdd(d,DateDiff(d,0,[Date])-DatePart(d,[Date]) 1,0)
, Sum( Price )
From Sales
Group By DateAdd(d,DateDiff(d,0,[Date])-DatePart(d,[Date]) 1,0)
Ответ №4:
Попробуйте это
SELECT A.CAL_YEAR AS YEAR ,A.CAL_WEEK AS WEEK,SUM(B.SUM_OF_PROFIT) as profit FROM
CALANDER A,SALES_FACT B WHERE A.DATE_ID=B.DATE_ID
GROUP BY A.CAL_YEAR,A.CAL_WEEK;