私の最近の投稿「t-sql 順次期間」に似ていますが、まったく同じではありませんが、列 x (私の場合は列「who」) の変更に基づいて行番号をリセットしたいと考えています。
生の (ish) データの小さなサンプルを返す最初のクエリを次に示します。
SELECT DISTINCT chr.custno,
CAST(LEFT(CONVERT( VARCHAR(20),chr.moddate,112),10)+ ' ' + chr.modtime AS DATETIME)as moddate,
chr.who
FROM <TABLE> chr
WHERE chr.custno = 581827
AND LEFT(chr.who, 5) = 'EMSZC'
AND chr.[description] NOT LIKE 'Recalled and viewed this customer'
ORDER BY chr.custno
結果:
custno moddate who
581827 2012-11-08 08:38:00.000 EMSZC14
581827 2012-11-08 08:41:10.000 EMSZC14
581827 2012-11-08 08:53:46.000 EMSZC14
581827 2012-11-08 08:57:04.000 EMSZC14
581827 2012-11-08 08:58:35.000 EMSZC14
581827 2012-11-08 08:59:13.000 EMSZC14
581827 2012-11-08 09:00:06.000 EMSZC14
581827 2012-11-08 09:04:39.000 EMSZC49 Reset row number to 1
581827 2012-11-08 09:05:04.000 EMSZC49
581827 2012-11-08 09:06:32.000 EMSZC49
581827 2012-11-08 09:12:03.000 EMSZC49
581827 2012-11-08 09:12:38.000 EMSZC49
581827 2012-11-08 09:14:18.000 EMSZC49
581827 2012-11-08 09:17:35.000 EMSZC14 Reset row number to 1
2 番目のステップは、行番号を追加することです (最初のクエリでは、DISTINCT という単語を使用しているため、これを実行しませんでした)。そう…</p>
WITH c1 AS (
SELECT DISTINCT chr.custno
CAST(LEFT(CONVERT( VARCHAR(20),chr.moddate,112),10)+ ' ' + chr.modtime AS DATETIME)as moddate,
chr.who
FROM <TABLE> chr
WHERE chr.custno = 581827
AND LEFT(chr.who, 5) = 'EMSZC'
AND chr.[description] NOT LIKE 'Recalled and viewed this customer'
)
SELECT ROW_NUMBER() OVER (PARTITION BY custno ORDER BY custno, moddate, who) AS RowID, custno, moddate, who
FROM c1
結果:
RowID custno moddate who
1 581827 2012-11-08 08:38:00.000 EMSZC14
2 581827 2012-11-08 08:41:10.000 EMSZC14
3 581827 2012-11-08 08:53:46.000 EMSZC14
4 581827 2012-11-08 08:57:04.000 EMSZC14
5 581827 2012-11-08 08:58:35.000 EMSZC14
6 581827 2012-11-08 08:59:13.000 EMSZC14
7 581827 2012-11-08 09:00:06.000 EMSZC14
8 581827 2012-11-08 09:04:39.000 EMSZC49 Reset row number to 1
9 581827 2012-11-08 09:05:04.000 EMSZC49
10 581827 2012-11-08 09:06:32.000 EMSZC49
11 581827 2012-11-08 09:12:03.000 EMSZC49
12 581827 2012-11-08 09:12:38.000 EMSZC49
13 581827 2012-11-08 09:14:18.000 EMSZC49
14 581827 2012-11-08 09:17:35.000 EMSZC14 Reset row number to 1
次のステップは、私が行き詰まっているところです。目標は、「who」列の値が変更されるたびに RowID を 1 にリセットすることです。次のコードは、「ほとんどそこにある」結果を取得します (このコードはどこかから盗んだ/借りたものであることに注意してください。しかし、今では Web サイトを見つけることができません)。
WITH c1 AS (
SELECT DISTINCT chr.custno,
CAST(LEFT(CONVERT( VARCHAR(20),chr.moddate,112),10)+ ' ' + chr.modtime AS DATETIME)as moddate,
chr.who
FROM <TABLE> chr
WHERE chr.custno = 581827
AND LEFT(chr.who, 5) = 'EMSZC'
AND chr.[description] NOT LIKE 'Recalled and viewed this customer'
)
, c1a AS (
SELECT ROW_NUMBER() OVER (PARTITION BY custno ORDER BY custno, moddate, who) AS RowID, custno, moddate, who
FROM c1
)
SELECT x.RowID - y.MinID + 1 AS Row,
x.custno, x.Touch, x.moddate, x.who
FROM (
SELECT custno, who, MIN(RowID) AS MinID
FROM c1a
GROUP BY custno, who
) AS y
INNER JOIN c1a x ON x.custno = y.custno AND x.who = y.who
結果:
Row custno moddate who
1 581827 2012-11-08 08:38:00.000 EMSZC14
2 581827 2012-11-08 08:41:10.000 EMSZC14
3 581827 2012-11-08 08:53:46.000 EMSZC14
4 581827 2012-11-08 08:57:04.000 EMSZC14
5 581827 2012-11-08 08:58:35.000 EMSZC14
6 581827 2012-11-08 08:59:13.000 EMSZC14
7 581827 2012-11-08 09:00:06.000 EMSZC14
1 581827 2012-11-08 09:04:39.000 EMSZC49 Reset row number to 1 (Hooray! It worked!)
2 581827 2012-11-08 09:05:04.000 EMSZC49
3 581827 2012-11-08 09:06:32.000 EMSZC49
4 581827 2012-11-08 09:12:03.000 EMSZC49
5 581827 2012-11-08 09:12:38.000 EMSZC49
6 581827 2012-11-08 09:14:18.000 EMSZC49
14 581827 2012-11-08 09:17:35.000 EMSZC14 Reset row number to 1 (Crappies.)
望ましい結果:
Row custno moddate who
1 581827 2012-11-08 08:38:00.000 EMSZC14
2 581827 2012-11-08 08:41:10.000 EMSZC14
3 581827 2012-11-08 08:53:46.000 EMSZC14
4 581827 2012-11-08 08:57:04.000 EMSZC14
5 581827 2012-11-08 08:58:35.000 EMSZC14
6 581827 2012-11-08 08:59:13.000 EMSZC14
7 581827 2012-11-08 09:00:06.000 EMSZC14
1 581827 2012-11-08 09:04:39.000 EMSZC49 Reset row number to 1
2 581827 2012-11-08 09:05:04.000 EMSZC49
3 581827 2012-11-08 09:06:32.000 EMSZC49
4 581827 2012-11-08 09:12:03.000 EMSZC49
5 581827 2012-11-08 09:12:38.000 EMSZC49
6 581827 2012-11-08 09:14:18.000 EMSZC49
1 581827 2012-11-08 09:17:35.000 EMSZC14 Reset row number to 1
任意の支援をいただければ幸いです。