Solved

Advanced Sql query

Posted on 2011-03-24
6
327 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 40

Expert Comment

by:Sharath
ID: 35210632
What is your sql server version?
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Is there a better way to combine two queries 7 29
Download ms sql express. 2 29
sql, case when & top 1 14 30
MS SQL Conditional WHERE clause 3 19
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

820 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