Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As others have already told you, you could just use the two queries as derived tables (subselects) and join them using each one's <code>Hora</code> column. It doesn't matter if the column is computed in the query, it's perfectly legal to use it in a join condition.</p> <p>However, I think you can get the same results more efficiently, by calculating both counts in a single query (i.e. without subqueries). Here's how you can do that:</p> <pre><code>SELECT ( CAST(DATEPART(HOUR, m.Ocr_DataHora) AS NVARCHAR) + ':' + CAST((DATEPART(MINUTE, m.Ocr_DataHora) % 6) AS NVARCHAR) + '0' ) AS Hora, <b>COUNT(*)</b> AS Movimentações, <b>COUNT(CASE WHEN s.Veiculo_Modelo &lt;> '' THEN 1 END)</b> AS Sucesso FROM Integracao m WHERE m.Ocr_DataHora >= '2012-09-17 00:00:00.000' AND m.Ocr_DataHora &lt; '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) </code></pre> <p>As you can see, the query doesn't filter rows on <code>s.Veiculo_Modelo &lt;&gt; ''</code>. However, that condition is used when counting rows for the <code>Sucesso</code> column. The argument of the second <code>COUNT</code> is a CASE expression. It returns a value (arbitrarily chosen to be <code>1</code>, but could be anything really) if the specified condition is met and <code>NULL</code> otherwise. Since <code>COUNT</code> omits nulls, the result will be the same as with your dedicated query calculating and returning <code>Sucesso</code>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload