连接第二个表时,Sqllite sum()返回错误的值

时间:2019-01-24 05:36:13

标签: sql sqlite

我正在尝试从两个表(按日期分组和将两个表连接在一起)中计算两列的总和

查询1

select 
    Dates
    , ifnull( sum(Amount),0) As ETA 
from 
    Expence as t1 
where 
    t1.Dates IN (
        select distinct Dates As A from ( 
            select 
                Expence.Dates 
            from
                Expence 

            union all 

            select 
                Pass.Dates 
            from
                Pass 
                left join Expence 
                    on Pass.Dates=Expence.Dates
        )
    ) 
group by 
    dates

查询2

select 
    Dates
    , ifnull( sum(Paid),0) As PTP
    , ifnull( sum(TotalAmount),0) As PTA 
from 
    pass As t2 
where 
    t2.Dates IN (
        select distinct 
            Dates As A 
        from ( 
            select 
                Expence.Dates 
            from 
                Expence 

            union all 

            select 
                Pass.Dates 
            from 
                Pass 
                left join Expence 
                    on Pass.Dates=Expence.Dates
        )
    ) 
group by 
    dates

两个查询都是独立工作的,但是结合在一起会返回错误的答案。

select distinct 
    t.Dates
    ,PTP
    ,PTA
    ,ETA 
from (
    select 
        Dates
        , ifnull( sum(Paid),0) As PTP
        , ifnull( sum(TotalAmount),0) As PTA 
    from 
        pass As t2 
    where 
        t2.Dates IN (
            select distinct 
                Dates As A 
            from( 
                select 
                    Expence.Dates 
                from 
                    Expence 

                    union all 

                select 
                    Pass.Dates 
                from 
                    Pass 
                    left join Expence 
                        on Pass.Dates=Expence.Dates
            )
        ) 
    group by dates
) AS t
, (
    select 
        Dates
        ,ifnull( sum(Amount),0) As ETA 
    from 
        Expence as t1 
    where 
        t1.Dates IN (
            select distinct 
                Dates As A from ( 
                    select 
                        Expence.Dates 
                    from Expence 

                    union all
                        select 
                            Pass.Dates 
                        from 
                            Pass 
                            left join Expence 
                                on Pass.Dates=Expence.Dates
                )
        ) 
    group by dates
) AS P

2 个答案:

答案 0 :(得分:0)

您得到的结果不正确,因为您编写的查询没有意义。

您的from子句从您声明的两个derived tables中提取。由于尚未声明任何连接逻辑,因此sqlite假设使用natural join。这意味着,对于每一个引入的列(对于两个表),它都会检查每一行是否匹配。

由于这些查询似乎彼此之间几乎没有任何关系,因此在记录匹配方面您不会经常得到很多帮助。

我对您的数据了解不多,因此很难提供“单独运行”以外的更好建议。我的假设是,您将要在连接语句的末尾添加on t.Dates = P.Dates

答案 1 :(得分:-1)

select distinct tdate AS `DATE`,PTA AS `TOTAL AMOUNT` ,PTP AS `TOTAL PAID`,ETA AS `TOTAL EXPENCE`, PTA -PTP  AS `BALANCE` from((
select Dates As tdate,ifnull( sum(Paid),0) As PTP, ifnull( sum(TotalAmount),0) As PTA from pass As t2 where t2.Dates IN  (select distinct Dates As A from( select Expence.Dates from Expence union all select Pass.Dates from Pass left join Expence on Pass.Dates=Expence.Dates))
 group by dates)AS t left join
(

(select Dates as pdate,ifnull( sum(Amount),0) As ETA from Expence as t1 where t1.Dates IN  (select distinct Dates As A from( select Expence.Dates from Expence union all select Pass.Dates from Pass left join Expence on Pass.Dates=Expence.Dates))  group by dates 

)AS p)  ON   tdate=pdate) where tdate between '24-01-2019' AND '25-01-2019'