Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 sum between dates

Author  Topic 

Pace
Constraint Violating Yak Guru

264 Posts

Posted - 2008-05-07 : 08:53:26
Hi,

I am trying to calculate our job value and product costs over a given time period. But if I run my query, the numbers just dont look right at all. Please can you have a look at this query and see if anything stands out?

-- FINISHED PRODUCTS PRODUCTION REPORTS DATA
-- gets current WIP Job information.
-- requires item details from this.
USE Staging
-- declare start and end variables
DECLARE @Start DATETIME
DECLARE @End DATETIME
SET DATEFORMAT DMY
SET @Start = '01/01/2008'
SET @End = '01/02/2008'
SELECT
SUM(jbs.Value) AS ProducionValue,
SUM(prd.Cost) AS Cost,
CONVERT(CHAR(10), @Start, 103) AS [From],
CONVERT(CHAR(10), @End, 103) AS [End]
FROM
(
SELECT
Job.[Job No],
Job.[Cust Name],
Job.[Req'd Date],
Job.ProdStartActual,
Job.Dept,
[Job].[Value],
Job.Loan
FROM
Job
WHERE
Job.[ProdStartActual] BETWEEN @start AND @End AND
Job.Cancel = 0 AND
Job.Sales = 1 AND
Job.Invoiced = 1 OR
Job.[ProdStartActual] BETWEEN @start AND @End AND
Job.Cancel = 0 AND
Job.Sales = 1 AND
Job.Invoiced = 2
) AS jbs INNER JOIN
(
SELECT
Item.[Job No],
Item.[Code]
FROM
Item
) AS itm ON jbs.[Job No] = itm.[Job No] INNER JOIN
(
SELECT
Product.[Product Code],
Product.[Cost]
FROM
Product
) AS prd ON itm.Code = prd.[Product Code]


"Impossible is Nothing"

Pace
Constraint Violating Yak Guru

264 Posts

Posted - 2008-05-07 : 08:55:03
As a note, it seems to be totalling all job values not paying attention to dates, im unsure on the cost one, it certainly doesnt look right, but that could be because I dont have my first part correct.

"Impossible is Nothing"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-05-07 : 09:03:48
Missing a GROUP BY?
USE Staging
GO

DECLARE @Start DATETIME,
@End DATETIME

SET DATEFORMAT DMY

SELECT @Start = '01/01/2008',
@End = '01/02/2008'

SELECT SUM(jbs.Value) AS ProductionValue,
SUM(prd.Cost) AS Cost,
CONVERT(CHAR(10), @Start, 103) AS [From],
CONVERT(CHAR(10), @End, 103) AS [End],
DATEADD(DAY, DATEDIFF(DAY, '19000101', jbs.ProdStartActual), '19000101') AS ProdStartActual
FROM Job AS jbs
INNER JOIN Item AS itm ON itm.[Job No] = jbs.[Job No]
INNER JOIN Product AS prd ON prd.[Product Code] = itm.Code
WHERE jbs.ProdStartActual >= @Start
AND jbs.ProdStartActual < @End
AND jbs.Cancel = 0
AND jbs.Sales = 1
AND jbs.Invoiced IN (1, 2)
GROUP BY DATEADD(DAY, DATEDIFF(DAY, '19000101', jbs.ProdStartActual), '19000101')



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

Pace
Constraint Violating Yak Guru

264 Posts

Posted - 2008-05-07 : 09:16:54
Ah thanks, this is making me wonder am I asking sql server the wrong question...

We have jobs which have start (ProdStartActual) and end dates (ComplDate). Now what I need is the sum of the amount of jobs value being worked on for a given range. Now each job has items, which when linked to product, expose a cost. I want to sum this up also for items being worked on in the dates specified. Eventually what I should be able to do is say between date1 and date2 you have/had a total of x amount of job value at a total item cost of x.
Is this possible?

"Impossible is Nothing"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-05-07 : 09:56:12
Yes.

For a solution, please follow these steps
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

Pace
Constraint Violating Yak Guru

264 Posts

Posted - 2008-05-07 : 11:01:56
Thanks peso,

My schema is massively de normalised so will take me a while to build this correctly

"Impossible is Nothing"
Go to Top of Page
   

- Advertisement -