Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Group  by week of year

Posted on 2009-03-31
4
2,553 Views
Last Modified: 2013-12-09
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
Comment
Question by:ollyatstithians
  • 2
4 Comments
 
LVL 4

Accepted Solution

by:
dprochownik earned 500 total points
ID: 24027840
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
 
LVL 19

Expert Comment

by:NickUpson
ID: 24027869
what he said
0
 
LVL 10

Author Closing Comment

by:ollyatstithians
ID: 31564766
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
 
LVL 4

Expert Comment

by:dprochownik
ID: 24028060
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating and Managing Databases with phpMyAdmin in cPanel.
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question