Solved

Need to group results by quarter (not month)

Posted on 2006-11-29
1
423 Views
Last Modified: 2008-02-01
Hi
I have a SQL statement that extracts sales by quarter, problem is I need to use the date field as part of the select query, and since there is an aggregation in the query, my output get grouped by month as opposed to by quarter ie; I get 3 lines for each quarter where  I only want 1 line - any ideas?
thanks
Fergal

Query:

select  'Q' + CAST(datepart(quarter, intel_date)AS VARCHAR) +'''' +  RIGHT(CAST(datepart(year, intel_date) AS VARCHAR),2) as sales_qtr,
case when geo='EU' then 'EMEA' when geo='AP' then 'APAC' when geo='AM' then 'AMO' end as geo,bgrp_nm, div_dscr, op_id,
SUM(net_blng_amt) from net_gross_sales_new
where op_id = 'BM' and intel_date >='01-01-2006' and geo = 'AP'
group by intel_date, geo,bgrp_nm, div_dscr, op_id
order by sales_qtr

Output:
Q1'06      APAC      Business Client Group      UPSD Division      BM      21720246.9100
Q1'06      APAC      Business Client Group      UPSD Division      BM      13782960.7000
Q1'06      APAC      Business Client Group      UPSD Division      BM      14164670.0600
Q2'06      APAC      Business Client Group      UPSD Division      BM      19134103.3100
Q2'06      APAC      Business Client Group      UPSD Division      BM      23659180.3400
Q2'06      APAC      Business Client Group      UPSD Division      BM      8435456.8200
Q3'06      APAC      Business Client Group      UPSD Division      BM      13244725.7300
Q3'06      APAC      Business Client Group      UPSD Division      BM      5082319.2900
Q3'06      APAC      Business Client Group      UPSD Division      BM      20983776.3700
Q4'06      APAC      Business Client Group      UPSD Division      BM      11132107.9000
Q4'06      APAC      Business Client Group      UPSD Division      BM      11751375.8500

0
Comment
Question by:fjkilken
1 Comment
 
LVL 23

Accepted Solution

by:
adathelad earned 500 total points
ID: 18035761
Hi fjkilken,

Try:

select  'Q' + CAST(datepart(quarter, intel_date)AS VARCHAR) +'''' +  RIGHT(CAST(datepart(year, intel_date) AS VARCHAR),2) as sales_qtr,
case when geo='EU' then 'EMEA' when geo='AP' then 'APAC' when geo='AM' then 'AMO' end as geo,bgrp_nm, div_dscr, op_id,
SUM(net_blng_amt) from net_gross_sales_new
where op_id = 'BM' and intel_date >='01-01-2006' and geo = 'AP'
group by 'Q' + CAST(datepart(quarter, intel_date)AS VARCHAR) +'''' +  RIGHT(CAST(datepart(year, intel_date) AS VARCHAR),2), geo,bgrp_nm, div_dscr, op_id
order by sales_qtr

0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
convert in derived column 7 27
Need help on t-sql 2012 10 53
Access sql to sql server express 10 32
SQL Error - Query 6 24
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

813 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

17 Experts available now in Live!

Get 1:1 Help Now