如何在Microsoft SQL Server中显示空行?

时间:2018-11-29 22:45:14

标签: sql sql-server

我有这个查询,在这里我需要知道特定列的每个状态的记录数。我需要零记录的状态来显示我的结果。这是我的查询,它仅显示具有记录的状态。

SELECT DISTINCT
    StateAbbr, count (*)
FROM
    DataWarehouse
WHERE 
    FiscalYear = 2017
    AND MaltreatmentSetCode != '999' 
    AND MaltreatmentSetCode != '' 
    AND StateAbbr IN (SELECT DISTINCT StateAbbr
                      FROM DataWarehouse
                      WHERE (NOT MaltreatmentSetCode = '999')
                        AND FiscalYear = 2017)
GROUP BY 
    StateAbbr
ORDER BY 
    StateAbbr ASC

3 个答案:

答案 0 :(得分:1)

您将需要从“状态”表中进行选择以获取状态列表,然后加入您的计数以获取总数,并在数据仓库没有返回数据时使用合并替换0。下面是一个简单的示例:

create table #state (state varchar(1))

create table #datawarehouse (state varchar(1), data int)


insert into #state 
    values   ('a')
            ,('b')
            ,('c')
            ,('d')

insert into #datawarehouse(state, data)
    values ('a', 1)
            ,('a',2)
            ,('a',3)
            ,('b',1)

SELECT State.State, COALESCE(DataWarehouse.c, 0)
FROM   #State state
         LEFT OUTER JOIN (  SELECT  State, Count(*) c
                            FROM    #DataWareHouse datawarehouse
                            GROUP BY State) Datawarehouse
              ON   State.State = DataWareHouse.State

答案 1 :(得分:0)

如果我们要返回的 Column 1 Column2 Column3 Processing Test CPI WCR Key Comp DD|FF Interp Set ( If both keywords are present then select this row) 的所有值都出现在String newStr = str.replaceAll("\\?", "-"); 表的行中,则可以使用条件聚合。

作为演示:

StateAbbr

请注意,对于每一行,将对CASE表达式进行计算,返回0或1,并且SUM()聚合将1和0加起来以获得计数。

如果DataWarehouse表中没有应该返回的SELECT dw.StateAbbr , SUM(CASE WHEN dw.FiscalYear = 2017 AND dw.MaltreatmentSetCode != '999' AND dw.MaltreatmentSetCode != '' THEN 1 ELSE 0 END) AS cnt FROM DataWarehouse dw GROUP BY dw.StateAbbr ORDER BY dw.StateAbbr ASC 值,那么我们需要一个用于这些值的行源。 (通常,这些值在数据集市的“维度”表中。)

假设StateAbbrDateWarehouse表中是唯一的...如果此查询返回了唯一的abbr状态列表,我们需要返回:

Abbr

然后我们可以做这样的事情:

dwstate

这些示例省略了有关子查询的位; (对我而言)尚不清楚该部分应满足规范的哪一部分。

SELECT s.Abbr
  FROM dwstate s
ORDER BY s.Abbr  

如果该子查询旨在返回我们应该返回的StateAbbr集,则可以将其用作行源来代替SELECT s.Abbr AS StateAbbr , SUM(CASE WHEN dw.FiscalYear = 2017 AND dw.MaltreatmentSetCode != '999' AND dw.MaltreatmentSetCode != '' THEN 1 ELSE 0 END) AS cnt FROM dwstate s LEFT JOIN DataWarehouse dw ON dw.StateAbbr = s.Abbr GROUP BY s.Abbr ORDER BY s.Abbr ASC ...

    SELECT DISTINCT StateAbbr
      FROM DataWarehouse
     WHERE (NOT MaltreatmentSetCode = '999' )
       AND FiscalYear = 2017

答案 2 :(得分:0)

创建所有状态的列表,然后将数据加入其中。计数。

extraerCalle(resultado: IResult): string | undefined {
    const route = resultado.address_components.find(ac => !!ac.types.find(type => type === 'route'));
    return route && route.long_name;
  }

  extraerNro(resultado: IResult): string | undefined {
    const streetNumber = resultado.address_components.find(ac => !!ac.types.find(type => type === 'street_number'));
    return streetNumber && streetNumber.long_name;
  }

  extraerCalleNro(resultado: IResult): string {
    const calle = this.extraerCalle(resultado);
    const nro = this.extraerNro(resultado);
    return (calle || '') + (calle && nro ? ` ${nro}` : '');
  }