Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Record columns adding to less than 10 as "Other"?

Author  Topic 

rattlehead02
Starting Member

7 Posts

Posted - 2011-07-21 : 13:45:06
Hi,

I'm trying to create an query consisting mainly of calculated fields to run a report.

Example:

Select foo1.Region, foo1.County, foo1.Schooldistrict, 
isnull(sum(case
when foo2.status = 'Active' then 1
end), 0) as Active,
isnull(sum(case
when foo2.status = 'Pending' then 1
end), 0) as Pending,
isnull(sum(case
when foo2.status = 'Sold' then 1
end), 0) as Sold
from foo1 inner join foo2 on foo1.region = foo2.region and foo1.id = foo2.id
group by foo1.Region, foo1.County, foo1.Schooldistrict


This part I have working fine. I get:


Cincy Butler Edgewood SD 2 3 1
Cincy Butler Hamilton SD 15 20 13
Cincy Butler Westwood SD 5 4 0
Cincy Butler Other 51 34 50


What I want, is that since Edgewood SD adds up to less than 10 (2+3+1=6) is for it to get rolled up into "Other". And "Other" may or may not already exist in a particular data grouping. That is, that could just as easily only contain records for Edgewood SD, Hamilton SD and Westwood SD with Edgewood SD still needing to become part of an Other record.

Any thoughts. Any thoughts on how to do that in a single (if complex) sql statement rather than dealing with a loop?

Thanks!

Jeff

Ghanta
Yak Posting Veteran

96 Posts

Posted - 2011-07-21 : 15:07:01
One way to do is use subquery and case statement or use CTE.
Go to Top of Page

rattlehead02
Starting Member

7 Posts

Posted - 2011-07-21 : 15:54:24
I did some looking into CTE. I managed to create my query as a CTE, the managed to select * from myCTE. I read that CTE is update-able so instead of the select statement I tried

update myCTE set SchoolDistrict = 'Other' where 'Active' + 'Pending' + 'Sold' < 10

But got an error telling me that "Cannot update the view or function "myCTE" because it contains aggregates or a DISTINCT clause."

So, any tips on what next now that I have my table as a CTE?
Go to Top of Page

Ghanta
Yak Posting Veteran

96 Posts

Posted - 2011-07-21 : 16:02:36
quote:
Originally posted by rattlehead02

I did some looking into CTE. I managed to create my query as a CTE, the managed to select * from myCTE. I read that CTE is update-able so instead of the select statement I tried

update myCTE set SchoolDistrict = 'Other' where 'Active' + 'Pending' + 'Sold' < 10

But got an error telling me that "Cannot update the view or function "myCTE" because it contains aggregates or a DISTINCT clause."

So, any tips on what next now that I have my table as a CTE?



Why do you want to do update... Use your CTE and create a Select using Case when Active + Pending + Select < 10 then 'Other' else SchoolDistrict end as SchoolDistrict...
Go to Top of Page

rattlehead02
Starting Member

7 Posts

Posted - 2011-07-21 : 16:08:12
That's much better than what I was doing, heh. I'm fairly new to SQL Server, that's why I had it all goofy.

Thanks for the help!
Go to Top of Page

rattlehead02
Starting Member

7 Posts

Posted - 2011-07-21 : 16:23:42
It works perfect until I go to Group By. I'll go ahead and past my actual code since I may be simplifying it in my examples:

USE [My DB]
GO
WITH myCTE (a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t)
as
(
SELECT mls_data.mlssource, mls_data.county, mls_data.schooldistrict, mls_data.new,
isnull(sum(case
when status.status = 'Active' and dateaddedmonth = month('7/1/2011') and dateaddedyear = year('7/31/2011') then 1
end), 0) as Active,
isnull(sum(case
when status.status = 'Pending' and dateaddedmonth = month('7/1/2011') and dateaddedyear = year('7/31/2011') then 1
end), 0) as Pending,
isnull(sum(case
when status.status = 'Sold' and sellingdateyear = year('6/1/2011') and sellingdatemonth = month('6/1/2011') then 1
end), 0) as CurrMthClose,
isnull(sum(case
when status.status = 'Sold' and sellingdateyear = year('6/1/2011') and sellingdatemonth <= month('6/1/2011') then 1
end), 0) as YTDClose,
isnull(sum(case
when status.status = 'Sold' and sellingdateyear = year('6/1/2010') then 1
end), 0) as Year1Closed,
isnull(sum(case
when status.status = 'Sold' and sellingdateyear = year('6/1/2009') then 1
end), 0) as Year2Closed,
isnull(sum(case
when status.status = 'Sold' and sellingdateyear = year('6/1/2008') then 1
end), 0) as Year3Closed,
isnull(sum(case
when status.status <> 'Sold' and dateaddedmonth = month('7/1/2011') and dateaddedyear = year('7/31/2011') then 1
end), 0) as CurrentMonthActive,
isnull(sum(case
when status.sellingdate >= '7/1/2010' and status.sellingdate < '7/1/2011' then 1
end), 0) as Prev12MonthsSold,
isnull(sum(case
when status.status = 'Active' and dateaddedmonth = month('7/1/2011') and dateaddedyear = year('7/31/2011') then status.listingprice
end), 0) as Active$,
isnull(sum(case
when status.status = 'Pending' and dateaddedmonth = month('7/1/2011') and dateaddedyear = year('7/31/2011') then status.listingprice
end), 0) as Pending$,
isnull(sum(case
when status.status = 'Sold' and sellingdateyear = year('6/1/2011') and sellingdatemonth = month('6/1/2011') then status.sellingprice
end), 0) as CurrMthClose$,
isnull(sum(case
when status.status = 'Sold' and sellingdateyear = year('6/1/2011') and sellingdatemonth <= month('6/1/2011') then status.sellingprice
end), 0) as YTDClosing$,
isnull(sum(case
when status.status = 'Sold' and sellingdateyear = year('6/1/2010') then status.sellingprice
end), 0) as Year1$,
isnull(sum(case
when status.status = 'Sold' and sellingdateyear = year('6/1/2009') then status.sellingprice
end), 0) as Year2$,
isnull(sum(case
when status.status = 'Sold' and sellingdateyear = year('6/1/2008') then status.sellingprice
end), 0) as Year3$
FROM mls_data INNER JOIN
Status ON mls_data.listingnumber = Status.listingnumber AND mls_data.mlssource = Status.mlssource
where status.dateadded between '1/1/2008' and '7/31/2011' and mls_data.county = 'Butler' and mls_data.mlssource = 'Cincy'
group by mls_data.new,mls_data.mlssource, mls_data.county, mls_data.schooldistrict
)
select a, b, case when e+f+g+h+i+j+k+l+m < 10 then 'Other' else c end as c, d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t
from myCTE
group by d,a,b,c
order by d,a,b,c


If I take out the Group By it works. And I sort of get why the group by won't work, I'm just not sure the solution.

If you notice, the only columns I want to see if add up to less than 10 are the ones that I'm using "Sum Then 1" to count up. The other ones are Summing prices.

Thanks,

Jeff
Go to Top of Page
   

- Advertisement -