Solved

SQL Crosstab query without using transform - subquery

Posted on 2009-07-16
3
497 Views
Last Modified: 2012-06-27
I am trying to perform a subquery or transform of sorts using SQL Server.  I think I have done something similar in Oracle, but cannot locate it.

The actual query I am working on pulls about 50 fields from 7 tables and I would like to avoid a huge "group by" statement.  All table except the transaction tables have single records per employee, but the transaction table can have many records.
select emp.name, emp.department, dept.deptname, emp.manager, mgr.name, other.field,
 

(Select 

   trans.emp

   sum(case when trans.acct = 'Travel' then trans.tranamt else 0 end) as 'Travel',

   sum(case when trans.acct = 'Meals' then trans.tranamt else 0 end) as 'Meals',

   sum(case when trans.acct = 'Airfare' then trans.tranamt else 0 end) as 'Airfare'

   from transactions trans

   where trans.employee = emp.employee

   group by trans.emp )
 

from

   employees emp

   left join departments dept on emp.department = dept.department

   left join employees mgr on emp.manager = mgr.employee

   left join AnotherTable other on emp.employee = other.employee

Open in new window

0
Comment
Question by:jransom2
3 Comments
 
LVL 9

Expert Comment

by:Hwkranger
ID: 24872255
A while ago, I found something similiar to this:

http://weblogs.sqlteam.com/jeffs/articles/5120.aspx

I use the original crosstab they wrote, but this works just as well.
0
 
LVL 9

Accepted Solution

by:
Ernariash earned 500 total points
ID: 24872313
Hope this will help. Thanks...

select emp.name, emp.department, dept.deptname, emp.manager, mgr.name, other.field,

(Select   sum(trans.tranamt)

   from transactions trans

   where trans.employee = emp.employee and  trans.acct = 'Travel')  as 'Travel' ,

 (Select   sum(trans.tranamt)

   from transactions trans

   where trans.employee = emp.employee and  trans.acct = 'Meals')  as 'Meals'   ,

 (Select   sum(trans.tranamt)

   from transactions trans

   where trans.employee = emp.employee and  trans.acct = 'Airfare')  as 'Airfare'  

 

from

   employees emp

   left join departments dept on emp.department = dept.department

   left join employees mgr on emp.manager = mgr.employee

   left join AnotherTable other on emp.employee = other.employee

Open in new window

0
 

Author Closing Comment

by:jransom2
ID: 31604323
Beautiful... I knew it was going to be something simple.
0

Featured Post

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with SQL Server Stoplist 2 18
Can someone plz fix this..getting an error 3 18
SQL 2008 R2 calc date formula 3 26
MS SQL Pivot table help 4 14
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.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

920 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

13 Experts available now in Live!

Get 1:1 Help Now