sql查询不存在的条目

时间:2018-12-05 18:33:24

标签: sql-server stored-procedures pivot

我继承了一个网站及其相应的数据库(SQL Server)。该网站使用存储过程从数据库中提取数据。这些存储过程之一包含一个枢轴,该枢轴需要4个小时以上才能运行。目前这是不可接受的。我正在寻求用标准SQL查询替换数据透视表的帮助,因为我认为这样做会更快并提供更好的性能。

这是有争议的重点:

SELECT *
FROM (
    SELECT ac.AID
        ,ac.CatName AS t
        ,convert(INT, ac.Code) AS c
    FROM categories AS ac
    ) AS s
Pivot(Sum(c) FOR t IN (
            [tob]
            ,[ecit]
            ,[tobwcom]
            ,[rnorm]
            ,[raddict]
            ,[rpolicy]
            ,[ryouth]
            ,[rhealth]
            ,…
            )) AS p;

枢轴的结果

|    AID    |    tob    |    ecit    |    tobwcom    |    rnorm    |
|-----------|-----------|------------|---------------|-------------|
|    1      |    1      |    NULL    |    NULL       |    0        |
|    2      |    1      |    NULL    |    NULL       |    1        |
|    3      |    1      |    NULL    |    NULL       |    0        |
|    4      |    1      |    NULL    |    NULL       |    0        |
|    5      |    1      |    NULL    |    NULL       |    0        |
|    6      |    1      |    NULL    |    NULL       |    1        |

这是源表categories和一些示例数据:

CREATE TABLE categories(
   ArticleID   INTEGER  NOT NULL  
  ,ThemeID     INTEGER  NOT NULL
  ,ThemeName   VARCHAR(7) NOT NULL
  ,Code        BIT  NOT NULL
  ,CreatedTime VARCHAR(7) NOT NULL
);
INSERT INTO categories(ArticleID,ThemeID,ThemeName,Code,CreatedTime) VALUES (1,1,'tob',1,'57:30.7');
INSERT INTO categories(ArticleID,ThemeID,ThemeName,Code,CreatedTime) VALUES (1,2,'ecig',1,'03:58.3');
INSERT INTO categories(ArticleID,ThemeID,ThemeName,Code,CreatedTime) VALUES (1,5,'rnorm',0,'42:56.5');
INSERT INTO categories(ArticleID,ThemeID,ThemeName,Code,CreatedTime) VALUES (2,1,'tob',1,'57:30.7');
INSERT INTO categories(ArticleID,ThemeID,ThemeName,Code,CreatedTime) VALUES (2,2,'ecig',0,'03:58.3');
INSERT INTO categories(ArticleID,ThemeID,ThemeName,Code,CreatedTime) VALUES (2,5,'rnorm',1,'42:56.5');
INSERT INTO categories(ArticleID,ThemeID,ThemeName,Code,CreatedTime) VALUES (2,6,'raddict',0,'42:59.8');
INSERT INTO categories(ArticleID,ThemeID,ThemeName,Code,CreatedTime) VALUES (3,1,'tob',1,'57:30.7');
INSERT INTO categories(ArticleID,ThemeID,ThemeName,Code,CreatedTime) VALUES (3,2,'ecig',0,'03:58.3');
INSERT INTO categories(ArticleID,ThemeID,ThemeName,Code,CreatedTime) VALUES (3,5,'rnorm',0,'42:56.5');
INSERT INTO categories(ArticleID,ThemeID,ThemeName,Code,CreatedTime) VALUES (21,1,'tob',1,'57:30.7');
INSERT INTO categories(ArticleID,ThemeID,ThemeName,Code,CreatedTime) VALUES (21,2,'ecig',0,'03:58.3');
INSERT INTO categories(ArticleID,ThemeID,ThemeName,Code,CreatedTime) VALUES (21,5,'rnorm',0,'42:56.5');
INSERT INTO categories(ArticleID,ThemeID,ThemeName,Code,CreatedTime) VALUES (21,6,'raddict',0,'42:59.8');

这是包含类别名称的表格–(目前为mytable)

CREATE TABLE mytable(
   CatID       INTEGER  NOT NULL PRIMARY KEY 
  ,CatName     VARCHAR(7) NOT NULL
  ,CreatedTime DATETIME  NOT NULL
);
INSERT INTO mytable(CatID,CatName,CreatedTime) VALUES (1,'tob','2015-03-12 10:07:54.173');
INSERT INTO mytable(CatID,CatName,CreatedTime) VALUES (2,'ecig','2015-05-18 11:48:16.297');
INSERT INTO mytable(CatID,CatName,CreatedTime) VALUES (4,'tobwcom','2015-06-19 11:12:01.537');
INSERT INTO mytable(CatID,CatName,CreatedTime) VALUES (5,'rnorm','2015-06-22 14:24:02.317');
INSERT INTO mytable(CatID,CatName,CreatedTime) VALUES (6,'raddict','2015-06-22 14:24:13.957');
INSERT INTO mytable(CatID,CatName,CreatedTime) VALUES (7,'ecit','2015-06-22 14:26:18.437');

我需要的是一种执行枢纽功能的方法,以在categories中查找不存在的数据。输出将类似于:

|    AID    |    tob    |    ecit    |    tobwcom    |    rnorm    |
|-----------|-----------|------------|---------------|-------------|
|    1      |    1      |    NULL    |    NULL       |    0        |
|    2      |    1      |    NULL    |    NULL       |    1        |

或者没有任何值的AIDCatName的列表。如:

| AID | CatName |
|-----|---------|
| 1   | ecit    |
| 1   | tobwcom |
| 2   | ecit    |
| 2   | tobwcom |

我尝试过

select distinct(AID) FROM [categories]
 where [CatName] not in ( 'ecit', 'tobwcom')

但是从结果来看,数字似乎并没有加起来,但是这对我来说可能是个错误。

1 个答案:

答案 0 :(得分:1)

不确定这样的大表是否足够快。但是对于第二个预期结果,这可能有助于找到失踪人员。

select a.ArticleID, c.CatName
from #myarticles a
cross join #mycategories c
left join categories ca on (ca.ArticleID = a.ArticleID and ca.ThemeID = c.CatID)
where ca.ArticleID is null;

可以找到测试here

请注意,此方法受益于(ArticleID,ThemeID)上的组合主键索引

或者,可以将带有NULL校验的LEFT JOIN更改为NOT EXISTS

select a.ArticleID, c.CatName
from #myarticles a
join #mycategories c on c.CatID between 1 and 7
where NOT EXISTS
(
   select 1
   from categories ca 
   where ca.ArticleID = a.ArticleID 
     and ca.ThemeID = c.CatID
);