SQL query improvement

hi Experts,

I want to shorten my query below. I want to embedded the bold part of query into the first cte query. Thanks.

with cte as (

 select

 sum (case when rating=1 then 1 else 0 end) as Rating1,

 sum (case when rating=2 then 1 else 0 end) as Rating2,

 sum (case when rating=3 then 1 else 0 end) as Rating3,

 sum (case when rating=4 then 1 else 0 end) as Rating4,

 sum (case when rating=5 then 1 else 0 end) as Rating5

 from [MMD_Feed].[dbo].[DealResponse])
 
 

select rating1, rating2, rating3, rating4,rating5, (rating1+rating2+rating3+rating4+rating5) as Total

 from CTE
tanj1035Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gplanaCommented:
Try this:

select sum (case when rating in (1,2,3,4,5) then 1 else 0 end) as Total
 from [MMD_Feed].[dbo].[DealResponse])
 

Open in new window


Hope this helps. Regards.
gplanaCommented:
Sorry, on my previous SELECT you get just the total and you want also rating1-5. So try this:

select
(case when rating=1 then 1 else 0 end) as Rating1,
(case when rating=2 then 1 else 0 end) as Rating2,
(case when rating=3 then 1 else 0 end) as Rating3,
(case when rating=4 then 1 else 0 end) as Rating4,
(case when rating=5 then 1 else 0 end) as Rating5,
sum (case when rating in (1,2,3,4,5) then 1 else 0 end) as Total
 from [MMD_Feed].[dbo].[DealResponse])

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mike EghtebasDatabase and Application DeveloperCommented:
What version of SQL are you working with?

Also it will help if you post some data so we can test the proposed solution first.

Mike
Acronis True Image 2019 just released!

Create a reliable backup. Make sure you always have dependable copies of your data so you can restore your entire system or individual files.

tanj1035Author Commented:
Thanks, your works!

  select
 sum(case when rating=1 then 1 else 0 end) as Rating1,
 sum(case when rating=2 then 1 else 0 end) as Rating2,
 sum(case when rating=3 then 1 else 0 end) as Rating3,
 sum(case when rating=4 then 1 else 0 end) as Rating4,
 sum(case when rating=5 then 1 else 0 end) as Rating5,
 sum (case when rating in (1,2,3,4,5) then 1 else 0 end) as Total
  from [MMD_Feed].[dbo].[DealResponse]
gplanaCommented:
My pleasure!

Glad to know that my solution works. Thank you for letting me know ;)
Vitor MontalvãoMSSQL Senior EngineerCommented:
If you only have 5 ratings then a COUNT can be used for the Total:
select 
 (case when rating=1 then 1 else 0 end) as Rating1,
 (case when rating=2 then 1 else 0 end) as Rating2,
 (case when rating=3 then 1 else 0 end) as Rating3,
 (case when rating=4 then 1 else 0 end) as Rating4,
 (case when rating=5 then 1 else 0 end) as Rating5,
 count (rating) as Total
  from [MMD_Feed].[dbo].[DealResponse]) 

Open in new window

Mike EghtebasDatabase and Application DeveloperCommented:
I wonder if you could try this also to see if it works.
select rating1, rating2, rating3, rating4,rating5, (rating1+rating2+rating3+rating4+rating5) as Total
From (
   Select Sum(rating=1 then 1 else 0 end) as Rating1,
          sum (case when rating=2 then 1 else 0 end) as Rating2,
          sum (case when rating=3 then 1 else 0 end) as Rating3,
          sum (case when rating=4 then 1 else 0 end) as Rating4,
          sum (case when rating=5 then 1 else 0 end) as Rating5
   from [MMD_Feed].[dbo].[DealResponse]) As T

Open in new window


Also if SQL Server 2012, you can make this whole lot shorter using IIF().

Mike
tanj1035Author Commented:
Hi Mike, yours works as well. Thanks.
Mike EghtebasDatabase and Application DeveloperCommented:
Thanks for the feedback.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.