SQL 将表联合成 jqGrid 的树状结构

SQL Union tables into treegrid structure for jqGrid

我已经用我目前拥有的数据库结构创建了一个示例:

here

我的目标是获得一个数据库 table,其中所有数据都已经在正确的位置,并以正确的顺序排序,以便输入到 jqGrid 中,最后应该如下所示:

Entity          |OrderTime  |City   |ProductType    |...
-------------------------------------------------------------------
AlexKlar        |           |       |
   SubPack     |17:00      |London |
       Mango   |           |       |Fruit  |
   WelcomePack |15:00      |London |
       Apple   |           |
       Banana  |           |       |Fruit  |
AnnaKlar        |           |       |
   WelcomePack |16:00      |London |
       Apple   |           |       |Fruit  |
JuliaKlar       |           |       |
   PremiumPack |18:00      |London |
       Lychee  |           |       |Fruit  |
   SubPack     |18:30      |London |
       Mango   |           |       |Fruit  |
   WelcomePack |15:00      |London |
       Apple   |           |       |Fruit  |
       Banana  |           |       |Fruit  |

问题 1:将所有 table 联合到一个主 table 以便为 jgGrid 定义 id/parents/level/leaf 的最有效方法是什么?由于并非所有级别都需要所有列的值,因此我必须为其分配一个空字符串 ''。我的代码目前是这样的:

SELECT 
CONVERT(VARCHAR,PersonID) AS id, 
0 AS level, 
'false' AS isLeaf, 
'null' AS parent, 
FullName AS entity,
'' AS OrderTime,
'' AS City,
'' AS Active,
'' AS ProductType,
'' AS Price
from Persons
UNION ALL
SELECT 
CONVERT(VARCHAR,PersonID) + '>' + CONVERT(VARCHAR,OrderID) AS id, 
1 AS level, 
'false' AS isLeaf, 
CONVERT(VARCHAR,PersonID) AS parent, 
OrderName AS entity,
COALESCE(CAST(OrderTime AS VARCHAR(5)),'') AS OrderTime,
City,
Active,
'' AS ProductType,
'' AS Price
from Orders
UNION ALL
SELECT 
CONVERT(VARCHAR,per.PersonID) + '>' + CONVERT(VARCHAR,ord.OrderID) + '>' + CONVERT(VARCHAR,prod.ProductID) AS id, 
2 AS level, 
'true' AS isLeaf, 
CONVERT(VARCHAR,per.PersonID) + '>' + CONVERT(VARCHAR,ord.OrderID) AS parent, 
ProductName AS entity,
'' AS OrderTime,
'' AS City,
so.Completed AS Active,
ProductType,
COALESCE(CONVERT(VARCHAR,Price),'') AS Price
from SubmittedOrders so
INNER JOIN Orders ord ON ord.OrderID = so.OrderID
INNER JOIN Persons per ON per.PersonID = ord.PersonID
INNER JOIN Products prod ON prod.ProductID = so.ProductID
Order by id

问题 2:在创建了我拥有的 4 个 table 的 table 并集之后,我想对其进行排序,使其按字母顺序排列,但仅限于它们自己的级别(以便它看起来像上图所示的 table)。我想避免使用 jqGrid 内置排序函数 grid.jqGrid('sortGrid', 'importJob');在客户端,因为如果我有 5000 orders/rows.

它真的很慢

在此先感谢您的帮助。

这将使他们按照您需要的方式进行排序:

;WITH
CTE_All
AS
(
SELECT 
    per.PersonID,
    per.FullName,
    per.PersonRow,
    ord.OrderID,
    ord.OrderName,
    ord.OrderTime,
    ord.City,
    ord.Active,
    ord.OrderRow,
    prod.ProductID,
    prod.ProductName,
    prod.ProductType,
    prod.Price,
    prod.ProductRow,
    so.Completed
from #SubmittedOrders so
LEFT JOIN (select *, row_number() over (order by OrderName) OrderRow from #Orders) ord 
ON ord.OrderID = so.OrderID
LEFT JOIN (select *, row_number() over (order by FullName) PersonRow from #Persons) per 
ON per.PersonID = ord.PersonID
LEFT JOIN (select *, row_number() over (order by ProductName) ProductRow from #Products) prod 
ON prod.ProductID = so.ProductID
)
SELECT DISTINCT
    CONVERT(VARCHAR,PersonID) AS id, 
    0 AS level, 
    'false' AS isLeaf, 
    'null' AS parent, 
    FullName AS entity,
    '' AS OrderTime,
    '' AS City,
    '' AS Active,
    '' AS ProductType,
    '' AS Price,
    PersonRow, 
    0 AS OrderRow, 
    0 AS ProductRow
from CTE_All Persons
UNION ALL
SELECT DISTINCT
    CONVERT(VARCHAR,PersonID) + '>' + CONVERT(VARCHAR,OrderID) AS id, 
    1 AS level, 
    'false' AS isLeaf, 
    CONVERT(VARCHAR,PersonID) AS parent, 
    OrderName AS entity,
    COALESCE(CAST(OrderTime AS VARCHAR(5)),'') AS OrderTime,
    City,
    Active,
    '' AS ProductType,
    '' AS Price,
    PersonRow, 
    OrderRow, 
    0 AS ProductRow
from CTE_All Orders
UNION ALL
SELECT 
    CONVERT(VARCHAR,PersonID) + '>' + CONVERT(VARCHAR,OrderID) + '>' + CONVERT(VARCHAR,ProductID) AS id, 
    2 AS level, 
    'true' AS isLeaf, 
    CONVERT(VARCHAR,PersonID) + '>' + CONVERT(VARCHAR,OrderID) AS parent, 
    ProductName AS entity,
    '' AS OrderTime,
    '' AS City,
    Completed AS Active,
    ProductType,
    COALESCE(CONVERT(VARCHAR,Price),'') AS Price,
    PersonRow, 
    OrderRow, 
    ProductRow
from CTE_All
Order by PersonRow, OrderRow, ProductRow

您可以使用创建摘要记录的组汇总来消除并集。但是你必须在 select 子句中添加一些 case 语句:

select 
    CONVERT(VARCHAR,PersonID) + isnull('>' + CONVERT(VARCHAR,OrderID),'') + isnull('>' + CONVERT(VARCHAR,ProductID),'') AS id,
    Case when ProductID IS NOT null then 2 when OrderID IS NOT null then 1 else 0 end level,
    case when ProductID IS NOT null then 'true' else 'false' end isLeaf,
    case when ProductID IS NOT null then CONVERT(VARCHAR,PersonID) + isnull('>' + CONVERT(VARCHAR,OrderID),'') when OrderID IS NOT null then CONVERT(VARCHAR,PersonID) else 'null' end parent,
    COALESCE(ProductName, OrderName, FullName) entity,
    OrderTime,
    City,
    Active,
    ProductType,
    Price
from
    (
    select #Persons.PersonID, 
        #Orders.OrderID,
        #Products.ProductID, 
        MIN(FullName) FullName,
        CASE WHEN #Orders.OrderID IS NOT NULL THEN MIN(OrderName) END OrderName,
        CASE WHEN #Products.ProductID IS NOT NULL THEN MIN(ProductName) END ProductName,
        CASE WHEN #Products.ProductID IS NOT NULL THEN MIN(convert(varchar(1),Completed)) ELSE '' END Active,
        CASE WHEN #Orders.OrderID IS NOT NULL AND #Products.ProductID IS NULL THEN MIN(OrderTime) ELSE '' END OrderTime,
        CASE WHEN #Orders.OrderID IS NOT NULL AND #Products.ProductID IS NULL THEN MIN(City) else '' END City,
        CASE WHEN #Products.ProductID IS NOT NULL THEN MIN(ProductType) ELSE '' END ProductType,
        CASE WHEN #Products.ProductID IS NOT NULL THEN convert(varchar(20),MIN(Price)) ELSE '' END Price        
    from #SubmittedOrders
    JOIN #Orders
    on #SubmittedOrders.OrderID = #Orders.OrderID
    JOIN #Persons
    on #Orders.PersonID = #Persons.PersonID
    JOIN #Products
    ON #SubmittedOrders.ProductID = #Products.ProductID
    GROUP BY ROLLUP (#Persons.PersonID, #Orders.OrderID,#Products.ProductID)
    having #Persons.PersonID is not null
    ) x
ORDER BY FullName, OrderName, ProductName