SQLite返回Android中具有相同行数的组

SQLite返回Android中具有相同行数的组,android,sqlite,android-sqlite,Android,Sqlite,Android Sqlite,我有以下SQLite查询: select product._id supermarket._id datetime(price.timestamp,'localtime') as LOCAL_TIMESTAMP, price.price from price inner join product on price.productid

我有以下SQLite查询:

        select  
                product._id
                supermarket._id
                datetime(price.timestamp,'localtime') as LOCAL_TIMESTAMP,
                price.price
        from price inner join product on price.productid = product._id
                   inner join supermarket on price.supermarketid = supermarket._id

        order by price.productid, price.supermarketid, price.timestamp
行按productid、supermarketid和时间戳排序。这样做时,它们被分组,但并非所有的pairs products supermarket都有相同的时间戳,那么是否有可能获得所有pairs products supermarket具有相同数量的时间戳?如果配对产品超市没有时间戳,则在返回结果中将其价格设置为0

例如,上述查询可能返回:

Product_A Supermarket_A "2014-03-10 00:00:00"   2.3
Product_A SUpermarket_A "2014-04-10 00:00:00"  15.0
Product_A SUpermarket_A "2014-04-20 00:00:00"  10.5

Product_B Supermarket_A "2014-01-01 00:00:00"  23.3
Product_B SUpermarket_A "2014-05-21 00:00:00"   1.0
我想获得:

Product_A Supermarket_A "2014-01-01 00:00:00"   0.0
Product_A Supermarket_A "2014-03-10 00:00:00"   2.3
Product_A SUpermarket_A "2014-04-10 00:00:00"  15.0
Product_A SUpermarket_A "2014-04-20 00:00:00"  10.5
Product_A SUpermarket_A "2014-05-21 00:00:00"   0.0

Product_B Supermarket_A "2014-01-01 00:00:00"  23.3
Product_B Supermarket_A "2014-03-10 00:00:00"   0.0
Product_B Supermarket_A "2014-04-10 00:00:00"   0.0
Product_B Supermarket_A "2014-04-20 00:00:00"   0.0
Product_B SUpermarket_A "2014-05-21 00:00:00"   1.0
在每个产品中,所有时间戳都会出现一对(就像一个联合体)。如果一对产品没有时间戳,则会创建它并将其价格设置为0.0

是否可以在SQL中执行此操作?

要获取所有可能的时间戳组合,请使用时间戳进行连接,但不带连接条件。(此处需要区分以避免重复。)

然后对价格进行分析:

SELECT productid,
       supermarketid,
       datetime(timestamp, 'localtime') AS local_timestamp,
       price.price
FROM (SELECT DISTINCT product._id     AS productid,
                      supermarket._id AS supermarketid
      FROM       product 
      INNER JOIN price       ON product._id         = price.productid
      INNER JOIN supermarket ON price.supermarketid = supermarket._id)
CROSS JOIN (SELECT DISTINCT timestamp
            FROM price)
LEFT JOIN price USING (productid, supermarketid, timestamp)
对于缺少的价格,返回NULL。
如果您确实想要零,请使用
IFNULL(price.price,0.0)

我没有得到列price的值。我也有重复的行。我已经通过在外部选择中使用DISTINCT解决了重复的行,我的意思是,第一次选择。最后它工作了。我在第一个选择中添加了DISTINCT(外部选择),并且在最后我将“和LOCAL_TIMESTAMP=price.TIMESTAMP”替换为“和LOCAL_TIMESTAMP=datetime(price.TIMESTAMP,'localtime')”修复。我已经将产品/超市移动到一个子查询中,以便在尽可能少的数据上计算DISTINCT。在同名列上连接允许使用。太好了!它就像一个符咒!还有一件事,关于性能和执行时间,我在每个表中的几个寄存器上执行了此操作,每个表中的寄存器不超过10个,每次执行随机时间从3毫秒到20毫秒,因此我担心会有很多寄存器。那么在很多寄存器上执行会花费很多时间吗?也许如果我在价格表上创建一个指数,它会加速。。。无论如何,非常感谢。正如我所期望的那样,时间戳列上应该有一个索引。