SQL Query以公司方式查找记录

时间:2015-11-30 12:56:10

标签: sql sql-server sql-server-2008

我想要一个显示公司 盗窃密钥丢失的行公司的行,状态为

请参阅下面的示例数据:

   addon        status  amt1    amt2    company
    theft cover yes      7       7      comp1
    key loss    yes      2       2      comp1
    theft cover no      NULL    NULL    comp2
    key loss    yes      2      33      comp2
    key loss    yes      1       1      comp3
    theft cover yes     12      22      comp3
    theft cover yes     11      22      comp4
    key loss    no      NULL    NULL    comp4
    theft cover yes     22      55      comp5
    key loss    yes     33      44      comp5

SELECT     addon, status, amt1, amt2, company
FROM         test
WHERE     (addon = 'theft cover' OR
                      addon = 'key loss') AND (status = 'yes')

请帮助我以上查询无法正常工作

并且我想要的输出如下:

 addon       status  amt1    amt2    company
    theft cover yes      7       7      comp1
    key loss    yes      2       2      comp1   
    key loss    yes      1       1      comp3
    theft cover yes     12      22      comp3   
    theft cover yes     22      55      comp5
    key loss    yes     33      44      comp5

4 个答案:

答案 0 :(得分:2)

使用Group BY子句按公司和插件对记录进行分组并汇总所有金额(仅当您有多条记录不在样本数据中时),如:

SELECT     addon, status, SUM(amt1), SUM(amt2), company
FROM         test
WHERE     (addon = 'theft cover' OR
                  addon = 'key loss') AND (status = 'yes')
GROUP BY company, addon

答案 1 :(得分:0)

SELECT addon,status,amt1,amt2,company 从测试 在哪里插入('盗窃封面','密钥丢失')和(状态='是') GROUP BY公司,addon,amt1,amt2,公司

如果您发现任何重复,可以使用上述查询来实现。

答案 2 :(得分:0)

您可以使用GROUP BY和HAVING的组合。 GROUP BY将为表中的每个公司返回一行。 HAVING可以用作过滤器,只允许插件数为2的公司进入最终结果。

如果您之前没有使用过HAVING子句,请参阅MSDN了解更多信息。

/* Declare a var to hold the sample data.
 */
DECLARE @SampleData TABLE
    (
        addon        VARCHAR(50),
        [status]        VARCHAR(3),
        amt1        INT,
        amt2        INT,
        company        VARCHAR(50)
    )
;

/* Populate sample data var.
 */
INSERT INTO @SampleData 
    (
        addon,
        [status],
        amt1,
        amt2,
        company
    )
VALUES
    ('theft cover', 'yes', 7, 7, 'comp1'),
    ('key loss', 'yes', 2, 2, 'comp1'),
    ('theft cover', 'no', NULL, NULL, 'comp2'),
    ('key loss', 'yes', 2, 33, 'comp2'),
    ('key loss', 'yes', 1, 1, 'comp3'),
    ('theft cover', 'yes', 12, 22, 'comp3'),
    ('theft cover', 'yes', 11, 22, 'comp4'),
    ('key loss', 'no', NULL, NULL, 'comp4'),
    ('theft cover', 'yes', 22, 55, 'comp5'),
    ('key loss', 'yes', 33, 44, 'comp5')
;

/* Using the HAVING clause I can count the number of addon types
 * each row has.  Any with 2 must have both theft cover and 
 * key loss.
 */
SELECT
    company,
    COUNT(DISTINCT addon) AS addon_count
FROM
    @SampleData
WHERE
    [status] = 'yes'
    AND addon IN ('theft cover', 'key loss')
GROUP BY
    company
HAVING
    COUNT(DISTINCT addon) = 2
;

答案 3 :(得分:0)

您的逻辑似乎是没有“不”状态的公司,而不仅仅是那些有“是”的公司。一种方法是使用带有having子句的聚合:

SELECT addon, status, amt1, amt2, company
FROM test t
WHERE addon in ('theft cover', 'key loss') AND
      NOT EXISTS (SELECT 1
                  FROM test t2
                  WHERE t2.company = t.company AND
                        t2.addon in ('theft cover', 'key loss') AND
                        t2.status <> 'yes'
                 );

实际上,表达它的一种更简单的方法是使用窗口函数。我认为这样做你想要的:

SELECT addon, status, amt1, amt2, company
FROM (SELECT addon, status, amt1, amt2, company,
             COUNT(*) OVER (PARTITION BY company) as cnt
      FROM test t
      WHERE addon in ('theft cover', 'key loss') AND status = 'yes'
     ) t
WHERE cnt = 2;

至少,这适用于你问题中的样本数据。