Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

sql query

Posted on 2011-02-17
4
Medium Priority
?
205 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 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

916 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