0

我有以下表格:
http ://sqlfiddle.com/#!2/1e991/1

但是我有问题!我的查询的输出是:

DATE                        SLOT    sum(SUCCESSFUL) SUCCESSFUL      PERCENTAGE  OTA_NAME     USERS_SI
July, 02 2013 00:00:00+0000 2       120             120             41.6667     campana 2      50
July, 02 2013 00:00:00+0000 1       200             200             25          campana 2      50
July, 02 2013 00:00:00+0000 1       150             150             53.3333     campana 3      80
July, 01 2013 00:00:00+0000 2       100             100             20          campana 1      20
July, 01 2013 00:00:00+0000 3       440             440             4.5455      campana 1      20
July, 01 2013 00:00:00+0000 1       700             700             2.8571      campana 1      20

我需要一个相同日期和活动的成功总和,例如,我在 7 月 1 日有活动 1 三行,那么我需要将三行的成功相加,而活动 2 有两行,那么我需要总和两行的成功,并且与活动 3 为一行,那么我需要将同一行的成功相加。最后百分比是 sum(SUCCESSFUL) 和 USERS_SI 之间的除法

我需要的输出是:

DATE                        SLOT    sum(SUCCESSFUL) SUCCESSFUL  PERCENTAGE  OTA_NAME     USERS_SI
July, 02 2013 00:00:00+0000 2       320             120             41.6667     campana 2      50
July, 02 2013 00:00:00+0000 1       320             200             25          campana 2      50
July, 02 2013 00:00:00+0000 1       150             150             53.3333     campana 3      80
July, 01 2013 00:00:00+0000 2       1240            100             20          campana 1      20
July, 01 2013 00:00:00+0000 3       1240            440             4.5455      campana 1      20
July, 01 2013 00:00:00+0000 1       1240            700             2.8571      campana 1      20


Can you help me?
4

2 回答 2

1

sqlfiddle在这里

SELECT other.date AS date,
  other.slot,
  other.successful,
  calc.sum_successful,
  max((case when (rule.tree_si = dms.tree) then dms.numberResponses end))/successful AS percentage, 
  other.name AS ota_name,
  other.successful AS successful,
  max((case when (rule.tree_si = dms.tree) then dms.numberResponses end)) AS users_si
FROM  aca_ota_other other 
join aca_dms_rules rule on other.name = rule.ota_name
join aca_dms dms on dms.date = other.date and rule.tree_si = dms.tree
join (SELECT name, sum(successful) as sum_successful FROM aca_ota_other GROUP BY name) as calc on other.name = calc.name
GROUP BY 
  other.date,
  other.name,
  other.successful
ORDER BY other.date desc
于 2013-08-15T21:07:46.917 回答
0

我不确定,但如果我理解得很好,你需要这样的东西:对不起,我对你的查询做了一些小改动。

SELECT other.date date,
  other.slot,
  other.name ota_name,
  sum(successful) successful,
  dms.numberResponses users_si,
  SUM(successful)/dms.numberResponses AS percentage
FROM aca_ota_other other 
JOIN aca_dms_rules rule ON other.name=rule.ota_name
JOIN aca_dms dms ON dms.date=other.date AND rule.tree_si=dms.tree     
GROUP BY other.date,
  other.name
ORDER BY other.date DESC

http://sqlfiddle.com/#!2/1e991/22

于 2013-08-15T21:16:23.037 回答