1

我是复杂 SQL 语句的新手,需要帮助。我已经设法显示了一半的结果集并努力剩余。

所需结果:

PSU |  pregnancy number| pregnancy percentage | % of live birth out of the pregnancy number.
1   |          2       |          67%         |         40%
2   |          1       |          33%         |         80%

使用下面编写的 sql,我设法获得了前 3 列。

第 2 列和第 3 列需要 where 语句fieldname = 'Q111'。而第 4 列需要带有 where 语句的百分比fieldname = 'Q112。如何将不同的 where 语句组合在一起。

SELECT sms_psu.name1 AS PSU,
       woman_data.answertext  AS NumberOfPreg,
       Count(woman_data.answertext) / (SELECT Count(woman_data.answertext)
                                       FROM   woman_data
                                              INNER JOIN sms_household
                                                      ON woman_data.prim_key =
                                                         ms_household.hhid
                                              INNER JOIN sms_psu
                                                      ON sms_psu.psu =
                                                         sms_household.psu
                                       WHERE  sms_psu.state = 19
                                              AND sms_psu.district = 1
                                              AND sms_psu.psu = 2
                                              AND fieldname = 'Q139') AS
       totalcpuntpreg
FROM   woman_data
       INNER JOIN sms_household
               ON woman_data.prim_key = sms_household.hhid
       INNER JOIN sms_psu
               ON sms_psu.psu = sms_household.psu
WHERE  sms_psu.state = 19
       AND sms_psu.district = 1
       AND sms_psu.psu = 2
       AND fieldname = 'Q111'
GROUP  BY woman_data.answertext,
          sms_household.psu  

戈登,你的建议对我帮助很大。我相应地更新了我的查询。我现在写的问题是我为 Q112 增加了一行

电源 | 妊娠数| 怀孕百分比| 妊娠数中活产的百分比。
1 | 2 | 67% | 40%
2 | 1 | 33% | 80%
2 | 1 | 0% | 20%(第 112 季度的第三行是额外的)

但是我在分组时不需要 Q112 条目。我可以在 case 语句和 main where 语句中分别使用 group by 吗?我尝试将“有”与 group by 一起使用,但得到了预期的结果。

4

3 回答 3

1

也许是这样的:

SELECT sms_psu.name1 AS PSU,
       woman_data.answertext  AS NumberOfPreg,
       SUM(fieldname = 'Q111') / SUM(fieldname = 'Q139') AS pregnancyPercentage,
       SUM(fieldname = 'Q112') / SUM(fieldname = 'Q139') AS liveBirthPercentage
FROM   woman_data
       INNER JOIN sms_household
               ON woman_data.prim_key = sms_household.hhid
       INNER JOIN sms_psu
               ON sms_psu.psu = sms_household.psu
WHERE  sms_psu.state = 19
       AND sms_psu.district = 1
       AND sms_psu.psu = 2
GROUP  BY woman_data.answertext,
          sms_household.psu  
HAVING SUM(fieldname = 'Q111') > 0

该查询首先检索具有所有fieldname值的行,并将它们与以前相同的分组。计数时,仅根据需要计算特定fieldname条目(使用SUM(condition)技巧)。为了确保输出仅包含具有 的行的组,添加fieldname = 'Q111'SUM(fieldname = 'Q111') > 0条件(作为 HAVING 子句)。

于 2012-08-21T10:27:44.233 回答
0

您的问题的答案是:CASE 声明。

这个想法是扩展外部 WHERE 子句以包含您关心的所有行。然后,在 SELECT 子句中使用 CASE 语句在单个变量的基础上过滤行。

我试图把它放在一起:

SELECT sms_psu.name1 AS PSU,
       woman_data.answertext AS NumberOfPreg,
       Count(case when sms_psu.state = 19 AND sms_psu.district = 1 AND sms_psu.psu = 2 AND fieldname = 'Q111'
                  then woman_data.answertext
             end) /
       (SELECT Count(woman_data.answertext)
        FROM woman_data INNER JOIN
              sms_household
              ON woman_data.prim_key = ms_household.hhid INNER JOIN
              sms_psu
              ON sms_psu.psu = sms_household.psu
        WHERE  sms_psu.state = 19 AND sms_psu.district = 1 AND sms_psu.psu = 2 AND
               fieldname = 'Q139'
       ) AS totalcpuntpreg,
       Count(case when sms_psu.state = 19 AND sms_psu.district = 1 AND sms_psu.psu = 2 AND fieldname = 'Q112'
                  then woman_data.answertext
             end) /
       (SELECT Count(woman_data.answertext)
        FROM woman_data INNER JOIN
              sms_household
              ON woman_data.prim_key = ms_household.hhid INNER JOIN
              sms_psu
              ON sms_psu.psu = sms_household.psu
        WHERE  sms_psu.state = 19 AND sms_psu.district = 1 AND sms_psu.psu = 2 AND
               fieldname = 'Q139'
       ) AS totalcpuntpreg

FROM woman_data INNER JOIN
     sms_household
     ON woman_data.prim_key = sms_household.hhid INNER JOIN
     sms_psu
     ON sms_psu.psu = sms_household.psu
WHERE sms_psu.state = 19 AND sms_psu.district = 1 AND sms_psu.psu = 2 AND
      fieldname in ('Q111', 'Q112')
GROUP BY woman_data.answertext
        sms_household.psu  

但是,我可能错过了一些细节。

于 2012-08-17T13:27:02.717 回答
0

正如您所提到的,您的查询很复杂。不熟悉您的架构的人很难理解它。因此,我将为汇总多个项目的查询提供一般性建议。

使用一个体面的客户端程序,让您保存查询文本。如果它还可以漂亮地打印(格式化)您的查询文本,那将很有帮助。

分别开发和测试每个汇总查询。确保每一个都单独生成您需要的正确摘要结果。

然后,将这些查询与报告键(我相信在您的情况下为 PSU)连接在一起,以便生成多个摘要结果。确保它正在工作。

将查询名称和版本号放在 SELECT 语句之后的注释中:例如,

SELECT /* pregnancy outcomes 1.1 */
       sms_psu.name1 AS PSU,
       woman_data.answertext AS NumberOfPreg, etc.

这将让您使用 MySQLSHOW FULL PROCESSLIST来确定正在运行的确切查询。

最后,如果结果查询(由许多嵌套的子查询组成)对于您的应用程序的要求来说太慢,请对其进行优化。

如果您遵循这些步骤,我相信您的下一步就是开发您的% live birth摘要查询。

于 2012-08-17T12:29:03.890 回答