SQL SERVER 2012中GROUP BY的输出

时间:2016-09-05 12:32:38

标签: sql sql-server

我有以下代码。

select S.BUNDLE_ID,
       S.COUNTRY, 
       SUM(CONVERT(float,LV.VALUE_0)) AS BUNDLE_SUM 
from   PM_SCOPE S
JOIN   PM_LOOKUP_VALUE LV
    ON LV.SCOPE_ID = S.SCOPE_ID
JOIN   PM_LOOKUP_DEFINITION LD
    ON LD.LOOKUP_DEFINITION_ID = LV.LOOKUP_DEFINITION_ID
WHERE  LD.LOOKUP_NAME = 'Competitor Bundle Details - new'
  AND  convert(nvarchar, LV.EFFECTIVE_END_TIME, 112) > convert(nvarchar, GETDATE(), 112) 
  AND  S.BUNDLE_ID NOT IN (
           select distinct BUNDLE_ID 
           FROM   PM_SCOPE S
           JOIN   PM_LOOKUP_VALUE LV
               ON LV.SCOPE_ID = S.SCOPE_ID
           JOIN   PM_LOOKUP_DEFINITION LD
               ON LD.LOOKUP_DEFINITION_ID = LV.LOOKUP_DEFINITION_ID 
              AND LD.LOOKUP_NAME = 'Competitor Bundle Details - new'
           where  convert(float, LV.VALUE_0) = 0
              AND convert(nvarchar, LV.EFFECTIVE_END_TIME, 112) 
                            > convert(nvarchar, GETDATE(), 112)
           )
GROUP BY S.BUNDLE_ID, S.COUNTRY

此查询会忽略BUNDLE_IDPM_LOOKUP_VALUEVALUE_0 = 0的所有VALUE_0。这里的问题是,即使对于任何一个国家VALUE_0为0,查询也将忽略所有其他国家的总和的计算。

我是否可以通过某种方式在查询中添加一个子句,以便Country BUNDLE_ID VALUE_0 US BUNDLE1 10 UK BUNDLE1 20 Poland BUNDLE1 0 US BUNDLE2 35 UK BUNDLE2 30 Poland BUNDLE2 10 为0时忽略该特定国家/地区的计算?

BUNDLE1

如果我们查看上面的示例,我希望我的查询仅忽略VALUE_0,因为其BUNDLE1为0并且仍向我显示美国和英国BUNDLE_ID的结果。

group by包含一些组件,这些组件是我需要执行let fooWithDefault = let cfgType = Assembly.GetExecutingAssembly().GetTypes() |> Seq.find(fun t -> t.Name = "MyModule") let propOpt = cfgType.GetProperties() |> Seq.tryFind( fun p -> p.Name = "foo") match propOpt with | Some foo -> foo.GetValue(null).ToString() | None -> "My Default Value for f" 并将其相加的原因。

另外一个例子:

Country BUNDLE_ID Bundle_Component VALUE_0 US BUNDLE1 A 10 US BUNDLE1 B 15 UK BUNDLE1 A 5 UK BUNDLE1 B 20 Poland BUNDLE1 A 10 Poland BUNDLE1 B 0 Poland BUNDLE1 C 30

Bundle_Component也是PM_SCOPE表中的列名。所以在这里我想要波兰的BUNDLE1总和不应该发生,因为B的价格为0但是对于美国和英国,应该计算总和。

4 个答案:

答案 0 :(得分:0)

您要按BUNDLE_IDCOUNTRY进行分组吗?如果是,HAVING应该有所帮助:

SELECT S.BUNDLE_ID, S.COUNTRY, SUM(CONVERT(float,LV.VALUE_0)) AS BUNDLE_SUM 
FROM PM_SCOPE S
    JOIN PM_LOOKUP_VALUE LV ON LV.SCOPE_ID = S.SCOPE_ID
    JOIN PM_LOOKUP_DEFINITION LD ON LD.LOOKUP_DEFINITION_ID = LV.LOOKUP_DEFINITION_ID
WHERE LD.LOOKUP_NAME = 'Competitor Bundle Details - new' 
(...)
GROUP BY S.BUNDLE_ID, S.COUNTRY
HAVING SUM(CONVERT(float,LV.VALUE_0)) > 0

答案 1 :(得分:0)

看起来BUNDLE_ID上的条件应该替换为不同的条件。也就是说,这里不需要整个使用子查询。您可以将零测试置于连接条件中:

select   S.BUNDLE_ID,
         S.COUNTRY, 
         SUM(CONVERT(float,LV.VALUE_0)) AS BUNDLE_SUM 
from     PM_SCOPE S
JOIN     PM_LOOKUP_VALUE LV
      ON LV.SCOPE_ID = S.SCOPE_ID
     AND COALESCE(convert(float, LV.VALUE_0), 0) <> 0
JOIN     PM_LOOKUP_DEFINITION LD
      ON LD.LOOKUP_DEFINITION_ID = LV.LOOKUP_DEFINITION_ID
WHERE    LD.LOOKUP_NAME = 'Competitor Bundle Details - new'
  AND    convert(nvarchar, LV.EFFECTIVE_END_TIME, 112) > convert(nvarchar, GETDATE(), 112) 
GROUP BY S.BUNDLE_ID, 
         S.COUNTRY

请注意从where子句和附加行中删除子查询条件:

     AND COALESCE(convert(float, LV.VALUE_0), 0) <> 0

使用having的解决方案也可以使用,但前提是您的值保证为非负值。

答案 2 :(得分:0)

希望这有帮助

    select * from ( select S.BUNDLE_ID,
   S.COUNTRY, 
   SUM(CONVERT(float,LV.VALUE_0)) AS BUNDLE_SUM 
from   PM_SCOPE S
JOIN   PM_LOOKUP_VALUE LV
ON LV.SCOPE_ID = S.SCOPE_ID
JOIN   PM_LOOKUP_DEFINITION LD
ON LD.LOOKUP_DEFINITION_ID = LV.LOOKUP_DEFINITION_ID
WHERE  LD.LOOKUP_NAME = 'Competitor Bundle Details - new'
AND  convert(nvarchar, LV.EFFECTIVE_END_TIME, 112) > convert(nvarchar, GETDATE(), 112) 
AND  S.BUNDLE_ID NOT IN (
       select distinct BUNDLE_ID 
       FROM   PM_SCOPE S
       JOIN   PM_LOOKUP_VALUE LV
           ON LV.SCOPE_ID = S.SCOPE_ID
       JOIN   PM_LOOKUP_DEFINITION LD
           ON LD.LOOKUP_DEFINITION_ID = LV.LOOKUP_DEFINITION_ID 
          AND LD.LOOKUP_NAME = 'Competitor Bundle Details - new'
       where  convert(float, LV.VALUE_0) = 0
          AND convert(nvarchar, LV.EFFECTIVE_END_TIME, 112) 
                        > convert(nvarchar, GETDATE(), 112)
       )
GROUP BY S.BUNDLE_ID, S.COUNTRY) a where a.BUNDLE_SUM !=0

答案 3 :(得分:0)

GROUP BY S.BUNDLE_ID,S.COUNTRY 具有SUM(CONVERT(float,LV.VALUE_0))&lt;&gt; 0