2

我使用的代码根据 where 子句中的日期为我提供了正确的信息。我想在其他日期获得相同的信息。所以现在我必须自己更改日期并运行代码,将其复制/粘贴到其他地方,然后以新的日期重新开始。如果我想要一年中每一天的信息,那需要做很多工作。是否可以自动更改 where 子句中的日期,最好或最简单的方法是什么?

Select t4.Count, t4.Status
From(
    SELECT count(l.VoerID) as Count, l.Datum, l.Status, l.LogID
      FROM (
             SELECT k.VoerID, k.Datum, MAX(k.LogID) AS LogID
               FROM DB.LogStatus k
                Where Datum < '2013-07-01'
              GROUP BY k.VoerID
           ) m
      JOIN DB.LogStatus l
        ON l.VoerID = m.VoerID AND l.LogID = m.LogID
    Where status in ('B','IN1','IN2''V','Reserv')
    Group by Status
)t4

编辑:

原始表(在一个 VoerID 上选择)(表由数千个 VoerID 组成)

LogID 数据 UserID 状态 时间 VoerID

1299772 2013-04-17 259 N 14:09:11 50174

1319774 2013-05-23 68 B 11:19:17 50174

1320038 2013-05-23 197 IN1 16:53:30 50174

1322002 2013-05-28 68 IN2 09:22:32 50174

1325052 2013-05-31 161 G 09:00:59 50174

1325166 2013-05-31 10 400 09:15:12 50174

1325182 2013-05-31 10 V 09:30:07 50174

1325208 2013-05-31 10 V 09:45:06 50174

1325406 2013-05-31 10 保留 11:45:06 50174

1325522 2013-05-31 10 保留 12:15:06 50174

1325954 2013-05-31 10 保留 15:15:13 50174

1328474 2013-06-05 10 保留 13:15:06 50174

1329230 2013-06-06 10 保留 09:45:03 50174

1329244 2013-06-06 10 存档时间 10:00:08 50174

1329268 2013-06-06 10 存档时间 10:15:08 50174

1330286 2013-06-07 10 存档时间 10:15:06 50174

我现在想知道 VoerID 在第一个月的状态是什么。所以在 2013-05-01 状态 = N,在 2013-06-01 状态 = 保留,从 2013-07-01 开始存档。

以上是针对一个 VoerID。我想计算每个月初的 VoerID 数量、下个月第一天之前的每个最后一个 LOGID 以及每个状态的数量

最后,如果我得到我想在 MSExcel 中将其编辑为交叉表和图表的信息:

2013 年 1 月 1 日 2013 年 1 月 2 日 2013 年 1 月 3 日 2013 年 1 月 4 日 2013 年 1 月 5 日

20 22 24 26 28

B 23 21,5 20 18,5 17

IN1 12 15 18 21 24

IN2 15 7 14 18 25

V 800 1000 1200 1400 1600

预留 50 63 76 89 102

存档 100000 101220 102440 103660 104880

4

1 回答 1

0

对一年中的所有日子进行交叉连接,然后按那天分组。

像这样的东西: -

SELECT COUNT(l.VoerID) as COUNT, m.aDate, l.Status
FROM 
(
    SELECT Sub1.aDate, k.VoerID, MAX(k.LogID) AS LogID
    FROM DB.LogStatus k
    CROSS JOIN 
    (
        SELECT DATE_ADD('2013-01-01', INTERVAL units.i + tens.i * 10 + hundreds.i * 100 DAY) AS aDate -- return the first day of the year + all the numbers from 0 to 999
        FROM (SELECT 0 AS i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) units -- Select units of days
        CROSS JOIN (SELECT 0 AS i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) tens -- select tens
        CROSS JOIN (SELECT 0 AS i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) hundreds -- select hundreds
        WHERE  DATE_ADD('2013-01-01', INTERVAL units.i + tens.i * 10 + hundreds.i * 100 DAY) <= '2013-12-31' -- limit the dates to the days of the specific year
    ) Sub1
    WHERE k.Datum < Sub1.aDate -- This should give up multiple copies of record, one for each date where the d Datum is less that that date
    GROUP BY Sub1.aDate, k.VoerID -- GRoup by date and id, so getting the max log id for each date and id
) m
JOIN DB.LogStatus l
ON l.VoerID = m.VoerID AND l.LogID = m.LogID -- Join where log it is the max log id
WHERE status in ('x','y','z')
GROUP BY m.aDate, Status

编辑-或每个月:-

SELECT COUNT(l.VoerID) as COUNT, m.aDate, l.Status
FROM 
(
    SELECT Sub1.aDate, k.VoerID, MAX(k.LogID) AS LogID
    FROM DB.LogStatus k
    CROSS JOIN 
    (
        SELECT DATE_ADD('2013-01-01', INTERVAL units.i MONTH) AS aDate -- return the first day of each month of the year
        FROM (SELECT 0 AS i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 10 UNION SELECT 11) units -- Select units of days
    ) Sub1
    WHERE k.Datum < Sub1.aDate -- This should give up multiple copies of record, one for each date where the d Datum is less that that date
    GROUP BY Sub1.aDate, k.VoerID -- GRoup by date and id, so getting the max log id for each date and id
) m
JOIN DB.LogStatus l
ON l.VoerID = m.VoerID AND l.LogID = m.LogID -- Join where log it is the max log id
WHERE status in ('x','y','z')
GROUP BY m.aDate, Status
于 2013-07-10T13:16:18.437 回答