Solved

sql query

Posted on 2011-02-17
4
189 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
  • 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 500 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now