- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的要求有点复杂,为了便于理解,让我用简单的例子说明一下。假设,我们必须计算过去 12 个月消费者按月购买的产品数量。所以我们要做的是:
SELECT DATENAME(MONTH, m.InvoiceDate) Month,
COUNT(m.Quantity) Quantity FROM CustomerInvoice m WHERE
DATEDIFF(month, m.InvoiceDate, GETDATE()) <= 12 AND YEAR(m.InvoiceDate) = '2018'
GROUP BY DATENAME(MONTH, m.InvoiceDate)
ORDER BY DATENAME(MONTH, m.InvoiceDate) DESC;
输出 - 这就是我们得到的:
Month Quantity
January 1
February 1
March 1
April 2
May 1
June 3
July 1
August 2
就我而言,它有点复杂,如以下示例:
因此,如果输入是 July 2018
,它应该计算过去 12 个月的数量或值(value)以及个别月份的过去 12 个月的值(value)。这似乎类似于递归调用。但非常不确定如何以正确的方式完成此任务 - 任何想法或示例将不胜感激 - 谢谢。
带有数据的示例表:
CREATE TABLE [dbo].[CustomerInvoice](
[ID] [int] IDENTITY(1,1) NOT NULL,
[ProductID] [int] NULL,
[Quantity] [float] NULL,
[Details] [varchar](max) NULL,
[InvoiceID] [varchar](40) NULL,
[CustomerID] [int] NULL,
[InvoiceDate] [datetime] NULL
)
INSERT [dbo].[CustomerInvoice] ([ID], [ProductID], [Quantity], [Details], [InvoiceID], [CustomerID], [InvoiceDate]) VALUES (1, 2, 2, N'Awesome Collections', N'20151129115910', 1, CAST(0x0000A94200000000 AS DateTime))
INSERT [dbo].[CustomerInvoice] ([ID], [ProductID], [Quantity], [Details], [InvoiceID], [CustomerID], [InvoiceDate]) VALUES (2, 1, 2, N'Joss Collections', N'20151129115910', 1, CAST(0x0000A94200000000 AS DateTime))
INSERT [dbo].[CustomerInvoice] ([ID], [ProductID], [Quantity], [Details], [InvoiceID], [CustomerID], [InvoiceDate]) VALUES (3, 2, 2, N'Awesome Collections', N'20151129115910', 1, CAST(0x0000A91900000000 AS DateTime))
INSERT [dbo].[CustomerInvoice] ([ID], [ProductID], [Quantity], [Details], [InvoiceID], [CustomerID], [InvoiceDate]) VALUES (4, 1, 2, N'Awesome Collections', N'20151129115910', 1, CAST(0x0000A8FC00000000 AS DateTime))
INSERT [dbo].[CustomerInvoice] ([ID], [ProductID], [Quantity], [Details], [InvoiceID], [CustomerID], [InvoiceDate]) VALUES (5, 2, 2, N'Awesome Collections', N'20151129115912', 2, CAST(0x0000A8F300000000 AS DateTime))
INSERT [dbo].[CustomerInvoice] ([ID], [ProductID], [Quantity], [Details], [InvoiceID], [CustomerID], [InvoiceDate]) VALUES (6, 1, 2, N'Joss Collections', N'20151129115912', 2, CAST(0x0000A8F400000000 AS DateTime))
INSERT [dbo].[CustomerInvoice] ([ID], [ProductID], [Quantity], [Details], [InvoiceID], [CustomerID], [InvoiceDate]) VALUES (7, 1, 4, N'Joss Collections', N'20151130120336', 1, CAST(0x0000A8DF00000000 AS DateTime))
INSERT [dbo].[CustomerInvoice] ([ID], [ProductID], [Quantity], [Details], [InvoiceID], [CustomerID], [InvoiceDate]) VALUES (8, 1, 10, N'Awesome Collections', N'20151130120336', 1, CAST(0x0000A8BA00000000 AS DateTime))
INSERT [dbo].[CustomerInvoice] ([ID], [ProductID], [Quantity], [Details], [InvoiceID], [CustomerID], [InvoiceDate]) VALUES (9, 2, 10, N'Awesome Collections', N'20151130120616', 1, CAST(0x0000A8BE00000000 AS DateTime))
INSERT [dbo].[CustomerInvoice] ([ID], [ProductID], [Quantity], [Details], [InvoiceID], [CustomerID], [InvoiceDate]) VALUES (10, 1, 10, N'Joss Collections', N'20151130120616', 1, CAST(0x0000A8AB00000000 AS DateTime))
INSERT [dbo].[CustomerInvoice] ([ID], [ProductID], [Quantity], [Details], [InvoiceID], [CustomerID], [InvoiceDate]) VALUES (11, 2, 12, N'Great', N'20151130125238', 2, CAST(0x0000A88700000000 AS DateTime))
INSERT [dbo].[CustomerInvoice] ([ID], [ProductID], [Quantity], [Details], [InvoiceID], [CustomerID], [InvoiceDate]) VALUES (12, 1, 12, N'Great', N'20151130125238', 2, CAST(0x0000A86F00000000 AS DateTime))
INSERT [dbo].[CustomerInvoice] ([ID], [ProductID], [Quantity], [Details], [InvoiceID], [CustomerID], [InvoiceDate]) VALUES (13, 2, 4, N'Great', N'20151202085309', 6, CAST(0x0000A85700000000 AS DateTime))
INSERT [dbo].[CustomerInvoice] ([ID], [ProductID], [Quantity], [Details], [InvoiceID], [CustomerID], [InvoiceDate]) VALUES (14, 1, 10, N'Joss Collections', N'20151202085309', 6, CAST(0x0000A81E00000000 AS DateTime))
INSERT [dbo].[CustomerInvoice] ([ID], [ProductID], [Quantity], [Details], [InvoiceID], [CustomerID], [InvoiceDate]) VALUES (15, 2, 10, N'Customer likes this a lot.', N'20151203101624', 1, CAST(0x0000A80F00000000 AS DateTime))
INSERT [dbo].[CustomerInvoice] ([ID], [ProductID], [Quantity], [Details], [InvoiceID], [CustomerID], [InvoiceDate]) VALUES (16, 2, 10, N'Customer likes this a lot.', N'20151203102205', 1, CAST(0x0000A7E500000000 AS DateTime))
INSERT [dbo].[CustomerInvoice] ([ID], [ProductID], [Quantity], [Details], [InvoiceID], [CustomerID], [InvoiceDate]) VALUES (17, 2, 2, N'Wao!! Great.', N'20151203103844', 1, CAST(0x0000A7CE00000000 AS DateTime))
INSERT [dbo].[CustomerInvoice] ([ID], [ProductID], [Quantity], [Details], [InvoiceID], [CustomerID], [InvoiceDate]) VALUES (18, 1, 4, N'Just Awesome.', N'20151203103844', 1, CAST(0x0000A7BD00000000 AS DateTime))
INSERT [dbo].[CustomerInvoice] ([ID], [ProductID], [Quantity], [Details], [InvoiceID], [CustomerID], [InvoiceDate]) VALUES (19, 2, 12, N'Customers just love the product.', N'20151203104143', 2, CAST(0x0000A78600000000 AS DateTime))
INSERT [dbo].[CustomerInvoice] ([ID], [ProductID], [Quantity], [Details], [InvoiceID], [CustomerID], [InvoiceDate]) VALUES (20, 1, 2, N'Awesome Collections.', N'20151203104143', 2, CAST(0x0000A79000000000 AS DateTime))
INSERT [dbo].[CustomerInvoice] ([ID], [ProductID], [Quantity], [Details], [InvoiceID], [CustomerID], [InvoiceDate]) VALUES (21, 2, 6, N'Awesome Collections.', N'20151203104945', 1, CAST(0x0000A78F00000000 AS DateTime))
INSERT [dbo].[CustomerInvoice] ([ID], [ProductID], [Quantity], [Details], [InvoiceID], [CustomerID], [InvoiceDate]) VALUES (22, 1, 6, N'Great Collection.', N'20151203104945', 1, CAST(0x0000A79300000000 AS DateTime))
INSERT [dbo].[CustomerInvoice] ([ID], [ProductID], [Quantity], [Details], [InvoiceID], [CustomerID], [InvoiceDate]) VALUES (23, 1, 10, N'Customer likes this a lot.', N'20151203105528', 1, CAST(0x0000A76F00000000 AS DateTime))
INSERT [dbo].[CustomerInvoice] ([ID], [ProductID], [Quantity], [Details], [InvoiceID], [CustomerID], [InvoiceDate]) VALUES (24, 2, 4, N'Great Collection.', N'20151203105528', 1, CAST(0x0000A75300000000 AS DateTime))
INSERT [dbo].[CustomerInvoice] ([ID], [ProductID], [Quantity], [Details], [InvoiceID], [CustomerID], [InvoiceDate]) VALUES (25, 2, 1, N'Just Great.', N'20151203110653', 7, CAST(0x0000A75C00000000 AS DateTime))
INSERT [dbo].[CustomerInvoice] ([ID], [ProductID], [Quantity], [Details], [InvoiceID], [CustomerID], [InvoiceDate]) VALUES (26, 1, 1, N'Customers are happy with the product.', N'20151203110653', 7, CAST(0x0000A76400000000 AS DateTime))
更新 1 - 此计算针对过去 12 个月进行,请查看下面的数量计算方式:
Month-Year Quantity
Jul 2018 80 (Sum of quantity from Jul 2018 to Jun 2017)
Jun 2018 70 (Sum of quantity from Jun 2018 to May 2017)
May 2018 42 (Sum of quantity from May 2018 to Apr 2017)
Apr 2018 11 ...........................................
Mar 2018 6 ...........................................
Feb 2018 10 ...........................................
Jan 2018 16 ...........................................
Dec 2017 1 ...........................................
..........................................................
Jun 2017 10 (Sum of quantity from Jun 2017 to May 2016)
最佳答案
这回答了 SQL Server 2012+ 的问题(我回答问题时没有指定 SQL Server 版本)。
我建议使用累计和。这假设您每个月都有数据,但这似乎是合理的。对于累计年初至今:
SELECT YEAR(m.InvoiceDate) as yyyy,
MONTH(m.InvoiceDate) as mm,
SUM(m.Quantity) as Quantity,
SUM(SUM(m.Quantity)) OVER (PARTITION BY YEAR(m.InvoiceDate)
ORDER BY MONTH(m.InvoiceDate)
)
FROM CustomerInvoice m
WHERE YEAR(m.InvoiceDate) = 2018
GROUP BY YEAR(m.InvoiceDate), MONTH(m.InvoiceDate)
ORDER BY YEAR(m.InvoiceDate), MONTH(m.InvoiceDate);
请注意,这使用 SUM(m.Quantity)
而不是 COUNT()
。 SUM()
根据您的描述更有意义。
如果您想要累计运行 12 个月的总和,忽略年份界限,请使用窗口子句:
SELECT YEAR(m.InvoiceDate) as yyyy,
MONTH(m.InvoiceDate) as mm,
SUM(m.Quantity) as Quantity,
SUM(SUM(m.Quantity)) OVER (PARTITION BY YEAR(m.InvoiceDate)
ORDER BY MONTH(m.InvoiceDate)
ROWS BETWEEN 11 PRECEDING AND CURRENT ROW
)
FROM CustomerInvoice m
GROUP BY YEAR(m.InvoiceDate), MONTH(m.InvoiceDate)
ORDER BY YEAR(m.InvoiceDate), MONTH(m.InvoiceDate);
编辑:
在 SQL Server 2008 中,您可以将第一个查询表示为:
WITH ym as (
SELECT YEAR(m.InvoiceDate) as yyyy,
MONTH(m.InvoiceDate) as mm,
SUM(m.Quantity) as Quantity,
FROM CustomerInvoice m
WHERE YEAR(m.InvoiceDate) = 2018
GROUP BY YEAR(m.InvoiceDate), MONTH(m.InvoiceDate)
)
SELECT ym.yyyymm, ym.quantity, ym2.sum_Quantity)
FROM ym CROSS APPLY
(SELECT SUM(ym2.quantity) as sum_quantity
FROM ym ym2
WHERE ym.yyyy = ym2.yyyy AND ym2.month <= ym.month
) ym2
ORDER BY YEAR(m.InvoiceDate), MONTH(m.InvoiceDate);
对于 SQL Server 2008 中的滚动总和:
WITH ym as (
SELECT YEAR(m.InvoiceDate) as yyyy,
MONTH(m.InvoiceDate) as mm,
SUM(m.Quantity) as Quantity,
FROM CustomerInvoice m
WHERE YEAR(m.InvoiceDate) = 2018
GROUP BY YEAR(m.InvoiceDate), MONTH(m.InvoiceDate)
)
SELECT ym.yyyymm, ym.quantity, ym2.sum_Quantity)
FROM ym CROSS APPLY
(SELECT SUM(ym2.quantity) as sum_quantity
FROM ym ym2
WHERE ym2.InvoiceDate >= DATEADD(year, -1, ym.InvoiceDate) AND
ym2.InvoiceDate <= ym.InvoiceDate
) ym2
ORDER BY YEAR(m.InvoiceDate), MONTH(m.InvoiceDate);
关于sql - 过去 12 个月和单个月份的列数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51982533/
您如何计算Oracle表中的列数? 最佳答案 SELECT count(*) FROM user_tab_columns WHERE table_name = 'FOO' 应该给你foo中的列数。您可
我有一个宽度为 100% 的 div,里面有大约 10 张图片,所有图片的宽度和大小都不同。我希望能够无缝地将这些图像彼此 float ,我已经做到了,但它们不会在屏幕上拉伸(stretch),它们都
请看下面的代码,表格应该根据下面selected id选项中选择的输入更改行数,但只读取select id的第一个值,行数不会根据选择,你能指出我代码中的错误吗? http://jsfiddle.ne
我需要一个可以在 3×3 和 4×4 之间切换的动态 gridlayout。我可以 setRowCount 和 setColumnCount 从 3 到4 但不是从 4 到 3。它会显示以下问题: C
这里有一个没有真正答案的类似问题:CSS columns bug — 5 column count only showing 4 (with images) 我正在使用 column-count 来显
我正在尝试找到一种方法来填充摊销时间表,如下所示。 我目前使用的公式是: =IF($B2=$1:$1,$A2/$C2," ") 但是,这仅填充了第一个点(“起点”),我想做的是根据摊销期抵消这一点。期
我运行以下代码: import cv2 import numpy as np img_file='pokemon.jpg' img=cv2.imread(img_file) # print tota
我正在使用 asp.net 和 vb。这里我将数据绑定(bind)到Gridview。该数据集有 5 条记录,3 列,并且 gridview 显示 5 条记录,3 列。但 Gridview.colum
我已经为我的数据库苦苦挣扎了一段时间,几乎不可能找到我的老师。我的代码如下: INSERT INTO `database28`.`activity` VALUES ("Stefan", 1.1.2.2
我有一个执行 SQL 命令的 python 脚本,并尝试将所有数据插入表中。问题是我有一个条目列表,例如: "Bob", "bob@gmail.com" "John", "john@gmail.com
有谁知道这是否可以通过 bootstrap mixins 来完成。 列(父级数) 像什么Neat有。 在 Neat the Columns 中,mixins 的工作方式如下 @mixin span-c
我有这样的网址:http://xn----7sbabhi8cwaajmue5o.xn--p1ai/cars/search/by_man_and_model?by_manufacturer=115 你可
使用 Bootstrap 3,我正在寻找一种网格布局,其中列宽在任何屏幕分辨率下都是固定的(比如 200 像素),但它们的数量取决于屏幕宽度。 换句话说,我知道列的宽度,但我不知道每行有多少列,因为这
public class SmallestColumn2{ public static void main(String[] args){ int [][] smallest
如果我只有 9 列,但我想将它们分散到 100% 的 div。如何做到这一点? NSW VIC QLD WA SA TAS ACT N
我想在这里更改列的顺序: #container { position: relative; width: 600px; } #column-wrapper { -webkit-
1、df=DataFrame([{‘A':'11','B':'12'},{‘A':'111','B':'121'},{‘A':'1111','B':'1211'}])
我想将csv文件形式的数据导入表中。[使用Oracle SQL Developer]。我有数百个文件,每个文件有大约50列。 从SQL * Loader Wiki(http://www.orafaq.
好吧,这对我来说似乎很基础,尽管我找不到任何关于如何实现它的信息。 假设我有某种 QGridLayout 包含相同大小的小部件。现在我想根据布局大小更新其行数/列数。 因此,例如在相对较宽的屏幕上它看
我在 https://cwiki.apache.org/confluence/display/Hive/Home 中找不到任何记录在案的限制。 我的猜测是行数或列数没有限制。文件大小受文件系统限制。通
我是一名优秀的程序员,十分优秀!