【问题标题】:automaticly change variable in where clause在where子句中自动更改变量
【发布时间】:2013-07-08 08:30:14
【问题描述】:

我使用的代码根据 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 Datum UserID Status Time 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 年 5 月 1 日状态 = N,在 2013 年 6 月 1 日状态 = 保留,从 2013 年 7 月 1 日开始存档。

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

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

1-1-2013 1-2-2013 1-3-2013 1-4-2013 1-5-2013

N 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

【问题讨论】:

  • 看看这个问题:For Loop in SQL
  • @holbrookbw1:这没有帮助......我是新手,所以我不知道如何循环它

标签: mysql variables date where-clause


【解决方案1】:

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

类似这样的:-

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

【讨论】:

  • 这似乎工作正常。你能分解年份代码以便我了解它的作用吗?我必须说代码非常慢,但由于 immens 表......
  • 因为每天的代码需要很长时间(失去连接),是否可以更改代码以在当月的第一天检索信息?
  • 更新了它以添加一些 cmets。您是否只打算处理最多 365 天(即一年)?如果是这样,获取日期的选择可能会获得更小的数字范围并且更快。
  • 感谢您的故障。可能是我想要过去五年左右的信息,这可能吗?或者,如果我最多只工作 365 天,你能更快地获得它吗?
  • 而 Adata 给出了 BLOB 的图像...我知道我可以打开一个选项来查看值,但要在代码中修复吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-20
  • 2011-05-11
相关资源
最近更新 更多