1

The problem in my query particularly in this piece:

@a:= concat(@a, ',', B.call_account_id) AS paid_account_id

Here is the whole query:

SELECT operator_id, paid_account_ids,  SUM( goods_count * price ) AS sales_volume, count(*) AS sales_cnt
FROM (
    SELECT B.operator_id, @a:= concat(@a, ',', B.call_account_id) AS paid_account_ids, B.call_time, A.goods_count, A.price, UNIX_TIMESTAMP( A.completion_date ) AS paid_ts
    FROM call_module_data B
    INNER JOIN ak_accounts A ON ( A.account_id = B.call_account_id AND A.goods_count >=1 )
    WHERE B.call_status IN (1,7) AND A.status_id = 5
    AND operator_id IN ( $op_ids )
    $and_str_accounts
    GROUP BY A.account_id
    HAVING call_time < (paid_ts + $time_shift)
    ) AS T
GROUP BY operator_id";

So the expression mentioned above should produce the concatenated string of account ids (e.g 3341,4355,4433...). But I got NULL instead of desired string. Please help to resolve. Thanks in advance.

1
  • You have a quote and a semicolon at the end there, that's why it won't work. Commented Jan 8, 2014 at 7:28

1 Answer 1

1

Use GROUP_CONCAT() function instead of String concatenation

Change

@a:= concat(@a, ',', B.call_account_id) AS paid_account_id

above string to

GROUP_CONCAT(B.call_account_id) AS paid_account_ids

Final Answer:

SELECT operator_id, GROUP_CONCAT(paid_account_ids) AS paid_account_ids, 
       SUM(goods_count * price) AS sales_volume, COUNT(*) AS sales_cnt
FROM (SELECT B.operator_id, GROUP_CONCAT(B.call_account_id) AS paid_account_ids, 
             B.call_time, A.goods_count, A.price, 
             UNIX_TIMESTAMP(A.completion_date) AS paid_ts
      FROM call_module_data B
      INNER JOIN ak_accounts A ON A.account_id = B.call_account_id AND A.goods_count >=1
      WHERE B.call_status IN (1,7) AND A.status_id = 5 AND operator_id IN ($op_ids)
            $and_str_accounts
      GROUP BY A.account_id HAVING call_time < (paid_ts + $time_shift)
     ) AS T
GROUP BY operator_id;
Sign up to request clarification or add additional context in comments.

1 Comment

It looks like it should work but I get only first account id instead of concatenated string !?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.