+ Reply to Thread
Results 1 to 5 of 5

Thread: Sql group by

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

    Sql group by

    I have a field with the following options: DEMAND, SUPPLY, SUPPLYP, SUPPLYF. Is there a way to GROUP BY all values that contain SUPPLY in my SQL?

    Current SQL is:

    Code:
    strSQL = "SELECT mrp_part, SUM(mrp_qty) AS mrp_qty_due, mrp_due_date, mrp_type " & _
    	"FROM mrp_det " & _
    	"WHERE mrp_part = '1140016058' " & _
    	"GROUP BY mrp_part, mrp_due_date, mrp_type " & _
    	"ORDER BY mrp_part, mrp_type, mrp_due_date"
    
    The field that contains these values is mrp_type.
    "You'll never be as perfect as BLaaaaaaaaarche."

  2. #2
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Reston, VA
    Posts
    4,547
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    Quote Originally Posted by BLaaaaaaaaaarche View Post
    I have a field with the following options: DEMAND, SUPPLY, SUPPLYP, SUPPLYF. Is there a way to GROUP BY all values that contain SUPPLY in my SQL?

    Current SQL is:

    Code:
    strSQL = "SELECT mrp_part, SUM(mrp_qty) AS mrp_qty_due, mrp_due_date, mrp_type " & _
        "FROM mrp_det " & _
        "WHERE mrp_part = '1140016058' " & _
        "GROUP BY mrp_part, mrp_due_date, mrp_type " & _
        "ORDER BY mrp_part, mrp_type, mrp_due_date"
    
    The field that contains these values is mrp_type.
    Hmm...a little tricky. Try this:
    Code:
    SELECT
      (CASE
        WHEN mrp_type LIKE 'Supply%' THEN 'Supply'
        WHEN mrp_type LIKE 'Demand%' THEN 'Demand'
        ELSE 'Other'
      END) AS mrpType,
      mrp_part, 
      SUM(mrp_qty) AS mrp_qty_due, 
      mrp_due_date
    FROM
      mrp_det
    GROUP BY
      mrp_part, mrp_due_date, mrpType
    ORDER BY
      mrp_part, mrpType, mrp_due_date;
    
    Not sure how well this will work or if it even returns exactly what you want...worth a shot, though.
    jmurrayhead
    If you agree, give me rep.
    If you like it here...throw us a few bones to help support us.


  3. #3
    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 seems to display the field name properly, either DEMAND or SUPPLY, but it does not perform the SUM function properly on the GROUP BY mrp_type. For instance, I have two records:

    Code:
    SUPPLYP - 2/18/09 - 10
    SUPPLY - 2/18/09 - 45
    
    It is currently showing those as two separate lines, rather than adding them together. Any ideas?
    "You'll never be as perfect as BLaaaaaaaaarche."

  4. #4
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    In JMH's SQL it groups by the alias he creates mrpType, not the field mrp_type.

    Just checking this is right in your code.

  5. #5
    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. I needed to also add a CASE to the GROUP BY field:

    Code:
    	strSQL = "SELECT " & _
    		"(CASE " & _
    			"WHEN mrp_type LIKE 'SUPPLY%' THEN 'SUPPLY' " & _
    			"WHEN mrp_type LIKE 'DEMAND%' THEN 'DEMAND' " & _
    			"ELSE 'Other' " & _
    		"END) AS mrp_type, " & _
    		"mrp_part, SUM(mrp_qty) AS mrp_qty_due, mrp_due_date " & _
    		"FROM mrp_det " & _
    		"WHERE mrp_part = '1160068' " & _
    		"GROUP BY mrp_part, mrp_due_date, " & _
    		"(CASE " & _
    			"WHEN mrp_type LIKE 'SUPPLY%' THEN 'SUPPLY' " & _
    			"WHEN mrp_type LIKE 'DEMAND%' THEN 'DEMAND' " & _
    			"ELSE 'Other' " & _
    		"END) " & _
    		"ORDER BY mrp_part, mrp_type, mrp_due_date"
    
    "You'll never be as perfect as BLaaaaaaaaarche."

+ Reply to Thread

Similar Threads

  1. Summing for each group only
    By Rebelle in forum ASP Development
    Replies: 13
    Last Post: June 5th, 2008, 02:22 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