INFORMIX:来自不同 callid 和 Max 变量值的子查询的每个元素名的计数
INFORMIX: a count of each elementname from sub-query of distinct callid and the Max varable value
这是我的表格:
我需要为每个 callid 计算元素名的数量,其中 varname 等于 'eCounter' 且 varvalue 最大。
这些表是 elementid 一个(元素)到多个(elementdetail)的关系。我包含了实际 elementid 的格式,因此您不会假设它们是连续的数字。
我尝试了以下代码只是为了获得每个调用的最大值 varvalue/elementid,但这也没有成功。
SELECT MAX(a.varvalue) 作为 MAXvarvalue,b.callgui
FROM elementdetail AS a,元素 AS b
其中 (a.elementid = b.elementid) 和 (a.varname = 'eCounter')
GROUP BY varname, callguid
这就是我需要的结果。
使用最新的 Informix 版本 ( 14.10.XC1 )。
假设以下 tables:
CREATE TABLE element
(
callid INTEGER,
elementid BIGINT,
elementname CHAR( 20 )
);
CREATE TABLE elementdetail
(
elementid BIGINT,
varname CHAR( 20 ),
varvalue INTEGER
);
其中填充了您的示例数据(OCR 来拯救,您真的应该 post 您的示例数据和 table 作为文本,而不是图像):
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 或窗口,因此我们使用临时 table:
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
我不知道当您使用更强大的数据集(每个 table 上有数十万或数百万行)时,这些查询将如何扩展。
我没有测试过任何极端情况,例如,当 callid
有 2 个 elementname
具有相同的 eCounter
varvalue
值时。
这是我的表格:
我需要为每个 callid 计算元素名的数量,其中 varname 等于 'eCounter' 且 varvalue 最大。
这些表是 elementid 一个(元素)到多个(elementdetail)的关系。我包含了实际 elementid 的格式,因此您不会假设它们是连续的数字。
我尝试了以下代码只是为了获得每个调用的最大值 varvalue/elementid,但这也没有成功。
SELECT MAX(a.varvalue) 作为 MAXvarvalue,b.callgui FROM elementdetail AS a,元素 AS b 其中 (a.elementid = b.elementid) 和 (a.varname = 'eCounter') GROUP BY varname, callguid
这就是我需要的结果。
使用最新的 Informix 版本 ( 14.10.XC1 )。
假设以下 tables:
CREATE TABLE element
(
callid INTEGER,
elementid BIGINT,
elementname CHAR( 20 )
);
CREATE TABLE elementdetail
(
elementid BIGINT,
varname CHAR( 20 ),
varvalue INTEGER
);
其中填充了您的示例数据(OCR 来拯救,您真的应该 post 您的示例数据和 table 作为文本,而不是图像):
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 或窗口,因此我们使用临时 table:
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
我不知道当您使用更强大的数据集(每个 table 上有数十万或数百万行)时,这些查询将如何扩展。
我没有测试过任何极端情况,例如,当 callid
有 2 个 elementname
具有相同的 eCounter
varvalue
值时。