我的老板希望得到一份报告,他会审核他应该从前一天(昨天)输入的每个销售订单。只需查看销售订单的快照详细信息,他还希望查看客户在我们公司的年初至今的销售历史。
我的问题是,我使用T0.[DocDate]
条款中的字段WHERE
仅显示前一天的销售情况。我如何查询相关的T0.[CardName]
及其YTD历史记录:YTD Sales Total
,YTD Gross Profit
,YTD GP%
?
这是我到目前为止输入的代码:
SELECT T0.[DocDate] AS 'Date',
T0.[DocNum] AS 'SO #',
T0.[CardName] AS 'Customer',
ROUND(T0.[DocTotal],2) AS 'Sales Total',
ROUND(T0.[GrosProfit],2) AS 'GP',
ROUND(((T0.[DocTotal]-(t0.[DocTotal]-t0.[GrosProfit]))/NULLIF(t0.[DocTotal],0)*100),2) AS 'GM%'
FROM dbo.ORDR T0
WHERE CAST(GETDATE()-1 AS DATE) = T0.DocDate
ORDER BY T0.[DocNum]
FOR BROWSE
在代码中,您会注意到我在SELECT
语句中未添加任何YTD列。当我添加字段时,它永远不会起作用,因为我不知道如何查询YTD信息。它甚至可能吗?我需要子查询吗?
对此的任何建议都非常有必要!!
感谢。
我现在正在尝试使用UNION ALL
代码,我觉得SOOO CLOSE但是我最后联合的2列总计所有内容以及今年的日期范围。如何让查询识别我希望它找到客户并将该公式应用于相关客户。不是整体SUM
?
这是新代码:
SELECT T0.[DocDate] AS 'Date',
T0.[DocNum] AS 'SO #',
T0.[CardName] AS 'Customer',
ROUND(T0.[DocTotal],2) AS 'Sales Total',
ROUND(T0.[GrosProfit],2) AS 'GP',
ROUND(((T0.[DocTotal]-(t0.[DocTotal]-t0.[GrosProfit]))/NULLIF(t0.[DocTotal],0)*100),2) AS 'GM%',
NULL,
NULL
FROM dbo.ORDR T0
WHERE CAST(GETDATE()-1 AS DATE) = T0.DocDate
UNION ALL
SELECT NULL,NULL,NULL,NULL,NULL,NULL,SUM(T0.[DocTotal]),SUM(T0.[GrosProfit])
FROM dbo.ORDR T0
WHERE CAST(GETDATE()-365 AS DATE) >= T0.[DocDate]
答案 0 :(得分:1)
SELECT cast(min(T0.[DocDate]) as date) AS 'Start Date',
cast(max(T0.[DocDate]) as date) AS 'End Date',
count(T0.[DocNum]) AS 'No.of Orders',
T0.[CardName] AS 'Customer',
sum(ROUND(T0.[DocTotal],2)) AS 'Sales Total',
sum(ROUND(T0.[GrosProfit],2)) AS 'GP'
-- ROUND(((T0.[DocTotal]-(t0.[DocTotal]-t0.[GrosProfit]))/NULLIF(t0.[DocTotal],0)*100),2) AS 'GM%'
FROM dbo.ORDR T0
WHERE T0.DocDate between CAST(dateadd(yy,-1,GETDATE()) AS DATE)
and CAST(GETDATE()-1 AS DATE)
group by t0.cardname
尝试使用此产品进行YTD销售。
编辑:
SELECT T0.[DocDate] AS 'Date',
T0.[DocNum] AS 'SO #',
T0.[CardName] AS 'Customer',
ROUND(T0.[DocTotal],2) AS 'Sales Total',
ROUND(T0.[GrosProfit],2) AS 'GP',
ROUND(((T0.[DocTotal]-(t0.[DocTotal]-t0.[GrosProfit]))/NULLIF(t0.[DocTotal],0)*100),2) AS 'GM%',
NULL as Total, NULL as Total_Gross_Profit -- '', ''
FROM dbo.ORDR T0
WHERE CAST(GETDATE()-1 AS DATE) = T0.DocDate
UNION ALL
SELECT NULL, NULL, NULL, NULL,NULL, NULL --'','','','','','',
SUM(T0.[DocTotal]),
SUM(T0.[GrosProfit])
FROM dbo.ORDR T0
WHERE CAST(GETDATE()-365 AS DATE) >= T0.[DocDate]
答案 1 :(得分:0)
如果你想要它们并排,你可以使用连接:
SELECT T0.[DocDate] AS 'Date',
T0.[DocNum] AS 'SO #',
T0.[CardName] AS 'Customer',
ROUND(T0.[DocTotal],2) AS 'Sales Total',
ROUND(T0.[GrosProfit],2) AS 'GP',
ROUND(((T0.[DocTotal]-(t0.[DocTotal]-t0.[GrosProfit]))/NULLIF(t0.[DocTotal],0)*100),2) AS 'GM%',
T1.[Sales Total][Sales Total YTD],
T1.GP [GP YTD],
T1.[No. of Orders][Orders YTD]
FROM dbo.ORDR T0
INNER JOIN (SELECT
count(T0.[DocNum]) AS 'No. of Orders',
T0.[CardName] as 'Customer',
sum(ROUND(T0.[DocTotal],2)) AS 'Sales Total',
sum(ROUND(T0.[GrosProfit],2)) AS 'GP'
ROUND(((sum(T0.[DocTotal])-(sum(t0.[DocTotal]-t0.[GrosProfit])))/NULLIF(t0.sum([DocTotal]),0)*100),2) AS 'GM%'
FROM dbo.ORDR T0
WHERE T0.DocDate between CAST(dateadd(yy,-1,GETDATE()) AS DATE)
and CAST(GETDATE()-1 AS DATE)
group by t0.cardname) T1
on T0.CardName = T1.Customer
WHERE CAST(GETDATE()-1 AS DATE) = T0.DocDate
ORDER BY T0.[DocNum]