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

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+r**ating4+rating5) as Total

from CTE

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].[DealResp

from CTE

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with Premium.
Start your 7-day free trial.

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.

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].[DealResp

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 trialAlso it will help if you post some data so we can test the proposed solution first.

Mike

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].[DealResp

```
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])
```

```
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
```

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

Mike

Microsoft SQL Server

From novice to tech pro — start learning today.

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with Premium.
Start your 7-day free trial.

Open in new window

Hope this helps. Regards.