+ Reply to Thread
Page 2 of 3 FirstFirst 1 2 3 LastLast
Results 11 to 20 of 25

Thread: CASE WHEN vs. BETWEEN

  1. #11
    Wolfmaster Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy's Avatar
    Join Date
    Mar 2008
    Location
    Peoria, IL
    Posts
    2,386
    Blog Entries
    5
    Real Name
    Wolff
    Rep Power
    15

    Try something like this
    Code:
      Select S.s_id, S.s_name, S.s_symbol, S.s_publisher, S.s_published_date
           , sum(Case when SD.sd_shares > 0 then SD.sd_shares else 0 end) as sd_shares
           , case
               when SD.sd_shares > 0 then (SELECT (SUM(sd_shares * sd_price_per_share) / SUM(sd_shares)) FROM tblStocks_detail) 
               ELSE 0 
             END As sd_price_per_share
           , MD.sd_date
        From dblStocks S
        Left Join tblStocks_Detail SD
          On S.s_id = sd.s_id
        Join (
              Select s_id, max(sd_date) as sd_date
                From tblStocks_Detail
             Group by s_id
             ) as MD
          On (S.s_id = MD.s_id)
       Group By  S.s_id, S.s_name, S.s_symbol, S.s_publisher, S.s_published_date, MD.sd_date
    
    Also, I think that
    Code:
           , case
               when SD.sd_shares > 0 then (SELECT (SUM(sd_shares * sd_price_per_share) / SUM(sd_shares)) FROM tblStocks_detail) 
               ELSE 0 
             END As sd_price_per_share
    
    will give you the AVG price per share over the whole database, not just the stock in question (i.e. you'll get the same number in each row).
    Last edited by Wolffy; July 20th, 2009 at 12:23 PM.
    Wolffy
    .-- ----- ..-. ..-. -.--
    Opinions expressed are my own and do not necessity reflect those of any sane person. Any code provided is intended to be an example and is provided AS IS. Void where prohibited by law. Not valid in California. Your mileage may vary.

  2. #12
    Barn Frequenter BLaaaaaaaaaarche will become famous soon enough BLaaaaaaaaaarche will become famous soon enough BLaaaaaaaaaarche's Avatar
    Join Date
    Mar 2008
    Posts
    188
    Rep Power
    5

    It is still giving me this error:

    Column 'sd.sd_shares' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
    When you add the sd_share to the GROUP BY, it shows each individual row, as I stated before.

    Also, the average price per share, as you were saying, is the same across the board. However, in my original SQL, it was calculating properly.
    "You'll never be as perfect as BLaaaaaaaaarche."

  3. #13
    Wolfmaster Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy's Avatar
    Join Date
    Mar 2008
    Location
    Peoria, IL
    Posts
    2,386
    Blog Entries
    5
    Real Name
    Wolff
    Rep Power
    15

    OK, while I noodle over this a second, try taking out
    Code:
           , case
               when SD.sd_shares > 0 then (SELECT (SUM(sd_shares * sd_price_per_share) / SUM(sd_shares)) FROM tblStocks_detail) 
               ELSE 0 
             END As sd_price_per_share
    
    from the query and see if works then. If it does, I'm on the right track.
    Wolffy
    .-- ----- ..-. ..-. -.--
    Opinions expressed are my own and do not necessity reflect those of any sane person. Any code provided is intended to be an example and is provided AS IS. Void where prohibited by law. Not valid in California. Your mileage may vary.

  4. #14
    Barn Frequenter BLaaaaaaaaaarche will become famous soon enough BLaaaaaaaaaarche will become famous soon enough BLaaaaaaaaaarche's Avatar
    Join Date
    Mar 2008
    Posts
    188
    Rep Power
    5

    Well, partially. The last trade date works. The total shares works. What does not work is the query showing all records in the tblStocks table. For those stocks that have no records in the tblStocks_detail table, it is not showing the stock. It should show the stock name with 0 shares and the last trade date of 'N/A'.
    "You'll never be as perfect as BLaaaaaaaaarche."

  5. #15
    Wolfmaster Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy's Avatar
    Join Date
    Mar 2008
    Location
    Peoria, IL
    Posts
    2,386
    Blog Entries
    5
    Real Name
    Wolff
    Rep Power
    15

    Well, progress is progress. While I cogitate on that last bit, does the follow line, which replaces the case above, give you the Avg PPS you are looking for?
    Code:
    avg(Case when SD.sd_shares > 0 then sd_price_per_share end) as sd_avg_price_per_share
    
    Wolffy
    .-- ----- ..-. ..-. -.--
    Opinions expressed are my own and do not necessity reflect those of any sane person. Any code provided is intended to be an example and is provided AS IS. Void where prohibited by law. Not valid in California. Your mileage may vary.

  6. #16
    Barn Frequenter BLaaaaaaaaaarche will become famous soon enough BLaaaaaaaaaarche will become famous soon enough BLaaaaaaaaaarche's Avatar
    Join Date
    Mar 2008
    Posts
    188
    Rep Power
    5

    No, because it is really not a straight average. It is a weighted average of all shares purchased and the price purchased. So, if you purchased 50 shares @ $20.00 and 100 shares at $10.00, your average is not $15.00, but rather $13.33 ($2,000 / 150 shares).
    "You'll never be as perfect as BLaaaaaaaaarche."

  7. #17
    Wolfmaster Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy's Avatar
    Join Date
    Mar 2008
    Location
    Peoria, IL
    Posts
    2,386
    Blog Entries
    5
    Real Name
    Wolff
    Rep Power
    15

    OK, my bad. My second join should have been a LEFT JOIN rather then the inner join.
    Wolffy
    .-- ----- ..-. ..-. -.--
    Opinions expressed are my own and do not necessity reflect those of any sane person. Any code provided is intended to be an example and is provided AS IS. Void where prohibited by law. Not valid in California. Your mileage may vary.

  8. #18
    Wolfmaster Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy's Avatar
    Join Date
    Mar 2008
    Location
    Peoria, IL
    Posts
    2,386
    Blog Entries
    5
    Real Name
    Wolff
    Rep Power
    15

    Quote Originally Posted by BLaaaaaaaaaarche View Post
    No, because it is really not a straight average. It is a weighted average of all shares purchased and the price purchased. So, if you purchased 50 shares @ $20.00 and 100 shares at $10.00, your average is not $15.00, but rather $13.33 ($2,000 / 150 shares).
    Ah, OK. back to the drawing board. Probably going to be another JOIN to a sub-query then.
    Wolffy
    .-- ----- ..-. ..-. -.--
    Opinions expressed are my own and do not necessity reflect those of any sane person. Any code provided is intended to be an example and is provided AS IS. Void where prohibited by law. Not valid in California. Your mileage may vary.

  9. #19
    Wolfmaster Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy's Avatar
    Join Date
    Mar 2008
    Location
    Peoria, IL
    Posts
    2,386
    Blog Entries
    5
    Real Name
    Wolff
    Rep Power
    15

    Here's my best guess without coding up some sample tables to try it on:
    Code:
      Select S.s_id, S.s_name, S.s_symbol, S.s_publisher, S.s_published_date
           , sum(Case when SD.sd_shares > 0 then SD.sd_shares else 0 end) as sd_shares
           , AP.sd_price_per_share
           , MD.sd_date
        From tblStocks S
        Left Join tblStocks_Detail SD
          On S.s_id = sd.s_id
        Left Join (
              Select s_id, max(sd_date) as sd_date
                From tblStocks_Detail
                Group by s_id
              ) as MD
          On (S.s_id = MD.s_id)
        Left Join (Select sd_id
                        , SUM(sd_shares * sd_price_per_share) / SUM(sd_shares) as sd_price_per_share
                     From tblStocks_Detail
                     Where sd_shares > 0
                     Group By sd_id
                    )  as AP
       Group By  S.s_id, S.s_name, S.s_symbol, S.s_publisher, S.s_published_date, AP.sp_price_per_share, MD.sd_date
    
    Hopefully that's close.
    Wolffy
    .-- ----- ..-. ..-. -.--
    Opinions expressed are my own and do not necessity reflect those of any sane person. Any code provided is intended to be an example and is provided AS IS. Void where prohibited by law. Not valid in California. Your mileage may vary.

  10. #20
    Barn Frequenter BLaaaaaaaaaarche will become famous soon enough BLaaaaaaaaaarche will become famous soon enough BLaaaaaaaaaarche's Avatar
    Join Date
    Mar 2008
    Posts
    188
    Rep Power
    5

    That seems to work (with a few minor tweaks). Now, how do I incoporate my CASE statements for the price_per_share and date fields. If the number of shares = 0, then I want to show 0 for the price_per_share. Also, same goes for the last trade date. If no trades, then show 'N/A'. Where do I put the CASE for these two?
    "You'll never be as perfect as BLaaaaaaaaarche."

+ Reply to Thread
Page 2 of 3 FirstFirst 1 2 3 LastLast

Similar Threads

  1. switch case in c#
    By guddu in forum .NET Development
    Replies: 2
    Last Post: January 28th, 2009, 11:33 AM
  2. asp[classic] CASE equiv in php
    By Borojim in forum PHP Development
    Replies: 2
    Last Post: September 20th, 2008, 01:46 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

SEO by vBSEO