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 November 13th, 2007, 04:43 PM
Authorized User
 
Join Date: Jul 2006
Location: Pukekohe, , New Zealand.
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
Default Select Query

I have a table that has - Printer, DatePrinted and Cost.

I would like to select the printer name and the total for each month and display the results as follows:

Printer Jan Feb March April

PPUK01 100.00 80.oo 30.00 70.00
PPUK02 54.00 100.00 20.00 104.00
I have the following script:

SELECT SUBSTRING(Printer, 16, 40) AS printer, DATEPART(mm, DatePrinted) AS moth, SUM(Cost) AS total
FROM tbl_pcounterlog
GROUP BY SUBSTRING(Printer, 16, 40), DATEPART(mm, DatePrinted)
ORDER BY SUBSTRING(Printer, 16, 40), DATEPART(mm, DatePrinted)

And the result:

PPUK01 1 154.9000
PPUK01 2 688.8000
PPUK01 3 584.0000
PPUK01 4 422.4000
PPUK01 5 552.5000
PPUK01 6 446.3000
PPUK01 7 397.1000
PPUK01 8 407.0000
PPUK01 9 447.2000
PPUK01 10 314.9000
PPUK01 11 178.3000
PPUK01 12 185.7000
PPUK02 2 .1000
PPUK02 3 3.0000
PPUK02 4 4.5000
PPUK02 5 2.6000
PPUK02 6 1.8000
PPUK02 7 1.0000
PPUK02 8 1.3000
PPUK02 9 19.2000
PPUK02 10 12.2000
PPUK02 11 .1000
PPUK02 12 18.8000
PPUK03 1 128.5000
PPUK03 2 351.1000
PPUK03 3 424.7000
PPUK03 4 283.6000
PPUK03 5 409.0000
PPUK03 6 354.8000
PPUK03 7 364.9000
PPUK03 8 408.9000
PPUK03 9 360.0000
PPUK03 10 233.1000
PPUK03 11 119.5000
PPUK03 12 122.0000

I'm a bit lost on how to write the script to display the results as I would like.Can anyone help me with this?

Thanks

Greg

Reply With Quote
  #2 (permalink)  
Old November 14th, 2007, 05:51 PM
Friend of Wrox
 
Join Date: May 2006
Location: Helsingborg, , Sweden.
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
Default

SELECT SUBSTRING(Printer, 16, 40) AS Printer,
        SUM(CASE WHEN DATEPART(MONTH, DatePrinted) = 1 THEN Cost ELSE 0 END ) AS Jan,
        SUM(CASE WHEN DATEPART(MONTH, DatePrinted) = 2 THEN Cost ELSE 0 END ) AS Feb,
        SUM(CASE WHEN DATEPART(MONTH, DatePrinted) = 3 THEN Cost ELSE 0 END ) AS Mar,
        SUM(CASE WHEN DATEPART(MONTH, DatePrinted) = 4 THEN Cost ELSE 0 END ) AS Apr,
        SUM(CASE WHEN DATEPART(MONTH, DatePrinted) = 5 THEN Cost ELSE 0 END ) AS May,
        SUM(CASE WHEN DATEPART(MONTH, DatePrinted) = 6 THEN Cost ELSE 0 END ) AS Jun,
        SUM(CASE WHEN DATEPART(MONTH, DatePrinted) = 7 THEN Cost ELSE 0 END ) AS Jul,
        SUM(CASE WHEN DATEPART(MONTH, DatePrinted) = 8 THEN Cost ELSE 0 END ) AS Aug,
        SUM(CASE WHEN DATEPART(MONTH, DatePrinted) = 9 THEN Cost ELSE 0 END ) AS Sep,
        SUM(CASE WHEN DATEPART(MONTH, DatePrinted) = 10 THEN Cost ELSE 0 END ) AS Oct,
        SUM(CASE WHEN DATEPART(MONTH, DatePrinted) = 11 THEN Cost ELSE 0 END ) AS Nov,
        SUM(CASE WHEN DATEPART(MONTH, DatePrinted) = 12 THEN Cost ELSE 0 END ) AS [Dec]
FROM tbl_pCounterLog
WHERE DatePrinted >= '20060101'
        AND DatePrinted < '20070101'
GROUP BY SUBSTRING(Printer, 16, 40)
ORDER BY SUBSTRING(Printer, 16, 40)
Reply With Quote
  #3 (permalink)  
Old November 15th, 2007, 10:56 PM
Authorized User
 
Join Date: Jul 2006
Location: Pukekohe, , New Zealand.
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Peso

I am using this in SSRS 2000 and want the "DatePrinted" to be (Year) as a parameter. I have written the following script and it gives me totals for December while December should be $000,00 as I have no data for December:

SELECT

SUBSTRING(Printer, 16, 40) AS Printer,
DATEPART (yyyy,DatePrinted) AS Year,
SUM(CASE WHEN DATEPART(MONTH,DatePrinted) = 1 THEN Cost ELSE 0 END) AS January,
SUM(CASE WHEN DATEPART(MONTH,DatePrinted) = 2 THEN Cost ELSE 0 END) AS February,
SUM(CASE WHEN DATEPART(MONTH,DatePrinted) = 3 THEN Cost ELSE 0 END) AS March,
SUM(CASE WHEN DATEPART(MONTH,DatePrinted) = 4 THEN Cost ELSE 0 END) AS April,
SUM(CASE WHEN DATEPART(MONTH,DatePrinted) = 5 THEN Cost ELSE 0 END) AS May,
SUM(CASE WHEN DATEPART(MONTH,DatePrinted) = 6 THEN Cost ELSE 0 END) AS June,
SUM(CASE WHEN DATEPART(MONTH,DatePrinted) = 7 THEN Cost ELSE 0 END) AS July,
SUM(CASE WHEN DATEPART(MONTH,DatePrinted) = 8 THEN Cost ELSE 0 END) AS August,
SUM(CASE WHEN DATEPART(MONTH,DatePrinted) = 9 THEN Cost ELSE 0 END) AS September,
SUM(CASE WHEN DATEPART(MONTH,DatePrinted) = 10 THEN Cost ELSE 0 END) AS October,
SUM(CASE WHEN DATEPART(MONTH,DatePrinted) = 11 THEN Cost ELSE 0 END) AS November,
SUM(CASE WHEN DATEPART(MONTH,DatePrinted) = 12 THEN Cost ELSE 0 END) AS December


FROM tbl_pcounterlog

GROUP BY SUBSTRING(Printer, 16, 40),DATEPART (yyyy,DatePrinted)
Order BY SUBSTRING(Printer, 16, 40)

THanks Greg

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
SELECT from SELECT query? seananderson Access 1 October 12th, 2007 12:40 AM
Need help on select query arul1984 SQL Server 2000 2 July 4th, 2007 01:49 AM
Select Query Help dbartelt Access 1 June 13th, 2005 08:06 AM
Select query help minhtri SQL Server 2000 4 March 28th, 2005 06:59 PM
select query collie SQL Server 2000 2 January 17th, 2005 03:13 AM



All times are GMT -4. The time now is 08:50 PM.


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