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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:15:58+00:00 2026-06-04T09:15:58+00:00

i have a sql query: SELECT TAT, SUM(CASE WHEN [Month Entered] = 1 THEN

  • 0

i have a sql query:

  SELECT
  TAT,
  SUM(CASE WHEN [Month Entered] = 1 THEN 1 ELSE 0 END) JAN,
   SUM(CASE WHEN [Month Entered] = 2 THEN 1 ELSE 0 END) FEB,
   SUM(CASE WHEN [Month Entered] = 3 THEN 1 ELSE 0 END) MAR,
   SUM(CASE WHEN [Month Entered] = 4 THEN 1 ELSE 0 END) APR,
   SUM(CASE WHEN [Month Entered] = 5 THEN 1 ELSE 0 END) MAY,
   SUM(CASE WHEN [Month Entered] = 6 THEN 1 ELSE 0 END) JUN,
   SUM(CASE WHEN [Month Entered] = 7 THEN 1 ELSE 0 END) JUL,
   SUM(CASE WHEN [Month Entered] = 8 THEN 1 ELSE 0 END) AUG,
   SUM(CASE WHEN [Month Entered] = 9 THEN 1 ELSE 0 END) SEP,
   SUM(CASE WHEN [Month Entered] = 10 THEN 1 ELSE 0 END) OCT,
   SUM(CASE WHEN [Month Entered] = 11 THEN 1 ELSE 0 END) NOV,
   SUM(CASE WHEN [Month Entered] = 12 THEN 1 ELSE 0 END) [DEC]

  FROM
  [SalesDWH].[dbo].[TurnAround]
  where DATEPART(yyyy,[datetime entered])=2010
  GROUP BY TAT
  ORDER BY 1;

here’s what it returns:

+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| TAT | JAN | FEB | MAR | APR | MAY | JUN | JUL | AUG | SEP | OCT | NOV | DEC |
+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
|   0 |   0 |   0 |   0 |   3 |   0 |   0 |   0 |   0 |   0 |   0 |   0 |   0 |
|   1 |   1 |   0 |   0 |   0 |   0 |   0 |   0 |   0 |   0 |   0 |   0 |   0 |
|   7 |   0 |   0 |   0 |   0 |   1 |   0 |   0 |   0 |   0 |   0 |   0 |   0 |
|  18 |  28 |   0 |   0 |   0 |   0 |   0 |   0 |   0 |   0 |   0 |   0 |   0 |
|  19 |  39 |   0 |   0 |   0 |   0 |   1 |   0 |   0 |   0 |  53 |   0 |   0 |
|  20 | 106 |   0 |   0 |   0 |   0 |   0 |   2 |  17 |   0 |   1 |   2 |   0 |
|  21 | 113 |  12 |   0 |   0 |   0 |  22 |   1 |  81 | 137 |  27 |   1 | 110 |
+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+

i would like the output to be:

+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| TAT | JAN | FEB | MAR | APR | MAY | JUN | JUL | AUG | SEP | OCT | NOV | DEC |
+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
|   0 |     |     |     |   3 |     |     |     |     |     |     |     |     |
|   1 |   1 |     |     |     |     |     |     |     |     |     |     |     |
|   2 |     |     |     |     |     |     |     |     |     |     |     |     |
|   3 |     |     |     |     |     |     |     |     |     |     |     |     |
|   4 |     |     |     |     |     |     |     |     |     |     |     |     |
|   5 |     |     |     |     |     |     |     |     |     |     |     |     |
|   6 |     |     |     |     |     |     |     |     |     |     |     |     |
|   7 |     |     |     |     |   1 |     |     |     |     |     |     |     |
|   8 |     |     |     |     |     |     |     |     |     |     |     |     |
|   9 |     |     |     |     |     |     |     |     |     |     |     |     |
|  10 |     |     |     |     |     |     |     |     |     |     |     |     |
|  11 |     |     |     |     |     |     |     |     |     |     |     |     |
|  12 |     |     |     |     |     |     |     |     |     |     |     |     |
|  13 |     |     |     |     |     |     |     |     |     |     |     |     |
|  14 |     |     |     |     |     |     |     |     |     |     |     |     |
|  15 |     |     |     |     |     |     |     |     |     |     |     |     |
|  16 |     |     |     |     |     |     |     |     |     |     |     |     |
|  17 |     |     |     |     |     |     |     |     |     |     |     |     |
|  18 |  28 |     |     |     |     |     |     |     |     |     |     |     |
|  19 |  39 |     |     |     |     |   1 |     |     |     |  53 |     |     |
|  20 | 106 |     |     |     |     |     |   2 |  17 |     |   1 |   2 |     |
|  21 | 113 |  12 |     |     |     |  22 |   1 |  81 | 137 |  27 |   1 | 110 |
+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+

please notice that all 0‘s have been removed and that the TAT column is sequential 0,1,2,3,4,……

how do i force these two conditions?

i need this for values of TAT between 0 and 191.

for values 192 and greater, i would like the TAT at 192 to be a sum of all values of 192 and greater

  • 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-04T09:16:00+00:00Added an answer on June 4, 2026 at 9:16 am

    EDIT for new info

    ;WITH d AS ( SELECT TAT = CASE WHEN TAT > 191 THEN 192 ELSE TAT END,
       SUM(CASE WHEN [Month Entered] = 1  THEN 1 ELSE 0 END) AS JAN,
       SUM(CASE WHEN [Month Entered] = 2  THEN 1 ELSE 0 END) AS FEB,
       SUM(CASE WHEN [Month Entered] = 3  THEN 1 ELSE 0 END) AS MAR,
       SUM(CASE WHEN [Month Entered] = 4  THEN 1 ELSE 0 END) AS APR,
       SUM(CASE WHEN [Month Entered] = 5  THEN 1 ELSE 0 END) AS MAY,
       SUM(CASE WHEN [Month Entered] = 6  THEN 1 ELSE 0 END) AS JUN,
       SUM(CASE WHEN [Month Entered] = 7  THEN 1 ELSE 0 END) AS JUL,
       SUM(CASE WHEN [Month Entered] = 8  THEN 1 ELSE 0 END) AS AUG,
       SUM(CASE WHEN [Month Entered] = 9  THEN 1 ELSE 0 END) AS SEP,
       SUM(CASE WHEN [Month Entered] = 10 THEN 1 ELSE 0 END) AS OCT,
       SUM(CASE WHEN [Month Entered] = 11 THEN 1 ELSE 0 END) AS NOV,
       SUM(CASE WHEN [Month Entered] = 12 THEN 1 ELSE 0 END) AS DEC
      FROM [SalesDWH].[dbo].[TurnAround]
      WHERE [datetime entered] >= '20100101'
        AND [datetime entered] <  '20110101'
      GROUP BY CASE WHEN TAT > 191 THEN 192 ELSE TAT END
    ),
    n AS ( SELECT TOP (193) n = ROW_NUMBER() OVER (ORDER BY [object_id]) - 1
      FROM sys.all_columns ORDER BY [object_id]
    )
    SELECT TAT = n.n,
      JAN = COALESCE(RTRIM(NULLIF(JAN, 0)), ''),
      FEB = COALESCE(RTRIM(NULLIF(FEB, 0)), ''),
      MAR = COALESCE(RTRIM(NULLIF(MAR, 0)), ''),
      APR = COALESCE(RTRIM(NULLIF(APR, 0)), ''),
      MAY = COALESCE(RTRIM(NULLIF(MAY, 0)), ''),
      JUN = COALESCE(RTRIM(NULLIF(JUN, 0)), ''),
      JUL = COALESCE(RTRIM(NULLIF(JUL, 0)), ''),
      AUG = COALESCE(RTRIM(NULLIF(AUG, 0)), ''),
      SEP = COALESCE(RTRIM(NULLIF(SEP, 0)), ''),
      OCT = COALESCE(RTRIM(NULLIF(OCT, 0)), ''),
      NOV = COALESCE(RTRIM(NULLIF(NOV, 0)), ''),
      DEC = COALESCE(RTRIM(NULLIF(DEC, 0)), '')
    FROM n LEFT OUTER JOIN d ON n.n = d.TAT 
    ORDER BY n.n; -- do not use ordinal numbers for ORDER BY!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this SQL query select case when AllowanceId is null then 2 else
I have a simple SQL query: Select ID, COUNT(ID) as Selections, OptionName, SUM(Units) as
I have an SQL query: SELECT ShipVia, SUM(Freight) FROM Orders GROUP BY ShipVia which
I have sql query like this: select a.x, a.y, sum(a.foo) as foo_sum from a
I have an SQL query : SELECT Sum(ABS([Minimum Installment])) AS SumOfMonthlyPayments FROM tblAccount INNER
I have the following SQL Query: SELECT porel.refcode, podetail.ponum, SUM(podetail.orderqty * podetail.unitcost) AS Total
i have sql query select * from Roles Join Users On Roles.Role=Users.RoleId it return
I have this sql query: SELECT S.SEARCH, S.STATUS, C.TITLE AS CategoryName, E.SEARCH_ENGINES AS Engine,
I have this SQL query: SELECT DISTINCT tb.SERIAL_NUM, tb.REVISION_NUM, tb.JOB_NUM, tb.JOB_TOTAL, tb.TEST_PROC, tb.BOX_NUM, tb.TEST_BENCH,
I have following SQL query SELECT TOP 10000 AVG(DailyNodeAvailability.Availability) AS AVERAGE_of_Availability FROM Nodes INNER

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.