Solved

Advanced Sql query

Posted on 2011-03-24
6
298 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]
Comment Utility
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
Comment Utility
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
Comment Utility
What is your sql server version?
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 40

Accepted Solution

by:
Sharath earned 250 total points
Comment Utility
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
Comment Utility
SQL version is 2005
0
 

Author Closing Comment

by:ankykele
Comment Utility
Thanks alot! It works perfect!
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
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.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

771 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

10 Experts available now in Live!

Get 1:1 Help Now