计算总行数,无限制

发布于 2024-11-01 18:06:20 字数 1239 浏览 0 评论 0原文

目前它正在工作,但我希望 xml 包含没有限制的总行数。

SET @query_result = (SELECT ID,Title 
        FROM
        ( 
            SELECT items.id AS "ID",items.title AS "Title" ,
ROW_NUMBER() OVER(ORDER BY date_added DESC) AS RowNum
                FROM [cars] 
                JOIN [items] ON items.id=cars.item_id
                WHERE
                rejected = 0 


    )AS MyDerivedTable
        WHERE
        MyDerivedTable.RowNum BETWEEN (@page-1)*2+1 AND (@page*2) 
        FOR XML PATH('car'),ROOT('items')
)

这返回

<items>
  <car>
    <ID>37</ID>
    <Title>Used 2004 Chevrolet Corvette Convertible</Title>
  </car>
</items>

我想要的

<items>
      <car>
        <ID>37</ID>
        <Title>Used 2004 Chevrolet Corvette Convertible</Title>
        <Count>6</Count>
      </car>
    </items>

虽然计数不是返回的行数,而是与查询匹配的行总数。或者,如果我的问题对任何人来说都难以理解,我正在寻找 FOUND_ROWS() 的 MSSQL 替代方案; 这个问题@SQL Count 总行数使用 LIMIT 试图回答同样的问题,但我想要一个解决方案是 MSSQL。

Currently its working but I want the xml to contain the total number of rows without the limit.

SET @query_result = (SELECT ID,Title 
        FROM
        ( 
            SELECT items.id AS "ID",items.title AS "Title" ,
ROW_NUMBER() OVER(ORDER BY date_added DESC) AS RowNum
                FROM [cars] 
                JOIN [items] ON items.id=cars.item_id
                WHERE
                rejected = 0 


    )AS MyDerivedTable
        WHERE
        MyDerivedTable.RowNum BETWEEN (@page-1)*2+1 AND (@page*2) 
        FOR XML PATH('car'),ROOT('items')
)

This returns

<items>
  <car>
    <ID>37</ID>
    <Title>Used 2004 Chevrolet Corvette Convertible</Title>
  </car>
</items>

I want

<items>
      <car>
        <ID>37</ID>
        <Title>Used 2004 Chevrolet Corvette Convertible</Title>
        <Count>6</Count>
      </car>
    </items>

While Count is not the number of rows returned but the total number of rows that matched the query .Or if my problem is too hard for anybody to understand ,I am looking for MSSQL alternative for FOUND_ROWS();
This question @SQL Count total number of rows whilst using LIMIT is trying to answer the same thing but I want a solution is MSSQL.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枫以 2024-11-08 18:06:20

好吧,我希望我现在明白你想要做什么。我相信您必须“扭转”您的选择并使用 CTE(通用表表达式)而不是子选择来实现此目的。

试试这个:

DECLARE @queryResult VARCHAR(MAX)

;WITH MyDerivedTable AS
(
    SELECT 
       items.id,
       items.title,
       ROW_NUMBER() OVER(ORDER BY date_added DESC) AS RowNum
    FROM dbo.cars
    INNER JOIN dbo.items ON items.id = cars.item_id
    WHERE rejected = 0 
) 
SELECT
   @queryResult = 
   (SELECT
       ID, Title,
       (SELECT MAX(RowNum) FROM MyDerivedTable) AS 'Count'
    FROM
       MyDerivedTable
    WHERE
        RowNum BETWEEN (@page-1)*2+1 AND (@page*2) 
    FOR XML PATH('car'),ROOT('items')
   )

SELECT @queryResult

应该输出您的 IDTitleCount(这是 RowNum 的最大值)对于每个汽车入口。

OK, I hope I understand now what you're trying to do. I believe you have to "turn around" your select and use a CTE (Common Table Expression) instead of a subselect to achieve this.

Try this:

DECLARE @queryResult VARCHAR(MAX)

;WITH MyDerivedTable AS
(
    SELECT 
       items.id,
       items.title,
       ROW_NUMBER() OVER(ORDER BY date_added DESC) AS RowNum
    FROM dbo.cars
    INNER JOIN dbo.items ON items.id = cars.item_id
    WHERE rejected = 0 
) 
SELECT
   @queryResult = 
   (SELECT
       ID, Title,
       (SELECT MAX(RowNum) FROM MyDerivedTable) AS 'Count'
    FROM
       MyDerivedTable
    WHERE
        RowNum BETWEEN (@page-1)*2+1 AND (@page*2) 
    FOR XML PATH('car'),ROOT('items')
   )

SELECT @queryResult

That should output your ID, Title and the Count (which is the max of the RowNum) for each car entry.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文