Solved

Group  by week of year

Posted on 2009-03-31
4
2,525 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
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…

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now