Solved

Advanced Sql query

Posted on 2011-03-24
6
310 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 142

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 40

Expert Comment

by:Sharath
ID: 35210632
What is your sql server version?
0
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
LVL 40

Accepted Solution

by:
Sharath earned 250 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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Parse field in SQL View 15 99
How to use odbc in vb to connect to ms sql 14 40
SQL Server Express 10 33
Sql Query Datatype 2 20
When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

863 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

21 Experts available now in Live!

Get 1:1 Help Now