2组数据之间的SQL比较

时间:2018-06-26 08:02:47

标签: mysql sql

如何从2个数据集之间的比较中返回值?得到比较的公式是((May-Apr)/ Apr)* 100%。进行比较的值为UnitMT。比较的值将是这样的结果。对不起,我的英语。

Expected Result

这是我对4月18日的查询,并显示:

SELECT
 'IMPORTED' as IMPORTED,
       (CASE WHEN itmnocate.GradeCategory = 'WHITE RICE' THEN 'WHITE RICE'
       WHEN itmnocate.GradeCategory = 'BASMATHI' THEN 'BASMATHI'
       WHEN itmnocate.GradeCategory = 'FRAGRANT' THEN 'FRAGRANT'
       WHEN itmnocate.GradeCategory = 'GLUTINOUS' THEN 'GLUTINOUS'
             ELSE 'IMPORTED OTHERS'
        END) as Cat,
  sum(tot451.Quantity/1000) AS UnitMT
FROM
  tot451
  INNER JOIN itmnocate ON tot451.Item = itmnocate.ItemNumber
WHERE
 tot451.Date >= '2018-04-01' and tot451.Date <= '2018-04-31'
 and
 tot451.Unit = 'KG'
 and
 tot451.Customeraccount not in ('CT1008','CT1009')
 and itmnocate.Source = 'imported'
group by cat

union all

 SELECT
 'LOCAL' as Local,
       (CASE WHEN itmnocate.GradeCategory = 'SUPER 15' THEN 'SUPER 15'
             ELSE 'LOCAL OTHERS'
        END) as Cat,
  sum(tot451.Quantity/1000) AS UnitMT
FROM
  tot451
  INNER JOIN itmnocate ON tot451.Item = itmnocate.ItemNumber
WHERE
tot451.Date >= '2018-04-01' and tot451.Date <= '2018-04-31'
 and
 tot451.Unit = 'KG'
 and
 tot451.Customeraccount not in ('CT1008','CT1009')
 and itmnocate.Source in ('local','by product')
 group by cat  

Output For Apr18

5月18日:

SELECT
 'IMPORTED' as IMPORTED,
       (CASE WHEN itmnocate.GradeCategory = 'WHITE RICE' THEN 'WHITE RICE'
       WHEN itmnocate.GradeCategory = 'BASMATHI' THEN 'BASMATHI'
       WHEN itmnocate.GradeCategory = 'FRAGRANT' THEN 'FRAGRANT'
       WHEN itmnocate.GradeCategory = 'GLUTINOUS' THEN 'GLUTINOUS'
             ELSE 'IMPORTED OTHERS'
        END) as Cat,
  sum(tot451.Quantity/1000) AS UnitMT
FROM
  tot451
  INNER JOIN itmnocate ON tot451.Item = itmnocate.ItemNumber
WHERE
 tot451.Date >= '2018-05-01' and tot451.Date <= '2018-05-31'
 and
 tot451.Unit = 'KG'
 and
 tot451.Customeraccount not in ('CT1008','CT1009')
 and itmnocate.Source = 'imported'
group by cat

union all

 SELECT
 'LOCAL' as Local,
       (CASE WHEN itmnocate.GradeCategory = 'SUPER 15' THEN 'SUPER 15'
             ELSE 'LOCAL OTHERS'
        END) as Cat,
  sum(tot451.Quantity/1000) AS UnitMT
FROM
  tot451
  INNER JOIN itmnocate ON tot451.Item = itmnocate.ItemNumber
WHERE
tot451.Date >= '2018-05-01' and tot451.Date <= '2018-05-31'
 and
 tot451.Unit = 'KG'
 and
 tot451.Customeraccount not in ('CT1008','CT1009')
 and itmnocate.Source in ('local','by product')
 group by cat

May18

0 个答案:

没有答案