Solved

sql syntax

Posted on 2014-10-23
4
121 Views
Last Modified: 2014-10-24
I am trying to work out the query column dynamically through the subquery but does not seems to able to get it to work. The column name is regularPrice

SELECT  C.promoCode,C.cartKey, c.userid,
      C.cartId,
      C.productType,
      C.productID,
      C.description,
      C.qty,
      C.price,
      C.taxable,
      CP.price as priceItem,
      C.taxable,
      C.weight,
      C.productKey,
      C.packageKey,
      CP.productKey AS lineProductKey,
      CP.qty AS lineQty,
      CP.packageKey as linePackageKey,
      CP.title AS lineTitle,
      CP.sessionKey,
      CP.sessionName,
      CP.sessionStart,
      CP.sessionEnd,
    PT.isClassIn,
    PT.isInCarIn,
      CP.sessionMapKey,
         S.locationKEy,
    CASE WHEN CP.sessionKey IS NOT NULL
          C.price + (SELECT
              abs(SUM(SF.sessionFeeValueNb))
        FROM lkup_fee F WITH (NOLOCK)
              INNER JOIN session_fee SF WITH (NOLOCK) ON F.feeKey = SF.feeKey
                  AND SF.sessionKey = CP.sessionKey
             WHERE F.feeActiveIn = 1
       ) as regularPrice
       
     ELSE
           regularPrice = C.price
     END
FROM carts C WITH (NOLOCK)
      LEFT OUTER JOIN carts_product CP WITH (NOLOCK) ON C.cartKey = CP.cartKey
    LEFT OUTER JOIN product P WITH (NOLOCK) ON CP.productKey = P.productKey
    LEFT OUTER JOIN lkup_productType PT WITH (NOLOCK) ON P.productTypeKey = PT.productTypeKey
   LEFT outer join session S WITH (NOLOCK) on s.sessionKey = CP.sessionKey
0
Comment
Question by:erikTsomik
  • 3
4 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40400565
>    CASE WHEN CP.sessionKey IS NOT NULL
For starters, you're missing a THEN that should be at the end of this line
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40400569
I think this is what you're after..
   CASE 
      WHEN CP.sessionKey IS NOT NULL THEN C.price + (
         SELECT abs(SUM(SF.sessionFeeValueNb))
         FROM lkup_fee F WITH (NOLOCK) 
            INNER JOIN session_fee SF WITH (NOLOCK) ON F.feeKey = SF.feeKey AND SF.sessionKey = CP.sessionKey
         WHERE F.feeActiveIn = 1)
      ELSE regularPrice = C.price END as regularPrice

Open in new window

btw if it helps I have an article out there called SQL Server CASE Solutions that is an code-heavy montage of common CASE block solutions.
0
 
LVL 15

Accepted Solution

by:
Haris Djulic earned 500 total points
ID: 40400571
Some corrections in the case structure:

SELECT  C.promoCode,
C.cartKey, c.userid,
      C.cartId,
      C.productType,
      C.productID,
      C.description,
      C.qty,
      C.price,
      C.taxable,
      CP.price as priceItem,
      C.taxable,
      C.weight,
      C.productKey,
      C.packageKey,
      CP.productKey AS lineProductKey,
      CP.qty AS lineQty,
      CP.packageKey as linePackageKey,
      CP.title AS lineTitle,
      CP.sessionKey,
      CP.sessionName,
      CP.sessionStart,
      CP.sessionEnd,
    PT.isClassIn,
    PT.isInCarIn,
      CP.sessionMapKey,
         S.locationKEy,
    CASE WHEN CP.sessionKey IS NOT NULL
          C.price + (SELECT 
              abs(SUM(SF.sessionFeeValueNb))
        FROM lkup_fee F WITH (NOLOCK) 
              INNER JOIN session_fee SF WITH (NOLOCK) ON F.feeKey = SF.feeKey
                  AND SF.sessionKey = CP.sessionKey
             WHERE F.feeActiveIn = 1
       ) 
       
     ELSE 
           C.price
     END as regularPrice
FROM carts C WITH (NOLOCK)
      LEFT OUTER JOIN carts_product CP WITH (NOLOCK) ON C.cartKey = CP.cartKey
    LEFT OUTER JOIN product P WITH (NOLOCK) ON CP.productKey = P.productKey
    LEFT OUTER JOIN lkup_productType PT WITH (NOLOCK) ON P.productTypeKey = PT.productTypeKey
   LEFT outer join session S WITH (NOLOCK) on s.sessionKey = CP.sessionKey

Open in new window

0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40402205
erikTsomik - Did you try my suggestion?
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

920 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

17 Experts available now in Live!

Get 1:1 Help Now