访问/ SQL间隔突破

时间:2016-07-25 20:16:51

标签: sql ms-access

编辑:简化代码

CODE   START                    END                       TLY
X      7/12/2016 10:30:00 AM    7/12/2016 11:00:00 AM     68
X      8/14/2016 09:30:00 AM    8/14/2016 11:00:00 AM     11

我正在寻找一些奇怪的表格,我已经将其拉入Access(使用SQL查询)。我所遇到的根本问题有时数据可能是每30分钟一次(10:00-10:30)所示,也可能是多个时间间隔,例如第二行09:30-11:00。

理想情况下,我想将它们表示为每30分钟报告一次,因此数据如下所示:

CODE   START                    END                       TLY
X      7/12/2016 10:30:00 AM    7/12/2016 11:00:00 AM     68
X      8/14/2016 09:30:00 AM    8/14/2016 10:00:00 AM     11
X      8/14/2016 10:00:00 AM    8/14/2016 10:30:00 AM     11
X      8/14/2016 10:30:00 AM    8/14/2016 11:00:00 AM     11

使用的代码:

SELECT
 SGT.CODE AS CODE, CVDate((SGT.START_MOMENT-240)/1440) AS START, 
  CVDate((SGT.STOP_MOMENT-240)/1440) AS END, 
  SGT.TLY AS TLY
FROM
  STF_GRP_TLY AS SGT

我可以尝试任何想法或建议吗?

谢谢!

乔治

1 个答案:

答案 0 :(得分:3)

如果您有数据表,暂且不要使用联接和日期/时间数学[... - 240)/ 1440]

[tblIntervals]

@Override
public void render(Map<String, ?> model, HttpServletRequest request,
    HttpServletResponse response) throws Exception {
    // create a new byte array outputstream and writer that uses the outputstream

    // create a new response wrapper and 'hi-jack' the outputstream
    ResponseWrapper responseWrapper = new ResponseWrapper(response);
    view.render(model, request, responseWrapper);
    byte[] htmlBytes = responseWrapper.getBytes();

    if (htmlBytes.length == 0) {
        throw new PhantomPdfException(
            "The rendered HTML response from the template was empty. "
            // this is ALWAYS empty
        );
    }
    // more code...

并创建一个以零开头的“数字表”,并将(按一个)计数到超过您预期遇到的最大半小时子间隔数,例如

[tblNumbers]

CODE          START                END                  TLY
------------  -------------------  -------------------  ---
Group_Sample  2016-07-12 10:30:00  2016-07-12 11:00:00   68
Group_Sample  2016-08-14 09:30:00  2016-08-14 11:00:00   11

然后您可以创建一个使用交叉连接列出各个子区间的查询,如下所示:

 n
--
 0
 1
 2
 3
 4
 5
 6
 7
 8
 9

返回

SELECT 
    tblIntervals.CODE,
    DateAdd("n", 30 * tblNumbers.n, tblIntervals.START) AS dtmSTART,
    DateAdd("n", 30 * (tblNumbers.n + 1), tblIntervals.START) AS dtmEND,
    tblIntervals.TLY
FROM
    tblIntervals, tblNumbers
WHERE
    DateAdd("n", 30 * tblNumbers.n, tblIntervals.START) < tblIntervals.END
ORDER BY 1, 2

修改

要合并从“名义”到“实际”日期/时间值的转换,您可以使用如下查询:

CODE          dtmSTART             dtmEND               TLY
------------  -------------------  -------------------  ---
Group_Sample  2016-07-12 10:30:00  2016-07-12 11:00:00   68
Group_Sample  2016-08-14 09:30:00  2016-08-14 10:00:00   11
Group_Sample  2016-08-14 10:00:00  2016-08-14 10:30:00   11
Group_Sample  2016-08-14 10:30:00  2016-08-14 11:00:00   11

虽然我倾向于创建一个保存的查询来转换为实际日期/时间值......

[qryIntervals]

SELECT 
    STF_GRP_TLY.STF_GRP_SK, 
    DateAdd("n", 30 * tblNumbers.n, CDate((STF_GRP_TLY.START_MOMENT-240)/1440)) AS dtmSTART, 
    DateAdd("n", 30 * (tblNumbers.n + 1), CDate((STF_GRP_TLY.START_MOMENT-240)/1440)) AS dtmEND, 
    STF_GRP_TLY.TLY 
FROM STF_GRP_TLY, tblNumbers 
WHERE 
    DateAdd("n", 30 * tblNumbers.n, CDate((STF_GRP_TLY.START_MOMENT-240)/1440)) < CDate((STF_GRP_TLY.STOP_MOMENT-240)/1440)

...然后在以下简化查询中使用它来生成子间隔

SELECT 
    STF_GRP_SK, 
    CDate((START_MOMENT-240)/1440) AS START, 
    CDate((STOP_MOMENT-240)/1440) AS END, 
    TLY 
FROM STF_GRP_TLY