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)
 Simplify Query

Author  Topic 

scott_hanebutt
Starting Member

10 Posts

Posted - 2009-06-05 : 15:04:57
I have a very complex sql query that works pefectly. I would however like to simplify it, speed it up, and increase my SQL knowledge. To that end I have coppied it below. Any suggestion on improvements would be greatly appreciated.

-- LIST GENERIC PARTS THAT HAVE DIFFERENT PRICES THIS YEAR OR LAST YEAR
-- IN THE PRICE SCHEDULER DEPENDING ON THE FINISH OPTION
SELECT REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(FSCHECODE,
'-3L','?'),'-4L','?'),'-25M','?'),'-25B','?'),'-25C','?'),'-2ML','?'),'-3ML','?')
,'-4ML','?'),'-5ML','?'),'-6LC','?'),'-7MC','?'),'-8DC','?'),'-2KL','?'),'-9BC','?')
,'-1NC','?'),'-2HC','?'),'?','-FINISH') AS GENERIC_PART
FROM INPRSC
GROUP BY REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(FSCHECODE,
'-3L','?'),'-4L','?'),'-25M','?'),'-25B','?'),'-25C','?'),'-2ML','?'),'-3ML','?')
,'-4ML','?'),'-5ML','?'),'-6LC','?'),'-7MC','?'),'-8DC','?'),'-2KL','?'),'-9BC','?')
,'-1NC','?'),'-2HC','?'),'?','-FINISH')
HAVING MAX(FADJUST1) <> MIN(FADJUST1) -- ZONE1 CURRENT YEAR
OR MAX(FADJUST2) <> MIN(FADJUST2) -- ZONE2 CURRENT YEAR
OR MAX(FADJUST3) <> MIN(FADJUST3) -- ZONE1 LAST YEAR
OR MAX(FADJUST4) <> MIN(FADJUST4) -- ZONE2 LAST YEAR
ORDER BY GENERIC_PART

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2009-06-05 : 15:28:41
In my opinion there's not so much wrong with your query but rather your model. You should probably have another attribute to define the generic parts so you don't need that ugly expression. And it seems that you have seperate columns representing prices for different years and zones in the same row. That is not a normalized design. But if that is your model I'm not sure you can significantly improve that query.

Be One with the Optimizer
TG
Go to Top of Page
   

- Advertisement -