Solved

View Multiple record in one row in SQL

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

This article explains all about SQL Server Piecemeal Restore with examples in step by step manner.
Read about achieving the basic levels of HRIS security in the workplace.
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…

707 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

11 Experts available now in Live!

Get 1:1 Help Now