简体   繁体   中英

group_concat concat sum if dates are equal

I have this group_concat with concat that is working fine except I need to find a way to get the same results but sum(pest_count) where scout_date is the same. The $scout_date is coming from a dropdown that is looking at the same table for the dates so it has the format 2014-03-13. Thanks for looking.

"SELECT group_concat(concat('<div class=\"',pest_name,'\">',pest_count,' ',pest_name,'</div>')   SEPARATOR ' ') pests,
  card_id,
  card_type,
  top_y,
  left_x,
  scout_date FROM scout_logpestnum WHERE custnum='$custnum' AND locationname='$locationname' AND scoutlogname = '$scoutlogname' AND date(scout_date) ='$scout_date' 
GROUP BY
   card_id,
   card_type,
   top_y,
   left_x,
   scout_date"

Here's my object that echos the result

<?php echo $obj['pests']; ?>

Here's my table data

pest_name   pest_count  card_id   card_type top_y   left_x   scout_date
Aphids      2              1       yellcard  652    703     2014-3-13 15:59:54
Thrips      4              1       yellcard  652    703     2014-3-13 15:59:54
Thrips      2              2       bluecard  754    707     2014-3-13 15:59:54
Thrips      1              3       yellcard  531    616     2014-3-13 15:59:54
Thrips      1              5       yellcard   80    613     2014-3-13 15:59:54
Aphids      1              1       yellcard  652    703     2014-3-13 16:00:04
Thrips      2              1       yellcard  652    703     2014-3-13 16:00:04

Here's what the query will return now for card_id 1

 2 Aphids
 4 Thrips
 1 Aphids
 2 Thrips

Here' the desired result

Here's what I want in the end for each pest_name and pest_count at each card. This would be card_id 1 for the date 2014-3-13

3 Aphids
6 Thrips

Here's my final version for now thanks to John for the much needed help.

SELECT card_id,left_x,top_y,card_type, 
GROUP_CONCAT(CONCAT('<div class=\"',pest_name,'\">',pest_count,' ',pest_name,'</div>')   SEPARATOR ' ') pests FROM(
SELECT card_id,left_x,top_y,card_type, pest_name AS pest_name, sum(pest_count) AS pest_count, scout_date
FROM scout_logpestnumtest
GROUP BY pest_name,card_id, date(scout_date)
) as t
  GROUP by card_id

I believe what you want to do is group by the DATE of the datetime field instead of including the timestamp.. when you do DATE(scout_date) it will ignore the timestamp and just group the date.. so since thats the only field that is different that would cause the grouping to show 4 instead of 2 results it seems like thats the problem there.

SELECT 
    group_concat(
        concat('<div class=\"',pest_name,'\">',pest_count,' ',pest_name,'</div>')   SEPARATOR ' '
    ) pests,
    card_id,
    card_type,
    top_y,
    left_x,
    scout_date 
FROM scout_logpestnum 
WHERE custnum='$custnum' 
    AND locationname='$locationname' 
    AND scoutlogname = '$scoutlogname' 
    AND date(scout_date) ='$scout_date' 
GROUP BY card_id, DATE(scout_date)

EDIT : my recommendation is to sum the data first then do the concat afterwards like this. see FIDDLE for example

SELECT
    card_id, 
    group_concat(
        concat('<div class=\"',pest_name,'\">',pest_count,' ',pest_name,'</div>')   SEPARATOR ' '
    ) pests 
FROM(
    SELECT 
        pest_name AS pest_name, 
        sum(pest_count) AS pest_count, 
        card_id,
        card_type,
        top_y,
        left_x,
        scout_date
    FROM scout_logpestnum
    WHERE custnum='$custnum' 
        AND locationname='$locationname' 
        AND scoutlogname = '$scoutlogname' 
        AND date(scout_date) ='$scout_date'
    GROUP BY card_id, card_type, top_y, left_x, date(scout_date)
) as t
GROUP BY card_id

EDIT: this FIDDLE shows the data by card id.. so my edit should work with the above query.. I removed the group concat just so you can read it better... it groups by the card and the name and the date so that way you have the correct number of div 's returned.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM