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 7948201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:39:23+00:00 2026-06-04T01:39:23+00:00

I am having an issue getting blast_seconds to show up as the correct value.

  • 0

I am having an issue getting blast_seconds to show up as the correct value. In my select subquery I need to get the blast_seconds to show up within the same time as in my main Select statement. When the query is executed Year, Month, ScheduledSeconds, TotalDays and Totaltrucks match up, but blast_seconds column shows the same value through out all the months.

Here is my query:

SELECT      DATEPART(YEAR, [Time])      AS [Year]
        ,   DATEPART(MONTH, [Time])     AS [Month]
        ,   SUM(Total)                  AS ScheduledSeconds
        ,   COUNT(DISTINCT([Time]))     AS TotalDays
        ,   COUNT(DISTINCT(Equipment))  AS TotalTrucks
        ,   (
                SELECT      SUM(CASE 
                                    WHEN dbo.reasons.status_id = '298' 
                                        THEN (dbo.by_operator_reasons.seconds) 
                                    ELSE 0 
                                END)        
                FROM        dbo.reasons     
                INNER JOIN  by_operator_reasons 
                ON          dbo.reasons.id = by_operator_reasons.reason_id
                INNER JOIN  equipment
                ON          by_operator_reasons.equipment_id = equipment.id     
                WHERE       reasons.descrip LIKE 'Blast' 
                AND         [Time] BETWEEN '2011-01-01' AND '2012-05-15'
                AND         by_operator_reasons.deleted_at IS NULL
                AND         equipment.type = 'Truck'
            ) AS Blast_Seconds              
FROM        by_equipment_times
WHERE       [Time] BETWEEN '2011-01-01' and '2012-05-15' 
AND         equipment_type  = 'Truck'
GROUP BY    DATEPART(YEAR, [Time])
        ,   DATEPART(MONTH, [Time])
ORDER BY    DATEPART(YEAR, [Time])      ASC
        ,   DATEPART(MONTH, [Time])     ASC

Here is my current output:

Year  Month  SchedSec  Days  TotalTrucks  Blast_Seconds
----  -----  --------  ----  -----------  -------------
2011    1    51340448   31        20      4931156
2011    2    51979509   28        22      4931156
2011    3    58845600   31        22      4931156
2011    4    59121967   30        24      4931156
2011    5    66857271   31        25      4931156
2011    6    67306766   30        28      4931156
2011    7    76976358   31        30      4931156
2011    8    80393145   31        30      4931156
2011    9    75556005   30        30      4931156
2011    10   77741205   31        29      4931156
2011    11   75272400   30        29      4931156
2011    12   77691044   31        29      4931156
2012    1    77683752   31        29      4931156
2012    2    72662400   29        29      4931156
2012    3    77574538   31        29      4931156
2012    4    75172177   30        29      4931156
2012    5    37584000   15        29      4931156
  • 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-04T01:39:25+00:00Added an answer on June 4, 2026 at 1:39 am

    The subquery is not corelated to main query, that is it does not depend on it. You need to connect it, and I believe that you want to do it using Time column.

    SELECT      DATEPART(YEAR, [Time])      AS [Year]
            ,   DATEPART(MONTH, [Time])     AS [Month]
            ,   SUM(Total)                  AS ScheduledSeconds
            ,   COUNT(DISTINCT([Time]))     AS TotalDays
            ,   COUNT(DISTINCT(Equipment))  AS TotalTrucks
            ,   (
                    SELECT      SUM(CASE 
                                        WHEN dbo.reasons.status_id = '298' 
                                            THEN (dbo.by_operator_reasons.seconds) 
                                        ELSE 0 
                                    END)        
                    FROM        dbo.reasons     
                    INNER JOIN  by_operator_reasons 
                    ON          dbo.reasons.id = by_operator_reasons.reason_id
                    INNER JOIN  equipment
                    ON          by_operator_reasons.equipment_id = equipment.id     
                    WHERE       reasons.descrip LIKE 'Blast' 
                    AND         DATEPART(YEAR, [Time]) = DATEPART(YEAR, by_equipment_times.[Time])
                    AND         DATEPART(MONTH, [Time]) = DATEPART(MONTH, by_equipment_times.[Time])
                    AND         by_operator_reasons.deleted_at IS NULL
                    AND         equipment.type = 'Truck'
                ) AS Blast_Seconds              
    FROM        by_equipment_times
    WHERE       [Time] BETWEEN '2011-01-01' and '2012-05-15' 
    AND         equipment_type  = 'Truck'
    GROUP BY    DATEPART(YEAR, [Time])
            ,   DATEPART(MONTH, [Time])
    ORDER BY    DATEPART(YEAR, [Time])      ASC
            ,   DATEPART(MONTH, [Time])     ASC
    

    If you regularly take more than few months it would probably pay off to convert subquery to derived table, grouping by year/month and outer-joining to main query.

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

Sidebar

Related Questions

I'm having an issue getting the correct value from my drop down box. Basically
I am having an issue getting my div's to show on select from the
I am having a bit of an issue with getting tumblr working within a
I'm having a little issue with getting a value of field in HTML using
I'm having an issue getting surefire to run Junit4 tests. This same issue was
Having an issue with getting a makefile to find the correct libraries and header
I'm having an issue getting a LINQ query to work. I have this XML:
I'm having an issue getting some code to work. Is there a way to
I'm having an issue getting files loaded into an app called GCS, by dragging
I'm having an issue getting this CSS for a sprite sheet to work on

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.