GROUP BY和总金额

时间:2014-04-08 22:25:11

标签: mysql sql

我有以下数据库表:

+----------+----------+---------+-----------+------------+-----------+
| animalID | foodtype | rAmount | rAmtUnits | rFrequency | rFreqUnit |
+----------+----------+---------+-----------+------------+-----------+
| 3498     | BANA     |       3 | lbs.      |          1 | day       |
| 3455     | NUTS     |      10 | lbs.      |          1 | day       |
| 3455     | VEGG     |      20 | lbs.      |          1 | day       |
| 3450     | NUTS     |      10 | lbs.      |          1 | day       |
| 3450     | VEGG     |      20 | lbs.      |          1 | day       |
| 3450     | HAY      |     100 | lbs.      |          1 | day       |
| 3452     | HAY      |     100 | lbs.      |          1 | day       |
| 3452     | NUTS     |      10 | lbs.      |          1 | day       |
| 3452     | VEGG     |      20 | lbs.      |          1 | day       |
| 9490     | BEFR     |       2 | lbs.      |          1 | day       |
| 2899     | BEFR     |       2 | lbs.      |          1 | day       |
| 8393     | ANTS     |      10 | ltr.      |          1 | day       |
| 8899     | EUCL     |       5 | lbs.      |          1 | day       |
| 4883     | FISH     |       4 | lbs.      |          1 | day       |
| 7838     | MICE     |       3 | ea.       |          1 | wk.       |
| 7838     | RATS     |       1 | ea.       |          2 | wk.       |
| 4884     | WPLN     |       3 | lbs.      |          1 | wk.       |
| 1863     | BEFR     |       3 | lbs.      |          1 | day       |
| 2847     | EUCL     |       3 | lbs.      |          1 | day       |
| 4336     | BANA     |       2 | lbs.      |          1 | day       |
| 4336     | NUTS     |       1 | lbs.      |          1 | day       |
| 4336     | CHWM     |       2 | cups      |          1 | day       |
| 2856     | CHWM     |       2 | cups      |          1 | day       |
| 2856     | BANA     |       1 | lbs.      |          1 | day       |
| 2856     | NUTS     |       2 | lbs.      |          1 | day       |
| 8483     | NUTS     |       2 | cups      |          1 | day       |
| 8483     | SEED     |       2 | cups      |          1 | day       |
+----------+----------+---------+-----------+------------+-----------+

我想列出一周所需的食物量和数量。我有以下查询:

SELECT foodtype,
CASE 
    WHEN rFrequency=1 AND rFreqUnit='day' THEN rAmount*7 
    WHEN rFrequency=2 AND rFreqUnit='wk.' THEN rAmount/2 
    ELSE rAmount END AS rAmount, rAmtUnits 
FROM Rations GROUP BY foodtype;

但是对于GROUP BY,rAmount列没有总结,大多数值都消失了。我正在寻找一种方法,以便GROUP BY将总结没有GROUP BY的所有值。有什么简单的方法吗?

2 个答案:

答案 0 :(得分:0)

怎么样:

FROM Rations WHERE foodtype = FoodType

把它放在一个函数(例如javascript,php ...)中,你将 FoodType 作为参数发送

答案 1 :(得分:0)

看起来你想要包装在SUM聚合中返回rAmount的表达式。

看起来你不想一起添加“磅”和“盎司”,所以看起来你也想要“GROUP BY”rAmtUnits。

根据表格中的数据,只要rFreqUnit为白天,您就会想要乘以7。在rFrequency被设置为2的情况下,我不清楚你想要除以2,看起来如果你需要每周两次,你需要两次rAmount。如果您每周需要"N"项内容,则需要在总数中加入"N * rAmount"。同样,如果您每天需要3件事,那么您希望在总数中加入"3 * rAmount * 7"

我想你想要这样的东西:

 SELECT r.foodtype
      , SUM(r.rAmount * r.rFrequency * IF(r.rFreqUnit='day',7,1)
        ) AS rAmount
      , r.rAmtUnits
   FROM Rations r
  GROUP BY r.foodtype, r.rAmtUnits

与表达式"IF(r.rFreqUnit='day',7,1)"等效的ANSI标准将是:

CASE r.rFreqUnit WHEN 'day' THEN 7 ELSE 1 END