SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


select statement


select statement

Author
Message
shaftut
shaftut
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
Points: 1 Visits: 14
hi,

I need help to write a select statement that take the upper table and select the lower table.
thanks


Phil Parkin
Phil Parkin
SSCoach
SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)

Group: General Forum Members
Points: 19203 Visits: 20461
PIVOT should get you what you need. Have a look at this example and see whether it helps you.


Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.

Please surround any code or links you post with the appropriate IFCode formatting tags. It helps readability a lot.
twin.devil
twin.devil
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2443 Visits: 2675
Following is another way of doing this


Declare @tblPersonTask Table
(
DayNm char(3),
Person varchar(20),
Task smallint
)

insert into @tblPersonTask
select 'sun', 'Jhon' , 1 union all
select 'sun', 'Smith' , 1 union all
select 'sun', 'Dan' , 2 union all
select 'mon', 'Smith' , 2 union all
select 'mon', 'Jhon' , 3 union all
select 'tue', 'Jhon' , 2 union all
select 'tue', 'Smith' , 3 union all
select 'tue', 'Dan' , 1 union all
select 'wed', 'Smith' , 1 union all
select 'thu', 'Jhon' , 2 union all
select 'fri', 'Jhon' , 2 union all
select 'sat', 'Smith' , 3


select Person,
max(Case when DayNm = 'sun' then Task end) AS 'sun',
max(Case when DayNm = 'mon' then Task end) AS 'mon',
max(Case when DayNm = 'tue' then Task end) AS 'tue',
max(Case when DayNm = 'wed' then Task end) AS 'wed',
max(Case when DayNm = 'thu' then Task end) AS 'thu',
max(Case when DayNm = 'fri' then Task end) AS 'fri',
max(Case when DayNm = 'sat' then Task end) AS 'sat'
from @tblPersonTask
group by Person



Daniel Matthee
Daniel Matthee
SSC-Enthusiastic
SSC-Enthusiastic (161 reputation)SSC-Enthusiastic (161 reputation)SSC-Enthusiastic (161 reputation)SSC-Enthusiastic (161 reputation)SSC-Enthusiastic (161 reputation)SSC-Enthusiastic (161 reputation)SSC-Enthusiastic (161 reputation)SSC-Enthusiastic (161 reputation)

Group: General Forum Members
Points: 161 Visits: 212
Hi

Please find the needed Pivot query below


Create Table #tblPersonTask
(
DayNm char(3),
Person varchar(20),
Task smallint
)

insert into #tblPersonTask
select 'sun', 'Jhon' , 1 union all
select 'sun', 'Smith' , 1 union all
select 'sun', 'Dan' , 2 union all
select 'mon', 'Smith' , 2 union all
select 'mon', 'Jhon' , 3 union all
select 'tue', 'Jhon' , 2 union all
select 'tue', 'Smith' , 3 union all
select 'tue', 'Dan' , 1 union all
select 'wed', 'Smith' , 1 union all
select 'thu', 'Jhon' , 2 union all
select 'fri', 'Jhon' , 2 union all
select 'sat', 'Smith' , 3

-- Pivot table with three row and eight columns
SELECT Person , [sun],[mon], [tue],[wed], [thu],[fri], [sat]
FROM
(
-- Select All the Columns needed in the above select statement as well as the pivot
SELECT Person ,Task,DayNm
FROM #tblPersonTask) AS SourceTable
PIVOT
-- Pivot the values (before the FOR statement) for the columns in ()
(
sum(Task)
FOR DayNm IN ([sun],[mon], [tue],[wed], [thu],[fri], [sat])
) AS PivotTable
order by Person



Please also have a look at this nice explanation about Pivot tables.
http://technet.microsoft.com/en-us/library/ms177410(v=sql.105).aspx

Please feel free to ask if you do not understand the code above.

--------------------------------------------------------------------------------------------------------------------------------------------------------
To some it may look best but to others it might be the worst, but then again to some it might be the worst but to others the best.
http://www.sql-sa.co.za
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search