我正在尝试使用以下多个联接将来自其他表的结果包括在以前的查询中:
SELECT mid as mID,
round((x.qty_sum / x.qty_count), 5) as qtAVG,
round(x.qty_stddev, 5) as qtSTDDEV,
x.qty_count as qtCOUNT,
round((x.rel_sum / x.rel_count), 5) as relAVG,
round(x.rel_stddev, 5) as relSTDDEV,
x.rel_count as relCOUNT,
FROM (SELECT t.mid,
SUM( mt = 'qt' ) as qty_count,
SUM(CASE WHEN t_r.mt = 'qt' THEN rt END) as qty_sum,
STD(CASE WHEN t_r.mt = 'qt' THEN rt END) as qty_stddev,
SUM( t_r.mt = 'rel' ) as rel_count,
SUM(CASE WHEN t_r.mt = 'rel' THEN rel END) as rel_sum,
STD(CASE WHEN t_r.mt = 'rel' THEN rel END) as rel_stddev
FROM t_r r
right join t_m t on t.mid = r.mid
right join m_k m on m.mid = t.mid
right join k_d k on m.kid = k.kid
GROUP BY t.mid
) x;
使用我上面的查询, qty_count
为了 111
什么时候 mt
是 qt
退货 6
而不是 2
. 2 * (count of 111 in table m_k)
当我移除连接的这一部分时,我得到了所需的和 qtCOUNT
以及 relCOUNT
```
right join m_k m on m.mid = t.mid
right join k_d k on m.kid = k.kid
我做错了什么,怎么解决?
数据:
好的
mid kid
109 2
110 2
110 4
111 1
111 2
111 3
库德
kid k_desc
1 desc1
2 desc2
3 desc3
4 desc4
穆德
mid col1 col2 col3 col4
109 val_a val_d val_g val_j
110 val_b val_e val_h val_k
111 val_c val_f val_i val_l
第
mid rt stamp mt
111 3 2018-12-08 01:30:31 rel
111 4 2018-12-08 03:41:56 qt
111 3 2018-12-08 02:29:10 qt
110 1 2018-12-08 06:13:51 rel
110 5 2018-12-08 11:44:39 qt
109 1 2018-12-08 10:39:51 rel
其他实现上述功能的查询也可以。
1条答案
按热度按时间7cjasjjr1#
我搬家解决了这个问题
在派生表之外
x
. 最终查询如下所示: