满足条件和所有结果的情况-SQL Oracle

时间:2018-08-29 08:42:50

标签: sql oracle

我有一张下表(客户):

theory Prop imports Main
begin

lemma 
  fixes func1 :: "'a => 'a"
    and func2 :: "'a => 'a"
    and func3 :: "'a => 'a"
  assumes "∀ x y. func1 x = func1 y --> func3 x = func3 y"
      and "∀ x. func1 x = func2 x"
  shows "∀ x y. func2 x = func2 y --> func3 x = func3 y"
proof -
  from assms show ?thesis by auto
qed

end

我需要结果以显示不活动客户与所有客户(即

)相比的统计信息
$fileLoader = new Translation\FileLoader(new Filesystem\Filesystem(), '');
$factory = new Validation\Factory( new Translation\Translator($fileLoader, 'en_US') );

$d = ['name' => 'te' ];
$rules = [
    'name' => [Rule::unique('page')],
];

$validator = $factory->make($d, $rules);

if($validator->fails()){
    $errors = $validator->errors();
    foreach($errors->all() as $message){
        d($message);
    }
}

由于CASE检查了满足条件的条件,所以我不知道如何先获得部分结果,然后再获得所有结果。

我使用了这两个选择:

client | status   | email | phone
---------------------------------
001    | active   | yes   | yes
002    | inactive | yes   | yes
003    | inactive | yes   | no
004    | deceased | no    | no
005    | active   | yes   | no
006    | deceased | no    | no
007    | active   | yes   | yes
008    | inactive | no    | yes
009    | active   | no    | no
010    | inactive | yes   | yes

很显然,第一部分没有按预期工作,但我将其放在此处以更好地展示我的问题。

是否可以在一个查询中完成?

3 个答案:

答案 0 :(得分:3)

第一个查询不需要大小写表达式,您可以根据需要过滤单个状态;然后结合第二个查询:

-- CTE for sample data
with clients (client, status, email, phone) as (
  select 001, 'active', 'yes', 'yes' from dual union all
  select 002, 'inactive', 'yes', 'yes' from dual union all
  select 003, 'inactive', 'yes', 'no' from dual union all
  select 004, 'deceased', 'no', 'no' from dual union all
  select 005, 'active', 'yes', 'no' from dual union all
  select 006, 'deceased', 'no', 'no' from dual union all
  select 007, 'active', 'yes', 'yes' from dual union all
  select 008, 'inactive', 'no', 'yes' from dual union all
  select 009, 'active', 'no', 'no' from dual union all
  select 010, 'inactive', 'yes', 'yes' from dual
)
-- actual query
SELECT STATUS, EMAIL, PHONE, COUNT(*) TOTAL
FROM CLIENTS
WHERE STATUS = 'inactive'
GROUP BY STATUS, EMAIL, PHONE
UNION ALL
SELECT 'all' AS STATUS, EMAIL, PHONE, COUNT(*) TOTAL
FROM CLIENTS
GROUP BY EMAIL, PHONE
ORDER BY 1 DESC, 2 DESC, 3 DESC;

STATUS   EMAIL PHONE      TOTAL
-------- ----- ----- ----------
inactive yes   yes            2
inactive yes   no             1
inactive no    yes            1
all      yes   yes            4
all      yes   no             2
all      no    yes            1
all      no    no             3

只是为了好玩,您可以使用条件聚合,而只需一次击中表格即可做到这一点:

select email, phone,
  count(case when status = 'inactive' then client end) as inactive,
  count(*) as total
from clients
group by email, phone
order by 1 desc, 2 desc;

EMAIL PHONE   INACTIVE      TOTAL
----- ----- ---------- ----------
yes   yes            2          4
yes   no             1          2
no    yes            1          1
no    no             0          3

然后取消透视:

select status, email, phone, total
from (
  select email, phone,
    count(case when status = 'inactive' then client end) as inactive,
    count(*) as total
  from clients
  group by email, phone
)
unpivot (total for status in (inactive as 'inactive', total as 'all'))
where total > 0
order by 1 desc, 2 desc, 3 desc;

STATUS   EMAIL PHONE      TOTAL
-------- ----- ----- ----------
inactive yes   yes            2
inactive yes   no             1
inactive no    yes            1
all      yes   yes            4
all      yes   no             2
all      no    yes            1
all      no    no             3

但是除非您在实际情况中有大量数据,否则可能无需使其变得如此复杂。

答案 1 :(得分:2)

尝试下面的联盟

SELECT STATUS, EMAIL, PHONE,COUNT(CLIENT) TOTAL
from CLIENTS where status='inactive'
group by STATUS,EMAIL, PHONE
union
SELECT 'All' as STATUS, EMAIL, PHONE,COUNT(CLIENT) TOTAL
from CLIENTS
group by EMAIL, PHONE

答案 2 :(得分:2)

您可以使用以下查询:

with stat ( status) as (
  select'inactive' from dual 
  union all
  select  'all' from dual
),
 clients (client, status, email, phone) as (
  select 001, 'active', 'yes', 'yes' from dual union all
  select 002, 'inactive', 'yes', 'yes' from dual union all
  select 003, 'inactive', 'yes', 'no' from dual union all
  select 004, 'deceased', 'no', 'no' from dual union all
  select 005, 'active', 'yes', 'no' from dual union all
  select 006, 'deceased', 'no', 'no' from dual union all
  select 007, 'active', 'yes', 'yes' from dual union all
  select 008, 'inactive', 'no', 'yes' from dual union all
  select 009, 'active', 'no', 'no' from dual union all
  select 010, 'inactive', 'yes', 'yes' from dual
)
SELECT  stat.status,  EMAIL, PHONE, COUNT(*) TOTAL
FROM CLIENTS, stat
where stat.status = clients.status or stat.status = 'all'
GROUP BY stat.status, EMAIL, PHONE
ORDER BY 1 DESC, 2 DESC, 3 DESC