没有得到行总和

时间:2018-12-06 08:49:57

标签: mysql

我编写了一个SQL查询,以使用组在某些字段上获取记录。我得到了期望的输出,但是新的要求是转换收益,因此没有重复,

我的查询

import React from 'react';

class CardComponent extends React.Component {
    render() {
        let data = this.props.data;


        let truncated = data.truncated;
        var test = new String(truncated);
        console.log(test);
        var finalText = '';

        var googleTranslate = require('google-translate')('apikey');
        googleTranslate.translate(data.text, 'en', function(err, translation) {
            finalText = translation;
          });

        return (
            <div>
                <div className="card-panel grey lighten-5 z-depth-3 hoverable thin">
                    <div className="row valign-wrapper">
                        <div className="col s2">
                            <img src={data.user.profile_image_url} alt={data.user.name} className="circle responsive-img" />
                        </div>
                        <div className="col s10 left-align">
                        {(() => {
                        if (test=='true') {
                        return (
                            <span className="black-text"> {data.extended_tweet.full_text}</span>
                        )
                        } else {
                        return (
                            <span className="black-text">translated text: {finalText}</span>
                        )
                        }
                    })()}
                        </div>

                    </div>
                    <div className="row valign-wrapper right-align chip hoverable">
                    {new Date(data.created_at).toLocaleTimeString()}
                    </div>
                    <div className="row valign-wrapper right-align chip hoverable">
                        <a href={`https://twitter.com/${data.user.screen_name}`} target="_blank">{`@${data.user.screen_name}`}</a>
                    </div>
                </div>

            </div>
        );
    }
}

export default CardComponent;

输出

select t.chain_code,t.voucher_type,t.expired_on,

                (select sum(count(*)) from vouchers t2 where t.`chain_code` = t2.`chain_code` AND t.voucher_type=t2.voucher_type AND t2.expired_on=t.expired_on AND t2.`expired_on` < CURRENT_DATE() + INTERVAL 9 MONTH ) as expiry_count,
                (select count(*) from vouchers t2 where t.`chain_code` = t2.`chain_code` AND t.voucher_type=t2.voucher_type AND t2.expired_on=t.expired_on ) as count

                from vouchers t GROUP BY `voucher_type`,chain_code

需要按照以下格式输出

select t.chain_code,t.voucher_type,t.expired_on,

                (select count(*) from vouchers t2 where t.`chain_code` = t2.`chain_code` AND t.voucher_type=t2.voucher_type AND t2.expired_on=t.expired_on AND t2.`expired_on` < CURRENT_DATE() + INTERVAL 9 MONTH ) as expiry_count,
                (select count(*) from vouchers t2 where t.`chain_code` = t2.`chain_code` AND t.voucher_type=t2.voucher_type AND t2.expired_on=t.expired_on ) as count

                from vouchers t GROUP BY `voucher_type`,chain_code, expired_on

chain_code  voucher_type    expired_on  expiry_count    count   
EM  AD  0000-00-00 00:00:00 389 389 
EM  AD  2000-07-03 15:25:39 2   2   
EM  AD  2000-07-03 15:27:55 2   2   

LI  AD  0000-00-00 00:00:00 265 265 
LI  AD  2000-07-03 15:25:39 4   4   

ME  AD  0000-00-00 00:00:00 655 655 
ME  AD  2000-07-03 15:25:39 11  11  
ME  AD  2000-07-03 15:27:55 5   5   

RE  AD  0000-00-00 00:00:00 210 210 
RE  AD  2000-07-03 15:25:39 1   1   

以此类推...

样本数据

chain_code  voucher_type    expiry_count    count   
EM           AD              393              393             
LI           AD              269              269 

1 个答案:

答案 0 :(得分:0)

将您的子查询作为表加入JOIN

SELECT DISTINCT t.chain_code, t.voucher_type, count_expired as expiry_count, count_all as count
FROM vouchers t
JOIN (SELECT chain_code, voucher_type, COUNT(*) as count_all 
      FROM vouchers 
      GROUP BY chain_code, voucher_type) AS t2 ON t.chain_code = t2.chain_code AND t.voucher_type = t2.voucher_type
JOIN (SELECT chain_code, voucher_type, COUNT(*) as count_expired 
      FROM vouchers 
      WHERE expired_on < CURRENT_DATE() + INTERVAL 9 MONTH 
      GROUP BY chain_code, voucher_type) AS t3 ON t.chain_code = t3.chain_code AND t.voucher_type = t3.voucher_type