SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Case select sql 2000


Case select sql 2000

Author
Message
masangombali2
masangombali2
Grasshopper
Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)

Group: General Forum Members
Points: 19 Visits: 32
Good day,

Is it possible to do this on a case select on sql query. Thanks

ALTER PROC DoubleSidedSheets
@StartDate datetime ,
@EndDate datetime ,
@SCount int=0,
@DCount int=0,
@DSheets int=0,
@SSheets int=0

AS

SELECT Right(rpt.[Cost Center],4) as [Cost Center Name] ,rpt.[User Id] as [User ID],

COUNT (CASE when rpt.Pages = rpt.[Sheets] THEN
@SCount=(@SCount + rpt.[Sheets]) as [Single Sided Print],
@DCount=0 as [Double Sided Print],
ELSE

@DSheets = (Pages /2)
@SSheets = (Sheets - @DSheets)

@DCount = @DCount + @DSheets as[Double Sided Print],
@SCount = @SCount + @SSheets as [Single Sided Print]

END
FROM rpt_print_transactions rpt

WHERE
rpt.[Date/Time] >=@StartDate and rpt.[Date/Time] <=@EndDate
Group By rpt.[Cost Center],rpt.[User Id]

exec DoubleSidedSheets
'2014-02-01','2014-02-02'
Luis Cazares
Luis Cazares
SSC-Forever
SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)

Group: General Forum Members
Points: 43927 Visits: 19863
You have some errors on the way you want to generate the query. You can't combine the assignment of variables with the return of results and you need to write a CASE for each column on your query (at least).
However, I don't understand why would you want to count a value returned by a formula. Wouldn't you need to use SUM?
Here's my interpretation from your query. I have nothing to test and I don't know what you expect from this. For better answers, please read the article linked in my signature.


ALTER PROCEDURE DoubleSidedSheets @StartDate DATETIME,
@EndDate DATETIME,
@SCount INT = 0,
@DCount INT = 0,
@DSheets INT = 0,
@SSheets INT = 0
AS
SELECT Right(rpt.[Cost Center], 4) AS [Cost Center Name],
rpt.[User Id] AS [User ID],
CASE
WHEN rpt.Pages = rpt.[Sheets]
THEN @SCount + rpt.[Sheets]
ELSE @SCount + (Sheets - (Pages / 2))
END AS [Single Sided Print],
CASE
WHEN rpt.Pages = rpt.[Sheets]
THEN @DCount = 0
ELSE (Pages / 2) + @DCount
END AS [Double Sided Print]
FROM rpt_print_transactions rpt
WHERE rpt.[Date/Time] >= @StartDate
AND rpt.[Date/Time] <= @EndDate
GROUP BY rpt.[Cost Center],
rpt.[User Id]
GO

---OR
ALTER PROCEDURE DoubleSidedSheets @StartDate DATETIME,
@EndDate DATETIME,
@SCount INT = 0,
@DCount INT = 0,
@DSheets INT = 0,
@SSheets INT = 0
AS
SELECT Right(rpt.[Cost Center], 4) AS [Cost Center Name],
rpt.[User Id] AS [User ID],
SUM(CASE
WHEN rpt.Pages = rpt.[Sheets]
THEN @SCount + rpt.[Sheets]
ELSE @SCount + (Sheets - (Pages / 2))
END) AS [Single Sided Print],
SUM(CASE
WHEN rpt.Pages = rpt.[Sheets]
THEN @DCount = 0
ELSE (Pages / 2) + @DCount
END) AS [Double Sided Print]
FROM rpt_print_transactions rpt
WHERE rpt.[Date/Time] >= @StartDate
AND rpt.[Date/Time] <= @EndDate
GROUP BY rpt.[Cost Center],
rpt.[User Id]
GO




Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
masangombali2
masangombali2
Grasshopper
Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)

Group: General Forum Members
Points: 19 Visits: 32
Thank you. That was meant to be SUM. I will test it and thank you for the link as well.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search