0

I'm having a database table named user_transaction whose structure is as below:

transaction_id    mediumint(6)   UNSIGNED (PK)
transaction_no    varchar(55)
transaction_cc_avenue_no    varchar(55)
transaction_card_category    varchar(100)
transaction_user_id      varchar(32)
transaction_user_name    varchar(255)
transaction_user_email_id    varchar(255)
transaction_deal_code    varchar(10)
transaction_dc_id    smallint(4)
transaction_amount    float(10,2)
transaction_discount    float(10,2)
transaction_total_amount    float(10,2)
transaction_data_assign    enum('0', '1')
transaction_status    enum('success', 'inprocess', 'fail', 'cancelled')     
transaction_date    bigint(12)
transaction_update_date    bigint(12)
transaction_update_user_id    varchar(32)

I'm using UNIX Timestamp values to store the dates in the column transaction_date. Now I want to display the monthwise no. of transactions of different transaction_status(i.e.Total transactions took place on each date,total no. of transactions of status 'success', 'inprocess', 'fail' and 'cancelled') for all the transaction_dates present in a table. Those records should be grouped by month. I tried a lot to get this result but not getting any success. For your reference I'm giving below my query:

SELECT COUNT(*) `total count`, SUM(transaction_status = 'success') `success`, SUM(transaction_status = 'inprocess') `inprocess`, SUM(transaction_status = 'fail') `fail`, SUM(transaction_status = 'cancelled') `cancelled` FROM user_transaction GROUP BY FROM_UNIXTIME(transaction_date)

Can anyone help me in this regard? Thanks in advance.

7
  • @Eric Wich & @D Mac: I want to show the month no. well. How should I write the query then?
    – PHPLover
    Jul 12, 2013 at 12:30
  • I've updated my answer to show year and month (you'll need year so the months are disambiguated across year changes).
    – D Mac
    Jul 12, 2013 at 12:32
  • @Eric Wich: By using your query I'm getting datewise results not monthwise.
    – PHPLover
    Jul 12, 2013 at 12:33
  • Did you change transaction_date to be a date instead of a bigint? And did you include the statement "GROUP BY MONTH(transaction_date)"?
    – D Mac
    Jul 12, 2013 at 12:36
  • @DMac:No, I didn't change the transaction_date datatype to date from bigint but used GROUP BY MONTH(transaction_date)
    – PHPLover
    Jul 12, 2013 at 12:38

1 Answer 1

1

It will be a lot easier if you just set transaction_date as a date. Then you can use the built-in functions.

For example, you could do

SELECT year(transaction_date), month(transaction_date),
    sum(whatever), ...
FROM user_transaction
GROUP BY YEAR(transaction_date), MONTH(transaction_date)
ORDER BY YEAR(transaction_date), MONTH(transaction_date);

If you have an aversion to changing the data type of transaction_date, then you can convert it on the fly with FROM_UNIXTIME. I don't recommend that, because it is an extra (unnecessary) layer of processing in every query. Nonetheless, that would mean you can leave your schema the way it is and write:

SELECT year(FROM_UNIXTIME(transaction_date)), month(FROM_UNIXTIME(transaction_date)),
    sum(whatever), ...
FROM user_transaction
GROUP BY YEAR(FROM_UNIXTIME(transaction_date)), MONTH(FROM_UNIXTIME(transaction_date))
ORDER BY YEAR(FROM_UNIXTIME(transaction_date)), MONTH(FROM_UNIXTIME(transaction_date));

Nasty, isn't it? You're better doing a 'ALTER TABLE CHANGE COLUMN transaction_date transaction_date date' and then you get all the advantages of doing date math with the easy to use MySQL functions.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.