Sql 如何组合多个查询?
我需要结合两个结果集,我觉得我很接近,但我不知道如何总结这一切: 这里有一个查询,结果集很小,请给我不活动项Sql 如何组合多个查询?,sql,oracle,Sql,Oracle,我需要结合两个结果集,我觉得我很接近,但我不知道如何总结这一切: 这里有一个查询,结果集很小,请给我不活动项 SELECT MAX(set_date) AS most_recent_inactive, key_value, statusid FROM status_history WHERE base_table = 'userinfo' AND statusid = 10 AND set_date > TO_DATE('2012-10-01', 'YYYY-MM-DD') GROUP
SELECT MAX(set_date) AS most_recent_inactive, key_value, statusid
FROM status_history
WHERE base_table = 'userinfo'
AND statusid = 10 AND set_date > TO_DATE('2012-10-01', 'YYYY-MM-DD')
GROUP BY key_value, statusid
输出:
另一个带有小结果集的查询为我提供了活动
SELECT MAX (set_date) AS most_recent_active, key_value, statusid
FROM status_history
WHERE base_table = 'userinfo'
AND statusid =11
GROUP BY key_value, statusid
输出:
我想把所有的活动和不活动放在一起,所以我把它们都合并起来
SELECT NULL AS most_recent_active, MAX(set_date) AS most_recent_inactive, key_value, statusid
FROM status_history
WHERE base_table = 'userinfo'
AND statusid = 10 AND set_date > TO_DATE('2012-10-01', 'YYYY-MM-DD')
GROUP BY key_value, statusid
UNION all
SELECT MAX(set_date) AS most_recent_active, NULL AS most_recent_inactive, key_value, statusid
FROM status_history
WHERE base_table = 'userinfo'
AND statusid = 11
GROUP BY key_value, statusid
ORDER by key_value
输出:
问题是键值15重复
这些值是正确的,但我希望该记录和所有后续的重复项展平,第15行和所有其他匹配项作为一个记录通过,并设置两个日期字段
再说一次,我觉得我离得很近,但我该如何结束这一切呢
谢谢?您可以使用案例陈述来划分哪些是非活动的,哪些是活动的。默认情况下,如果案例未满足,则为NULL,这样您将得到所需的结果
select max(case when statusid = 10 then set_date end) as most_recent_inactive
, max(case when statusid = 11 then set_date end) as most_recent_active
, key_value
, max(statusid) as statusid
from status_history
where base_table = 'userinfo'
and statusid in (10, 11)
group by key_value, statusid
在非活动日期中进行的日期操作有点奇怪,但如果要限制,只需将其放在非活动日期的情况下:
select max(case when statusid = 10
and set_date > to_date('2012-10-01', 'YYYY-MM-DD')
then set_date
end) as most_recent_inactive
, max(case when statusid = 11 then set_date end) as most_recent_active
, key_value
, max(statusid) as statusid
from status_history
where base_table = 'userinfo'
and statusid in (10, 11)
group by key_value, statusid
我假设,如果某个东西同时处于活动和非活动状态,您希望显示它处于活动状态。如果要将其显示为非活动,请使用minstatusid;如果要同时显示这两个列,则需要另一列。。。遵循同样的逻辑;使用案例陈述。如果两者都不需要,则将其从SELECT和GROUP BY子句中完全删除。您可以使用CASE语句来拆分哪些是非活动的,哪些是活动的。默认情况下,如果案例未满足,则为NULL,这样您将得到所需的结果
select max(case when statusid = 10 then set_date end) as most_recent_inactive
, max(case when statusid = 11 then set_date end) as most_recent_active
, key_value
, max(statusid) as statusid
from status_history
where base_table = 'userinfo'
and statusid in (10, 11)
group by key_value, statusid
在非活动日期中进行的日期操作有点奇怪,但如果要限制,只需将其放在非活动日期的情况下:
select max(case when statusid = 10
and set_date > to_date('2012-10-01', 'YYYY-MM-DD')
then set_date
end) as most_recent_inactive
, max(case when statusid = 11 then set_date end) as most_recent_active
, key_value
, max(statusid) as statusid
from status_history
where base_table = 'userinfo'
and statusid in (10, 11)
group by key_value, statusid
我假设,如果某个东西同时处于活动和非活动状态,您希望显示它处于活动状态。如果要将其显示为非活动,请使用minstatusid;如果要同时显示这两个列,则需要另一列。。。遵循同样的逻辑;使用案例陈述。如果两者都不需要,则将其从SELECT和GROUP BY子句中完全删除。在并集后执行GROUP BY
SELECT MAX(active) As most_recent_active, MAX(inactive) As most_recent_in_active,
key_value, statusid
FROM
(SELECT null as active, set_date as inactive, key_value, statusid
FROM status_history
WHERE base_table = 'userinfo'
AND statusid = 10 and set_date > to_date('2012-10-01', 'YYYY-MM-DD')
UNION ALL
SELECT set_date as active, null as inactive, key_value, statusid
FROM status_history
WHERE base_table = 'userinfo'
AND statusid = 11)
GROUP BY key_value, statusid
ORDER BY by key_value
工会结束后请分组
SELECT MAX(active) As most_recent_active, MAX(inactive) As most_recent_in_active,
key_value, statusid
FROM
(SELECT null as active, set_date as inactive, key_value, statusid
FROM status_history
WHERE base_table = 'userinfo'
AND statusid = 10 and set_date > to_date('2012-10-01', 'YYYY-MM-DD')
UNION ALL
SELECT set_date as active, null as inactive, key_value, statusid
FROM status_history
WHERE base_table = 'userinfo'
AND statusid = 11)
GROUP BY key_value, statusid
ORDER BY by key_value
这是假设您的非活动状态\u id始终小于活动状态\u id。如果存在其他可能的状态\u id值,这可能不起作用
select max(most_recent_active), max(most_recent_inactive), key_value, min(status_id)
from (select null as most_recent_active, max(set_date) as most_recent_inactive, key_value,statusid
from status_history
where base_table = 'userinfo'
and statusid = 10 and set_date > to_date('2012-10-01', 'YYYY-MM-DD')
group by key_value,statusid
UNION all
select max(set_date) as most_recent_active, null as most_recent_inactive, key_value,statusid
from status_history
where base_table = 'userinfo'
and statusid = 11
group by key_value,statusid
order by key_value)
group by key_value
这是假设您的非活动状态\u id始终小于活动状态\u id。如果存在其他可能的状态\u id值,这可能不起作用
select max(most_recent_active), max(most_recent_inactive), key_value, min(status_id)
from (select null as most_recent_active, max(set_date) as most_recent_inactive, key_value,statusid
from status_history
where base_table = 'userinfo'
and statusid = 10 and set_date > to_date('2012-10-01', 'YYYY-MM-DD')
group by key_value,statusid
UNION all
select max(set_date) as most_recent_active, null as most_recent_inactive, key_value,statusid
from status_history
where base_table = 'userinfo'
and statusid = 11
group by key_value,statusid
order by key_value)
group by key_value
展平第15行时,您需要哪个状态id?我需要10的状态id。但是,如果“最近未激活”有一个值,statusid将始终为10,因为根据定义,我有一个未激活的日期,因为它们是未激活的,statusid为10。当展平第15行时,您想要哪个状态id?我需要statusid为10。但是,如果“最近未激活”有一个值,statusid将始终为10,因为根据定义,我有一个未激活的日期,因为它们是未激活的,statusid为10。第一个查询不好,因为我得到了多个键值的结果。第二个查询,缺少表达式。如果某个对象处于非活动状态且处于活动状态,您希望它处于非活动状态还是处于活动状态@发呆并感到困惑?第二个很简单;我有两个AND,你可以去掉一个。。。但结果是一样的,statusid不重要。不活跃的日期告诉我我不活跃。我只需要日期。我改变了答案;如果statusid不重要,那么只需删除它。这就是造成你问题的原因。这给了我想要的正确结果。另一个答案也一样,但这是干净的,因为它没有我丑陋的联盟。现在我希望我能正确地将这个结果集与这里没有提到的更大的外部集连接起来。谢谢第一个查询不太好,因为我得到了多个key\u值的结果。第二个查询,缺少表达式。如果某个对象处于非活动状态且处于活动状态,您希望它处于非活动状态还是处于活动状态@发呆并感到困惑?第二个很简单;我有两个AND,你可以去掉一个。。。但结果是一样的,statusid不重要。不活跃的日期告诉我我不活跃。我只需要日期。我改变了答案;如果statusid不重要,那么只需删除它。这就是造成你问题的原因。这给了我想要的正确结果。另一个答案也一样,但这是干净的,因为它没有我丑陋的联盟。现在我希望我能正确地将这个结果集与这里没有提到的更大的外部集连接起来。谢谢