如何基于层级查询?
How to query based on a hierarchy level?
我有 table,其中包含 3 个主要属性:STAGING_ID、DEVELOPMENT_ID 和 PRODUCTION_ID,每个 NUMBER_ID。我想查询一个 table 基于这个 table 说:
IF there are NO null values in any of the three columns, select STAGING_ID in 'MAIN_ID' column and 'LEVEL_DESCRIPTION' to be STAGING
IF there is a NULL value in only STAGING_ID (not DEVELOPMENT_ID or PRODUCTION_ID), then select the next level, which would be DEVELOPMENT_ID in 'MAIN_ID' column, and 'LEVEL_DESCRIPTION' to be DEVELOPMENT
IF there is a NULL value in STAGING & DEVELOPMENT (not PRODUCT_ID), then select the next level, which would be PRODUCT_ID in 'MAIN_ID' column, and 'LEVEL_DESCRIPTION' to be PRODUCTION
因此级别始终遵循此层次结构,暂存 > 开发 > 生产。
So in the picture,
FIRST ROW, should be: LEVEL_DESCRIPTION: STAGING, MAIN_ID: 3532
SECOND ROW, should be: LEVEL_DESCRIPTION: DEVELOPMENT, MAIN_ID: 3532
THIRD ROW, should be: LEVEL_DESCRIPTION: PRODUCTION, MAIN_ID: 1081
所以当我基于这个 table 查询时,我需要 3 列:
NUMBER_ID, MAIN_ID, LEVEL_DESCRIPTION
我可以做到吗?我正在考虑案例陈述,但我在制定它时遇到了问题。
DROP TABLE TBL_1;
CREATE TABLE TBL_1
(
number_id int,
production_id int,
develpoment_id int,
staging_id int
);
INSERT INTO TBL_1
values
(11111,2331, 1233, 3532),
(11122,9080, 1291, null),
(11144,1082, null, null)
使用 COALESCE 和 CASE:
SELECT *,
COALESCE(staging_id, development_id, production_id, 'Not found') AS main_id,
CASE WHEN staging_id IS NOT NULL THEN 'Staging'
WHEN development_id IS NOT NULL THEN 'Development'
WHEN production_id IS NOT NULL THEN 'Production'
ELSE 'Not found'
END AS Level_description
FROM TBL_1;
我有 table,其中包含 3 个主要属性:STAGING_ID、DEVELOPMENT_ID 和 PRODUCTION_ID,每个 NUMBER_ID。我想查询一个 table 基于这个 table 说:
IF there are NO null values in any of the three columns, select STAGING_ID in 'MAIN_ID' column and 'LEVEL_DESCRIPTION' to be STAGING
IF there is a NULL value in only STAGING_ID (not DEVELOPMENT_ID or PRODUCTION_ID), then select the next level, which would be DEVELOPMENT_ID in 'MAIN_ID' column, and 'LEVEL_DESCRIPTION' to be DEVELOPMENT
IF there is a NULL value in STAGING & DEVELOPMENT (not PRODUCT_ID), then select the next level, which would be PRODUCT_ID in 'MAIN_ID' column, and 'LEVEL_DESCRIPTION' to be PRODUCTION
因此级别始终遵循此层次结构,暂存 > 开发 > 生产。
So in the picture,
FIRST ROW, should be: LEVEL_DESCRIPTION: STAGING, MAIN_ID: 3532
SECOND ROW, should be: LEVEL_DESCRIPTION: DEVELOPMENT, MAIN_ID: 3532
THIRD ROW, should be: LEVEL_DESCRIPTION: PRODUCTION, MAIN_ID: 1081
所以当我基于这个 table 查询时,我需要 3 列:
NUMBER_ID, MAIN_ID, LEVEL_DESCRIPTION
我可以做到吗?我正在考虑案例陈述,但我在制定它时遇到了问题。
DROP TABLE TBL_1;
CREATE TABLE TBL_1
(
number_id int,
production_id int,
develpoment_id int,
staging_id int
);
INSERT INTO TBL_1
values
(11111,2331, 1233, 3532),
(11122,9080, 1291, null),
(11144,1082, null, null)
使用 COALESCE 和 CASE:
SELECT *,
COALESCE(staging_id, development_id, production_id, 'Not found') AS main_id,
CASE WHEN staging_id IS NOT NULL THEN 'Staging'
WHEN development_id IS NOT NULL THEN 'Development'
WHEN production_id IS NOT NULL THEN 'Production'
ELSE 'Not found'
END AS Level_description
FROM TBL_1;