在一次选择中返回两个 Count(*) 结果

发布于 2024-10-14 22:27:04 字数 466 浏览 1 评论 0原文

我有一个存储过程,我希望它返回以下内容......

TotalItems  |  FailedItems
@totalItems | @failedItems

where --> @totalItems = `SELECT COUNT(*)
    From dbo.OdsBuild AS P 
    where P.StartTime Between @StartDate and @EndDate 
    AND P.Official = 1`

where --> @failedItems = `SELECT COUNT(*)
    From dbo.Items AS P
    where p.StartTime Between @StartDate and @EndDate 
    AND P.Official = 1 AND ( P.Result = 7 OR P.Result = 8 OR P.Result = 14)`

I have a stored procedure and I want it to return the following...

TotalItems  |  FailedItems
@totalItems | @failedItems

where --> @totalItems = `SELECT COUNT(*)
    From dbo.OdsBuild AS P 
    where P.StartTime Between @StartDate and @EndDate 
    AND P.Official = 1`

where --> @failedItems = `SELECT COUNT(*)
    From dbo.Items AS P
    where p.StartTime Between @StartDate and @EndDate 
    AND P.Official = 1 AND ( P.Result = 7 OR P.Result = 8 OR P.Result = 14)`

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

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

发布评论

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

评论(3

千秋岁 2024-10-21 22:27:05

您正在寻找的是 GROUP BY 和 HAVING

What you are looking for is GROUP BY and HAVING

悟红尘 2024-10-21 22:27:04

子查询 SELECT COUNT

SELECT
    (SELECT COUNT(*)
        From dbo.OdsBuild AS P 
        where P.StartTime Between @StartDate and @EndDate 
        AND P.Official = 1) totalItems ,
    (SELECT COUNT(*)
        From dbo.Items AS P
        where p.StartTime Between @StartDate and @EndDate 
        AND P.Official = 1 AND ( P.Result = 7 OR P.Result = 8 OR P.Result = 14)) failedItems

如果您已经将它们设置为变量,当然您不必重复 SELECT COUNT。

SELECT @totalItems AS totalItems, @failedItems AS failedItems

SELECT 语句可以独立存在,无需 FROM 子句。

Subquery the SELECT COUNTs

SELECT
    (SELECT COUNT(*)
        From dbo.OdsBuild AS P 
        where P.StartTime Between @StartDate and @EndDate 
        AND P.Official = 1) totalItems ,
    (SELECT COUNT(*)
        From dbo.Items AS P
        where p.StartTime Between @StartDate and @EndDate 
        AND P.Official = 1 AND ( P.Result = 7 OR P.Result = 8 OR P.Result = 14)) failedItems

If you already had them set as variables, of course you don't have to repeat the SELECT COUNTs.

SELECT @totalItems AS totalItems, @failedItems AS failedItems

SELECT statements are allows to stand alone without FROM clauses.

吝吻 2024-10-21 22:27:04

您不能简单地在过程结束时选择这些变量吗?

SELECT @totalitems AS TotalItems, @faileditems AS FailedItems

Can't you simply select those variables at the end of your proc?

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