Skip to main content
Inspiring
December 11, 2009
Question

Query help/question

  • December 11, 2009
  • 1 reply
  • 388 views

I have this query below. It basically sums then number of days and other stuff. Orginally, for the city, I just had a where city = 'New York' and it would give me one line of output only and I was able to insert into a table.

Now they want to include ten cities. So instead of having ten queries, one for each city, I combimed all the cities into one query using the group by.

here is the query

select city,
sum(days_GT_60) as days_GT_60,
sum(days_31_60) as days_31_60,
sum(days_5_30) as days_5_30,
sum(days_LT_5) as days_LT_5
From
(
select city,
(case when (agedDays > 60) then 1 else 0 end) as days_GT_60,
(case when (agedDays > 30 and agedDays < 61) then 1 else 0 end) as days_31_60,
(case when (agedDays > 4 and agedDays < 31) then 1 else 0 end) as days_5_30,
(case when (agedDays < 5) then 1 else 0 end) as days_LT_5
from
(
select DATEDIFF(day, date_arrived, GETDATE()) + 1 AS agedDays,
city
FROM receivefile
WHERE status NOT IN ('Voided', 'Closed')
group by city
) as I
group by city
) as K
group by city

The figures are not coming out correctl now. When I just had one city, New York, I had a certain count. Now will all ten cities, the counts for New York are now differnent.

What is wroing with my query ? I think there are too many group by clauses ?

Ideally, it would give me ten lines of output, one for each city, then I can use cfloop to loop thru and add each record into a table, one record per city.

This topic has been closed for replies.

1 reply

Inspiring
December 11, 2009

Here,

WHERE status NOT IN ('Voided', 'Closed')

group by city
) as I

the group by clause is not necessary and might be affecting your answer.