Mysql 将相同的表连接到重复的行

Mysql 将相同的表连接到重复的行,mysql,sql,join,Mysql,Sql,Join,我试图在查询中联接同一个表,但得到重复的行 我想得到的是一个列,其中包含一年的发票总额,以及每个客户每个季度的一个列 以下是查询的简化版本: select client.name, sum(total.totdoc) as TOTAL, sum(t1.totdoc) as T1 from client join invoices total on total.idclient=client.idclient join invoices t1

我试图在查询中联接同一个表,但得到重复的行

我想得到的是一个列,其中包含一年的发票总额,以及每个客户每个季度的一个列

以下是查询的简化版本:

select client.name, sum(total.totdoc) as TOTAL, sum(t1.totdoc) as T1 
from client
     join invoices total
          on total.idclient=client.idclient
     join invoices t1
          on t1.idclient=total.idclient
where t1.date between '01/01/2016' and '30/03/2016'
      and total.date between '01/01/2016' and '31/12/2016'
group by client.name
having sum(total.income) > '1000'
示例数据(客户端仅为idclient和name):

输出点应为:

client name       Total         T1
    A            1382.17      123.56
    B           2,557.32     2,557.32
当我手动检查其中一个时,结果应该在3000左右,我得到了81.000左右

我尝试了
sum(distinct total.totdoc)
并且它对一些(总数不同的)有效,但是重复的那些会丢失

同样,这是一个简化的例子,所以如果有什么不清楚的地方,请询问


谢谢

我认为您不应该使用发票T1加入,因为这样,您将获得重复的记录,只有一次加入就足够了。如果看到查询,则会选择满足第一个联接的行,然后通过第二个联接选择同一行。我认为您需要为每个客户分别查询年度和三个月的发票总额


我认为你不应该加入发票T1,因为这样你会得到重复的记录,只有一次加入就足够了。如果看到查询,则会选择满足第一个联接的行,然后通过第二个联接选择同一行。我认为您需要为每个客户分别查询年度和三个月的发票总额


我想你只需要条件聚合。大概是这样的:

select c.name, sum(i.totdoc) as total,
       sum(case when month(date) in (1, 2, 3) then i.totdoc end) as q1,
       sum(case when month(date) in (4, 5, 6) then i.totdoc end) as q2,
       sum(case when month(date) in (7, 8, 9) then i.totdoc end) as q3,
       sum(case when month(date) in (10, 11, 12) then i.totdoc end) as q4
from client c join
     invoices i
     on i.idclient = c.idclient
where i.date >= '2016-01-01' and i.date < '2017-01-01'
group by c.name
having sum(i.income) > 1000;
选择c.name、sum(i.totdoc)作为总计,
将(1、2、3)中的月份(日期)和i.totdoc结束时的总和作为q1,
将(4、5、6)中的月份(日期)和i.totdoc结束时的总和作为第2季度,
第三季度的总和(如第(7、8、9)个月(日期),然后i.totdoc结束),
第四季度合计(如第(10、11、12)个月(日期),然后i.totdoc结束)
从客户端c连接
发票一
在i.idclient=c.idclient上
其中i.date>='2016-01-01'和i.date<'2017-01-01'
按c.name分组
总收入(i.收入)>1000;

我认为您只需要条件聚合。大概是这样的:

select c.name, sum(i.totdoc) as total,
       sum(case when month(date) in (1, 2, 3) then i.totdoc end) as q1,
       sum(case when month(date) in (4, 5, 6) then i.totdoc end) as q2,
       sum(case when month(date) in (7, 8, 9) then i.totdoc end) as q3,
       sum(case when month(date) in (10, 11, 12) then i.totdoc end) as q4
from client c join
     invoices i
     on i.idclient = c.idclient
where i.date >= '2016-01-01' and i.date < '2017-01-01'
group by c.name
having sum(i.income) > 1000;
选择c.name、sum(i.totdoc)作为总计,
将(1、2、3)中的月份(日期)和i.totdoc结束时的总和作为q1,
将(4、5、6)中的月份(日期)和i.totdoc结束时的总和作为第2季度,
第三季度的总和(如第(7、8、9)个月(日期),然后i.totdoc结束),
第四季度合计(如第(10、11、12)个月(日期),然后i.totdoc结束)
从客户端c连接
发票一
在i.idclient=c.idclient上
其中i.date>='2016-01-01'和i.date<'2017-01-01'
按c.name分组
总收入(i.收入)>1000;


认为你缺少。。。按客户分组。名称此处缺少一个报价:
其中t1.date介于'01/01/2016'和30/06/2016'之间。
您能否提供一些您认为缺少的示例数据。。。按客户分组。名称此处缺少一个报价:
其中t1.date介于'01/01/2016'和30/06/2016'之间。
您能提供一些示例数据吗?我看不到与我提供的示例有任何不同现在试试希望这会有所不同1未声明,所以它现在找不到t1.date或t1.totdocTry实际上我只是放弃了删除t1这会返回两个具有相同值的列。我看不到与我提供的示例有任何不同现在尝试希望这会有所不同1未声明,所以它现在找不到t1.date或t1.totdocTry,实际上我只是放弃了删除t1,这将返回两个具有相同值的列。这非常有效!我不知道在SQL中可以使用这样的条件。谢谢,你是救命恩人!(作为旁注,关于having子句,您必须求和i.totdoc:P)这非常有效!我不知道在SQL中可以使用这样的条件。谢谢,你是救命恩人!(作为旁注,关于having条款,您必须将i.totdoc:P加总)
select c.name, sum(i.totdoc) as total,
       sum(case when month(date) in (1, 2, 3) then i.totdoc end) as q1,
       sum(case when month(date) in (4, 5, 6) then i.totdoc end) as q2,
       sum(case when month(date) in (7, 8, 9) then i.totdoc end) as q3,
       sum(case when month(date) in (10, 11, 12) then i.totdoc end) as q4
from client c join
     invoices i
     on i.idclient = c.idclient
where i.date >= '2016-01-01' and i.date < '2017-01-01'
group by c.name
having sum(i.income) > 1000;