Solved

View Multiple record in one row in SQL

Posted on 2007-11-21
5
154 Views
Last Modified: 2010-07-27
Hi, i have view in SQL the return multiple row with same name but defrent action:

Name       Action       Total
------------------------------
user1      close            4
user1      resolved       3
user2      close            5
user2      resolved       2
user3      pending        1

i want my result to be:

Name      close      resolved       pending
----------------------------------------------------------
user1        4               3                    0
user2        5               2                    0
user3        0               0                    1

is there any easy way using SQL statment to do this insted of writting a big code to get the resultin .Net using c#.
Thank you
0
Comment
Question by:AZZA-KHAMEES
[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
5 Comments
 
LVL 15

Expert Comment

by:spprivate
ID: 20332889
Use this stored procedure
http://p2p.wrox.com/topic.asp?TOPIC_ID=25952
and execute the proc like this

EXEC sp_crossTab  'urTable', 'user',NULL', NULL, 'Action','Total'

HTH
0
 

Author Comment

by:AZZA-KHAMEES
ID: 20332945
I not that goo in stored procedure that why i didnt understand whats writtin in this link
0
 
LVL 25

Expert Comment

by:imitchie
ID: 20332997
how many rows are there in total, and how many different types of action can you have? if there's not many action types, we can create a one-off solution
0
 
LVL 25

Expert Comment

by:imitchie
ID: 20333017
select [Name],
 sum(case when Action = 'close' then Total else 0 end) as [Close],
 sum(case when Action = 'resolved' then Total else 0 end) as [Action],
 sum(case when Action = 'pending' then Total else 0 end) as [Pending]
from act
group by [Name]
0
 
LVL 25

Accepted Solution

by:
imitchie earned 500 total points
ID: 20333018
if you have more types of actions, just add more cases to it
hope this helps
0

Featured Post

Do you have a plan for Continuity?

It's inevitable. People leave organizations creating a gap in your service. That's where Percona comes in.

See how Pepper.com relies on Percona to:
-Manage their database
-Guarantee data safety and protection
-Provide database expertise that is available for any situation

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

631 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