Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 My head hurts!

Author  Topic 

mparter
Yak Posting Veteran

86 Posts

Posted - 2004-11-23 : 10:35:08
I have a query that currently outputs the following (truncated):-

Year Apps Interviews
----------------------------
2003/4 3640 1671
2004/5 3869 2183


I'd like this to output in the following format:-

Year Stage Value
-------------------------
2003/4 Apps 3640
2003/4 Interviews 1671
2004/5 Apps 3869
2004/5 Interviews 2183

I've been scratching my head on this for the past couple of hours. Any pointers/ideas greatly appreciated?

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2004-11-23 : 10:46:07
you just need to go through it twice, with a UNION, each time taking the column desired and returning the proper string literal in the Stage column:

select Year, 'Apps' as Stage, Apps
from YourTable
UNION ALL
select Year, 'Interviews' as Stage, Interviews
from YourTable



you could also use a cross join:
[code]
select
Year, Stage, case stage when 'Apps' then Apps else Interviews End As Value
from
YourTable
cross join
(select 'Apps' as Stage union all select 'Interviews') Stages
[/end]



- Jeff
Go to Top of Page

tuenty
Constraint Violating Yak Guru

278 Posts

Posted - 2004-11-23 : 10:54:21
[code]set nocount on
declare @t table (YearN char(7), Apps int, Interviews int)
insert into @t
Select '2003/4', 3640, 1671 union
Select '2004/5', 3869, 2183

Select YearN, 'Applications' as Stage, apps as Value from @t Union
Select YearN, 'Interviews' as Stage, interviews as Value from @t
order by YearN[/code]

EDIT: GOT DISTRACTED DID NOT SEE PREVIOUS POST
*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*
A candle loses nothing by lighting another candle
Go to Top of Page

mparter
Yak Posting Veteran

86 Posts

Posted - 2004-11-23 : 10:56:39
Thnaks for taking the time to reply, unfortunately, my headache caused me to post the wrong source layout The source layout is infact:-


Year Team Apps Int Cond Uncond Acc With Waiting
---------------------------------------------------------------------------------
2003/4 Team1 267 110 15 199 173 69 0
2003/4 Team2 22 1 48 43 10 0 0
2003/4 Team3 229 114 23 171 136 63 1
2004/5 Team1 808 424 24 441 365 284 40
2004/5 Team2 78 36 5 54 48 22 0
2004/5 Team3 181 96 2 131 121 41 0
2004/5 Team4 49 32 0 44 44 4 0


But I'm still looking for the same output.
Go to Top of Page

tuenty
Constraint Violating Yak Guru

278 Posts

Posted - 2004-11-23 : 11:01:16
quote:
Originally posted by mparter

But I'm still looking for the same output.

Same logic then

*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*
A candle loses nothing by lighting another candle
Go to Top of Page

mparter
Yak Posting Veteran

86 Posts

Posted - 2004-11-23 : 11:12:00
The problem with that is that it gives me the following:-


Year Stage Value
----------------------------------
2003/4 Applications 0
2003/4 Applications 1
2003/4 Applications 49
2003/4 Applications 59
2003/4 Applications 78
2003/4 Applications 118
2003/4 Applications 156
2003/4 Applications 157
2003/4 Applications 181
2003/4 Applications 229
2003/4 Applications 253
2003/4 Applications 262
2003/4 Applications 267
2003/4 Applications 285
2003/4 Applications 301
2003/4 Applications 436
2003/4 Applications 808
2003/4 Interviews 0
2003/4 Interviews 1
2003/4 Interviews 22
2003/4 Interviews 32
2003/4 Interviews 36
2003/4 Interviews 55
2003/4 Interviews 56
2003/4 Interviews 58
2003/4 Interviews 96
2003/4 Interviews 103
2003/4 Interviews 110
2003/4 Interviews 114
2003/4 Interviews 117
2003/4 Interviews 134
2003/4 Interviews 155
2003/4 Interviews 158
2003/4 Interviews 424
2004/5 Applications 0
2004/5 Applications 1
2004/5 Applications 58
2004/5 Applications 100
2004/5 Applications 140
2004/5 Applications 159
2004/5 Applications 171
2004/5 Applications 183
2004/5 Applications 204
2004/5 Applications 235
2004/5 Applications 261
2004/5 Applications 272
2004/5 Applications 276
2004/5 Applications 280
2004/5 Applications 287
2004/5 Applications 524
2004/5 Applications 717
2004/5 Interviews 0
2004/5 Interviews 36
2004/5 Interviews 54
2004/5 Interviews 83
2004/5 Interviews 94
2004/5 Interviews 99
2004/5 Interviews 113
2004/5 Interviews 119
2004/5 Interviews 125
2004/5 Interviews 158
2004/5 Interviews 166
2004/5 Interviews 168
2004/5 Interviews 184
2004/5 Interviews 290
2004/5 Interviews 381



What I'd like is:-


Year Stage Value
----------------------------------
2003/4 Applications 3640
2003/4 Interviews 1671
2004/5 Applications 3869
2004/5 Interviews 2183


Sorry about the length of the post. And thanks :)

Go to Top of Page

mparter
Yak Posting Veteran

86 Posts

Posted - 2004-11-23 : 11:14:46
I think I just worked it out myself


Select acyear, 'Applications' as Stage, SUM(Applications) as Value from dbo.tbl_RS_Applications
group by acyear
Union
Select acyear, 'Interviews' as Stage, SUM(Interviews) as Value from dbo.tbl_RS_Applications
group by acyear_ayrc


Thanks for all the help guys.

As a side issue, I only posted a couple of columns here but I'll need to 'UNION' 7 all together, is this going to be slow?
Go to Top of Page
   

- Advertisement -