You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SQL统计需求:指定团队某日在办申请数(转移时防重复计数)

Got it, let's tackle this problem step by step. The key challenge here is making sure transferred applications on the target date are only counted for the receiving team, with no duplicates. First, let's clarify the necessary table structures (since the original Application table was incomplete, I'll add the fields we need for transfer tracking):

Solution for Counting In-Progress Applications by Team on a Specific Date

Table Definitions

First, here are the full table structures we’ll need to implement the logic correctly:

  • Users:

    ColumnTypeDescription
    IdINTUnique user ID
    NameVARCHAR(50)User's name
    TeamIdINTID of the team the user belongs to
  • Application:

    ColumnTypeDescription
    IdINTUnique application ID
    NameVARCHAR(100)Application name
    StatusVARCHAR(20)Application status (use InProgress for active, in-process applications)
    CreatedDateTimeDATETIMETimestamp when the application was created
    AssigneeIdINTCurrent user assigned to handle the application
  • ApplicationTransfer (critical for tracking transfer history):

    ColumnTypeDescription
    IdINTUnique transfer record ID
    ApplicationIdINTID of the transferred application
    FromUserIdINTUser who initiated the transfer
    ToUserIdINTUser who received the application
    TransferDateTimeDATETIMETimestamp when the transfer was completed

SQL Query to Count Applications

This query will calculate the number of in-progress applications for a specified team on a target date, handling transfer day logic correctly:

-- Set your target parameters here
DECLARE @TargetDate DATE = '2024-05-20';
DECLARE @TargetTeamId INT = 2;

WITH ApplicationDailyOwnership AS (
    SELECT
        a.Id AS ApplicationId,
        -- Determine which team owns the application on the target date
        COALESCE(
            -- If the app was transferred on the target date, use the LAST transfer's receiving team
            (SELECT TOP 1 u.TeamId
             FROM ApplicationTransfer t
             JOIN Users u ON t.ToUserId = u.Id
             WHERE t.ApplicationId = a.Id
               AND CAST(t.TransferDateTime AS DATE) = @TargetDate
             ORDER BY t.TransferDateTime DESC),
            -- If no transfer that day, use the current assignee's team
            (SELECT u.TeamId FROM Users u WHERE u.Id = a.AssigneeId)
        ) AS OwnedTeamId
    FROM Application a
    WHERE a.Status = 'InProgress' -- Only count active, in-process applications
      AND CAST(a.CreatedDateTime AS DATE) <= @TargetDate -- Ensure the app existed by the target date
)
SELECT COUNT(DISTINCT ApplicationId) AS InProgressApplicationCount
FROM ApplicationDailyOwnership
WHERE OwnedTeamId = @TargetTeamId;

Stored Procedure for Reusability

If you need to run this calculation frequently, wrap the logic in a stored procedure for easier reuse:

CREATE PROCEDURE GetTeamInProgressApplications
    @TargetDate DATE,
    @TargetTeamId INT,
    @ApplicationCount INT OUTPUT
AS
BEGIN
    SET NOCOUNT ON;

    WITH ApplicationDailyOwnership AS (
        SELECT
            a.Id AS ApplicationId,
            COALESCE(
                (SELECT TOP 1 u.TeamId
                 FROM ApplicationTransfer t
                 JOIN Users u ON t.ToUserId = u.Id
                 WHERE t.ApplicationId = a.Id
                   AND CAST(t.TransferDateTime AS DATE) = @TargetDate
                 ORDER BY t.TransferDateTime DESC),
                (SELECT u.TeamId FROM Users u WHERE u.Id = a.AssigneeId)
            ) AS OwnedTeamId
        FROM Application a
        WHERE a.Status = 'InProgress'
          AND CAST(a.CreatedDateTime AS DATE) <= @TargetDate
    )
    SELECT @ApplicationCount = COUNT(DISTINCT ApplicationId)
    FROM ApplicationDailyOwnership
    WHERE OwnedTeamId = @TargetTeamId;
END;

How to Use the Stored Procedure:

DECLARE @ResultCount INT;
EXEC GetTeamInProgressApplications 
    @TargetDate = '2024-05-20', 
    @TargetTeamId = 2, 
    @ApplicationCount = @ResultCount OUTPUT;
SELECT @ResultCount AS InProgressApplications;

Key Logic Breakdown

  • Transfer Day Handling: For applications transferred on the target date, we prioritize the last transfer's receiving team to ensure the app is only counted for the team that ends up with it that day—no duplicates across teams.
  • Duplicate Prevention: Using COUNT(DISTINCT ApplicationId) ensures even if an application is transferred multiple times in a single day, it’s only counted once.
  • In-Progress Filter: We only include applications marked as InProgress that existed on or before the target date, so we don’t count apps created after the date in question.

内容的提问来源于stack exchange,提问作者Ady B

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:29:40