• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2632
  • Last Modified:

Group by week of year

I want to aggregate records by week of the year to get a sum of charges for that week. My problem is that although I can achieve a week number on the first part of my select statement, it throws a wobbly about it being in the group by clause. It also doesn't like it NOT being in the group by clause (surprise!). Code follows. Database is Firebird 1.5

Thanks in advance.

Olly.

ps. I am trying to avoid stored procedures if I can.
select
  extract(year from E.EENDDATE) as "Year",
  (extract(yearday from E.EENDDATE) - extract(weekday from E.EENDDATE-1) + 7) / 7 as "Week",
  sum(A.AMOUNTCHARGED) as "Value"
from CCREVENTS E
inner join CCRACC A on (E.CONTRACTNO = A.CONTRACTNO)
group by
  extract(year from E.EENDDATE),
  (extract(yearday from E.EENDDATE) - extract(weekday from E.EENDDATE-1) + 7) / 7 -- it doesn't like this line having brackets and operators in it!

Open in new window

0
ollyatstithians
Asked:
ollyatstithians
  • 2
1 Solution
 
dprochownikCommented:
I'm not sure if this will work on fb1.5 (on 2.1 it will :D), but you can try pass column number to group by clausule instead column definition

select
  extract(year from E.EENDDATE) as "Year",
  (extract(yearday from E.EENDDATE) - extract(weekday from E.EENDDATE-1) + 7) / 7 as "Week",
  sum(A.AMOUNTCHARGED) as "Value"
from CCREVENTS E
inner join CCRACC A on (E.CONTRACTNO = A.CONTRACTNO)
group by
  1,2

Open in new window

0
 
NickUpsonCommented:
what he said
0
 
ollyatstithiansAuthor Commented:
Brilliant! Thanks. I first read the solution and had a feeling that that may be a FB2 feature, but it does in fact work perfectly in FB1.5.
0
 
dprochownikCommented:
Ok perhaps my english isn't very good and in most cases I have to write my answers under time pressure so once again: I'm sorry for my languages skills.

My answer is:
If you wanto to use any SQL aggregate functions in yours query, like "sum" function in third column, than you have to put all other fields in group by clause. If you use only simple fields in yours query than you can just put them in group by clause like that:

select
  E.EENDDATE,
  (extract(yearday from E.EENDDATE) - extract(weekday from E.EENDDATE-1) + 7) / 7 as "Week",
  sum(A.AMOUNTCHARGED) as "Value"
from CCREVENTS E
inner join CCRACC A on (E.CONTRACTNO = A.CONTRACTNO)
group by E.EENDDATE

But in yours case you can't do that because you use extract function, but there is other way to tell FB which columns should by aggregate. You can specify its numbers in goup by. First column will be 1, second 2 etc.
So you can do that like that:

select
  extract(year from E.EENDDATE) as "Year",
  (extract(yearday from E.EENDDATE) - extract(weekday from E.EENDDATE-1) + 7) / 7 as "Week",
  sum(A.AMOUNTCHARGED) as "Value"
from CCREVENTS E
inner join CCRACC A on (E.CONTRACTNO = A.CONTRACTNO)
group by
  1,2
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now