Solved

View Multiple record in one row in SQL

Posted on 2007-11-21
5
153 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

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
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…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

728 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