我有一个查询可以获得当天的总销售数据
SELECT
SUM(cso.SubTotal) - (
SELECT SUM(cso.CreditAvailable) / 1.1
FROM dbo.CustomerCredit cso
WHERE CONVERT(VARCHAR(10), cso.DateCreated, 102) = CONVERT(VARCHAR(10), SYSDATETIME(), 102)
) AS total_value
FROM dbo.CustomerInvoice cso
WHERE
Convert(VARCHAR(10), cso.InvoiceDate, 102) = Convert(VARCHAR(10), SYSDATETIME(), 102)
我现在需要的是一张表格,其中列出了左栏目前当月的所有日期,右栏则列出了每个日期的总销售额(使用上述查询)
+---------+---------+
| date | total |
+---------+---------+
| 1/2/16 | 256232 |
| 2/2/16 | 285632 |
| 3/2/16 | 265231 |
| 4/2/16 | 254215 |
| 5/2/16 | 0 |
| ....... | ..... |
| 28/2/16 | 0 |
| 29/2/16 | 0 |
+-------------------+
将来或周末日期的日期零销售额无关紧要。
我绞尽脑汁寻求解决方案,但由于我不熟悉SQL,我决定联系社区。 p>
答案 0 :(得分:0)
首先,我相信您的查询可以写成:
SELECT
SUM(SubTotal) - (SUM(CreditAvailable) / 1.1) AS total
FROM dbo.CustomerInvoice
WHERE CAST(InvoiceDate AS DATE) = CAST(SYSDATETIME() AS DATE)
要获取每个日期的总计,只需添加GROUP BY
:
SELECT
SUM(SubTotal) - (SUM(CreditAvailable) / 1.1) AS total
FROM dbo.CustomerInvoice
GROUP BY CAST(InvoiceDate AS DATE)
现在,要包含所有日期,即使是总计为0的日期,您也必须使用tally table。
DECLARE @fromDate AS DATE = '20160101',
@toDate AS DATE = '20160131'
;WITH E1(N) AS(
SELECT 1 FROM(VALUES (1),(1),(1),(1),(1),(1),(1),(1),(1),(1))t(N)
),
E2(N) AS(SELECT 1 FROM E1 a CROSS JOIN E1 b),
E4(N) AS(SELECT 1 FROM E2 a CROSS JOIN E2 b),
CteTally(N) AS(
SELECT TOP(DATEDIFF(DAY, @fromDate, @toDate) + 1) ROW_NUMBER() OVER(ORDER BY(SELECT NULL))
FROM E4
),
CteDates(dt) AS(
SELECT DATEADD(DAY, N - 1, @fromDate) FROM CteTally
)
SELECT
d.dt, t.total
FROM CteDates d
LEFT JOIN (
SELECT
dt = CAST(InvoiceDate AS DATE),
total = SUM(SubTotal) - (SUM(CreditAvailable) / 1.1)
FROM dbo.CustomerInvoice
WHERE InvoiceDate >= @fromDate AND InvoiceDate < DATEADD(DAY, 1, @toDate)
GROUP BY CAST(InvoiceDate AS DATE)
) t
ON t.dt = d.dt
答案 1 :(得分:0)
您可以使用以下查询:
;with date_cur_month as(
select convert(datetime, convert(varchar(6), getdate(), 112) + '01', 112) as dt
union all
select dateadd(day, 1, dt) as dt
from date_cur_month
where datepart(month, dateadd(day, 1, dt)) = datepart(month, getdate()))
select convert(nvarchar(8), d.dt, 3) as date
, isnull(sum(ci.SubTotal), 0)
- isnull((select sum(cc.CreditAvailable) / 1.1
from dbo.CustomerCredit cc
where cast(cc.DateCreated as date) = d.dt
), 0) as total
from dbo.CustomerInvoice ci
right outer join date_cur_month d on cast(ci.InvoiceDate as date) = d.dt
group by d.dt
答案 2 :(得分:0)
WITH cteRange
AS (SELECT
DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0) AS Start_Date
UNION ALL
SELECT
DATEADD(DAY, 1, cteRange.Start_Date)
FROM cteRange
WHERE cteRange.Start_Date < DATEADD(DAY, -1, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) + 1, 0))
)
SELECT
cteRange.Start_Date, ISNULL(inv.SubTotal,0), ISNULL(crd.CredAvail,0)
, ISNULL(inv.SubTotal,0) - ISNULL(crd.CredAvail / 1.1,0) AS total
FROM cteRange
LEFT JOIN (SELECT
CAST(cc.DateCreated AS date) AS DateCreated
, SUM(cc.CreditAvailable) AS CredAvail
FROM dbo.CustomerCredit AS cc
WHERE cc.DateCreated >= DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)
GROUP BY CAST(cc.DateCreated AS date)
) AS crd ON cteRange.Start_Date = crd.DateCreated
LEFT JOIN (SELECT
CAST(cso.InvoiceDate AS date) AS InvoiceDate
, SUM(cso.SubTotal) As SubTotal
FROM dbo.CustomerInvoice AS cso
WHERE cso.InvoiceDate >= DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)
GROUP BY CAST(cso.InvoiceDate AS date)
) AS inv ON cteRange.Start_Date = inv.InvoiceDate
;
此计算返回当前月份的第一天:DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)
然后&#34;递归公用表表达式&#34; (CTE)在当月的所有有效日期为该日期添加一天。所以我们最终得到了一个月的一组行。然后我们使用左连接到您的财务数据。
对于所涉及的两个表中的每一个,我们总结了当前月份中允许联接发生的任何数据。
请注意,为了获得日期/时间信息的最佳效果,您应尽量避免将这些信息转换为varchars。