修改查询sql
我有一个查询将数据从 1 分钟聚合(压缩)到任何其他时间范围,并且它工作得很好。
Use StockDataFromSella;
DECLARE @D1 DateTime
DECLARE @D2 DateTime
DECLARE @Interval FLOAT
SET @D1 = '2008-09-21T09:00:00.000'
SET @D2 = '2010-10-20T17:30:00.000'
SET @Interval = 15
;WITH
L0 AS (SELECT 1 AS c UNION ALL SELECT 1),
L1 AS (SELECT 1 AS c FROM L0 A CROSS JOIN L0 B),
L2 AS (SELECT 1 AS c FROM L1 A CROSS JOIN L1 B),
L3 AS (SELECT 1 AS c FROM L2 A CROSS JOIN L2 B),
L4 AS (SELECT 1 AS c FROM L3 A CROSS JOIN L3 B),
Nums AS (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 0)) AS i FROM L4),
Ranges AS(
SELECT
DATEADD(MINUTE,@Interval*(i-1),@D1) AS StartRange,
DATEADD(MINUTE,@Interval*i,@D1) AS NextRange
FROM Nums where i <= 1+CEILING(DATEDIFF(MINUTE,@D1,@D2)/@Interval))
,cte AS (
SELECT
*
,ROW_NUMBER() OVER (PARTITION BY Simbolo,r.StartRange ORDER BY [DataOra]) AS RN_ASC
,ROW_NUMBER() OVER (PARTITION BY Simbolo,r.StartRange ORDER BY [DataOra] DESC) AS RN_DESC
FROM Ranges r
JOIN dbo.tbl1MinENI p ON p.[DataOra] >= r.StartRange and p.[DataOra] < r.NextRange )
SELECT
Simbolo,
MAX(CASE WHEN RN_ASC=1 THEN [DataOra] END) AS DataOra,
MAX(CASE WHEN RN_ASC=1 THEN [Apertura] END) AS [Apertura],
MAX(Massimo) Massimo,
MIN(Minimo) Minimo,
MAX(CASE WHEN RN_DESC=1 THEN [Chiusura] END) AS [Chiusura],
SUM(Volume) Volume
/*MAX(CASE WHEN RN_DESC=1 THEN [DataOra] END) AS ChiusuraDataOra*/
FROM cte
GROUP BY Simbolo,StartRange
ORDER BY DataOra
我想将第二列 DataOra(DateTime) 分成两个不同的列,一列用于日期(如果可能采用 dd/mm/yyyy 格式),第二列仅用于时间。 非常感谢任何帮助,谢谢。 阿尔贝托
I have a query to aggregate (compress) data from 1 min to any other time frame, and it works perfectly.
Use StockDataFromSella;
DECLARE @D1 DateTime
DECLARE @D2 DateTime
DECLARE @Interval FLOAT
SET @D1 = '2008-09-21T09:00:00.000'
SET @D2 = '2010-10-20T17:30:00.000'
SET @Interval = 15
;WITH
L0 AS (SELECT 1 AS c UNION ALL SELECT 1),
L1 AS (SELECT 1 AS c FROM L0 A CROSS JOIN L0 B),
L2 AS (SELECT 1 AS c FROM L1 A CROSS JOIN L1 B),
L3 AS (SELECT 1 AS c FROM L2 A CROSS JOIN L2 B),
L4 AS (SELECT 1 AS c FROM L3 A CROSS JOIN L3 B),
Nums AS (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 0)) AS i FROM L4),
Ranges AS(
SELECT
DATEADD(MINUTE,@Interval*(i-1),@D1) AS StartRange,
DATEADD(MINUTE,@Interval*i,@D1) AS NextRange
FROM Nums where i <= 1+CEILING(DATEDIFF(MINUTE,@D1,@D2)/@Interval))
,cte AS (
SELECT
*
,ROW_NUMBER() OVER (PARTITION BY Simbolo,r.StartRange ORDER BY [DataOra]) AS RN_ASC
,ROW_NUMBER() OVER (PARTITION BY Simbolo,r.StartRange ORDER BY [DataOra] DESC) AS RN_DESC
FROM Ranges r
JOIN dbo.tbl1MinENI p ON p.[DataOra] >= r.StartRange and p.[DataOra] < r.NextRange )
SELECT
Simbolo,
MAX(CASE WHEN RN_ASC=1 THEN [DataOra] END) AS DataOra,
MAX(CASE WHEN RN_ASC=1 THEN [Apertura] END) AS [Apertura],
MAX(Massimo) Massimo,
MIN(Minimo) Minimo,
MAX(CASE WHEN RN_DESC=1 THEN [Chiusura] END) AS [Chiusura],
SUM(Volume) Volume
/*MAX(CASE WHEN RN_DESC=1 THEN [DataOra] END) AS ChiusuraDataOra*/
FROM cte
GROUP BY Simbolo,StartRange
ORDER BY DataOra
I would like to split second column DataOra(DateTime) into two diferent columns, one for Date (if possibile in dd/mm/yyyy format) and the second for Time only.
Any help very appreciated, thanks.
Alberto
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
获取这些值的最简单方法是使用
CONVERT
。当您转换日期时,SQL Server 有一些内置的日期格式。这两个代码(103 和 114)对应于您想要的格式。请参阅 http://msdn.microsoft.com/en-us/library/ms187928。 aspx 了解有关这些格式以及您可以转换成的其他格式的更多详细信息。
The easiest way to get these values would be to use
CONVERT
. SQL Server has some built-in date formatting when you convert a Date.The two codes (103 and 114) correspond to the formats that you want. See http://msdn.microsoft.com/en-us/library/ms187928.aspx for more details about those and the other formats you can convert to.
看来您只需要更改选择即可。
而不是这个
使用这个:
希望它有帮助。
As it seems you just only need to change the select.
Instead of this
Use this:
Hope it helps.