选择查询 - 每两行进入单个结果行

时间:2013-10-04 03:33:49

标签: android sql sqlite

让我们假设我有以下'产品'表:

ProductID | ProductName 
----------+---------
0255463   | ProductA       
0254483   | ProductB
0255341   | ProductC
0905454   | ProductD

有没有办法(在Android的SQLite中)选择每两个连续的行进入单个结果行? 这是所需的查询结果:

FirstProductID | FirstProductName | SecondProductID | SecondProductName 
---------------+------------------+-----------------+---------
0255463        | ProductA         |        0254483  | ProductB
0255341        | ProductC         |       0905454   | ProductD

我想要一个可以用于任何表的通用解决方案,无论表内容如何。

5 个答案:

答案 0 :(得分:3)

创建一个带有自动增量列的临时表

CREATE TEMP TABLE temp(
  id int not null primary auto increment,
  pid int,
  pname text,    
);

将此数据插入临时表

INSERT INTO temp (pid, pname) SELECT * FROM Products;

加入第一个实例id = id + 1

id % 2 = 0上的临时表
SELECT t1.pid AS t1_pid, t1.pname AS t1_pname,
  t2.pid AS t2_pid, t2.pname AS t2_pname
FROM temp as t1 LEFT JOIN temp AS t2
  ON t1.id + 1 = t2.id
WHERE t1.id % 2 = 0;

答案 1 :(得分:1)

单一查询(不是更快):

SELECT
    First.ProductId AS FirstProductId,
    First.ProductName AS FirstProductName,
    Second.ProductId AS SecondProductId,
    Second.ProductName AS SecondProductName 
FROM
    (SELECT *, Cnt/2 AS Line FROM (
        SELECT *, (
            SELECT COUNT() FROM Products AS _ WHERE ROWID<Products.ROWID
        ) AS Cnt FROM Products WHERE Cnt%2=0
    )) AS First
LEFT JOIN
    (SELECT *, Cnt/2 AS Line FROM (
        SELECT *, (
            SELECT COUNT() FROM Products AS _ WHERE ROWID<Products.ROWID
        ) AS Cnt FROM Products WHERE Cnt%2=1
    )) AS Second
ON First.Line = Second.Line
ORDER BY First.Line;

如果您需要更快的解决方案,@ kzarns提出了一个好的解决方案。

答案 2 :(得分:0)

如果您有连续的行ID,则很容易:

SELECT t1.productID AS productID1, t1.ProductName AS productName1, t2.productID AS productID2, t2.ProductName AS ProductName2
FROM product t1
JOIN product t2 WHERE t1.id+1 = t2.id
WHERE MOD(t1.id,2) = 1
ORDER BY t1.id

答案 3 :(得分:0)

这是样本的想法,基本上我正在做的是从2个select语句中获取不同的结果并加入它,试一试,欢呼=)

- 样本表

DECLARE @SAMPLE TABLE
(
ProductID INT,
ProductName NVARCHAR(255)
)

- 样本数据

INSERT INTO @SAMPLE
VALUES
('1','ProductA'),
('2','ProductB'),
('3','ProductC'),
('4','ProductD')

SELECT FirstProductID,FirstProductName,SecondProductID,SecondProductName FROM
(

- 查询获取firstProduct项目

SELECT ROW_NUMBER() OVER (ORDER BY FirstProductID) firstrn,FirstProductID,FirstProductName
FROM(
SELECT ProductID 'FirstProductID',ProductName 'FirstProductName' FROM 
(
SELECT ROW_NUMBER() OVER (Order by productid) firstrn1,ProductID,ProductName
FROM @SAMPLE
) FIRSTPRODUCTTABLE
WHERE firstrn1%2 = 1
) FIRSTPRODUCTTABLE1 
)t1

- 执行连接

LEFT OUTER JOIN (

- 查询以获取第二个产品

SELECT * FROM
(
SELECT ROW_NUMBER() OVER (ORDER BY SecondProductID) rownumber,SecondProductID,SecondProductName
FROM(
SELECT ProductID 'SecondProductID',ProductName 'SecondProductName' FROM 
(
SELECT ROW_NUMBER() OVER (Order by productid) rn,ProductID,ProductName
FROM @SAMPLE
) SECONDPRODUCTTABLE
WHERE rn%2 = 0
)SECONDPRODUCTTABLE1
) t2
) t3 ON t1.firstrn=t3.rownumber

答案 4 :(得分:0)

在SQL中执行此操作会相当复杂和缓慢。

重新排序这样的列值的最简单方法是实现自己的Cursor类,它包装原始数据库游标,但是列数增加一倍,并将所有列访问重定向到相应的记录。 像这样:

class DoubleColumnCursor implements Cursor {
    Cursor baseCursor;
    int baseColumns;
    int currentPosition;

    public getColumnCount() {
        return baseColumns * 2;
    }
    public String getColumnName(int columnIndex) {
        if (columnIndex < baseColumns)
            return baseCursor.getColumnName(columnIndex) + "1";
        else
            return baseCursor.getColumnName(columnIndex - baseColumns) + "2";
    }
    public boolean moveToPosition(int position) {
        boolean result = baseCursor.moveToPosition(position * 2);
        if (result)
            currentPosition = position;
        return result;
    }
    public String getString(int columnIndex) {
        if (columnIndex < baseColumns) {
            baseCursor.moveToPosition(currentPosition * 2);
            return baseCursor.getString(columnIndex);
        } else {
            baseCursor.moveToPosition(currentPosition * 2 + 1);
            return baseCursor.getString(columnIndex - baseColumns);
        }
    }
    ...
};