Wrox Programmer Forums
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 29th, 2006, 05:40 AM
Authorized User
 
Join Date: Jun 2003
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

 
Old June 29th, 2006, 02:06 PM
Friend of Wrox
 
Join Date: May 2006
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?
 
Old June 29th, 2006, 04:08 PM
Authorized User
 
Join Date: Jun 2003
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
 
Old June 29th, 2006, 04:22 PM
Friend of Wrox
 
Join Date: May 2006
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





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





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