具有计数和日期的SQL联合

时间:2018-05-17 17:56:51

标签: sql sql-server

再次询问,但希望更清楚。我有以下情况,这是完美的工作你可以看到Sql Unions Part 1原来的问题得到了解答!

DECLARE @imvw_woap_code_with_cust TABLE (
        pkey INT,
        Code_id VARCHAR(200),
        code_type_id VARCHAR(200),
        asset_standard_id VARCHAR(200),
        date_completed DATETIME
    )
INSERT INTO @imvw_woap_code_with_cust(pkey,Code_id,code_type_id,asset_standard_id,date_completed)
    SELECT 101480,'Alarmining','A-Problem','1 East','2017-08-10 02:00:30.013'
    UNION SELECT 103350,'Fluid/Gas Leak (Out)','A-Problem','2 west','2017-08-10 02:00:30.013'
    UNION SELECT 104033,'plug/Pwr Cord Damg','A-Problem','2 west','2017-08-10 02:00:30.013'
    UNION SELECT 107296,'Alarmining','A-Problem','2 west','2017-09-11 02:00:30.013'
    UNION SELECT 106404,'Incoming Inspect','A-Problem','2 west','2017-08-10 02:00:30.013'
    UNION SELECT 106407,'Incoming Inspect','A-Problem','2 west','2017-09-11 02:00:30.013'
    UNION SELECT 106408,'Incoming Inspect','A-Problem','2 west','2017-09-12 02:00:30.013'

DECLARE @IMTBL_CODE TABLE (
    pkey INT,
    Code_id VARCHAR(200),
    code_type_id VARCHAR(200)
)

INSERT INTO @IMTBL_CODE(pkey,Code_id,code_type_id)
    SELECT 100064,'1-NO PROBLEM CODE ENTERED','A-Problem'
    UNION SELECT 100068,'Alarmining','A-Problem'
    UNION SELECT 100069,'Battery Failure','A-Problem'
    UNION SELECT 100070,'Incoming Inspect','A-Problem'
    UNION SELECT 100071,'Fluid/Gas Leak (Out)','A-Problem'
    UNION SELECT 100072,'Case Damaged','A-Problem'
    UNION SELECT 100073,'plug/Pwr Cord Damg','A-Problem'


CREATE VIEW cause_codes as
SELECT
        possibilities.code_id AS code_id,
        possibilities.asset_standard_id,
        Count(U.pkey) AS [COUNT]
FROM (
    SELECT
        a.asset_standard_id,b.code_id
    FROM (SELECT DISTINCT asset_standard_id FROM @imvw_woap_code_with_cust) AS a
    CROSS JOIN (
        SELECT DISTINCT code_id FROM @imtbl_code
        UNION
        SELECT DISTINCT CODE_ID AS [ID] FROM @imvw_woap_code_with_cust 
    ) AS b
) AS possibilities
LEFT OUTER JOIN @imvw_woap_code_with_cust AS U ON U.code_id = possibilities.code_id
    AND possibilities.asset_standard_id=u.asset_standard_id
    and u.code_type_id='A-Problem'
GROUP BY possibilities.code_id, possibilities.asset_standard_id
order by [count]

一旦我将它创建到视图(cause_codes)中,它就会返回一个整齐的表,其中包含代码出现次数(包括0次计数)。

CODE ID                   asset_standard_id     count
1-NO PROBLEM CODE ENTERED   1 East                0
1-NO PROBLEM CODE ENTERED   2 west                0
Battery Failure             1 East                0
Battery Failure             2 west                0
Case Damaged                1 East                0
Case Damaged                2 west                0
Fluid/Gas Leak (Out)        1 East                0
Incoming Inspect            1 East                0
plug/Pwr Cord Damg          1 East                0
plug/Pwr Cord Damg          2 west                1
Fluid/Gas Leak (Out)        2 west                1
Alarmining                  1 East                1
Alarmining                  2 west                1
Incoming Inspect            2 west                3

然后客户回来了,希望添加日期 所以,如果我在视图中添加日期

ALTER VIEW cause_codes as
    SELECT
            possibilities.code_id AS code_id,
            possibilities.asset_standard_id,
            Count(U.pkey) AS [COUNT],
            U.date_created
    FROM (
        SELECT
            a.asset_standard_id,b.code_id
        FROM (SELECT DISTINCT asset_standard_id FROM @imvw_woap_code_with_cust) AS a
        CROSS JOIN (
            SELECT DISTINCT code_id FROM @imtbl_code
            UNION
            SELECT DISTINCT CODE_ID AS [ID] FROM @imvw_woap_code_with_cust 
        ) AS b
    ) AS possibilities
    LEFT OUTER JOIN @imvw_woap_code_with_cust AS U ON U.code_id = possibilities.code_id
        AND possibilities.asset_standard_id=u.asset_standard_id
        and u.code_type_id='A-Problem'
    GROUP BY possibilities.code_id, possibilities.asset_standard_id,date_created
    order by [count]

然后进行搜索

 select * from cause_codes where asset_standard_id = '2 west' and date_completed > '2017-09-10 02:00:30.013' and date_completed < '2017-09-12 02:00:30.013'

表格分割这样的行 - &gt;

Incoming Inspect       2 west                1   2017-09-11 02:00:30.013' 
Incoming Inspect       2 west                1   2017-09-12 02:00:30.013' 

有没有办法可以执行此操作(添加搜索日期),但仍然保持单行的正确计数?

我希望这很清楚......这是一个很大的问题。 感谢您抽出宝贵时间阅读我的书:)

2 个答案:

答案 0 :(得分:0)

每当按日期分组,并且日期有多个唯一值时,您将获得每个日期的多行,每个唯一日期的分组总和。请注意,您现在要求为每个代码,资产ID和日期提供汇总摘要数据。

您必须获取新日期值的最小值或最大值(不同的摘要),而不是按日期分组,或者如果您愿意,并且您确定不会获得大量日期,如果你想这样做,你可以在逗号描述的列表中列出它们可能的行日期。以下是使用For Xml技巧的其他表的日期列表的示例。网上还有其他例子。

select
    RepName, DateList
from
    (
        select
            FirstName + ' ' + LastName as RepName, PersonID
        from
            Person
        where
            PersonID in (23164,30637,29985,25643)
    ) as p
    cross apply (
        select stuff ( (
            select
                ', ' + convert(varchar(16), CallDate, 101)
            from    
            (
                select
                    distinct TheDate
                from
                    dbo.Info as c
                    inner join Person as p2 on p2.PersonID = c.PersonID
                where
                    p2.PersonID = p.PersonID
            ) as dat
            order by CallDate
            for xml path('')
        ), 1, 2, '') as DateList
    ) as dateList

答案 1 :(得分:0)

我首先到达那里,我没有衡量表现。

;WITH Activity As
(
    SELECT Count = COUNT(*),Customer.Code_id,Customer.asset_standard_id
    FROM @imvw_woap_code_with_cust Customer
    GROUP BY Customer.Code_id,asset_standard_id
),
Groups AS
(
    SELECT Code_id,asset_standard_id 
    FROM @IMTBL_CODE Codes
    CROSS APPLY (SELECT DISTINCT asset_standard_id FROM @imvw_woap_code_with_cust) X
), 
GroupedActivity AS
(
    SELECT Groups.Code_id,Groups.asset_standard_id,Count = CASE WHEN Activity.Count IS NULL THEN 0 ELSE Activity.Count END
    FROM Groups
    LEFT OUTER JOIN Activity ON Activity.Code_id = Groups.Code_id AND Activity.asset_standard_id = Groups.asset_standard_id
)
SELECT
    C.pkey,
    G.Code_id,
    G.asset_standard_id,
    G.Count,
    C.date_completed
FROM
    @imvw_woap_code_with_cust C
    INNER JOIN GroupedActivity G ON G.Code_id = C.Code_id AND G.asset_standard_id = C.asset_standard_id
WHERE
    G.asset_standard_id = '2 west' and date_completed > '2017-09-10 02:00:30.013' and date_completed < '2017-09-12 02:00:30.013'

如果可以省略没有活动的标准,可以缩短到下面的查询。

;WITH Activity As
(
    SELECT Count = COUNT(*),Customer.Code_id,Customer.asset_standard_id
    FROM @imvw_woap_code_with_cust Customer
    GROUP BY Customer.Code_id,asset_standard_id
)

SELECT
    C.pkey,
    G.Code_id,
    G.asset_standard_id,
    G.Count,
    C.date_completed
FROM
    @imvw_woap_code_with_cust C
    INNER JOIN Activity G ON G.Code_id = C.Code_id AND G.asset_standard_id = C.asset_standard_id
WHERE
    G.asset_standard_id = '2 west' and date_completed > '2017-09-10 02:00:30.013' and date_completed < '2017-09-12 02:00:30.013'

使用您在上面发布的那个包含在CTE中并删除了date_completed的相同查询。当计数与细节对齐时,您可以覆盖最终查询中完成的日期。

;WITH GroupedData AS
(
    SELECT
            possibilities.code_id AS code_id,
            possibilities.asset_standard_id,
            Count(U.pkey) AS [COUNT]
    FROM (
        SELECT
            a.asset_standard_id,b.code_id
        FROM (SELECT DISTINCT asset_standard_id FROM @imvw_woap_code_with_cust) AS a
        CROSS JOIN (
            SELECT DISTINCT code_id FROM @imtbl_code
            UNION
            SELECT DISTINCT CODE_ID AS [ID] FROM @imvw_woap_code_with_cust 
        ) AS b
    ) AS possibilities
    LEFT OUTER JOIN @imvw_woap_code_with_cust AS U ON U.code_id = possibilities.code_id
        AND possibilities.asset_standard_id=u.asset_standard_id
        and u.code_type_id='A-Problem'
    GROUP BY possibilities.code_id, possibilities.asset_standard_id
)

SELECT
    G.code_id,
    G.asset_standard_id,
    G.COUNT
    C.date_completed
FROM
    @imvw_woap_code_with_cust C
    INNER JOIN GroupedData G ON G.Code_id = C.Code_id AND G.asset_standard_id = C.asset_standard_id
ORDER BY count