SQL 查看声明

SQL View Statement

我正在尝试制作一个有 6 列的视图,每行是另一个 table 的 2 列的前 3 行。

例如:

Asset Table
Asset_ID     Asset_Name
1            Kitchen
2            Bathroom
3            Bedroom
4            Bed
5            Knife
6            Basement

View Combined
Asset_ID_A   Asset_Name_A   Asset_ID_B   Asset_Name_B   Asset_ID_C   Asset_Name_C
1            Kitchen        2            Bathroom       3            Bedroom
4            Bed            5            Knife          6            Basement

这样的事情可能吗?

抱歉,这纯粹是SQL。我不该说举报的。

至于哪些列,就是sqltableZ的X列和Y列。

是的,这是可能的。 首先,您需要对每一行进行编号。之后,使用行号 mod 3 递归加入一些 table 并等于 0、1 和 2。

Ps:抱歉英语不好。

让我在这里说清楚:在 SQL 中这样做是一个 非常糟糕的主意 。也就是说,完全有可能:

WITH Ordered As (
    select Asset_ID, Asset_Name, row_number() over (order by Asset_ID) as Sequence
    from Asset
)
SELECT o1.Asset_ID Asset_ID_A, o1.Asset_Name Asset_Name_A
      ,o2.Asset_ID Asset_ID_B, o2.Asset_Name Asset_Name_B
      ,o3.Asset_ID Asset_ID_C, o3.Asset_Name Asset_Name_C
FROM Ordered o1
LEFT JOIN Ordered o2 ON o2.Sequence = o1.Sequence + 1
LEFT JOIN Ordered o3 ON o3.Sequence = o1.Sequence + 2
WHERE o1.Sequence % 3 = 1

如果你只有 6 行

SELECT 
    t1.Asset_ID as Asset_ID_A,   
    t1.Asset_Name as Asset_Name_A,   
    t2.Asset_ID as Asset_ID_B,   
    t2.Asset_Name as Asset_Name_B,   
    t3.Asset_ID as Asset_ID_C, 
    t3.Asset_Name as Asset_Name_C
FROM yourtable t1
INNER JOIN yourtable t2
ON t2.Asset_ID  - 1 = t1.Asset_ID 
INNER JOIN yourtable t3
ON t3.Asset_ID  - 1 = t2.Asset_ID 
WHERE t1.Asset_ID in (1,4)