SQL中CTE表联合后如何获取记录总数?

时间:2015-06-19 03:01:57

标签: sql union common-table-expression

我在3到4个表上应用CTE并使用UNION组合结果。我没有将组合结果存储在任何地方。所以现在我面临的挑战是在这四个表结合后得到总数记录。

此外,我必须根据某些标志设置选择有限数量的行,如果设置了导出到Excel,则选择25000条记录,否则选择10000条记录。

请帮我解决这个问题。

代码示例如下所示:

WITH Item_Characteristics_CTE AS
(
    SELECT 
       sequence, item_id
    FROM 
       Item_Characteristics_Log
),
Item_Required_Quantity_Log_CTE AS
(
    SELECT 
       sequence, item_id
    FROM 
       Item_Required_Quantity_Log
)
SELECT 
    c1.item_id 
FROM 
    Item_Characteristics_CTE c1
INNER JOIN 
    Item_Characteristics_CTE c2 ON c1.sequence = c2.sequence 

UNION

SELECT 
    c1.item_id AS item_id
FROM 
    Item_Required_Quantity_Log_CTE c1 
INNER JOIN        
    Item_Required_Quantity_Log_CTE c2 ON c1.sequence = c2.sequence 
WHERE 
    C2.RN = C1.RN 

2 个答案:

答案 0 :(得分:0)

您可以在CTE之后将查询包装为子查询并计算这些行。

;WITH Item_Characteristics_CTE AS (
   -- YOUR EXISTING CTE QUERIES
), Item_Required_Quantity_Log_CTE (
   -- YOUR EXISTING CTE QUERIES
)
    SELECT COUNT(*)
    FROM (
        -- YOUR EXISTING SELECT + HOWEVER YOU FILTER BY THE FLAG
    ) T

如果您需要CTE查询的输出和计数,那么您可以将CTE输出到临时表中,然后有1个查询从临时表中进行选择,另一个用于计算行。

;WITH Item_Characteristics_CTE AS (
   -- YOUR EXISTING CTE QUERIES
), Item_Required_Quantity_Log_CTE (
   -- YOUR EXISTING CTE QUERIES
)
   SELECT c1.item_id INTO #T FROM ... -- (EXISTING QUERIES)
SELECT * FROM #T
SELECT COUNT(*) FROM #T

基于反馈@@ ROWCOUNT和可选最大行的更新

下面的查询获取标志并确定要获取的最大行数,然后将最大行数提供给CTE结果的外部查询。

DECLARE @IsExcel BIT = 1 -- Your flag whether excel or not
DECLARE @Rows INT = CASE WHEN @IsExcel = 1 THEN 25000 ELSE 10000 END
WITH Item_Characteristics_CTE AS (
    SELECT
        sequence,
        item_id
    FROM Item_Characteristics_Log
), Item_Required_Quantity_Log_CTE AS (
    SELECT
        sequence,
        item_id
    FROM Item_Required_Quantity_Log
)
    SELECT TOP (@Rows) *
    FROM (
        SELECT
            c1.item_id
        FROM Item_Characteristics_CTE c1
            INNER JOIN Item_Characteristics_CTE c2 
                ON c1.sequence = c2.sequence 
        UNION
        SELECT
            c1.item_id AS item_id
        FROM Item_Required_Quantity_Log_CTE c1
            INNER JOIN Item_Required_Quantity_Log_CTE c2
                ON c1.sequence = c2.sequence 
        WHERE C2.RN = C1.RN 
    )
DECLARE @TotalRows INT = @@ROWCOUNT -- OR "SELECT @@ROWCOUNT"

答案 1 :(得分:0)

我不确定你在上述问题中的标志是什么。但为了得到计数,你可以使用这样一个额外的CTE:

;WITH Item_Characteristics_CTE
AS (
    SELECT sequence
        ,item_id
    FROM Item_Characteristics_Log
    )
    ,Item_Required_Quantity_Log_CTE
AS (
    SEELECT sequence
    ,item_id FROM Item_Required_Quantity_Log
    )
    ,Item_Id_Count
AS (
    SELECT c1.item_id
    FROM Item_Characteristics_CTE c1
    INNER JOIN Item_Characteristics_CTE c2 ON c1.sequence = c2.sequence

    UNION

    SELECT c1.item_id AS item_id
    FROM Item_Required_Quantity_Log_CTE c1
    INNER JOIN Item_Required_Quantity_Log_CTE c2 ON c1.sequence = c2.sequence
    WHERE C2.RN = C1.RN
    )
SELECT item_id
    ,count(item_id)
FROM Item_Id_Count