+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 13

Thread: SQL where clause help

  1. #1
    Barn Legend Rebelle will become famous soon enough Rebelle's Avatar
    Join Date
    Mar 2008
    Posts
    1,522
    Rep Power
    5

    SQL where clause help

    Hi All,

    Ok, I want the sum of either JobDescID = 1 thru 11 or any JobDesc containing the word "Field"....I tried the following but not getting the correct results.

    this is a new field in my select statement query....
    Code:
      (SELECT     SUM(ActThreeMoAgo)
                                FROM          vwPlanning B
                                WHERE      LocID = B.LocID AND JobDescID = 1 OR
                                                       LocID = B.LocID AND JobDescID = 2 OR
                                                       LocID = B.LocID AND JobDescID = 3 and so til 11... :o) AS SumAllField
    
    my results for LocID 1 should be 33 but its returning 3003

    also tried...
    Code:
     (SELECT     SUM(ActThreeMoAgo)
                                FROM          vwPlanning B
                                WHERE      LocID = B.LocID AND JobDesc = '%Field%') AS SumAllField
    
    results come back NULL ....

    Thanks for any help with this....

  2. #2
    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

    Where JobDesc like '%field%' or (JobDescID between 1 and 11)
    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.

  3. #3
    Barn Legend Rebelle will become famous soon enough Rebelle's Avatar
    Join Date
    Mar 2008
    Posts
    1,522
    Rep Power
    5

    Ok, Thanks w0lffy

    Still getting the same funny results but my next question...I'm sum an already summed field in the same query, is this ok?

    Ex:
    Code:
    my select statement has 
    select Sum(ActThreeMoAgo) as ActThreeMoAgo, more fields...etc., then I have this as the last field...,  (SELECT     SUM(ActThreeMoAgo)
                                FROM          vwPlanning B
                                WHERE      LocID = B.LocID AND JobDesc like '%Field%') AS SumAllField
    
    then from and where and groupby...
    

  4. #4
    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

    If I understand the query correctly no you can't use the results of an aggregate in the same select statement. If you need a sum of sum....
    Code:
    Select sum(subTotal) as GrandTotal
      From (Select sum(lineItems) as subTotal)
    
    or in SQL 2005 and up
    Code:
    With CTE as (
      Select sum(lineItems) as subTotal
    ) Select sum(subTotal) as GrandTotal From CTE
    
    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.

  5. #5
    Barn Legend Rebelle will become famous soon enough Rebelle's Avatar
    Join Date
    Mar 2008
    Posts
    1,522
    Rep Power
    5

    Quote Originally Posted by Wolffy View Post
    If I understand the query correctly no you can't use the results of an aggregate in the same select statement. If you need a sum of sum....
    Code:
    Select sum(subTotal) as GrandTotal
      From (Select sum(lineItems) as subTotal)
    
    or in SQL 2005 and up
    Code:
    With CTE as (
      Select sum(lineItems) as subTotal
    ) Select sum(subTotal) as GrandTotal From CTE
    
    ok, so if I have the correct results I want for the first field Sum(ActThreeMoAgo) as ActThreeMoAgo, and I get values 10, 20, and 3, then if I want to get sum 33 in new column i should create new query?

  6. #6
    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
    Code:
    Select 
        grouper
      , Sum(ActThreeMoAgo) as ActThreeMoAgo
      , (Select Sum(ActThreeMoAgo) from aTable) as TotalActThreeMoAgo
    From aTable
    Group By grouper , (Select Sum(ActThreeMoAgo) from aTable)
    
    Tho why do you need to Grand Total on each record? I think what you are trying to do is a ROLLUP.

    See http://odetocode.com/articles/85.aspx for a really good explanation of WITH ROLLUP
    Last edited by Wolffy; November 4th, 2009 at 12:11 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.

  7. #7
    Barn Legend Rebelle will become famous soon enough Rebelle's Avatar
    Join Date
    Mar 2008
    Posts
    1,522
    Rep Power
    5

    Ok, let me look at what you are suggesting.

    I have good results but I need to get more results from those results.

    ex:
    without new fields wanting

    Code:
    LocID-JobDescID-JobDesc-Act3MoAgo-Act2MoAgo-Act1MoAgo-LastFcast..etc.
    1-1-Field1-10-8-7-8
    1-2-Field2-20-9-9-9
    1-3-Field3-0-1-1-1
    1-4-Field4-3-2-3-3
    1-5-Field5-0-1-1-1
    1-14-Admin-11-9-9-9
    1-15-Support-15-13-13-20
    
    I would like to have a new column that will give me the sum of all in LocID = 1 and JobDescID between 1 and 11..so an extra column would be added at the right end and show value 33 (according to items in blue highlighted above).

  8. #8
    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

    Again, why do you need this on each record?
    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. #9
    Barn Legend Rebelle will become famous soon enough Rebelle's Avatar
    Join Date
    Mar 2008
    Posts
    1,522
    Rep Power
    5

    oh...well, i don't i guess...I was just thinking of how can I get one complete query will all fields I want displayed on my asp page.

    I have about 5 different values i need to retrieve...that one is all containing the word "Field" in description, then All with word "Field" minus - "FieldTrainee", then sum Admin and Support, etc.

    On my asp page, I have revenue displaying once even the it repeats in this main query but I need the values of the above to do calculations like ....

    field divided by Support
    Field-minusTrainee divided by sum(support + admin)

    etc.
    Last edited by Rebelle; November 4th, 2009 at 12:37 PM.

  10. #10
    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

    It can be done, but I usually do that in a stored procedure using an UPDATE query. In your case, I'd just write a separate query to get the results you want or do the math in the ASP page.
    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.

+ Reply to Thread
Page 1 of 2 1 2 LastLast

Similar Threads

  1. Including SQL Function in WHERE Clause
    By BLaaaaaaaaaarche in forum Microsoft SQL Server
    Replies: 2
    Last Post: July 16th, 2009, 12:36 PM
  2. Using a field value in an IN(...) clause
    By richyrich in forum MySQL
    Replies: 5
    Last Post: April 13th, 2008, 12:56 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