复杂的SQLite查询

时间:2015-03-04 10:29:36

标签: sql sqlite

您好我正在尝试编写一个SQL查询,但这对我来说很重要。

我有加油的记录,我想计算公里间隔的价格。

每条记录都有实际的公里状态,我买了多少升,价格和货币以及我是否填满了满罐。只有当我买满油箱时才能计算消耗量,因为只有我知道我使用了多少升。 所以应该像我使用柴油一样花费1.3欧元/升,消耗6.2升/ 100公里,我有23升。然后我开始使用柴油,其成本为1.38欧元/升,我有45升,然后消耗量变为6.5升/ 100公里,......

我希望你知道我的意思:)。

所以查询的结果应该是:

KmFrom    KmTo     PricePerKmEUR
156123    156874   0.1
156875    157147   0.12

这是我的记录:

CREATE TABLE "CAR_Refueling"(
[Id] INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL
,[Date] DATE NOT NULL
,[KmTotal] INTEGER NOT NULL
,[Liters] NUMERIC(9) NOT NULL
,[PricePerLiter] NUMERIC(9) NOT NULL
,[CurrencyId] INTEGER NOT NULL
,[FullTank] BOOLEAN NOT NULL
,[Consumption] numeric(9)

);
Insert  Into [CAR_Refueling] ([Id],[Date],[KmTotal],[Liters],[PricePerLiter],[CurrencyId],[FullTank],[Consumption]) Values("1","2011-06-21","156327","70","32.88","8","1","0");
Insert  Into [CAR_Refueling] ([Id],[Date],[KmTotal],[Liters],[PricePerLiter],[CurrencyId],[FullTank],[Consumption]) Values("2","2011-09-02","157036","52.28","1.359","9","1","7.37");
Insert  Into [CAR_Refueling] ([Id],[Date],[KmTotal],[Liters],[PricePerLiter],[CurrencyId],[FullTank],[Consumption]) Values("3","2011-09-15","157832","52.32","5.47","13","1","6.57");
Insert  Into [CAR_Refueling] ([Id],[Date],[KmTotal],[Liters],[PricePerLiter],[CurrencyId],[FullTank],[Consumption]) Values("4","2011-10-03","158830","61.48","2.44","14","1","6.16");
Insert  Into [CAR_Refueling] ([Id],[Date],[KmTotal],[Liters],[PricePerLiter],[CurrencyId],[FullTank],[Consumption]) Values("5","2011-10-06","159054","14.38","2.46","14","1","6.42");
Insert  Into [CAR_Refueling] ([Id],[Date],[KmTotal],[Liters],[PricePerLiter],[CurrencyId],[FullTank],[Consumption]) Values("6","2011-10-31","160070","20","1.456","9","0","0");
Insert  Into [CAR_Refueling] ([Id],[Date],[KmTotal],[Liters],[PricePerLiter],[CurrencyId],[FullTank],[Consumption]) Values("7","2012-01-02","160367","6.765","1.478","9","0","0");
Insert  Into [CAR_Refueling] ([Id],[Date],[KmTotal],[Liters],[PricePerLiter],[CurrencyId],[FullTank],[Consumption]) Values("8","2012-04-27","160414","19.59","1.48","9","0","0");
Insert  Into [CAR_Refueling] ([Id],[Date],[KmTotal],[Liters],[PricePerLiter],[CurrencyId],[FullTank],[Consumption]) Values("9","2012-05-04","160808","66.36","187","16","1","6.43");
Insert  Into [CAR_Refueling] ([Id],[Date],[KmTotal],[Liters],[PricePerLiter],[CurrencyId],[FullTank],[Consumption]) Values("10","2012-05-11","161767","56.37","9.99","15","1","5.88");
Insert  Into [CAR_Refueling] ([Id],[Date],[KmTotal],[Liters],[PricePerLiter],[CurrencyId],[FullTank],[Consumption]) Values("11","2012-05-14","162088","21.07","1.337","9","1","6.56");
Insert  Into [CAR_Refueling] ([Id],[Date],[KmTotal],[Liters],[PricePerLiter],[CurrencyId],[FullTank],[Consumption]) Values("12","2012-05-19","163043","65.12","1.439","9","1","6.82");
Insert  Into [CAR_Refueling] ([Id],[Date],[KmTotal],[Liters],[PricePerLiter],[CurrencyId],[FullTank],[Consumption]) Values("13","2012-10-17","164029","65.67","1.203","9","1","6.66");
Insert  Into [CAR_Refueling] ([Id],[Date],[KmTotal],[Liters],[PricePerLiter],[CurrencyId],[FullTank],[Consumption]) Values("14","2012-10-23","164916","51.14","1.369","9","1","5.77");
Insert  Into [CAR_Refueling] ([Id],[Date],[KmTotal],[Liters],[PricePerLiter],[CurrencyId],[FullTank],[Consumption]) Values("15","2013-05-07","165937","62.45","1.329","9","1","6.12");
Insert  Into [CAR_Refueling] ([Id],[Date],[KmTotal],[Liters],[PricePerLiter],[CurrencyId],[FullTank],[Consumption]) Values("16","2013-05-18","167098","15.05","1.329","9","0","0");
Insert  Into [CAR_Refueling] ([Id],[Date],[KmTotal],[Liters],[PricePerLiter],[CurrencyId],[FullTank],[Consumption]) Values("17","2013-05-18","167359","63.47","1.138","9","1","5.52");
Insert  Into [CAR_Refueling] ([Id],[Date],[KmTotal],[Liters],[PricePerLiter],[CurrencyId],[FullTank],[Consumption]) Values("18","2013-05-20","168335","56.83","1.309","9","1","5.82");
Insert  Into [CAR_Refueling] ([Id],[Date],[KmTotal],[Liters],[PricePerLiter],[CurrencyId],[FullTank],[Consumption]) Values("19","2013-05-21","169051","38.43","1.369","9","1","5.37");
Insert  Into [CAR_Refueling] ([Id],[Date],[KmTotal],[Liters],[PricePerLiter],[CurrencyId],[FullTank],[Consumption]) Values("20","2013-08-25","170096","65.12","1.399","9","1","6.23");
Insert  Into [CAR_Refueling] ([Id],[Date],[KmTotal],[Liters],[PricePerLiter],[CurrencyId],[FullTank],[Consumption]) Values("21","2013-08-28","170636","30","1.43","9","1","5.56");
Insert  Into [CAR_Refueling] ([Id],[Date],[KmTotal],[Liters],[PricePerLiter],[CurrencyId],[FullTank],[Consumption]) Values("22","2013-09-03","171471","30","1.349","9","0","0");
Insert  Into [CAR_Refueling] ([Id],[Date],[KmTotal],[Liters],[PricePerLiter],[CurrencyId],[FullTank],[Consumption]) Values("23","2013-10-26","171953","61.62","1.136","9","1","6.96");
Insert  Into [CAR_Refueling] ([Id],[Date],[KmTotal],[Liters],[PricePerLiter],[CurrencyId],[FullTank],[Consumption]) Values("24","2013-10-29","172196","13.02","1.278","9","1","5.36");
Insert  Into [CAR_Refueling] ([Id],[Date],[KmTotal],[Liters],[PricePerLiter],[CurrencyId],[FullTank],[Consumption]) Values("25","2013-11-18","173101","55.92","1.359","9","1","6.18");
Insert  Into [CAR_Refueling] ([Id],[Date],[KmTotal],[Liters],[PricePerLiter],[CurrencyId],[FullTank],[Consumption]) Values("26","2014-02-26","173963","50.49","1.309","9","1","5.86");
Insert  Into [CAR_Refueling] ([Id],[Date],[KmTotal],[Liters],[PricePerLiter],[CurrencyId],[FullTank],[Consumption]) Values("27","2014-06-03","174667","49.78","1.349","9","1","7.07");
Insert  Into [CAR_Refueling] ([Id],[Date],[KmTotal],[Liters],[PricePerLiter],[CurrencyId],[FullTank],[Consumption]) Values("28","2014-08-05","175501","62.78","1.289","9","1","7.53");
Insert  Into [CAR_Refueling] ([Id],[Date],[KmTotal],[Liters],[PricePerLiter],[CurrencyId],[FullTank],[Consumption]) Values("29","2014-08-09","177195","55.74","1.279","9","1","6.36");
Insert  Into [CAR_Refueling] ([Id],[Date],[KmTotal],[Liters],[PricePerLiter],[CurrencyId],[FullTank],[Consumption]) Values("30","2014-09-10","178266","66.22","34.9","8","1","6.18");
Insert  Into [CAR_Refueling] ([Id],[Date],[KmTotal],[Liters],[PricePerLiter],[CurrencyId],[FullTank],[Consumption]) Values("31","2014-08-07","176318","51.93","1.278","9","1","6.36");
Insert  Into [CAR_Refueling] ([Id],[Date],[KmTotal],[Liters],[PricePerLiter],[CurrencyId],[FullTank],[Consumption]) Values("32","2014-10-16","178888","41.04","1.339","9","1","6.6");
Insert  Into [CAR_Refueling] ([Id],[Date],[KmTotal],[Liters],[PricePerLiter],[CurrencyId],[FullTank],[Consumption]) Values("33","2014-10-18","179498","42.28","1.239","9","1","6.93");
Insert  Into [CAR_Refueling] ([Id],[Date],[KmTotal],[Liters],[PricePerLiter],[CurrencyId],[FullTank],[Consumption]) Values("34","2014-10-19","180037","34.71","1.214","9","1","6.44");
Insert  Into [CAR_Refueling] ([Id],[Date],[KmTotal],[Liters],[PricePerLiter],[CurrencyId],[FullTank],[Consumption]) Values("35","2014-12-02","180860","55.79","1.229","9","1","6.78");
Insert  Into [CAR_Refueling] ([Id],[Date],[KmTotal],[Liters],[PricePerLiter],[CurrencyId],[FullTank],[Consumption]) Values("36","2015-02-04","181579","53.26","1.089","9","1","7.41");
Insert  Into [CAR_Refueling] ([Id],[Date],[KmTotal],[Liters],[PricePerLiter],[CurrencyId],[FullTank],[Consumption]) Values("37","2015-02-17","182125","32.6","1.119","9","1","5.97");

每公里价格可能会在两种情况下发生变化:

  1. 我开始使用不同价格的柴油。

  2. 消费已经改变。

  3. 我可以在我装满油箱时计算消耗量。因此,如果我下次不做充分坦克,我必须总和升和公里。例如2011-10-31 - 2012-05-04 =>的记录((100 /(160808 - 159054))*(20 + 6.765 + 19.59 + 66.36)= 6.426。

    所以一步就是计算消费=> KmFrom | KmTo |消费。 下一步可以看看坦克,因为它是无限的,并计算LitersFrom | LitersTo | PricePerLiter。

    下一步将它们结合在一起。

    所以这是用sumed升计算的消耗量。

        select KmFrom, KmTo, Consumption, 
         (select sum(Liters) from CAR_Refueling where KmTotal >= KmFrom and KmTotal <= KmTo) as Liters
        from
        (select (select max(B.KmTotal) 
                         from CAR_Refueling B 
                         where B.KmTotal < A.KmTotal and B.FullTank = 1) + 1 as KmFrom, 
                         A.KmTotal as KmTo, A.Consumption
         from CAR_Refueling A 
         where A.Consumption != 0 
         order by KmFrom)
    

    这给了我LitersFrom | LitersTo | PricePerLiter无限制坦克。

    SELECT Liters, LitersFrom, (LitersFrom + Liters - 1) AS LitersTo, PricePerLiter 
    FROM (SELECT 
                 (SELECT ifnull( sum( B.Liters ) , 0 )
                     FROM CAR_Refueling B 
                     WHERE B.KmTotal < A.KmTotal 
                    ) AS LitersFrom, A.Liters, A.PricePerLiter 
                FROM CAR_Refueling A)
    

    我不确定在制作间隔时是否应该向升和公里添加+1。

    最后,我想在汽车中插入实际的千米状态,然后计算出我开车了多少以及花了多少钱。并且可能发生我用于旅程消费6.5的一部分和其他6.8或我将用于一部分柴油价格1.36和其他1.24甚至组合两者。

1 个答案:

答案 0 :(得分:0)

这是你需要的吗?我认为自我加入可以完成这项工作。

更新:根据您的解释,我在查询中添加了按日期排序,因此现在在内部子查询中有一个与日期顺序匹配的顺序Id生成器

   select c2.[KmTotal]+1 as KmFrom,  c1.[KmTotal] as KmTo, 
   case c1.[FullTank] when 0 then 'NA' else round((c2.[PricePerLiter]*c1.   [Liters])/(c1.[KmTotal]-c2.[KmTotal]),2) 
   end as PricePerKm
   from (select (select COUNT(0) 
            from [CAR_Refueling] t1 
            where t1.Date <= t2.Date 
            ) as Id, KmTotal, Liters, PricePerLiter, FullTank from  [CAR_Refueling] t2  ORDER BY [Date] 
) c1, 

(select (select COUNT(0) 
            from [CAR_Refueling] t1 
            where t1.Date <= t2.Date 
            ) as Id, KmTotal, Liters, PricePerLiter, FullTank from   [CAR_Refueling] t2  ORDER BY [Date] 
 ) c2
where c1.id=c2.id+1
order by c2.id asc;

请参阅SQLFiddle

由于您正在引用未包含在您的示例中的货币换算表,因此它无法正常工作。此外,我不确定您是否愿意参考您的[消耗]列值,或者计算Km和升的消耗量 - 我选择了第二个选项。