Wrox Programmer Forums
| 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 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
  #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

  #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)
  #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



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





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