脚本:
IF OBJECT_ID('tempdb..#Table2') IS NOT NULL DROP TABLE #Table;
CREATE TABLE #GPIO ([NUM] TINYINT, [MaterialIntID] BIGINT, [ElementIntID] BIGINT, [DateAdded] DATETIME);
INSERT INTO #GPIO VALUES (4, 8, NULL, '2019-01-06 00:01:14.190');
INSERT INTO #GPIO VALUES (4, 8, NULL, '2019-01-06 00:01:26.023');
INSERT INTO #GPIO VALUES (6, 9, 11, '2019-01-06 03:29:07.840');
INSERT INTO #GPIO VALUES (4, 8, NULL, '2019-01-06 03:40:33.300');
INSERT INTO #GPIO VALUES (4, 9, 11, '2019-01-06 03:40:33.300');
INSERT INTO #GPIO VALUES (5, 8, NULL, '2019-01-06 03:50:18.490');
INSERT INTO #GPIO VALUES (5, 9, 17, '2019-01-06 03:50:18.490');
INSERT INTO #GPIO VALUES (5, 8, NULL, '2019-01-06 03:52:33.303');
INSERT INTO #GPIO VALUES (5, 9, 5, '2019-01-06 03:52:33.303');
INSERT INTO #GPIO VALUES (4, 8, NULL, '2019-01-06 03:55:28.417');
INSERT INTO #GPIO VALUES (4, 9, 10, '2019-01-06 03:55:28.417');
INSERT INTO #GPIO VALUES (4, 9, 7, '2019-01-06 03:56:35.370');
INSERT INTO #GPIO VALUES (4, 9, 2, '2019-01-06 03:57:36.370');
INSERT INTO #GPIO VALUES (5, 9, 5,'2019-01-06 04:01:15.127');
INSERT INTO #GPIO VALUES (5, 9, 7, '2019-01-06 03:58:35.370');
INSERT INTO #GPIO VALUES (5, 9, 7,'2019-01-06 04:01:16.127');
INSERT INTO #GPIO VALUES (5, 8, 7,'2019-01-06 04:01:17.127');
INSERT INTO #GPIO VALUES (5, 9, 7,'2019-01-06 04:01:18.127');
INSERT INTO #GPIO VALUES (4, 9, 7, '2019-01-06 04:01:00.370');
我有这些声明:
CASE 1
@ActionDate DATE = '2019-01-07',
@MaterialIntID BIGINT = NULL,
@ElementIntID BIGINT = NULL,
@SameNum BIT = 0,
@Counti INT = 2;
CASE 2
@ActionDate DATE = '2019-01-07',
@MaterialIntID BIGINT = NULL,
@ElementIntID BIGINT = NULL,
@SameNum BIT = 0,
@Counti INT = 3;
CASE 3
@ActionDate DATE = '2019-01-07',
@MaterialIntID BIGINT = 9,
@ElementIntID BIGINT = NULL,
@SameNum BIT = 0,
@Counti INT = 2;
CASE 4
@ActionDate DATE = '2019-01-07',
@MaterialIntID BIGINT = 9,
@ElementIntID BIGINT = 7,
@SameNum BIT = 0,
@Counti INT = 2;
CASE 5
@ActionDate DATE = '2019-01-07',
@MaterialIntID BIGINT = 9,
@ElementIntID BIGINT = 7,
@SameNum BIT = 1,
@Counti INT = 2;
CASE 6
@ActionDate DATE = '2019-01-07',
@MaterialIntID BIGINT = 9,
@ElementIntID BIGINT = NULL,
@SameNum BIT = 1,
@Counti INT = 2;
CASE 7
@ActionDate DATE = '2019-01-07',
@MaterialIntID BIGINT = NULL,
@ElementIntID BIGINT = NULL,
@SameNum BIT = 1,
@Counti INT = 3;
我执行了此脚本,但仅执行了[MaterialIntID]的PARTITION。我还需要考虑通过[ElementIntID]进行PARTITION,但前提是我在声明中具有名为@ElementIntID的参数,并且也需要通过[NUM]进行PARTITION,但前提是我具有参数
计数必须至少等于@Counti
;WITH [FirstList] AS
(
SELECT [NUM], [DateAdded], [MaterialIntID], [ElementIntID],
ROW_NUMBER() OVER (ORDER BY [DateAdded]) -
ROW_NUMBER() OVER (PARTITION BY [MaterialIntID] ORDER BY [DateAdded]) AS [GRP]
FROM [Gpio]
WHERE DATEDIFF(DAY, [DateAdded], @ActionDate)=1
)
SELECT [TT].[NUM], [TT].[MaterialIntID], [TT].[ElementIntID], [TT].[DateAdded], [TT].[GRP]
FROM [FirstList] AS [TT]
JOIN ( SELECT [MaterialIntID], [GRP]
FROM [FirstList]
GROUP BY [MaterialIntID], [GRP]
HAVING COUNT(*) >= @Counti) AS [HV] ON [TT].[MaterialIntID]=[HV].[MaterialIntID] AND [TT].[GRP]=[HV].[GRP]