dbTalk Databases Forums  

SQL Group by query

comp.databases comp.databases


Discuss SQL Group by query in the comp.databases forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Rose
 
Posts: n/a

Default SQL Group by query - 10-27-2006 , 12:28 PM






Hi,

I am not able to find the answer to the following query.

A table has three columns: Year, Quarter and Sales price.
Each year has four quarters and values for corresponding sales price
for each quarter is given. Populate the table and give the output as
five columns: year, Qrtr1, Qrtr2, Qrtr3, Qrtr4 and sales price.

There can be multiple prices in quarter but in the result set i
just want average price per quarter and per year respectively (I mean
Quarter1 column will contain average price for all quarter-1 entries
for a specific year, whereas the Year column will contain average price

for that whole year).

I am struggling with the "Qarter-wise sales" display part.

Thanks in advance!


Reply With Quote
  #2  
Old   
Ed Prochak
 
Posts: n/a

Default Re: SQL Group by query - 10-27-2006 , 03:22 PM








On Oct 27, 1:28 pm, "Rose" <rose.washing... (AT) yahoo (DOT) com> wrote:
Quote:
Hi,

I am not able to find the answer to the following query.

A table has three columns: Year, Quarter and Sales price.
Each year has four quarters and values for corresponding sales price
for each quarter is given. Populate the table and give the output as
five columns: year, Qrtr1, Qrtr2, Qrtr3, Qrtr4 and sales price.

There can be multiple prices in quarter but in the result set i
just want average price per quarter and per year respectively (I mean
Quarter1 column will contain average price for all quarter-1 entries
for a specific year, whereas the Year column will contain average price

for that whole year).

I am struggling with the "Qarter-wise sales" display part.

Thanks in advance!
The solution may vary according to your DBMS which you failed to
mention.
You might look into Analytic functions if your DBMS supports them. This
is a case of a cross tab query. If that's not enough information. post
again.
Ed



Reply With Quote
  #3  
Old   
Tonkuma
 
Posts: n/a

Default Re: SQL Group by query - 10-28-2006 , 10:57 AM



Every year have 4 quarters. In another word fixed number of quarters.
So, following query will give your required results.

SELECT Year
, AVG(Sales_price) AS "Year_AVG"
, AVG(CASE
WHEN Quarter = 1 THEN
Sales_price
END) AS "Qrtr1"
, AVG(CASE
WHEN Quarter = 2 THEN
Sales_price
END) AS "Qrtr2"
, AVG(CASE
WHEN Quarter = 3 THEN
Sales_price
END) AS "Qrtr3"
, AVG(CASE
WHEN Quarter = 4 THEN
Sales_price
END) AS "Qrtr4"
FROM Price_Table
GROUP BY Year
ORDER BY Year
;


Reply With Quote
  #4  
Old   
Rose
 
Posts: n/a

Default Re: SQL Group by query - 10-30-2006 , 07:56 AM



Thanks a lot Tonkuma!

Tonkuma wrote:
Quote:
Every year have 4 quarters. In another word fixed number of quarters.
So, following query will give your required results.

SELECT Year
, AVG(Sales_price) AS "Year_AVG"
, AVG(CASE
WHEN Quarter = 1 THEN
Sales_price
END) AS "Qrtr1"
, AVG(CASE
WHEN Quarter = 2 THEN
Sales_price
END) AS "Qrtr2"
, AVG(CASE
WHEN Quarter = 3 THEN
Sales_price
END) AS "Qrtr3"
, AVG(CASE
WHEN Quarter = 4 THEN
Sales_price
END) AS "Qrtr4"
FROM Price_Table
GROUP BY Year
ORDER BY Year
;


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.