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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:25:33+00:00 2026-05-31T23:25:33+00:00

I have a SQL which can count the condition(Bad,Poor,Worse) in a day and group

  • 0

I have a SQL which can count the condition(Bad,Poor,Worse) in a day and group it into a month and displays as follows:

YEARS MONTHS LV    COUNTVAL
----- ------ ----- --------
 2009 Apr    Bad          5 
 2009 Apr    Poor         3 
 2009 Apr    Worse        2 
 2009 Aug    Bad          3 
 2009 Aug    Poor         2 
 2009 Aug    Worse        5 
 2009 Dec    Bad          5 
 2009 Dec    Poor         2 
 2009 Dec    Worse        3 
 2009 Feb    Bad          5 
 2009 Feb    Poor         3 
 2009 Feb    Worse        2 
 2009 Jan    Bad          3 
 2009 Jan    Poor         3 
 2009 Jan    Worse        4 
 2009 Jul    Bad          2 
 2009 Jul    Poor         3 
 2009 Jul    Worse        5 
 2009 Jun    Bad          1 
 2009 Jun    Poor         4 
 2009 Jun    Worse        5 
 2009 Mar    Bad          4 
 2009 Mar    Poor         4 
 2009 Mar    Worse        2 
 2009 May    Bad          3 
 2009 May    Poor         5 
 2009 May    Worse        2 
 2009 Nov    Bad          1 
 2009 Nov    Poor         5 
 2009 Nov    Worse        4 
 2009 Oct    Bad          5 
 2009 Oct    Poor         3 
 2009 Oct    Worse        2 
 2009 Sep    Bad          2 
 2009 Sep    Poor         4 
 2009 Sep    Worse        4 
 2010 Apr    Bad          6 
 2010 Apr    Poor         1 
 2010 Apr    Worse        3 
 2010 Aug    Bad          3 
 2010 Aug    Poor         2 
 2010 Aug    Worse        5 
 2010 Dec    Bad          4 
 2010 Dec    Poor         2 
 2010 Dec    Worse        4 
 2010 Feb    Bad          4 
 2010 Feb    Poor         3 
 2010 Feb    Worse        3 
 2010 Jan    Bad          6 
 2010 Jan    Poor         3 
 2010 Jan    Worse        1 
 2010 Jul    Bad          2 
 2010 Jul    Poor         4 
 2010 Jul    Worse        4 
 2010 Jun    Bad          4 
 2010 Jun    Poor         3 
 2010 Jun    Worse        3 
 2010 Mar    Bad          6 
 2010 Mar    Poor         1 
 2010 Mar    Worse        3 
 2010 May    Bad          4 
 2010 May    Worse        6 
 2010 Nov    Bad          5 
 2010 Nov    Poor         2 
 2010 Nov    Worse        3 
 2010 Oct    Bad          3 
 2010 Oct    Poor         3 
 2010 Oct    Worse        4 
 2010 Sep    Bad          3 
 2010 Sep    Poor         4 
 2010 Sep    Worse        3

The SQL that I am using:

select years, months, lv, countVal
from
(
select years, months, lv, count (lv) OVER (PARTITION BY years,months,lv) countVal  from

(
SELECT x.years, x.months, x.days, x.lv
FROM airtest, 
XMLTABLE ('$d/cdata/name' passing xmldoc as "d" 
   COLUMNS 
  years integer path 'year',
  months varchar(3) path 'month',
  days varchar(2) path 'day',
  lv varchar(5) path 'value'
  ) as X 
  group by x.years, x.months, x.days, x.lv
  order by x.years, x.months, x.days
)
)
group by years, months, lv, countVal
order by years,months

The problem for now is how to modify it to become in this format??

YEARS MONTHS   Bad    Poor     Worse
----- ------ ----- -------- --------
 2009 Apr        5        3        2 
 2009 Aug        3        2        5 
 2009 Dec        5        2        3 
 .........
 .........

After modified, It will become something like this.

You can see the seperated data are group into a row in each month.

Thanks for help!!

  • 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-05-31T23:25:34+00:00Added an answer on May 31, 2026 at 11:25 pm

    Conditionally sum into each column and group by year/month:

    SELECT years, months,
           SUM(DECODE(lv, 'Bad',   countval, 0)) Bad,
           SUM(DECODE(lv, 'Poor',  countval, 0)) Poor,
           SUM(DECODE(lv, 'Worse', countval, 0)) Worse
    FROM ( <your query> )
    GROUP BY years, months
    

    [Gaurav Soni added the following]

    OR

    WITH Base_Table AS 
    ( <your query> )
    SELECT years, months,
           SUM(DECODE(lv, 'Bad',   countval, 0)) Bad,
           SUM(DECODE(lv, 'Poor',  countval, 0)) Poor,
           SUM(DECODE(lv, 'Worse', countval, 0)) Worse
    FROM base_Table 
    GROUP BY years, months
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a SQL Server database which I can read from, is there a
Can we have a SQL query which will basically help in viewing table and
I have a table which contains my ads that can be searched in sql-server-2008.
I have a messy stored procedure which uses dynamic sql. I can debug it
I have an sql scenario as follows which I have been trying to improve.
I have a sql like this: SELECT *, count(*) as cc FROM manytomany GROUP
I have a question Can we convert date 20-06-2011 (dd-mm-yyyy) into 0611 using sql
I have a stored procedure in SQL which I can't change. It needs few
I have two SQL scripts which get called within a loop that accept a
I have a SQL table which has a number of fields ID | Value

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.