Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Group  by week of year

Posted on 2009-03-31
4
Medium Priority
?
2,615 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
[X]
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
  • 2
4 Comments
 
LVL 4

Accepted Solution

by:
dprochownik earned 2000 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

In this article, we’ll look at how to deploy ProxySQL.
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
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…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Suggested Courses

610 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