[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

SQL Crosstab query without using transform - subquery

Posted on 2009-07-16
3
Medium Priority
?
504 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 2000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

649 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