Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8558777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:53:20+00:00 2026-06-11T15:53:20+00:00

I have a table that i wanted to create some charts. Basically i need

  • 0

I have a table that i wanted to create some charts.
Basically i need to know how many registers i have in interval of 10 minutes and how many of them are already pos-processed. With this 2 queries i have the info i need, but i need they 2 in just 1 result table.

First query

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)

Second query

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)

How can i join them if my common field is generated dynamically?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T15:53:21+00:00Added an answer on June 11, 2026 at 3:53 pm

    As others have already told you, you could just use the two queries as derived tables (subselects) and join them using each one’s Hora column. It doesn’t matter if the column is computed in the query, it’s perfectly legal to use it in a join condition.

    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:

    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)
    

    As you can see, the query doesn’t filter rows on s.Veiculo_Modelo <> ''. However, that condition is used when counting rows for the Sucesso column. The argument of the second COUNT is a CASE expression. It returns a value (arbitrarily chosen to be 1, but could be anything really) if the specified condition is met and NULL otherwise. Since COUNT omits nulls, the result will be the same as with your dedicated query calculating and returning Sucesso.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table that is dynamically created in some c# code-behind when a
i need some help with nsarrays and uipicker i have a table of data
I have a table that has some 50 rows. The last cell of each
I have the need to create a database for some data I have, and
Say I have a table animals that has a column parent_id Say also that
I have a function that updates a MySQL table from a CSV file. The
I have four DB tables in an Oracle database that need to be rewritten/refreshed
I need to create a tool that is able to merge clients production databases.
I have a table with a DEC field named product_price and I wanted to
The problem im having is that I have a table that when a user

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.