-2

诚然,我已经在这里看到过几次这个问题——但所有答案似乎都通过使用 aGROUP BY或 a来解决他们的问题WHERE,所以我很好奇如果你的查询变得太大,那将如何解决这个问题不工作。

例如,我正在写一些东西,它使用两个左连接到我的主表,将重叠带到结果中。由于我对 SQL 还比较陌生,所以我不确定它在做什么——但我知道当我运行计数时,我会得到一千左右的人。我想这是这种情况,因为在我要加入的两个表中,每个人(故意)都有重复的 ID。

我的所有查询都填充了我想为这个项目获得的结果,正在使用COUNT()SUM()待处理列。有没有一种方法可以DISTINCT让我一次只制作一列将我的 ID 视为一个?根据我到目前为止所做的事情,我注意到,无论何时设置DISTINCT它都超出了您试图将其归因于的一列。有什么建议么?将不胜感激!

到目前为止,这是我的代码示例,其中包含重复的 ID:

SELECT
     targeted.person AS "Person",
     targeted.work AS "Occu",
     (COUNT(targeted.id)) AS "Targeted",
     (COALESCE(SUM(targeted.signed="Yes"),0)) AS "Signed",
     (COALESCE(SUM(targeted.signed="Yes"),0))/COUNT(targeted.id)*100 AS "Signed %",
     (COALESCE(COUNT(question.questionid="96766"),0)) AS "Donated",
     (COALESCE(COUNT(question.questionid="96766"),0))/(COALESCE(SUM(targeted.signed="Yes"),0))*100 AS "Donated %",
     (COALESCE(SUM(question.surveyresponsename),0)) AS "Donation $",
     ROUND((COALESCE(SUM(question.surveyresponsename),0))/(COALESCE(COUNT(question.questionid="96766"),0)),2) AS "Avg Donation",
     (CASE WHEN (left(targeted.datesigned,1)="5" AND right(question.datecontacted,2)="13") THEN (COALESCE(SUM(targeted.signed="Yes"),0)) ELSE 0 END) AS "Signed This Month",
     (CASE WHEN (left(question.datecontacted,1)="5" AND right(question.datecontacted,2)="13") THEN (COALESCE(COUNT(question.questionid="96766"),0)) ELSE 0 END) AS "Donated This Month",
     (CASE WHEN question.ContactType="House Visit" THEN COUNT(question.id) ELSE 0 END) AS "At Home",
     (CASE WHEN question.ContactType="Worksite" THEN COUNT(question.id) ELSE 0 END) AS "At Work",
     (CASE WHEN (left(events.day,1)="5" AND right(events.day,2)="13") THEN COUNT(events.id) ELSE 0 END) AS "Events This Month"
      FROM targeted
       LEFT JOIN question ON targeted.id=question.id
       LEFT JOIN events ON targeted.id=events.id
      GROUP BY targeted.person, targeted.work;

以下是表结构的基础知识:

目标:

Field       Type            Null    Key     Default
ID          bigint(11)      YES     Primary NO
Work        varchar(255)    YES             NULL
Person      varchar(255)    YES             NULL
Signed      varchar(255)    YES             NULL
DateSigned  varchar(255)    YES             NULL

问题:

Field               Type            Null    Key     Default
ID                  bigint(11)      YES     Primary NO
QuestionID          int(11)         YES             NULL
SurveyResponseId    int(11)         YES             NULL
SurveyResponseName  varchar(255)    YES             NULL
DateContacted       varchar(255)    YES             NULL
ContactType         varchar(255)    YES             NULL

事件:

Field       Type            Null    Key     Default
ID          bigint(11)      NO      Primary NO
Day         varchar(255)    YES             NULL
EventType   varchar(255)    YES             NULL

结果将类似于:

Person  Occu    Targeted    Signed  Signed % ...
1       Job 1   1413        765     54.14 ...
2       Job 2   111         80      72.072 ...
2       Job 3   931         715     76.7991 ...
3       Job 4   2720        1435    52.7573 ...
4       Job 5   401         218     54.364 ...

谢谢您的帮助!

4

1 回答 1

0

解决这个问题的正确方法是在子查询中进行聚合。要将问题和事件汇总到正确的级别,您需要加入目标表。然后,您将不需要最外层的聚合:

select . . .
from (select t.name, t.work,
             count(t.id) as Targeted,
             . . .
      from targets t
      group by t.name, t.work
     ) t left join
     (select t.name, t.work,
             sum(case when question_id = 96766 then 1 else 0 end) as Donated,
             . . .
      from question q join
           targeted t
           on t.id = t.id
      group by t.name, t.work
    ) q
    on t.name = q.name and t.work = q.work left join
    (select t.name, t.work,
            sum(CASE WHEN (left(events.day,1)="5" AND right(events.day,2)="13") THEN 1 ELSE 0 END
               ) AS "Events This Month"
    from events e join
          targeted t
          on e.id = t.id
    ) e
    on e.name = t.name and e.work = t.work
于 2013-05-10T14:22:13.877 回答