Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL-来自多个表的多个聚合_Sql_Sum_Aggregate_Derby - Fatal编程技术网

SQL-来自多个表的多个聚合

SQL-来自多个表的多个聚合,sql,sum,aggregate,derby,Sql,Sum,Aggregate,Derby,如果已经有人问过,请道歉。我失去了这些桌子 create table sales ( sale_date timestamp not null, amount double precision } create table customers ( event_date timestamp not null, cust_cnt integer } create table details ( detail_date timestamp not null,

如果已经有人问过,请道歉。我失去了这些桌子

create table sales (
    sale_date timestamp not null,
    amount double precision
}

create table customers (
    event_date timestamp not null,
    cust_cnt integer
}

create table details (
    detail_date timestamp not null,
    type smallint,
    qnty integer
}
示例数据将类似于

sale_date   amount
------------------
1/1/2015    1000
1/2/2015     750
1/3/2015     486

event_date  cust_cnt
--------------------
1/1/2015    10
1/2/2015    15
1/3/2015    12

detail_date type qnty
----------------------
1/1/2015    0    20
1/1/2015    1    18
1/2/2015    0    12
1/2/2015    1     9
1/3/2015    0    31
1/3/2015    1    27
select sum(amount) as total_sales, 
sum(cust_cnt) as customer_srvd, 
sum(qnty where type=0) as total_type0_sold, 
sum(qnty where type=1) as total_type1_sold 
from sales, customers, details
where sale_date >= 1/1/2015 and  sale_date <= 1/31/2015
and detail_date = sale_date
and event_date = sale_date
我想要这个输出

total_sales customer_srvd   total_type0_sold    total_type1_sold
-----------------------------------------------------------------
2236           37                    63                  54
我猜,查询将类似于

sale_date   amount
------------------
1/1/2015    1000
1/2/2015     750
1/3/2015     486

event_date  cust_cnt
--------------------
1/1/2015    10
1/2/2015    15
1/3/2015    12

detail_date type qnty
----------------------
1/1/2015    0    20
1/1/2015    1    18
1/2/2015    0    12
1/2/2015    1     9
1/3/2015    0    31
1/3/2015    1    27
select sum(amount) as total_sales, 
sum(cust_cnt) as customer_srvd, 
sum(qnty where type=0) as total_type0_sold, 
sum(qnty where type=1) as total_type1_sold 
from sales, customers, details
where sale_date >= 1/1/2015 and  sale_date <= 1/31/2015
and detail_date = sale_date
and event_date = sale_date

如果有人能给我举个工作的例子,我将不胜感激。在这一点上,我尝试在德比。谢谢。

我不知道德比db。 但在Oracle的情况下,下面的代码是有效的

select
(
    select sum(amount) from sales where sale_date >= to_date('01/01/2015', 'MM/DD/YYYY') and sale_date <= to_date('01/03/2015', 'MM/DD/YYYY')
) as total_sales,
(
    select sum(cust_cnt) from customers where event_date >= to_date('01/01/2015', 'MM/DD/YYYY') and event_date <= to_date('01/03/2015', 'MM/DD/YYYY')
) as customer_srvd,
(
    select sum(qnty) from details where type=0 and detail_date >= to_date('01/01/2015', 'MM/DD/YYYY') and detail_date <= to_date('01/03/2015', 'MM/DD/YYYY')
) as total_type0_sold,
(
    select sum(qnty) from details where type=1 and detail_date >= to_date('01/01/2015', 'MM/DD/YYYY') and detail_date <= to_date('01/03/2015', 'MM/DD/YYYY')
) as total_type1_sold
from dual;
德比似乎没有双表。
如果在derby db上运行,请将从dual替换为从SYSIBM.SYSDUMMY1。

我不知道derby db。 但在Oracle的情况下,下面的代码是有效的

select
(
    select sum(amount) from sales where sale_date >= to_date('01/01/2015', 'MM/DD/YYYY') and sale_date <= to_date('01/03/2015', 'MM/DD/YYYY')
) as total_sales,
(
    select sum(cust_cnt) from customers where event_date >= to_date('01/01/2015', 'MM/DD/YYYY') and event_date <= to_date('01/03/2015', 'MM/DD/YYYY')
) as customer_srvd,
(
    select sum(qnty) from details where type=0 and detail_date >= to_date('01/01/2015', 'MM/DD/YYYY') and detail_date <= to_date('01/03/2015', 'MM/DD/YYYY')
) as total_type0_sold,
(
    select sum(qnty) from details where type=1 and detail_date >= to_date('01/01/2015', 'MM/DD/YYYY') and detail_date <= to_date('01/03/2015', 'MM/DD/YYYY')
) as total_type1_sold
from dual;
德比似乎没有双表。
如果在derby db上运行,请将从dual替换为从SYSIBM.SYSDUMMY1。

谢谢您的建议。下面似乎也提供了我想要的,尽管我不确定这是否是性能方面的,是否比建议的更好或更差

    select sum(s.amount) as total_sales, sum(c.cust_cnt) as customer_srvd, 
        sum(type0.qnty) as total_type0_sold, sum(type1.qnty) as total_type1_sold
    from sales s
    inner join customers c 
        on date(s.sale_date) = date(c.event_date)
    inner join details type0 
        on date(s.sale_date) = date(type0.detail_date) and type0.type = 0
    inner join details type1 
        on date(s.sale_date) = date(type1.detail_date) and type1.type = 1
    where s.sale_date >= timestamp('2015-01-01 00:00:00') and s.sale_date <= timestamp('2015-01-03 00:00:00') 

谢谢你的建议。下面似乎也提供了我想要的,尽管我不确定这是否是性能方面的,是否比建议的更好或更差

    select sum(s.amount) as total_sales, sum(c.cust_cnt) as customer_srvd, 
        sum(type0.qnty) as total_type0_sold, sum(type1.qnty) as total_type1_sold
    from sales s
    inner join customers c 
        on date(s.sale_date) = date(c.event_date)
    inner join details type0 
        on date(s.sale_date) = date(type0.detail_date) and type0.type = 0
    inner join details type1 
        on date(s.sale_date) = date(type1.detail_date) and type1.type = 1
    where s.sale_date >= timestamp('2015-01-01 00:00:00') and s.sale_date <= timestamp('2015-01-03 00:00:00') 

要了解如何使用Derby分析不同的SQL语句,以及哪些语句性能更好、为什么更好,请从这里开始:要了解如何使用Derby分析不同的SQL语句,以及哪些语句性能更好、为什么更好,请从这里开始: