如何从Sqlite数据库中获取数据

时间:2017-06-26 06:46:16

标签: android sql sqlite android-sqlite

我是一个新的Android开发人员,我想从三个表中获取sqlite数据库中的数据,例如ItemMaster,ItemGroupMaster,SalesDetail

ItemMaster enter image description here ItemGroupMaster

enter image description here

SalesDetails enter image description here

使用此查询后:选择sd.salesDate,igm.groupName,im.itemName,im.itemprice from salesdetails sd inner join itemmaster im on sd.itemId = im.itemId inner join itemgroupmaster igm on im.groupId = igm.groupId

结果

enter image description here

现在我想得到的实际结果如下图enter image description here

请帮帮我 提前致谢

1 个答案:

答案 0 :(得分:0)

试试这个。 QtyNetAmount列都是聚合,NetAmount依赖于Qty,因此我使用了几个嵌套查询:

SELECT Y.salesDate
    , Y.GroupName
    , Y.ItemName
    , Y.Qty
    , Y.Price
    , Y.Price * Y.Qty AS NetAmount

FROM

    (SELECT X.salesDate
        , X.groupName AS GroupName
        , X.itemName AS ItemName
        , X.itemprice AS Price
        , COUNT(*) AS Qty 

    FROM

        (SELECT sd.salesDate
            ,igm.groupName
            ,im.itemName
            ,im.itemprice
        FROM salesdetails sd
        INNER JOIN itemmaster im 
            ON sd.itemId = im.itemId
        INNER JOIN itemgroupmaster igm 
            ON im.groupId = igm.groupId
        ) X

    GROUP BY 1,2,3
    ) Y

GROUP BY 1,2,3,4,5
ORDER BY 1,2,3,4,5