Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

sql query

Posted on 2011-02-17
4
Medium Priority
?
203 Views
Last Modified: 2012-05-11
hi all

i have a sql report, and in the primary select for the report i have the below case statement,

i now need to refer to this 'case' a few times but each time i have to repeat the code - can anyone advise of a better/cleaner way of doing this?
select
(case when (MPriceUom = m.StockUom) and (MPrice >= ((UnitCost/100)*10+UnitCost) or  MPrice <=(UnitCost-((UnitCost/100)*10))) then MPrice 
      when (MPriceUom <> m.StockUom) and ConvMulDiv = 'M' and ((MPrice/ConvFactAltUom) >= ((UnitCost/100)*10+UnitCost) or (MPrice/ConvFactAltUom)
		<=(UnitCost-((UnitCost/100)*10))) then MPrice/ConvFactAltUom
		when (MPriceUom <> m.StockUom) and ConvMulDiv = 'D' and ((MPrice*ConvFactAltUom) >= ((UnitCost/100)*10+UnitCost) or (MPrice*ConvFactAltUom)
		<=(UnitCost-((UnitCost/100)*10))) then MPrice*ConvFactAltUom else null end ) as price

Open in new window

0
Comment
Question by:malraff
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 34914533
One thing you can do like

get the result for this computed columns in sql query and then attach this query in from for case statements.

For Example

Select (case when (MPriceUom = m.StockUom) and (MPrice >= U or  MPrice <=U1 then MPrice
      when (MPriceUom <> m.StockUom) and ConvMulDiv = 'M' and (U2 >= U or U2
            <=U1 then MPrice/ConvFactAltUom
            when (MPriceUom <> m.StockUom) and ConvMulDiv = 'D' and (U3 >= (U or U3
            <=U1 then MPrice*ConvFactAltUom else null end ) as price
From (

Select (UnitCost/100)*10+UnitCost) as U ,(UnitCost-((UnitCost/100)*10))) as U1 , as (MPrice/ConvFactAltUom) as U2 , (MPrice*ConvFactAltUom) as U3 From tablename )
0
 

Author Comment

by:malraff
ID: 34914713
ok, its not really saving me code though?
0
 
LVL 50

Accepted Solution

by:
Lowfatspread earned 2000 total points
ID: 34914977
well you could restructure it a bit (like this) to reduce some of the repeated terms....

and then put it in a with/cte structure so that the calculation becomes available for you to use...

(similar to a nested subquery) but you are allowed to specify your common code in a more linear fashion
whilst the dbms eventually integrates and constructs your final request...


;with Mpriced as (
select
Mprice * (case

          when (MPriceUom <> m.StockUom)
          Then Case 
               When ConvMulDiv = 'M' 
                and ((MPrice/ConvFactAltUom) >= ((UnitCost/100)*10+UnitCost) 
                       or (MPrice/ConvFactAltUom) <=(UnitCost-((UnitCost/100)*10)))
               then 1.00/ConvFactAltUom

               when ConvMulDiv = 'D'
                and ((MPrice*ConvFactAltUom) >= ((UnitCost/100)*10+UnitCost)
                      or (MPrice*ConvFactAltUom) <=(UnitCost-((UnitCost/100)*10))) 
               then ConvFactAltUom
              
               end

         when (MPrice >= ((UnitCost/100)*10+UnitCost) 
                 or  MPrice <=(UnitCost-((UnitCost/100)*10)))
         then 1
 
          end ) as price

       ,......
 
   from ....
   where ....
 )
Select m.Price
  from Mpriced as M

Open in new window

0
 

Author Comment

by:malraff
ID: 34915308
yes i like that idea - used cte queries a few times before but it didnt strike me  to use them here !

cheers LFS
0

Featured Post

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question