使用列名作为参数并在WHERE / HAVING子句中使用输入参数

时间:2016-12-11 09:28:26

标签: sql sql-server

我试图根据布尔字段获取项目的编号。布尔字段是在运行时期间作为参数传递的动态字段(如此post中所示)。

编辑:终于解决了。刚刚将WHEN行更改为:require 'rake' task :calculate_averages => :environment do products = Product.all products.each do |product| puts "Calculating average rating for #{product.name}..." product.update_attribute(:average_rating, product.reviews.average("rating")) end end 。谢谢@FDavidov!

WHEN 'SI_A'   THEN COUNT(CASE WHEN SI_A=1 THEN 1 END)

编辑:忘记包括完整的表格。我尝试使用UNPIVOT,但我收到了错误:' OrganismID在u'中多次指定。但是这个没有将列名称作为参数传递:

    SELECT
        OrganismName
        , MONTH(DateDiscovered) AS 'Month'
        , CASE @Column
            WHEN 'SI_A' THEN COUNT(CASE WHEN SI_A=1 THEN 1 END)
            WHEN 'SI_B' THEN COUNT(CASE WHEN SI_B=1 THEN 1 END)
            WHEN 'SI_C' THEN COUNT(CASE WHEN SI_C=1 THEN 1 END)
            WHEN 'SI_D' THEN COUNT(CASE WHEN SI_D=1 THEN 1 END)
        END AS 'Cases'
    FROM tblFindings
    INNER JOIN
        tblOrganism
    ON
        tblFindings.OrganismID = tblOrganism.OrganismID
    WHERE
        (SELECT 
            CASE @Column
            WHEN 'SI_A' THEN COUNT(CASE WHEN SI_A=1 THEN 1 END)
            WHEN 'SI_B' THEN COUNT(CASE WHEN SI_B=1 THEN 1 END)
            WHEN 'SI_C' THEN COUNT(CASE WHEN SI_C=1 THEN 1 END)
            WHEN 'SI_D' THEN COUNT(CASE WHEN SI_D=1 THEN 1 END)
        END AS 'Cases'
        FROM tblFindings
        INNER JOIN
            tblOrganism
        ON
            tblFindings.OrganismID = tblOrganism.OrganismID
        ) > 0
    GROUP BY
        OrganismName
        , MONTH(DateDiscovered)
    ORDER BY
        OrganismName
        , MONTH(DateDiscovered)

Table structure/sample data

使用此code,我还获得总计为0的行。 这是我试图实现的结果:

SELECT
    OrganismName
    , MONTH(DateDiscovered) AS 'Month'
    , u.Cases
    , u.Sites
FROM tblFindings
INNER JOIN tblOrganism ON tblFindings.OrganismID = tblOrganism.OrganismID
UNPIVOT (
    Cases FOR Sites in (SI_A, SI_B, SI_C, SI_D) 
    ) u
WHERE u.Cases > 0

相反,我得到了这个(如果我提供" SI_A"作为参数):

=========================
|OrganismName|Month|SI_A|
=========================
|nota        |2    |2   |
|something   |10   |1   |
|woo         |2    |1   |
=========================

=========================
|OrganismName|Month|SI_B|
=========================
|something   |4    |1   |
|something   |10   |1   |
=========================

=========================
|OrganismName|Month|SI_C|
=========================
|something   |10   |2   |
=========================

=========================
|OrganismName|Month|SI_D|
=========================
|something   |10   |1   |
=========================

我使用了WHERE子句但得到了上面的结果。然后我尝试了一个HAVING子句,但我得到了相同的结果。除了做简单的查询之外,我对SQL不太熟悉,但我怎么能够实现这个呢?

1 个答案:

答案 0 :(得分:0)

我不确定您的展示结果中反映了什么,但这是一个明显错误的暗示(从这里您可以解决您的问题):

SELECT 
   OrganismName 
   MONTH(DateDiscovered) AS 'Month' 
 , CASE @Column 
      WHEN 'SI_A'   THEN SUM(CASE WHEN SI_A=1 THEN 1 ELSE 0 END) 
      WHEN 'SI_B'   THEN SUM(CASE WHEN SI_B=1 THEN 1 ELSE 0 END) 
      WHEN 'SI_C'   THEN SUM(CASE WHEN SI_C=1 THEN 1 ELSE 0 END) 
      WHEN 'SI_D'   THEN SUM(CASE WHEN SI_D=1 THEN 1 ELSE 0 END) 
      ELSE NULL 
 END AS 'Cases' 
FROM tblFindings 
....

您正在检查(例如)SI_A 1是否1添加0否则SI_A = 2。现在,如果0会发生什么?您还要添加... WHEN 'SI_A' THEN SUM(CASE WHEN SI_A > 0 THEN SI_A ELSE 0 END) ...

由于我不太清楚你是否<0> > 所有值或 COUNT 你有多少个案例值&gt; 0,我在这里发布两个案例:

对于SUM:

... WHEN 'SI_A' THEN SUM(SI_A) ...

0

(第二个是等效的,假设您只获得0或数字> ... WHEN 'SI_A' THEN SUM(CASE WHEN SI_A > 0 THEN 1 ELSE 0 END) ... 。)

对于COUNT:

SELECT 
   OrganismName 
   MONTH(DateDiscovered) AS 'Month' 
 , CASE @Column 
      WHEN 'SI_A'   THEN SUM(CASE WHEN SI_A > 0 THEN 1 ELSE 0 END) 
      WHEN 'SI_B'   THEN SUM(CASE WHEN SI_B > 0 THEN 1 ELSE 0 END) 
      WHEN 'SI_C'   THEN SUM(CASE WHEN SI_C > 0 THEN 1 ELSE 0 END) 
      WHEN 'SI_D'   THEN SUM(CASE WHEN SI_D > 0 THEN 1 ELSE 0 END) 
      ELSE NULL 
 END AS @Column 
FROM tblFindings

希望这是你正在寻找的解决方案。

<强>更新

以下是一个简单的选择:

{{1}}

我想这会做到。