Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Advanced Sql query

Posted on 2011-03-24
6
Medium Priority
?
349 Views
Last Modified: 2012-05-11
Greetings,
I have a table like such:

Year       Type    
1990        A
1990        A
1990        B
1991        A
1991        B
1992        A
1992        A
1992       B

What I would like to do is write a query that will give me the additive cumulative values per year.  So the output might look like this:

Year         ACount        BCount
1990           2                      1
1991           3                      2
1992           5                      3


I hope this is clear enough:)  Thanks in advance!
0
Comment
Question by:ankykele
  • 3
  • 2
6 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 35210510
select year
, sum(case when Type = 'A' then 1 else 0 end) Acount
, sum(case when Type = 'B' then 1 else 0 end) Bcount
from yourtable
 group by year
0
 

Author Comment

by:ankykele
ID: 35210577
Thanks for the quick reply.  The query you provided only gives the cumulative amount for each year.  I would like a 'rolling' cumulative amount per year, retreiving TypeA and TypeB as an additive function.

Thanks in advance.


0
 
LVL 41

Expert Comment

by:Sharath
ID: 35210632
What is your sql server version?
0
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
LVL 41

Accepted Solution

by:
Sharath earned 1000 total points
ID: 35210649
you can try this.
select [YEAR]
, sum(case when [Type] = 'A' then 1 else 0 end) Acount
, sum(case when [Type] = 'B' then 1 else 0 end) Bcount,
(select COUNT(*) from your_table t2 where [Type] = 'A' and t2.[Year]<= t1.[Year]) CumACount,
(select COUNT(*) from your_table t2 where [Type] = 'B' and t2.[Year]<= t1.[Year]) CumBCount
from your_table t1
 group by [YEAR]

Open in new window

0
 

Author Comment

by:ankykele
ID: 35210658
SQL version is 2005
0
 

Author Closing Comment

by:ankykele
ID: 35210736
Thanks alot! It works perfect!
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

877 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