SELECT kodeagent
, IFNULL((
SELECT COUNT(1)
FROM bsn_data
WHERE bsn_data.periode LIKE '2018-12-%%'
AND bsn_data.kodeupline2 = bsn_kode_agent.kodeagent
AND bsn_data.kodeagent IN(
SELECT bsn_data.kodeagent
FROM bsn_data
WHERE bsn_data.periode LIKE '2018-12-%%'
AND bsn_data.kodeupline2 = bsn_kode_agent.kodeagent
GROUP BY bsn_data.kodeagent ORDER BY COUNT(1) DESC LIMIT 1
)
), 0) AS totps
FROM bsn_kode_agent
WHERE fungsi = 'sales agent'
ORDER BY totps DESC
获得结果
此版本的MariaDB尚不支持“ LIMIT&IN / ALL / ANY / SOME子查询”
我该如何解决?我想在子查询中添加限制查询..谢谢..
答案 0 :(得分:0)
没有什么可检查我的答案。请尝试此操作,将子查询包装在其他花药中,以提供tmp表别名(tmp_bsn_data):
SELECT kodeagent
, IFNULL((
SELECT COUNT(1)
FROM bsn_data
WHERE bsn_data.periode LIKE '2018-12-%%'
AND bsn_data.kodeupline2 = bsn_kode_agent.kodeagent
AND bsn_data.kodeagent IN( select tmp_bsn_data.kodeagent from (
SELECT bsn_data.kodeagent
FROM bsn_data
WHERE bsn_data.periode LIKE '2018-12-%%'
AND bsn_data.kodeupline2 = bsn_kode_agent.kodeagent
GROUP BY bsn_data.kodeagent ORDER BY COUNT(1) DESC LIMIT 1
) tmp_bsn_data
)
), 0) AS totps
FROM bsn_kode_agent
WHERE fungsi = 'sales agent'
ORDER BY totps DESC
答案 1 :(得分:0)
我认为这会起作用:
SELECT ka.kodeagent
(SELECT COUNT(1)
FROM bsn_data d
WHERE d.periode >= '2018-12-01' AND
d.periode < '2019-01-01' AND
d.kodeupline2 = ka.kodeagent AND
d.kodeagent = (SELECT d2.kodeagent
FROM bsn_data d2
d2.periode >= '2018-12-01' AND
d2.periode < '2019-01-01' AND
d2.kodeupline2 = ka.kodeagent
GROUP BY d2.kodeagent
ORDER BY COUNT(1) DESC
LIMIT 1
)
) AS totps
FROM bsn_kode_agent ka
WHERE ka.fungsi = 'sales agent'
ORDER BY totps DESC;
注意:
=
可以使用limit
,尽管in
不能。COUNT()
不返回NULL
,因此不需要进行NULL
比较。我仍然认为查询不会起作用,因为您有一个双重嵌套的相关子句。但这确实解决了您眼前的问题。
如果仍然无法解决问题,请提出另一个问题,提供示例数据,所需结果以及对您要实现的逻辑的解释。
答案 2 :(得分:0)
避免使用IN ( SELECT ... )
在这种情况下,将其变成JOIN
应该很容易。
更改中间查询:
SELECT COUNT(1)
FROM bsn_data
WHERE bsn_data.periode LIKE '2018-12-%%'
AND bsn_data.kodeupline2 = bsn_kode_agent.kodeagent
AND bsn_data.kodeagent IN (
SELECT bsn_data.kodeagent
FROM bsn_data
WHERE bsn_data.periode LIKE '2018-12-%%'
AND bsn_data.kodeupline2 = bsn_kode_agent.kodeagent
GROUP BY bsn_data.kodeagent
ORDER BY COUNT(1) DESC
LIMIT 1
)
到
SELECT COUNT(1)
FROM
( SELECT bsn_data.kodeagent
FROM bsn_data
WHERE bsn_data.periode >= '2018-12-01'
AND bsn_data.periode < '2018-12-01' + INTERVAL 1 MONTH
AND bsn_data.kodeupline2 = bsn_kode_agent.kodeagent
GROUP BY bsn_data.kodeagent
ORDER BY COUNT(1) DESC
LIMIT 1
) AS x
JOIN bsn_data ON x.kodeagent = bsn_data.kodeagent
WHERE bsn_data.periode >= '2018-12-01'
AND bsn_data.periode < '2018-12-01' + INTERVAL 1 MONTH
AND bsn_data.kodeupline2 = bsn_kode_agent.kodeagent
索引:
bsn_data: INDEX(kodeupline2, periode, kodeagent) -- in this order
bsn_data: (kodeagent) -- is this the PRIMARY KEY?
但是等等!不能简化为
SELECT COUNT(1) AS ct
FROM bsn_data
WHERE bsn_data.periode >= '2018-12-01'
AND bsn_data.periode < '2018-12-01' + INTERVAL 1 MONTH
AND bsn_data.kodeupline2 = bsn_kode_agent.kodeagent
GROUP BY bsn_data.kodeagent
ORDER BY COUNT(1) DESC
LIMIT 1
答案 3 :(得分:0)
MariaDB提供了“窗口功能”,我相信可以利用此功能(也指前面的问题,似乎只需要“前3名”代理的计数即可):
CREATE TABLE bsn_kode_agent( kodeagent VARCHAR(10) NOT NULL PRIMARY KEY ,fungsi VARCHAR(40) NOT NULL );
INSERT INTO bsn_kode_agent(kodeagent,fungsi) VALUES ('a','sales agent') , ('b','sales agent');
CREATE TABLE bsn_data( kodeagent VARCHAR(1) NOT NULL ,kodeupline2 VARCHAR(2) NOT NULL ,periode DATE NOT NULL );
INSERT INTO bsn_data(kodeagent,kodeupline2,periode) VALUES ('a','b1','2018-12-01') , ('a','b1','2018-12-01') , ('a','b1','2018-12-01') , ('a','c1','2018-12-01') , ('a','c1','2018-12-01') , ('a','c1','2018-12-01') , ('a','d1','2018-12-01') , ('a','d1','2018-12-01') , ('a','e1','2018-12-01') , ('a','f1','2018-12-01') ;
SELECT b.kodeagent , IFNULL( SUM( d.total ), 0 ) AS totps FROM bsn_kode_agent AS b LEFT JOIN ( SELECT tableb.kodeupline2 , tableb.kodeagent , tableb.total , ROW_NUMBER() OVER (PARTITION BY tableb.kodeagent ORDER BY tableb.total DESC) as rn FROM ( SELECT bsn_data.kodeupline2 , bsn_data.kodeagent , COUNT( 1 ) total FROM bsn_data WHERE bsn_data.periode >= '2018-12-01' AND bsn_data.periode < '2018-12-01' + INTERVAL 1 MONTH GROUP BY bsn_data.kodeupline2 , bsn_data.kodeagent ) AS tableb ) d ON d.kodeagent = b.kodeagent and d.rn <=3 WHERE b.fungsi = 'sales agent' group by b.kodeagent ORDER BY totps DESC
kodeagent | totps :-------- | ----: a | 8 b | 0
以下:如果独立运行子查询结果。请注意,它是rn
列,它允许仅对计数最高的代理进行后续过滤。
SELECT tableb.kodeupline2 , tableb.kodeagent , tableb.total , ROW_NUMBER() OVER (PARTITION BY tableb.kodeagent ORDER BY tableb.total DESC) as rn FROM ( SELECT bsn_data.kodeupline2 , bsn_data.kodeagent , COUNT( 1 ) total FROM bsn_data WHERE bsn_data.periode >= '2018-12-01' AND bsn_data.periode < '2018-12-01' + INTERVAL 1 MONTH GROUP BY bsn_data.kodeupline2 , bsn_data.kodeagent ) AS tableb
kodeupline2 | kodeagent | total | rn :---------- | :-------- | ----: | -: b1 | a | 3 | 1 c1 | a | 3 | 2 d1 | a | 2 | 3 e1 | a | 1 | 4 f1 | a | 1 | 5
db <>提琴here
还请注意,有一些样本数据非常有用,但是,因为没有提供,我可能对此处看到的样本做出了不正确的假设-如果提供样本数据,总会更好问题。