SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to select results from column that groups results into different years


How to select results from column that groups results into different years

Author
Message
sjerromeharris
sjerromeharris
Grasshopper
Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)

Group: General Forum Members
Points: 20 Visits: 50
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
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26231 Visits: 17550
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 Modens 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)
hardik.panchal
hardik.panchal
SSC-Addicted
SSC-Addicted (422 reputation)SSC-Addicted (422 reputation)SSC-Addicted (422 reputation)SSC-Addicted (422 reputation)SSC-Addicted (422 reputation)SSC-Addicted (422 reputation)SSC-Addicted (422 reputation)SSC-Addicted (422 reputation)

Group: General Forum Members
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
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26231 Visits: 17550
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 Modens 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)
Lynn Pettis
Lynn Pettis
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39638 Visits: 38560
One other question, are we talking calendar years here?

Cool
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)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search