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
 Transact-SQL (2000)
 SQL QUERY HELP

Author  Topic 

mk_garg20
Constraint Violating Yak Guru

343 Posts

Posted - 2004-09-07 : 22:42:04
Hi All,
Here is some code from a stored procedure.

CREATE TABLE #GL(
actindx INT,
amnt NUMERIC (19,5),
ORDOCNUM VARCHAR(21)
)

CREATE TABLE #GL00100
(
actindx INT,
Actnumbr_2 VARCHAR(5),
Actnumbr_3 VARCHAR(5)
)

CREATE TABLE #SOP10106
(
SOPNUMBE VARCHAR(21),
Userdef1 VARCHAR(21)
)

CREATE TABLE #DATA (
Userdef1 VARCHAR(21),
JobASXCode VARCHAR(6),
LASER NUMERIC (19,5),
MAIL NUMERIC (19,5),
MATERIAL NUMERIC (19,5),
PROGRAM NUMERIC (19,5),
EBIZ NUMERIC (19,5),
DESIGN NUMERIC (19,5),
LODGEMENT NUMERIC (19,5),
POSTAGE NUMERIC (19,5),
FREIGHT NUMERIC (19,5)
)

INSERT INTO #DATA
SELECT #SOP10106.Userdef1, Job.JobASXCode,
(CASE WHEN #GL00100.Actnumbr_2='14' then sum(amnt) else 0 end) as LASER,
(CASE WHEN #GL00100.Actnumbr_2='15' then sum(amnt) else 0 end) as MAIL,
(CASE WHEN #GL00100.Actnumbr_2='17' then sum(amnt) else 0 end) as MATERIAL,
(CASE WHEN #GL00100.Actnumbr_2='12' then sum(amnt) else 0 end) as PROGRAM,
(CASE WHEN #GL00100.Actnumbr_2='19' then sum(amnt) else 0 end) as EBIZ,
(CASE WHEN #GL00100.Actnumbr_2='11' then sum(amnt) else 0 end) as DESIGN,
(CASE WHEN (#GL00100.Actnumbr_2='04' then sum(amnt) else 0 end) as LODGEMENT,
(CASE WHEN (#GL00100.Actnumbr_2='04' then sum(amnt) else 0 end) as POSTAGE,
(CASE WHEN #GL00100.Actnumbr_2='03' then sum(amnt) else 0 end) as FREIGHT
FROM #GL
LEFT JOIN #SOP10106 ON #GL.ORDOCNUM = #SOP10106.SOPNUMBE
LEFT OUTER JOIN Job on Job.JobNumber = #SOP10106.Userdef1
LEFT JOIN #GL00100 ON #GL.actindx=#GL00100.actindx
GROUP BY #SOP10106.Userdef1, Job.JobASXCode,#GL00100.Actnumbr_2
ORDER BY #SOP10106.Userdef1

DROP TABLE #GL
DROP TABLE #GL00100
DROP TABLE #SOP10106
DROP TABLE #DATA
GO


I want to modify these two lines.
(CASE WHEN (#GL00100.Actnumbr_2='04' then sum(amnt) else 0 end) as LODGEMENT,
(CASE WHEN (#GL00100.Actnumbr_2='04' then sum(amnt) else 0 end) as POSTAGE,

How can i check
1. check #GL00100.actnumbr_3= 102 for LODGEMENT
2. check #GL00100.actnumbr_3= 180 OR #GL00100.actnumbr_3= 181 for POSTAGE

I can not group on #GL00100.actnumbr_3 because all other calculations will be affected.

What can i do?

Thanks

mk_garg

derrickleggett
Pointy Haired Yak DBA

4184 Posts

Posted - 2004-09-07 : 22:56:31
Do another LEFT JOIN to the table with Actnumber_3 as a derived table selecting the DISTINCT. Then it shouldn't affect the other calculations.

MeanOldDBA
derrickleggett@hotmail.com

When life gives you a lemon, fire the DBA.
Go to Top of Page

mk_garg20
Constraint Violating Yak Guru

343 Posts

Posted - 2004-09-09 : 22:08:13
Derrick can you provide me syntax for that.

Thanks

mk_garg
Go to Top of Page

mk_garg20
Constraint Violating Yak Guru

343 Posts

Posted - 2004-09-10 : 02:26:42
Look like i asked wrong question here.

mk_garg
Go to Top of Page

rrb
SQLTeam Poet Laureate

1479 Posts

Posted - 2004-09-10 : 04:06:26
no, but providing some ddl and example data would make it easier for those of us reading the code and trying to work out what it's supposed to be doing....

--
I hope that when I die someone will say of me "That guy sure owed me a lot of money"
Go to Top of Page

RickD
Slow But Sure Yak Herding Master

3608 Posts

Posted - 2004-09-10 : 04:11:50
Or just wrap another case around the existing ones and sum the whole line, then no need to group by actnumbr_3 and you will still get the same set of results...

e.g.
SUM(CASE WHEN #GL00100.actnumbr_3=102 then (CASE WHEN #GL00100.Actnumbr_2='04' then amnt else 0 end) else 0 end) as LODGEMENT

is this what you're looking for??
Go to Top of Page

mk_garg20
Constraint Violating Yak Guru

343 Posts

Posted - 2004-09-12 : 19:19:21
Hi Rick,

It worked!

Thanks alot.



mk_garg
Go to Top of Page
   

- Advertisement -