1

我有一张表,我想创建一些图表。基本上我需要知道每隔 10 分钟我有多少个寄存器,以及其中有多少已经被后处理。有了这 2 个查询,我就有了我需要的信息,但我只需要 1 个结果表中的 2 个。

第一次查询

SELECT  
    (
        CAST(DATEPART(HOUR, m.Ocr_DataHora) AS NVARCHAR) +
        ':' +
        CAST((DATEPART(MINUTE, m.Ocr_DataHora) % 6) AS NVARCHAR) +
        '0'         
    )                                       AS Hora,
    COUNT(*)                                AS Movimentações
FROM
    Integracao m
WHERE
        m.Ocr_DataHora      >=  '2012-09-17 00:00:00.000'
    AND m.Ocr_DataHora      <   '2012-09-18 00:00:00.000'
GROUP BY        
    DATEPART(HOUR, m.Ocr_DataHora),
    (DATEPART(MINUTE, m.Ocr_DataHora) % 6)
ORDER BY    
    DATEPART(HOUR, m.Ocr_DataHora),
    (DATEPART(MINUTE, m.Ocr_DataHora) % 6)

第二次查询

SELECT  
    (
        CAST(DATEPART(HOUR, s.Ocr_DataHora) AS NVARCHAR) +
        ':' +
        CAST((DATEPART(MINUTE, s.Ocr_DataHora) % 6) AS NVARCHAR) +
        '0'         
    )                                       AS Hora,
    COUNT(*)                                AS Sucesso
FROM
    Integracao s
WHERE
        s.Veiculo_Modelo    <>  ''
    AND s.Ocr_DataHora      >=  '2012-09-17 00:00:00.000'
    AND s.Ocr_DataHora      <   '2012-09-18 00:00:00.000'
GROUP BY        
    DATEPART(HOUR, s.Ocr_DataHora),
    (DATEPART(MINUTE, s.Ocr_DataHora) % 6)

如果我的公共字段是动态生成的,我如何加入他们?

4

3 回答 3

1

正如其他人已经告诉您的那样,您可以将这两个查询用作派生表(子选择)并使用每个Hora列连接它们。列是否在查询中计算并不重要,在连接条件中使用它是完全合法的。

但是,我认为通过在单个查询中计算两个计数(即没有子查询),您可以更有效地获得相同的结果。您可以这样做:

SELECT  
    (
        CAST(DATEPART(HOUR, m.Ocr_DataHora) AS NVARCHAR) +
        ':' +
        CAST((DATEPART(MINUTE, m.Ocr_DataHora) % 6) AS NVARCHAR) +
        '0'         
    )                                                  AS Hora,
    COUNT(*)                                           AS Movimentações,
    COUNT(CASE WHEN s.Veiculo_Modelo <> '' THEN 1 END) AS Sucesso
FROM
    Integracao m
WHERE
        m.Ocr_DataHora      >=  '2012-09-17 00:00:00.000'
    AND m.Ocr_DataHora      <   '2012-09-18 00:00:00.000'
GROUP BY        
    DATEPART(HOUR, m.Ocr_DataHora),
    (DATEPART(MINUTE, m.Ocr_DataHora) % 6)
ORDER BY    
    DATEPART(HOUR, m.Ocr_DataHora),
    (DATEPART(MINUTE, m.Ocr_DataHora) % 6)

如您所见,查询不会过滤s.Veiculo_Modelo <> ''. 但是,在计算列的行数时会使用该条件Sucesso。第二个参数COUNT是一个 CASE 表达式。1如果满足指定条件,则返回一个值(任意选择为,但实际上可以是任何值),NULL否则返回。由于COUNT省略了空值,因此结果将与您的专用查询计算和返回的结果相同Sucesso

于 2012-09-20T19:39:07.900 回答
0

我假设您希望输出如下:

Hora | Movimentações | Sucesso

您可以像使用表一样使用这两个 SQL 查询,使用WITH如下语句:

WITH 
firstQuery(Hora, Movimentações) AS (<first query definition with out ORDER BY >),
secondQuery(Hora, Sucesso) AS (<second query definition with out ORDER BY >)
SELECT
     f.Hora, f.Movimentações , s.Sucesso
FROM 
    firstQuery f
    JOIN secondQuery s
    ON f.Hora = s.Hora
于 2012-09-20T19:00:07.517 回答
0

您应该能够通过将它们放在 from 子句中的子查询中来做到这一点。我指的方式有以下形式:

Select A.Hora,A.Movimentações,B.Sucesso
From (*First Query*) as A
inner join (*Second Query*) as B on A.Hora = B.Hora

最终结果是:

Select A.Hora,A.Movimentações,B.Sucesso
From (SELECT  
    (
        CAST(DATEPART(HOUR, m.Ocr_DataHora) AS NVARCHAR) +
        ':' +
        CAST((DATEPART(MINUTE, m.Ocr_DataHora) % 6) AS NVARCHAR) +
        '0'         
    )                                       AS Hora,
    COUNT(*)                                AS Movimentações
FROM
    Integracao m
WHERE
        m.Ocr_DataHora      >=  '2012-09-17 00:00:00.000'
    AND m.Ocr_DataHora      <   '2012-09-18 00:00:00.000'
GROUP BY        
    DATEPART(HOUR, m.Ocr_DataHora),
    (DATEPART(MINUTE, m.Ocr_DataHora) % 6)
ORDER BY    
    DATEPART(HOUR, m.Ocr_DataHora),
    (DATEPART(MINUTE, m.Ocr_DataHora) % 6)) As A
inner join (SELECT  
    (
        CAST(DATEPART(HOUR, s.Ocr_DataHora) AS NVARCHAR) +
        ':' +
        CAST((DATEPART(MINUTE, s.Ocr_DataHora) % 6) AS NVARCHAR) +
        '0'         
    )                                       AS Hora,
    COUNT(*)                                AS Sucesso
FROM
    Integracao s
WHERE
        s.Veiculo_Modelo    <>  ''
    AND s.Ocr_DataHora      >=  '2012-09-17 00:00:00.000'
    AND s.Ocr_DataHora      <   '2012-09-18 00:00:00.000'
GROUP BY        
    DATEPART(HOUR, s.Ocr_DataHora),
    (DATEPART(MINUTE, s.Ocr_DataHora) % 6)) As B on B.Hora = A.Hora
于 2012-09-20T19:05:04.090 回答