473,748 Members | 7,118 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Optimizing the Query

1 New Member
Hi All,

I need help in optimizing the query mentioned below:

SELECT

SUM(CASE WHEN PROD_TYP='HBRMC ' AND INC_MULTIPLE < 2.50 AND LTV <= 0.75 OR LTV IS NULL THEN ADV_IN_QTR ELSE 0 END) HBRMC_2p5,
SUM(CASE WHEN PROD_TYP='HBNMC ' AND INC_MULTIPLE < 2.50 AND LTV <= 0.75 OR LTV IS NULL THEN ADV_IN_QTR ELSE 0 END) HBNMC_2p5,
SUM(CASE WHEN PROD_TYP='HBOS' AND INC_MULTIPLE < 2.50 AND LTV <= 0.75 OR LTV IS NULL THEN ADV_IN_QTR ELSE 0 END) HBOS_2p5,
SUM(CASE WHEN PROD_TYP='CMRMC ' AND INC_MULTIPLE < 2.50 AND LTV <= 0.75 OR LTV IS NULL THEN ADV_IN_QTR ELSE 0 END) CMRMC_2p5,
SUM(CASE WHEN PROD_TYP='CMNRM ' AND INC_MULTIPLE < 2.50 AND LTV <= 0.75 OR LTV IS NULL THEN ADV_IN_QTR ELSE 0 END) CMNRM_2p5,
SUM(CASE WHEN PROD_TYP='CMOS' AND INC_MULTIPLE < 2.50 AND LTV <= 0.75 OR LTV IS NULL THEN ADV_IN_QTR ELSE 0 END) CMOS_2p5,
SUM(CASE WHEN PROD_TYP='PRRMC ' AND INC_MULTIPLE < 2.50 AND LTV <= 0.75 OR LTV IS NULL THEN ADV_IN_QTR ELSE 0 END) PRRMC_2p5,
SUM(CASE WHEN PROD_TYP='PRNRM ' AND INC_MULTIPLE < 2.50 AND LTV <= 0.75 OR LTV IS NULL THEN ADV_IN_QTR ELSE 0 END) PRNRM_2p5,
SUM(CASE WHEN PROD_TYP='PROS' AND INC_MULTIPLE < 2.50 AND LTV <= 0.75 OR LTV IS NULL THEN ADV_IN_QTR ELSE 0 END) PROS_2p5,
SUM(CASE WHEN PROD_TYP='HBRMC _OTH' AND INC_MULTIPLE < 2.50 AND LTV <= 0.75 OR LTV IS NULL THEN ADV_IN_QTR ELSE 0 END) HBRMC_OTH_2p5,
SUM(CASE WHEN PROD_TYP='HBNRM _OTH' AND INC_MULTIPLE < 2.50 AND LTV <= 0.75 OR LTV IS NULL THEN ADV_IN_QTR ELSE 0 END) HBNRM_OTH_2p5,
SUM(CASE WHEN PROD_TYP='HBOS_ OTH' AND INC_MULTIPLE < 2.50 AND LTV <= 0.75 OR LTV IS NULL THEN ADV_IN_QTR ELSE 0 END) HBOS_OTH_2p5,

SUM(CASE WHEN PROD_TYP='HBRMC ' AND INC_MULTIPLE >= 2.50 OR INC_MULTIPLE <3.0 AND LTV <= 0.75 OR LTV IS NULL THEN ADV_IN_QTR ELSE 0 END) HBRMC_3,
SUM(CASE WHEN PROD_TYP='HBNMC ' AND INC_MULTIPLE >= 2.50 OR INC_MULTIPLE <3.0 AND LTV <= 0.75 OR LTV IS NULL THEN ADV_IN_QTR ELSE 0 END) HBNMC_3,
SUM(CASE WHEN PROD_TYP='HBOS' AND INC_MULTIPLE >= 2.50 OR INC_MULTIPLE <3.0 AND LTV <= 0.75 OR LTV IS NULL THEN ADV_IN_QTR ELSE 0 END) HBOS_3,
SUM(CASE WHEN PROD_TYP='CMRMC ' AND INC_MULTIPLE >= 2.50 OR INC_MULTIPLE <3.0 AND LTV <= 0.75 OR LTV IS NULL THEN ADV_IN_QTR ELSE 0 END) CMRMC_3,
SUM(CASE WHEN PROD_TYP='CMNRM ' AND INC_MULTIPLE >= 2.50 OR INC_MULTIPLE <3.0 AND LTV <= 0.75 OR LTV IS NULL THEN ADV_IN_QTR ELSE 0 END) CMNRM_3,
SUM(CASE WHEN PROD_TYP='CMOS' AND INC_MULTIPLE >= 2.50 OR INC_MULTIPLE <3.0 AND LTV <= 0.75 OR LTV IS NULL THEN ADV_IN_QTR ELSE 0 END) CMOS_3,
SUM(CASE WHEN PROD_TYP='PRRMC ' AND INC_MULTIPLE >= 2.50 OR INC_MULTIPLE <3.0 AND LTV <= 0.75 OR LTV IS NULL THEN ADV_IN_QTR ELSE 0 END) PRRMC_3,
SUM(CASE WHEN PROD_TYP='PRNRM ' AND INC_MULTIPLE >= 2.50 OR INC_MULTIPLE <3.0 AND LTV <= 0.75 OR LTV IS NULL THEN ADV_IN_QTR ELSE 0 END) PRNRM_3,
SUM(CASE WHEN PROD_TYP='PROS' AND INC_MULTIPLE >= 2.50 OR INC_MULTIPLE <3.0 AND LTV <= 0.75 OR LTV IS NULL THEN ADV_IN_QTR ELSE 0 END) PROS_3,
SUM(CASE WHEN PROD_TYP='HBRMC _OTH' AND INC_MULTIPLE >= 2.50 OR INC_MULTIPLE <3.0 AND LTV <= 0.75 OR LTV IS NULL THEN ADV_IN_QTR ELSE 0 END) HBRMC_OTH_3,
SUM(CASE WHEN PROD_TYP='HBNRM _OTH' AND INC_MULTIPLE >= 2.50 OR INC_MULTIPLE <3.0 AND LTV <= 0.75 OR LTV IS NULL THEN ADV_IN_QTR ELSE 0 END) HBNRM_OTH_3,
SUM(CASE WHEN PROD_TYP='HBOS_ OTH' AND INC_MULTIPLE >= 2.50 OR INC_MULTIPLE <3.0 AND LTV <= 0.75 OR LTV IS NULL THEN ADV_IN_QTR ELSE 0 END) HBOS_OTH_3,

SUM(CASE WHEN PROD_TYP='HBRMC ' AND INC_MULTIPLE >= 3.0 OR INC_MULTIPLE <3.5 AND LTV <= 0.75 OR LTV IS NULL THEN ADV_IN_QTR ELSE 0 END) HBRMC_3p5,
SUM(CASE WHEN PROD_TYP='HBNMC ' AND INC_MULTIPLE >= 3.0 OR INC_MULTIPLE <3.5 AND LTV <= 0.75 OR LTV IS NULL THEN ADV_IN_QTR ELSE 0 END) HBNMC_3p5,
SUM(CASE WHEN PROD_TYP='HBOS' AND INC_MULTIPLE >= 3.0 OR INC_MULTIPLE <3.5 AND LTV <= 0.75 OR LTV IS NULL THEN ADV_IN_QTR ELSE 0 END) HBOS_3p5,
SUM(CASE WHEN PROD_TYP='CMRMC ' AND INC_MULTIPLE >= 3.0 OR INC_MULTIPLE <3.5 AND LTV <= 0.75 OR LTV IS NULL THEN ADV_IN_QTR ELSE 0 END) CMRMC_3p5,
SUM(CASE WHEN PROD_TYP='CMNRM ' AND INC_MULTIPLE >= 3.0 OR INC_MULTIPLE <3.5 AND LTV <= 0.75 OR LTV IS NULL THEN ADV_IN_QTR ELSE 0 END) CMNRM_3p5,
SUM(CASE WHEN PROD_TYP='CMOS' AND INC_MULTIPLE >= 3.0 OR INC_MULTIPLE <3.5 AND LTV <= 0.75 OR LTV IS NULL THEN ADV_IN_QTR ELSE 0 END) CMOS_3p5,
SUM(CASE WHEN PROD_TYP='PRRMC ' AND INC_MULTIPLE >= 3.0 OR INC_MULTIPLE <3.5 AND LTV <= 0.75 OR LTV IS NULL THEN ADV_IN_QTR ELSE 0 END) PRRMC_3p5,
SUM(CASE WHEN PROD_TYP='PRNRM ' AND INC_MULTIPLE >= 3.0 OR INC_MULTIPLE <3.5 AND LTV <= 0.75 OR LTV IS NULL THEN ADV_IN_QTR ELSE 0 END) PRNRM_3p5,
SUM(CASE WHEN PROD_TYP='PROS' AND INC_MULTIPLE >= 3.0 OR INC_MULTIPLE <3.5 AND LTV <= 0.75 OR LTV IS NULL THEN ADV_IN_QTR ELSE 0 END) PROS_3p5,
SUM(CASE WHEN PROD_TYP='HBRMC _OTH' AND INC_MULTIPLE >= 3.0 OR INC_MULTIPLE <3.5 AND LTV <= 0.75 OR LTV IS NULL THEN ADV_IN_QTR ELSE 0 END) HBRMC_OTH_3p5,
SUM(CASE WHEN PROD_TYP='HBNRM _OTH' AND INC_MULTIPLE >= 3.0 OR INC_MULTIPLE <3.5 AND LTV <= 0.75 OR LTV IS NULL THEN ADV_IN_QTR ELSE 0 END) HBNRM_OTH_3p5,
SUM(CASE WHEN PROD_TYP='HBOS_ OTH' AND INC_MULTIPLE >= 3.0 OR INC_MULTIPLE <3.5 AND LTV <= 0.75 OR LTV IS NULL THEN ADV_IN_QTR ELSE 0 END) HBOS_OTH_3p5,

SUM(CASE WHEN PROD_TYP='HBRMC ' AND INC_MULTIPLE >= 3.5 OR INC_MULTIPLE <4.0 AND LTV <= 0.75 OR LTV IS NULL THEN ADV_IN_QTR ELSE 0 END) HBRMC_4,
SUM(CASE WHEN PROD_TYP='HBNMC ' AND INC_MULTIPLE >= 3.5 OR INC_MULTIPLE <4.0 AND LTV <= 0.75 OR LTV IS NULL THEN ADV_IN_QTR ELSE 0 END) HBNMC_4,
SUM(CASE WHEN PROD_TYP='HBOS' AND INC_MULTIPLE >= 3.5 OR INC_MULTIPLE <4.0 AND LTV <= 0.75 OR LTV IS NULL THEN ADV_IN_QTR ELSE 0 END) HBOS_4,
SUM(CASE WHEN PROD_TYP='CMRMC ' AND INC_MULTIPLE >= 3.5 OR INC_MULTIPLE <4.0 AND LTV <= 0.75 OR LTV IS NULL THEN ADV_IN_QTR ELSE 0 END) CMRMC_4,
SUM(CASE WHEN PROD_TYP='CMNRM ' AND INC_MULTIPLE >= 3.5 OR INC_MULTIPLE <4.0 AND LTV <= 0.75 OR LTV IS NULL THEN ADV_IN_QTR ELSE 0 END) CMNRM_4,
SUM(CASE WHEN PROD_TYP='CMOS' AND INC_MULTIPLE >= 3.5 OR INC_MULTIPLE <4.0 AND LTV <= 0.75 OR LTV IS NULL THEN ADV_IN_QTR ELSE 0 END) CMOS_4,
SUM(CASE WHEN PROD_TYP='PRRMC ' AND INC_MULTIPLE >= 3.5 OR INC_MULTIPLE <4.0 AND LTV <= 0.75 OR LTV IS NULL THEN ADV_IN_QTR ELSE 0 END) PRRMC_4,
SUM(CASE WHEN PROD_TYP='PRNRM ' AND INC_MULTIPLE >= 3.5 OR INC_MULTIPLE <4.0 AND LTV <= 0.75 OR LTV IS NULL THEN ADV_IN_QTR ELSE 0 END) PRNRM_4,
SUM(CASE WHEN PROD_TYP='PROS' AND INC_MULTIPLE >= 3.5 OR INC_MULTIPLE <4.0 AND LTV <= 0.75 OR LTV IS NULL THEN ADV_IN_QTR ELSE 0 END) PROS_4,
SUM(CASE WHEN PROD_TYP='HBRMC _OTH' AND INC_MULTIPLE >= 3.5 OR INC_MULTIPLE <4.0 AND LTV <= 0.75 OR LTV IS NULL THEN ADV_IN_QTR ELSE 0 END) HBRMC_OTH_4,
SUM(CASE WHEN PROD_TYP='HBNRM _OTH' AND INC_MULTIPLE >= 3.5 OR INC_MULTIPLE <4.0 AND LTV <= 0.75 OR LTV IS NULL THEN ADV_IN_QTR ELSE 0 END) HBNRM_OTH_4,
SUM(CASE WHEN PROD_TYP='HBOS_ OTH' AND INC_MULTIPLE >= 3.5 OR INC_MULTIPLE <4.0 AND LTV <= 0.75 OR LTV IS NULL THEN ADV_IN_QTR ELSE 0 END) HBOS_OTH_4,

SUM(CASE WHEN PROD_TYP='HBRMC ' AND (INC_MULTIPLE >= 4.0) AND LTV <= 0.75 OR LTV IS NULL THEN ADV_IN_QTR ELSE 0 END) HBRMC_4AB,
SUM(CASE WHEN PROD_TYP='HBNMC ' AND (INC_MULTIPLE >= 4.0) AND LTV <= 0.75 OR LTV IS NULL THEN ADV_IN_QTR ELSE 0 END) HBNMC_4AB,
SUM(CASE WHEN PROD_TYP='HBOS' AND (INC_MULTIPLE >= 4.0) AND LTV <= 0.75 OR LTV IS NULL THEN ADV_IN_QTR ELSE 0 END) HBOS_4AB,
SUM(CASE WHEN PROD_TYP='CMRMC ' AND (INC_MULTIPLE >= 4.0) AND LTV <= 0.75 OR LTV IS NULL THEN ADV_IN_QTR ELSE 0 END) CMRMC_4AB,
SUM(CASE WHEN PROD_TYP='CMNRM ' AND (INC_MULTIPLE >= 4.0) AND LTV <= 0.75 OR LTV IS NULL THEN ADV_IN_QTR ELSE 0 END) CMNRM_4AB,
SUM(CASE WHEN PROD_TYP='CMOS' AND (INC_MULTIPLE >= 4.0) AND LTV <= 0.75 OR LTV IS NULL THEN ADV_IN_QTR ELSE 0 END) CMOS_4AB,
SUM(CASE WHEN PROD_TYP='PRRMC ' AND (INC_MULTIPLE >= 4.0) AND LTV <= 0.75 OR LTV IS NULL THEN ADV_IN_QTR ELSE 0 END) PRRMC_4AB,
SUM(CASE WHEN PROD_TYP='PRNRM ' AND (INC_MULTIPLE >= 4.0) AND LTV <= 0.75 OR LTV IS NULL THEN ADV_IN_QTR ELSE 0 END) PRNRM_4AB,
SUM(CASE WHEN PROD_TYP='PROS' AND (INC_MULTIPLE >= 4.0) AND LTV <= 0.75 OR LTV IS NULL THEN ADV_IN_QTR ELSE 0 END) PROS_4AB,
SUM(CASE WHEN PROD_TYP='HBRMC _OTH' AND (INC_MULTIPLE >= 4.0) AND LTV <= 0.75 OR LTV IS NULL THEN ADV_IN_QTR ELSE 0 END) HBRMC_OTH_4AB,
SUM(CASE WHEN PROD_TYP='HBNRM _OTH' AND (INC_MULTIPLE >= 4.0) AND LTV <= 0.75 OR LTV IS NULL THEN ADV_IN_QTR ELSE 0 END) HBNRM_OTH_4AB,
SUM(CASE WHEN PROD_TYP='HBOS_ OTH' AND (INC_MULTIPLE >= 4.0) AND LTV <= 0.75 OR LTV IS NULL THEN ADV_IN_QTR ELSE 0 END) HBOS_OTH_4AB

FROM
STAG_FSA_MLAR

---------------------------------------------------------------------------------------------------

I tried using Group By clause to reduce the size of the query but this executes only the 2nd part of the select query and union also takes more time to execute the query:

SELECT INC_MULTIPLE,PR OD_TYP,LTV,SUM( ADV_IN_QTR) AS ADV_IN_QTR
FROM
STAG_FSA_MLAR1
GROUP BY INC_MULTIPLE,PR OD_TYP,LTV
HAVING (INC_MULTIPLE < 2.50) AND LTV <= 0.75 OR LTV IS NULL

UNION

SELECT INC_MULTIPLE,PR OD_TYP,LTV,SUM( ADV_IN_QTR)
FROM
STAG_FSA_MLAR1
GROUP BY INC_MULTIPLE,PR OD_TYP,LTV
HAVING INC_MULTIPLE >= 3.0 OR INC_MULTIPLE <3.5 AND LTV <= 0.75 OR LTV IS NULL.


Any other work around?

Regards,
Rashmi
Sep 28 '06 #1
0 2143

Sign in to post your reply or Sign up for a free account.

Similar topics

6
2875
by: A Future Computer Scientist | last post by:
A question: Is it really important to think about optimizing the native code or optimizing it for P Code? Or does the code you write make a difference?
6
2012
by: Uros | last post by:
Hello! I have some trouble getting good results from my query. here is structure stat_views id | integer id_zone | integer created | timestamp
6
4563
by: Ryan | last post by:
I came across a situation that I've been unable to explain and was hoping somebody had an answer: I had written an update query which was taking about 8 seconds to run and considered it too slow. I copied the SQL statement from the query and tried executing it from code which then ran in 1 second. To make sure that I didn't miss anything, I copied the SQL statement back into a query and tried running it again. It now also only took 1...
2
1562
by: Brian | last post by:
In particular, this question goes out to the Microsoft C++ development team. Back in June, Ronald Laeremans posted the following message. Has the optimizing compiler been included with the standard edition of VC++ yet? Don't think that we've forgotten about it.... Thank, Brian From: Ronald Laeremans (ronaldl@online.microsoft.com)
4
3494
by: Got2Go | last post by:
Hello Group, I have a table that has millions of records in it. About 100 records are added every 5 minutes (one per OIDID) (the sample provided below has data for 2 OIDIDs (99 and 100) And I have a webpage that executes 9 queries one after the other, and then displays the results on the webpage. When the database was empty, this process was very quick. But, as the DB grew, it became slower.
2
1265
by: Nidhi | last post by:
Hello, In my stored procedure i m using same view 5 times. Will this calculate and retrieve from views 5 times ??? Is there any way we can optimise my stored procedure Regards Nidhi
1
1422
by: xpcer | last post by:
hi, friends, i have an problem, like this, i have tables, when i want to use "select" statement that include "join" sintaxt, my query will execute about 1 hour, so long. can u tell me how to optimizing my query, may be using index, or another?
5
1728
by: Merennulli | last post by:
To start with, I'll give a simplified overview of my data. BaseRecord (4mil rows, 25k in each Region) ID | Name | Region | etc OtherData (7.5mil rows, 1 or 2 per ID) ID | Type(1/2) | Data ProblemTable (4mil rows) ID | ConcatenatedHistory
5
2877
by: John Rivers | last post by:
Hello has anybody else noticed I have queries that SQL 2000 optimizes correctly and they run very fast Yet SQL 2005 keeps using a dumb query plan and queries run very slow The problem seems to stem from the assumption that data in a derived
0
8984
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8823
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9238
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6793
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4593
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2206
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.