Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 29th, 2006, 05:40 AM
Authorized User
 
Join Date: Jun 2003
Location: Bromsgrove, Worcestershire, United Kingdom.
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Default Cross Tab query

Hi

I think this is a cross tab query, but not sure, either way I's appreciate some help.

I have a single table of weekly ratings similar to this
id
date
cost centre
rating
organisation

What I need out a row per cost centre with the last 4 weeks rating by organisation

like this

org1
cost centre1 week1 week2 week3 week4
cost centre2 week1 week2 week3 week4
cost centre3 week1 week2 week3 week4

org2
cost centre1 week1 week2 week3 week4
cost centre2 week1 week2 week3 week4
cost centre3 week1 week2 week3 week4

Any help would be great

Regards

ANdy

Reply With Quote
  #2 (permalink)  
Old June 29th, 2006, 02:06 PM
Friend of Wrox
 
Join Date: May 2006
Location: Helsingborg, , Sweden.
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
Default

1. Last 4 weeks including present week? Or last 4 weeks before present week?
2. Define week. Running 7 days or first week of year starting with January 1 or any sunday to monday?
3. Rating is ascending or descending? Rating on what aggregation for organization?
Reply With Quote
  #3 (permalink)  
Old June 29th, 2006, 04:08 PM
Authorized User
 
Join Date: Jun 2003
Location: Bromsgrove, Worcestershire, United Kingdom.
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi

Last 4 weeks including this week, the weeks is stored in the table as a week number, so no date manipulation is required.

Just the last 4 entries per cost centre by org.

The rating is a varchar field and can be either B R A or G.

Does that clear things up.

Regards & thanks for your time

Andy
Reply With Quote
  #4 (permalink)  
Old June 29th, 2006, 04:22 PM
Friend of Wrox
 
Join Date: May 2006
Location: Helsingborg, , Sweden.
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Too much is unknown for completing this task, but this should give you an idea how to proceed.

Code:
declare @test table (cc varchar(50), dt datetime, amount int)

insert @test
select 'cost centre 1', '2006-01-19', 12 union all
select 'cost centre 1', '2006-02-19', 36 union all
select 'cost centre 1', '2006-03-19',  3 union all
select 'cost centre 1', '2006-04-19', 34 union all
select 'cost centre 1', '2006-05-19', 11 union all
select 'cost centre 1', '2006-06-19',  5 union all
select 'cost centre 1', '2006-07-19', 33 union all
select 'cost centre 2', '2006-01-19', 22 union all
select 'cost centre 2', '2006-02-19', 78 union all
select 'cost centre 2', '2006-03-19', 12 union all
select 'cost centre 2', '2006-04-19', 95 union all
select 'cost centre 2', '2006-05-19', 41 union all
select 'cost centre 2', '2006-06-19', 39 union all
select 'cost centre 2', '2006-07-19', 16

declare @today datetime
select @today = getdate()

select    cc,
          sum(case when convert(varchar(6), dt, 112) = CONVERT(varchar(6), dateadd(mm, -3, @today), 112) THEN amount else 0 end) 'Month 1',
          sum(case when convert(varchar(6), dt, 112) = CONVERT(varchar(6), dateadd(mm, -2, @today), 112) THEN amount else 0 end) 'Month 2',
          sum(case when convert(varchar(6), dt, 112) = CONVERT(varchar(6), dateadd(mm, -1, @today), 112) THEN amount else 0 end) 'Month 3',
          sum(case when convert(varchar(6), dt, 112) = CONVERT(varchar(6), dateadd(mm, -0, @today), 112) THEN amount else 0 end) 'Month 4'
from      @test
group by  cc

select @today = '2006-03-25'

select    cc,
          sum(case when convert(varchar(6), dt, 112) = CONVERT(varchar(6), dateadd(mm, -3, @today), 112) THEN amount else 0 end) 'Month 1',
          sum(case when convert(varchar(6), dt, 112) = CONVERT(varchar(6), dateadd(mm, -2, @today), 112) THEN amount else 0 end) 'Month 2',
          sum(case when convert(varchar(6), dt, 112) = CONVERT(varchar(6), dateadd(mm, -1, @today), 112) THEN amount else 0 end) 'Month 3',
          sum(case when convert(varchar(6), dt, 112) = CONVERT(varchar(6), dateadd(mm, -0, @today), 112) THEN amount else 0 end) 'Month 4'
from      @test
group by  cc
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Cross-Tab query subhanak Access 2 March 3rd, 2008 06:50 PM
Creating Cross-Tab query in SQL? WebLadyBug SQL Server ASP 0 March 9th, 2007 09:57 AM
cross tab help drachx SQL Server 2000 3 October 24th, 2005 03:26 PM
cross tab s_a_ravi BOOK: Professional Crystal Reports for VS.NET 0 February 17th, 2005 06:49 AM
Cross Tab ? abdusalam Crystal Reports 2 July 25th, 2004 11:44 PM



All times are GMT -4. The time now is 07:21 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.