问题
please help me. I am having problem with my data and my query. Please look at my table design first.
CREATE TABLE [dbo].[my_table](
[ID] [int] NOT NULL,
[date] [datetime] NULL,
[value] [int] NULL,
[product_code] [varchar](50) NULL,
[expectedvalue] [int] NULL,
CONSTRAINT [PK_my_table] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
And this is the data, all data got from machine in every minutes.
Data Scenario 1 (Value at 06:30 is not 0):
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code], [expectedvalue]) VALUES (1521798, CAST(0x0000A97C006A9B06 AS DateTime), 1164, N'17NPPN_M838', 1164)
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code], [expectedvalue]) VALUES (1521799, CAST(0x0000A97C006AE1D4 AS DateTime), 1164, N'17NPPN_M838', 1164)
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code], [expectedvalue]) VALUES (1521800, CAST(0x0000A97C006B2894 AS DateTime), 1172, N'17NPPN_M838', 8)
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code], [expectedvalue]) VALUES (1521801, CAST(0x0000A97C006B6E38 AS DateTime), 1172, N'17NPPN_M838', 8)
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code], [expectedvalue]) VALUES (1521802, CAST(0x0000A97C006BB501 AS DateTime), 1172, N'17NPPN_M838', 8)
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code], [expectedvalue]) VALUES (1521803, CAST(0x0000A97C006BFA9F AS DateTime), 1172, N'17NPPN_M838', 8)
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code], [expectedvalue]) VALUES (1521804, CAST(0x0000A97C006C4167 AS DateTime), 1181, N'17NPPN_M838', 17)
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code], [expectedvalue]) VALUES (1521805, CAST(0x0000A97C006C882E AS DateTime), 1193, N'17NPPN_M838', 29)
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code], [expectedvalue]) VALUES (1521806, CAST(0x0000A97C006CCDCC AS DateTime), 1202, N'17NPPN_M838', 38)
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code], [expectedvalue]) VALUES (1521807, CAST(0x0000A97C006D1493 AS DateTime), 1210, N'17NPPN_M838', 46)
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code], [expectedvalue]) VALUES (1521808, CAST(0x0000A97C006D5A31 AS DateTime), 1220, N'17NPPN_M838', 56)
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code], [expectedvalue]) VALUES (1521809, CAST(0x0000A97C006DA0F9 AS DateTime), 1226, N'17NPPN_M838', 62)
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code], [expectedvalue]) VALUES (1521810, CAST(0x0000A97C006DE7C0 AS DateTime), 1237, N'17NPPN_M838', 73)
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code], [expectedvalue]) VALUES (1521811, CAST(0x0000A97C006E2D5F AS DateTime), 1248, N'17NPPN_M838', 84)
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code], [expectedvalue]) VALUES (1521812, CAST(0x0000A97C006E7427 AS DateTime), 1253, N'17NPPN_M838', 89)
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code], [expectedvalue]) VALUES (1521813, CAST(0x0000A97C006EB9C5 AS DateTime), 1264, N'17NPPN_M838', 100)
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code], [expectedvalue]) VALUES (1521814, CAST(0x0000A97C006F008C AS DateTime), 1276, N'17NPPN_M838', 112)
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code], [expectedvalue]) VALUES (1521815, CAST(0x0000A97C006F4753 AS DateTime), 1286, N'17NPPN_M838', 122)
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code], [expectedvalue]) VALUES (1521816, CAST(0x0000A97C006F8CF2 AS DateTime), 1293, N'17NPPN_M838', 129)
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code], [expectedvalue]) VALUES (1521817, CAST(0x0000A97C006FD3B8 AS DateTime), 1305, N'17NPPN_M838', 141)
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code], [expectedvalue]) VALUES (1521818, CAST(0x0000A97C00701957 AS DateTime), 1317, N'17NPPN_M838', 153)
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code], [expectedvalue]) VALUES (1521819, CAST(0x0000A97C00706025 AS DateTime), 1327, N'17NPPN_M838', 163)
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code], [expectedvalue]) VALUES (1521820, CAST(0x0000A97C0070A6E6 AS DateTime), 1336, N'17NPPN_M838', 172)
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code], [expectedvalue]) VALUES (1521821, CAST(0x0000A97C0070EC85 AS DateTime), 1348, N'17NPPN_M838', 184)
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code], [expectedvalue]) VALUES (1521822, CAST(0x0000A97C0071334C AS DateTime), 1360, N'17NPPN_M838', 196)
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code], [expectedvalue]) VALUES (1521823, CAST(0x0000A97C007178F0 AS DateTime), 1371, N'17NPPN_M838', 207)
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code], [expectedvalue]) VALUES (1521824, CAST(0x0000A97C0071BFB2 AS DateTime), 0, N'17LPPL_M848', 207)
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code], [expectedvalue]) VALUES (1521825, CAST(0x0000A97C0072067F AS DateTime), 0, N'17LPPL_M848', 207)
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code], [expectedvalue]) VALUES (1521826, CAST(0x0000A97C00724C16 AS DateTime), 0, N'17LPPL_M848', 207)
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code], [expectedvalue]) VALUES (1521827, CAST(0x0000A97C007292DE AS DateTime), 12, N'17LPPL_M848', 219)
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code], [expectedvalue]) VALUES (1521828, CAST(0x0000A97C0072D87C AS DateTime), 21, N'17LPPL_M848', 228)
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code], [expectedvalue]) VALUES (1521829, CAST(0x0000A97C00731F44 AS DateTime), 30, N'17LPPL_M848', 237)
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code], [expectedvalue]) VALUES (1521830, CAST(0x0000A97C0073660B AS DateTime), 42, N'17LPPL_M848', 249)
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code], [expectedvalue]) VALUES (1521831, CAST(0x0000A97C0073ABA8 AS DateTime), 50, N'17LPPL_M848', 257)
Data Scenario 2 (Value at 06:30 is 0) :
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code]) VALUES (1521798, CAST(0x0000A97C006A9B06 AS DateTime), 1164, N'17NPPN_M838')
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code]) VALUES (1521799, CAST(0x0000A97C006AE1D4 AS DateTime), 1164, N'17NPPN_M838')
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code]) VALUES (1521800, CAST(0x0000A97C006B2894 AS DateTime), 0, N'17LPPL_M848')
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code]) VALUES (1521801, CAST(0x0000A97C006B6E38 AS DateTime), 0, N'17LPPL_M848')
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code]) VALUES (1521802, CAST(0x0000A97C006BB501 AS DateTime), 0, N'17LPPL_M848')
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code]) VALUES (1521803, CAST(0x0000A97C006BFA9F AS DateTime), 12, N'17LPPL_M848')
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code]) VALUES (1521804, CAST(0x0000A97C006C4167 AS DateTime), 32, N'17LPPL_M848')
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code]) VALUES (1521805, CAST(0x0000A97C006C882E AS DateTime), 43, N'17LPPL_M848')
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code]) VALUES (1521806, CAST(0x0000A97C006CCDCC AS DateTime), 54, N'17LPPL_M848')
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code]) VALUES (1521807, CAST(0x0000A97C006D1493 AS DateTime), 54, N'17LPPL_M848')
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code]) VALUES (1521808, CAST(0x0000A97C006D5A31 AS DateTime), 65, N'17LPPL_M848')
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code]) VALUES (1521809, CAST(0x0000A97C006DA0F9 AS DateTime), 67, N'17LPPL_M848')
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code]) VALUES (1521810, CAST(0x0000A97C006DE7C0 AS DateTime), 76, N'17LPPL_M848')
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code]) VALUES (1521811, CAST(0x0000A97C006E2D5F AS DateTime), 97, N'17LPPL_M848')
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code]) VALUES (1521812, CAST(0x0000A97C006E7427 AS DateTime), 104, N'17LPPL_M848')
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code]) VALUES (1521813, CAST(0x0000A97C006EB9C5 AS DateTime), 108, N'17LPPL_M848')
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code]) VALUES (1521814, CAST(0x0000A97C006F008C AS DateTime), 204, N'17LPPL_M848')
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code]) VALUES (1521815, CAST(0x0000A97C006F4753 AS DateTime), 224, N'17LPPL_M848')
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code]) VALUES (1521816, CAST(0x0000A97C006F8CF2 AS DateTime), 245, N'17LPPL_M848')
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code]) VALUES (1521817, CAST(0x0000A97C006FD3B8 AS DateTime), 267, N'17LPPL_M848')
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code]) VALUES (1521818, CAST(0x0000A97C00701957 AS DateTime), 289, N'17LPPL_M848')
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code]) VALUES (1521819, CAST(0x0000A97C00706025 AS DateTime), 290, N'17LPPL_M848')
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code]) VALUES (1521820, CAST(0x0000A97C0070A6E6 AS DateTime), 0, N'17LPPL_D220')
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code]) VALUES (1521821, CAST(0x0000A97C0070EC85 AS DateTime), 0, N'17LPPL_D220')
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code]) VALUES (1521822, CAST(0x0000A97C0071334C AS DateTime), 0, N'17LPPL_D220')
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code]) VALUES (1521823, CAST(0x0000A97C007178F0 AS DateTime), 0, N'17LPPL_D220')
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code]) VALUES (1521824, CAST(0x0000A97C0071BFB2 AS DateTime), 5, N'17LPPL_D220')
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code]) VALUES (1521825, CAST(0x0000A97C0072067F AS DateTime), 8, N'17LPPL_D220')
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code]) VALUES (1521826, CAST(0x0000A97C00724C16 AS DateTime), 18, N'17LPPL_D220')
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code]) VALUES (1521827, CAST(0x0000A97C007292DE AS DateTime), 25, N'17LPPL_D220')
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code]) VALUES (1521828, CAST(0x0000A97C0072D87C AS DateTime), 36, N'17LPPL_D220')
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code]) VALUES (1521829, CAST(0x0000A97C00731F44 AS DateTime), 46, N'17LPPL_D220')
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code]) VALUES (1521830, CAST(0x0000A97C0073660B AS DateTime), 54, N'17LPPL_D220')
INSERT [dbo].[my_table] ([ID], [date], [value], [product_code]) VALUES (1521831, CAST(0x0000A97C0073ABA8 AS DateTime), 66, N'17LPPL_D220')
I want to count value from 06:30 today until 06:29 tomorrow in every minutes. Because I need to make chart from it.
Here is my current query :
;WITH RESULT AS (SELECT
TT.productcode
, MaxValueThatday = max(maxval.MAXVALUE )
, MaxValueBefore630NextDay = max(MAXValBefore630.MAXVALUE)
, ResultSubstraction =
CASE WHEN max(maxval.MAXVALUE ) <> max(MAXValBefore630.MAXVALUE)
THEN max(maxval.MAXVALUE ) - max(MAXValBefore630.MAXVALUE)
ELSE max(maxval.MAXVALUE )
END
FROM [dbo].my_table TT
OUTER APPLY(
SELECT max(value) MAXVALUE
, product_code
FROM [dbo].my_table aa
WHERE Aa.productcode = tt.productcode
group by productcode
)maxval
OUTER APPLY(
SELECT max(A.value) MAXVALUE
, productcode
FROM [dbo].my_table A
WHERE DATEPART(HOUR,date) <= 6 AND DATEPART(MINUTE,date) < 30
and A.productcode = tt.productcode
group by productcode
)MAXValBefore630
where ( date> DATEADD(MINUTE,390,CAST({ fn CURDATE()} AS DATETIME) ) ) --6:30 today
group by tt.productcode
)
SELECT SUM(ResultSubstraction)
FROM RESULT
So, how to count total value in every minutes start from 06:30 today until 06:29 tomorrow?
important notes are :
The value should count from 06:30.
If at 06:30 the value is 0, so no need to sum value with previous row (value), just include the current value to the expectedvalue column.
- If at 06:30 the value is not 0, so we need to do value in the current row (06:30) - value current row -1 (06:29). The result should include in to the expectedvalue column. And then we need to sum them in every row using formula : (current value - current value -1) + current expectedvalue.
- If the the current value (current row) is same with previous row, no need to sum them. Just use the current expectedvalue.
Do my explanation clear ?
I hope all of you get what I mean and want to help me to get out of my problem..
Thank you so much for your help.
回答1:
Initial Answer:
Just use a CROSS APPLY
and do a SUM
on the value
SELECT ID, DATE, VALUE, VALUEEXPECTED
FROM [dbo].my_table TT
CROSS APPLY
(
SELECT SUM (VALUE) AS VALUEEXPECTED
FROM [dbo].my_table AS X
WHERE X.ID <= TT.ID
AND X.DATE > DATEADD(MINUTE,390,CAST({ fn CURDATE()} AS DATETIME) ) )
) AS VE
New Answer :
for changes to the question, new way of calculating the expected value.
select t.*,
expectedvalue = t.value - isnull(s.value, 0),
-- the following 3 value are included for your reference on how the query works
r.ref_date, s.date, s.value
from my_table t
cross apply
(
-- calculate the reference 06:30 date & time for each date
select ref_date = dateadd(day, datediff(day, 0, dateadd(minute, -390, t.[date])), '06:30')
) r
outer apply
(
-- get the last value before 06:30 ref date
select top 1 x.date, value = x.value
from my_table x
where x.id < t.id
and x.[date] < r.ref_date
order by x.date desc
) s
来源:https://stackoverflow.com/questions/52809103/sql-server-query-to-count-value-every-0630