这是我的桌子:
我需要获取每个每个Callid的元素名称计数,其中varname等于'eCounter'并且varvalue最大。
这些表通过元素标识一个(元素)与许多(元素细节)相关。我包括了实际elementid的格式,因此您不要假设它们是连续的数字。
我尝试下面的代码只是为了获得每次调用的最大varvalue / elementid,但这也没有解决。
选择MAX(a.varvalue)作为MAXvarvalue,b.callgui FROM elementdetail AS a,元素AS b 在(a.elementid = b.elementid)和(a.varname ='eCounter') GROUP BY变量名,callguid
这就是我需要的结果看起来像的样子。
答案 0 :(得分:1)
使用最新的Informix版本(14.10.XC1)。
假设有以下表格:
CREATE TABLE element
(
callid INTEGER,
elementid BIGINT,
elementname CHAR( 20 )
);
CREATE TABLE elementdetail
(
elementid BIGINT,
varname CHAR( 20 ),
varvalue INTEGER
);
其中填充了示例数据(进行救援的OCR,您确实应该将示例数据和表格以文本形式发布,而不是以图片形式发布):
INSERT INTO element VALUES ( 1, 1001901560322810000, 'set_Page1' );
INSERT INTO element VALUES ( 1, 1001921560322810000, 'set_Page5' );
INSERT INTO element VALUES ( 1, 1001181560322820000, 'set_Page4' );
INSERT INTO element VALUES ( 1, 1001021560322820000, 'set_Page3' );
INSERT INTO element VALUES ( 1, 1001331560322830000, 'set_Page6' );
INSERT INTO element VALUES ( 2, 1002281560322920000, 'set_Page1' );
INSERT INTO element VALUES ( 2, 1002301560322920000, 'set_Page5' );
INSERT INTO element VALUES ( 2, 1002881560322940000, 'set_Page4' );
INSERT INTO element VALUES ( 3, 1002271560322950000, 'set_Page1' );
INSERT INTO element VALUES ( 3, 1002951560322970000, 'set_Page4' );
INSERT INTO element VALUES ( 3, 1002231560322980000, 'set_Page6' );
INSERT INTO element VALUES ( 4, 1002781560323000000, 'set_Page1' );
INSERT INTO element VALUES ( 4, 1002891560323020000, 'set_Page5' );
INSERT INTO element VALUES ( 4, 1002391560323040000, 'set_Page4' );
INSERT INTO elementdetail VALUES ( 1001901560322810000, 'eCounter', 0 );
INSERT INTO elementdetail VALUES ( 1001901560322810000, 'other_variables', NULL );
INSERT INTO elementdetail VALUES ( 1001921560322810000, 'eCounter', 1 );
INSERT INTO elementdetail VALUES ( 1001921560322810000, 'other_variables', NULL );
INSERT INTO elementdetail VALUES ( 1001181560322820000, 'eCounter', 2 );
INSERT INTO elementdetail VALUES ( 1001181560322820000, 'other_variables', NULL );
INSERT INTO elementdetail VALUES ( 1001021560322820000, 'eCounter', 4 );
INSERT INTO elementdetail VALUES ( 1001021560322820000, 'other_variables', NULL );
INSERT INTO elementdetail VALUES ( 1001331560322830000, 'eCounter', 5 );
INSERT INTO elementdetail VALUES ( 1001331560322830000, 'other_variables', NULL );
INSERT INTO elementdetail VALUES ( 1002281560322920000, 'eCounter', 0 );
INSERT INTO elementdetail VALUES ( 1002281560322920000, 'other_variables', NULL );
INSERT INTO elementdetail VALUES ( 1002301560322920000, 'eCounter', 1 );
INSERT INTO elementdetail VALUES ( 1002301560322920000, 'other_variables', NULL );
INSERT INTO elementdetail VALUES ( 1002881560322940000, 'eCounter', 2 );
INSERT INTO elementdetail VALUES ( 1002881560322940000, 'other_variables', NULL );
INSERT INTO elementdetail VALUES ( 1002271560322950000, 'eCounter', 0 );
INSERT INTO elementdetail VALUES ( 1002271560322950000, 'other_variables', NULL );
INSERT INTO elementdetail VALUES ( 1002951560322970000, 'eCounter', 1 );
INSERT INTO elementdetail VALUES ( 1002951560322970000, 'other_variables', NULL );
INSERT INTO elementdetail VALUES ( 1002231560322980000, 'eCounter', 2 );
INSERT INTO elementdetail VALUES ( 1002231560322980000, 'other_variables', NULL );
INSERT INTO elementdetail VALUES ( 1002781560323000000, 'eCounter', 0 );
INSERT INTO elementdetail VALUES ( 1002781560323000000, 'other_variables', NULL );
INSERT INTO elementdetail VALUES ( 1002891560323020000, 'eCounter', 1 );
INSERT INTO elementdetail VALUES ( 1002891560323020000, 'other_variables', NULL );
INSERT INTO elementdetail VALUES ( 1002391560323040000, 'eCounter', 2 );
INSERT INTO elementdetail VALUES ( 1002391560323040000, 'other_variables', NULL );
我们可以利用CTE做这样的事情:
WITH cte_element AS
(
SELECT
e.callid,
e.elementname,
d.varvalue
FROM
element AS e
INNER JOIN
elementdetail AS d
ON
e.elementid = d.elementid
WHERE
d.varname = 'eCounter'
ORDER BY
e.callid,
d.varvalue
)
SELECT
vt1.elementname,
COUNT( vt1.elementname ) AS count_elementname
FROM
(
SELECT
tmp1.callid,
tmp1.elementname,
tmp1.varvalue
FROM
cte_element AS tmp1
WHERE
tmp1.varvalue =
(
SELECT
MAX( tmp2.varvalue )
FROM
cte_element AS tmp2
WHERE
tmp1.callid = tmp2.callid
GROUP BY
tmp2.callid
)
) AS vt1
GROUP BY
vt1.elementname
;
-- Results
elementname count_elementname
set_Page6 2
set_Page4 2
仍然使用Informix的最新版本,使用窗口而不是CTE:
SELECT
vt1.elementname,
COUNT( vt1.elementname ) AS count_elementname
FROM
(
SELECT
e.callid,
e.elementname,
d.varvalue,
RANK() OVER
(
PARTITION BY e.callid ORDER BY e.callid, d.varvalue DESC
) AS rank
FROM
element AS e
INNER JOIN
elementdetail AS d
ON
e.elementid = d.elementid
WHERE
d.varname = 'eCounter'
ORDER BY
e.callid,
d.varvalue
) AS vt1
WHERE
vt1.rank = 1
GROUP BY
vt1.elementname
;
-- Results
elementname count_elementname
set_Page6 2
set_Page4 2
对于较旧的Informix版本,没有CTE或窗口,因此我们使用一个临时表:
SELECT
e.callid,
e.elementname,
d.varvalue
FROM
element AS e
INNER JOIN
elementdetail AS d
ON
e.elementid = d.elementid
WHERE
d.varname = 'eCounter'
ORDER BY
e.callid,
d.varvalue
INTO TEMP temp_element
;
SELECT
vt1.elementname,
COUNT( vt1.elementname ) AS count_elementname
FROM
(
SELECT
tmp1.callid,
tmp1.elementname,
tmp1.varvalue
FROM
temp_element AS tmp1
WHERE
tmp1.varvalue =
(
SELECT
MAX( tmp2.varvalue )
FROM
temp_element AS tmp2
WHERE
tmp1.callid = tmp2.callid
GROUP BY
tmp2.callid
)
) AS vt1
GROUP BY
vt1.elementname
;
-- Results
elementname count_elementname
set_Page6 2
set_Page4 2
我不知道当您使用更健壮的数据集(每个表上成千上万个行)时,这些查询将如何扩展。
例如,当callid
具有2个elementname
且具有相同的eCounter
varvalue
值时,我没有测试过任何极端情况。