3

過去7日間の売上データを返すクエリがあります。過去30日間の売上も取得する方法(過去7日間と過去30日間の売上を結果で確認するには)?

SELECT 
  a.row_id,
  MAX(ad.new_value) - MIN(ad.new_value) AS sales7days 
FROM
  _audit a 
  LEFT JOIN _audit_data ad 
    ON a.audit_id = ad.audit_id 
WHERE ad.col = 'sales' 
  AND a.triggered_datetime > NOW() - INTERVAL 7 DAY 
GROUP BY a.row_id 
ORDER BY sales7days DESC;
4

2 に答える 2

2

おそらくCASE式で:

SELECT a.row_id
   ,  MAX(case when a.triggered_datetime > NOW() - INTERVAL 7 DAY 
               then ad.new_value else NULL end)
    - MIN(case when a.triggered_datetime > NOW() - INTERVAL 7 DAY
               then ad.new_value else NULL end) AS sales7days 
   ,  MAX(case when a.triggered_datetime > NOW() - INTERVAL 30 DAY 
               then ad.new_value else NULL end)
    - MIN(case when a.triggered_datetime > NOW() - INTERVAL 30 DAY
               then ad.new_value else NULL end) AS sales30days 
FROM  _audit a, _audit_data ad 
WHERE a.audit_id = ad.audit_id AND ad.col = 'sales' 
GROUP BY a.row_id;
于 2012-12-31T20:38:30.343 に答える
1
SELECT 
  d7.row_id,
  d7.salesdays, d30.salesdays
FROM
(
  Select a.row_id, MAX(ad.new_value) - MIN(ad.new_value) AS salesdays
  From _audit a 
  LEFT JOIN _audit_data ad ON a.audit_id = ad.audit_id 
WHERE ad.col = 'sales' AND a.triggered_datetime > NOW() - INTERVAL 7 DAY 
GROUP BY a.row_id 
) d7,
(
  Select a.row_id, MAX(ad.new_value) - MIN(ad.new_value) AS salesdays
  From       _audit a 
  LEFT JOIN _audit_data ad ON a.audit_id = ad.audit_id 
WHERE ad.col = 'sales' AND a.triggered_datetime > NOW() - INTERVAL 30 DAY 
GROUP BY a.row_id 
) d30

where d7.row_id = d30.row_id
ORDER BY sales7days DESC;

両方に同じ行IDが必要であり、どちらかの値が表示されると仮定すると、値フィールドを内部または外部に結合したり、値フィールドを結合したりする場合としない場合があります(データについて十分に理解していません)。

于 2012-12-31T20:51:34.297 に答える