基于SQLITE中的一个数字列检索已排序的数据

时间:2017-08-17 05:07:15

标签: sqlite android-sqlite system.data.sqlite sqlite-net

我在SQLITE数据库中有一个像附加图像的表。

Sqlite table

我正在尝试根据SUM(运费)列检索已排序的数据。为此,我使用了以下查询。

选择ShipCountry 来自CountryDetails GROUP BY ShipCountry 按订单顺序(运费)ASC
当我运行这个时,我得到的结果如下。

Sqlite result

如果我运行以下查询,我会得到如下结果。没关系。

选择ShipCountry,ShipCity 来自CountryDetails GROUP BY ShipCountry,ShipCity 按订单(运费),ShipCity ASC

Result

而不是这个,我需要一个像下面这样的结果。按顺序,SUM(Freight)应仅考虑ShipCountry。它不应该同时考虑ShipCountry和ShipCity。我的预期结果是

Tableau result

如何通过SQLITE查询实现此结果?

在SQL中我们可以实现如下查询。

选择ShipCountry,ShipCountry从Countrydetails组发货ShipCity,按SUM(SUM(货运))发货的ShipCity订单(ShipCountry分区),Shipcity Asc。

我们在Sqlite中需要这样的等效查询。

1 个答案:

答案 0 :(得分:2)

使用子查询查找每个国家/地区的信息总额,然后仅使用此国家/地区级别的订单进行订购:

$("#e1").select2();
$("#checkbox").click(function(){
    if($("#checkbox").is(':checked') ){
        $("#e1 > option").prop("selected","selected");
        $("#e1").trigger("change");
    }else{
        $("#e1 > option").removeAttr("selected");
         $("#e1").trigger("change");
     }
});

$("#button").click(function(){
       alert($("#e1").val());
});

我们可以为每个城市的SELECT cd1.ShipCountry, cd1.ShipCity FROM Countrydetails cd1 INNER JOIN ( SELECT ShipCountry, SUM(freight) AS freight_sum FROM Countrydetails GROUP BY ShipCountry ) cd2 ON cd1.ShipCountry = cd2.ShipCountry ORDER BY cd1.freight_sum, cd1.ShipCountry, -- needed in case two countries happen to have the same sum cd1.ShipCity 添加另一个排序级别,以便在特定国家/地区内进行排序。但是你的预期输出并不意味着你想要这个。