我需要你的帮助与Oracle.我需要做一个控制报告,通过使用一个表称为 restaurant_info
这个表是这样的。
|------------------|---------------|
| COMP_NAME | CODE |
|------------------|---------------|
| Pizza Co | PIC |
|------------------|---------------|
| So Asian | SOA |
|------------------|---------------|
我有另一个表,叫做 restaurant_inc
返回一天结束时餐馆的收入。
|------------------|---------------|-----------------|
| CODE | DATE_RES | INCOME |
|------------------|---------------|-----------------|
| PIC | 14/04/2020 | 15908 |
|------------------|---------------|-----------------|
| PIC | 15/04/2020 | 10890 |
|------------------|---------------|-----------------|
| PIC | 16/04/2020 | 10000 |
|------------------|---------------|-----------------|
| PIC | 17/04/2020 | 12890 |
|------------------|---------------|-----------------|
| PIC | 18/04/2020 | 13890 |
|------------------|---------------|-----------------|
| PIC | 20/04/2020 | 10880 |
|------------------|---------------|-----------------|
| PIC | 21/04/2020 | 9890 |
|------------------|---------------|-----------------|
| PIC | 22/04/2020 | 9500 |
|------------------|---------------|-----------------|
正如你所看到的,该表显示的是由代码标识的餐馆的收入。我想做的是做一个查询,读作 COMP_NAME
,CODE
并通过CASE报表来判断收入是否缺失。结果是这样的。
|------------------|---------------|---------------|---------------|
| COMP_NAME | CODE | DATE_RES | CHECK |
|------------------|---------------|---------------|---------------|
| Pizza Co | PIC | 14/04/2020 | |
|------------------|---------------|---------------|---------------|
| Pizza Co | PIC | 15/04/2020 | |
|------------------|---------------|---------------|---------------|
| Pizza Co | PIC | 16/04/2020 | |
|------------------|---------------|---------------|---------------|
| Pizza Co | PIC | 17/04/2020 | |
|------------------|---------------|---------------|---------------|
| Pizza Co | PIC | 18/04/2020 | |
|------------------|---------------|---------------|---------------|
| Pizza Co | PIC | 19/04/2020 | Missing |
|------------------|---------------|---------------|---------------|
| Pizza Co | PIC | 20/04/2020 | |
|------------------|---------------|---------------|---------------|
| Pizza Co | PIC | 21/04/2020 | |
|------------------|---------------|---------------|---------------|
| Pizza Co | PIC | 22/04/2020 | |
|------------------|---------------|---------------|---------------|
| So Asian | SOA | 14/04/2020 | Missing |
|------------------|---------------|---------------|---------------|
| So Asian | SOA | 15/04/2020 | Missing |
|------------------|---------------|---------------|---------------|
| So Asian | SOA | 16/04/2020 | Missing |
|------------------|---------------|---------------|---------------|
| So Asian | SOA | 17/04/2020 | Missing |
|------------------|---------------|---------------|---------------|
| So Asian | SOA | 18/04/2020 | Missing |
|------------------|---------------|---------------|---------------|
| So Asian | SOA | 20/04/2020 | Missing |
|------------------|---------------|---------------|---------------|
| So Asian | SOA | 21/04/2020 | Missing |
|------------------|---------------|---------------|---------------|
| So Asian | SOA | 22/04/2020 | Missing |
|------------------|---------------|---------------|---------------|
正如你所看到的,在我的例子中,值为 CHECK
对于 So Asia
是空的,因为在表中没有记录 restaurant_inc
我不知道如何在我的select查询中添加日期,以及如何处理表中的缺失值。restaurant_inc
. 在我的控制请求中,我需要告诉列的日期范围。DATE_RES
.你能帮我找到一种方法来进行这个控制请求吗?
EDIT : 我试过这个查询......我的日期范围是在4月14日到4月22日之间。
SELECT inf.comp_name,inf.code,inc.date_res,case when
(select income from restaurant_inc r where r.date_res=inc.date_res)
is null then 'Missing' end as CHECK FROM restaurant_info inf,
restaurant_inc inc where inf.code=inc.code and
inc.date_res>=to_date('14/04/2020','DD/MM/YYYY')
and inc.date_res<=to_date('22/04/2020','DD/MM/YYYY')
我的日期范围是在4月14日到4月22日之间。So Asia
和 Pizza Co
. 我想这是因为日期不在表格中 restaurant_inc
.提前感谢您的帮助。
在这种情况下,我正在创建一个内联视图(别名为
-- set up sample data
with restaurant_info as (select 'Pizza Co' as comp_name, 'PIC' as code from dual union select 'So Asian', 'SO' from dual),
restaurant_inc as (select 'PIC' as code,to_date('14/04/2020','dd/mm/yyyy') as date_res,15908 as income from dual union
select 'PIC',to_date('15/04/2020','dd/mm/yyyy'),10890 from dual union
select 'PIC',to_date('16/04/2020','dd/mm/yyyy'),10000 from dual union
select 'PIC',to_date('17/04/2020','dd/mm/yyyy'),12890 from dual union
select 'PIC',to_date('18/04/2020','dd/mm/yyyy'),13890 from dual union
select 'PIC',to_date('20/04/2020','dd/mm/yyyy'),10880 from dual union
select 'PIC',to_date('21/04/2020','dd/mm/yyyy'),9890 from dual union
select 'PIC',to_date('22/04/2020','dd/mm/yyyy'), 9500 from dual)
-- actual query
select r.comp_name, r.code, d.date_res, case when i.date_res is null then 'Missing' end as "Check"
from restaurant_info r
cross join (select date '2020-04-14' + level as date_res from dual
connect by level <= 8) d
left join restaurant_inc i on r.code = i.code and i.date_res = d.date_res
order by 1, 3;
),持有04-14至04-22这几天。如果有什么不明白的地方,请告诉我。d