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 2000 Forums
 SQL Server Development (2000)
 Loop resultset and calculate result

Author  Topic 

ankur_gurha
Starting Member

20 Posts

Posted - 2006-07-31 : 04:31:35

Hi Guys,

I have run into a problem of which i am not really sure as how to go about it.

First have a look at the query ...

DECLARE @FromDate BigInt
DECLARE @ToDate BigInt

SET @FromDate = dbo.DateToBigInt('20060110')
SET @ToDate = dbo.DateToBigInt('20060120')

SELECT
DISTINCT
j.ID AS 'ID',
j.inquiryRef AS 'EnquiryRef' ,
CAST( dbo.BigIntToDate(mod.date) AS DateTime) AS 'EnquiryDate',
j.jobRef AS 'JobReference' ,
CAST( dbo.GetJobDate(j.Id) AS DateTime) AS 'JobDate',
si.tranref AS 'InvoiceNumber'

INTO #temp1

FROM
Job AS j
LEFT OUTER JOIN Modification mod ON mod.id = j.inquiryLogged
LEFT OUTER JOIN SalesInvoice si ON si.job = j.id

WHERE mod.[date] BETWEEN @FromDate AND @ToDate

SELECT * From #temp1
ORDER BY InvoiceNumber



Ok.. now my problem is that for each Job Reference i need to calculate the Cost of Sales.. for which i have the query as below..

SELECT

SUM(CASE
WHEN ISNULL(jce.actualCost,0)<>0 THEN jce.actualCost
WHEN ISNULL(jce.actualCost,0)=0 and ISNULL(jce.manualOverRide,0)<>0 THEN jce.manualOverRide
ELSE ISNULL(jce.originalEstimate,0)
END) AS 'CostofSales'
INTO #TmpCostSales

FROM
Job AS j
LEFT OUTER JOIN JobCostElements jce ON j.Id = jce.job
LEFT OUTER JOIN Modification mod ON mod.id = j.inquiryLogged

WHERE mod.[date] BETWEEN @FromDate AND @ToDate



I dont know as how to go about it.. one of the ideas which come to my mind is probably to write a cursor which would loop thorugh each row of the #temp1 table and then calculate "Cost of Sales" for each row but i really dont have a idea as how u write a cursor, nor i know if thats the best solution for the given problem...

Ur help would be much appreciated... Thanks in advance.

Cheers!!
A..

chiragkhabaria
Master Smack Fu Yak Hacker

1907 Posts

Posted - 2006-07-31 : 04:40:12
Somthing like this ??


-- First Create the #Temp Table ...

SELECT
DISTINCT
j.ID AS 'ID',
j.inquiryRef AS 'EnquiryRef' ,
CAST( dbo.BigIntToDate(mod.date) AS DateTime) AS 'EnquiryDate',
j.jobRef AS 'JobReference' ,
CAST( dbo.GetJobDate(j.Id) AS DateTime) AS 'JobDate',
si.tranref AS 'InvoiceNumber', j.inquiryLogged,0 As CostOfSales -- here we added the cost of Sale in #Temp Table.
INTO #temp1
FROM
Job AS j
LEFT OUTER JOIN Modification mod ON mod.id = j.inquiryLogged
LEFT OUTER JOIN SalesInvoice si ON si.job = j.id

--Now the update Query for the updating the CostOfSales.

Update t Set CostofSales = f.CostOfSales From #Temp t Inner Join
(
SELECT j.inquiryLogged,
SUM(CASE
WHEN ISNULL(jce.actualCost,0)<>0 THEN jce.actualCost
WHEN ISNULL(jce.actualCost,0)=0 and ISNULL(jce.manualOverRide,0)<>0 THEN jce.manualOverRide
ELSE ISNULL(jce.originalEstimate,0)
END) AS 'CostofSales'
FROM
Job AS j
LEFT OUTER JOIN JobCostElements jce ON j.Id = jce.job
LEFT OUTER JOIN Modification mod ON mod.id = j.inquiryLogged
WHERE mod.[date] BETWEEN @FromDate AND @ToDate
Group by j.inquiryLogged
) As F
On f.inquiryLogged = t.inquiryLogged




Chirag
Go to Top of Page

ankur_gurha
Starting Member

20 Posts

Posted - 2006-07-31 : 05:02:10

The query throws up the error as

The column prefix 'j' does not match with a table name or alias name used in the query.

and

The column prefix 'si' does not match with a table name or alias name used in the query.

i think its becoz of the query here is not structed properly

Update t Set CostofSales = f.CostOfSales From #Temp t Inner Join
(
SELECT j.inquiryLogged,
SUM(CASE
WHEN ISNULL(jce.actualCost,0)<>0 THEN jce.actualCost
WHEN ISNULL(jce.actualCost,0)=0 and ISNULL(jce.manualOverRide,0)<>0 THEN jce.manualOverRide
ELSE ISNULL(jce.originalEstimate,0)
END) AS 'CostofSales'
FROM
Job AS j
LEFT OUTER JOIN JobCostElements jce ON j.Id = jce.job
LEFT OUTER JOIN Modification mod ON mod.id = j.inquiryLogged
WHERE mod.[date] BETWEEN @FromDate AND @ToDate
Group by j.inquiryLogged
) As F
On f.inquiryLogged = t.inquiryLogged


but i really dont know as how to get about it.. any comments would be appreciated.

Thanks in advance

Cheers!!
A..
Go to Top of Page

chiragkhabaria
Master Smack Fu Yak Hacker

1907 Posts

Posted - 2006-07-31 : 05:08:16
Can you post the Table structure and some sample data ???

Chirag
Go to Top of Page

ankur_gurha
Starting Member

20 Posts

Posted - 2006-07-31 : 05:28:30
Thanks Chirag.. it actually worked... i was doing something wrong earlier. Ur qery actually worked and did a brilliant job the thing i was looking for. Thanks for the help.. would post up if i run across some other problem in the query.. Thanks a million...


Cheers!!
A..
Go to Top of Page
   

- Advertisement -