Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

How to select results from column that groups results into different years Expand / Collapse
Author
Message
Posted Friday, April 4, 2014 8:05 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, April 4, 2014 8:50 AM
Points: 16, Visits: 48
I have a table that stores what is essentially the value of each sale a rep makes. All the sales data from various years is stored in the same table.

AcFeesHistory (table)
FeeEarnerCode (varchar)
CostsAllocated (money)
BillDate (datetime)

I have a query that selects the sum of CostsAllocated (billed amount) for a specified time period. But what I really want to do is to return the CostsAllocated figure for the same month over several years in order to identify general trends in sales (e.g. Feb 2014, Feb 2013, Feb 2012).

select isnull(sum(CostsAllocated),0) as CostsThisYear
from AcFeeHistory
where FeeEarnerCode like 'prw' and BillDate >'2014-01-01'

How do I form a query that selects data from the same column, but with different filtering criteria so that the results from ThisYear, LastYear, TwoYearsAgo etc appear next to each other.

I'm looking for results that look roughly like this

TwoYearsAgo LastYear ThisYear
PRW 1234 1050 775

Should the filtering to return multiple date ranges be included as part of the 'select' statement?

select isnull(sum(CostsAllocated),0) as CostsThisYear (FILTER ON DATES SOMEWHERE HERE?)
from AcFeeHistory
where FeeEarnerCode like 'prw' and BillDate >'2014-01-01'

Thanks
Post #1558523
Posted Friday, April 4, 2014 8:42 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 10:38 AM
Points: 13,266, Visits: 12,096
Pretty hard to tell from what you posted how to do this. I can think of 2 or 3 ways this might be done but it all depends on your actual table structure.

In order to help we will need a few things:

1. Sample DDL in the form of CREATE TABLE statements
2. Sample data in the form of INSERT INTO statements
3. Expected results based on the sample data

Please take a few minutes and read the first article in my signature for best practices when posting questions.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1558546
Posted Monday, April 21, 2014 4:24 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Thursday, May 29, 2014 3:13 AM
Points: 422, Visits: 25
Try it out below query, You can add more conditions as per your requirement

;WITH Sales AS
(
SELECT CostsAllocated ,
CAST(MONTH(BillDate)AS VARCHAR) +'-' +CAST (YEAR(BillDate)AS VARCHAR) AS BillDate
FROM AcFeesHistory WHERE FeeEarnerCode like 'prw' and BillDate >'2014-01-01'
)
SELECT SUM(CostsAllocated),BillDate FROM Sales Group By BillDate
Post #1563418
Posted Monday, April 21, 2014 7:21 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 10:38 AM
Points: 13,266, Visits: 12,096
hardik.panchal (4/21/2014)
Try it out below query, You can add more conditions as per your requirement

;WITH Sales AS
(
SELECT CostsAllocated ,
CAST(MONTH(BillDate)AS VARCHAR) +'-' +CAST (YEAR(BillDate)AS VARCHAR) AS BillDate
FROM AcFeesHistory WHERE FeeEarnerCode like 'prw' and BillDate >'2014-01-01'
)
SELECT SUM(CostsAllocated),BillDate FROM Sales Group By BillDate


This is nowhere close to what the OP wanted for output. Where is this year? Last year?


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1563473
Posted Monday, April 21, 2014 7:28 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 10:24 AM
Points: 23,226, Visits: 31,911
One other question, are we talking calendar years here?



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1563477
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse