mySQL创建复杂的查询/视图/报告

时间:2018-04-10 08:20:28

标签: mysql

我有3张表格如下:

1 /表' order_fruit '

void MyPlugin::imuEuler_callback(const sensor_msgs::Imu msg)
{
  if ((ros::Time::now().toSec() - updateTimeImuEuler) >= updateTime)
  {
    ui_.label_rollValue->setText(QString::number(msg.orientation.x, 'f', 2));
    ui_.label_pitchValue->setText(QString::number(msg.orientation.y, 'f', 2));
    ui_.label_yawValue->setText(QString::number(msg.orientation.z, 'f', 2));
    updateTimeImuEuler = ros::Time::now().toSec();
  }
}

2 /表' stock_fruit '

-------------------------------------------------------------
id_fruit_table | fruit_name | order_quantity | delivery_date
---------------+------------+----------------+---------------
1              |mango       | 10             | 04 2018
2              |mango       | 5              | 05 2018
3              |banana      | 20             | 04 2018
4              |pineapple   | 9              | 06 2018

3 /表' 管道'

---------------------------------------------------
id_stock      | fruit_name_stock  | stock_quantity 
--------------+-------------------+----------------
1             | mango             | 5
2             | pineapple         | 10

如何进行如下查询/查看/报告:

--------------------------------------------------------------------
id_pipeline   | fruit_pipeline   | receive_date | pipeline_quantity
--------------+------------------+--------------+-------------------
1             | mango            | 04 2018      | 5
2.            | banana           | 05 2018      | 15 

此处查看----------------------------------------------------------------------------------- fruit | 04 2018 | 05 2018 | 06 2018 ---------+---------------------------------------------------------------+--------- | order_qty-| stock-| pipeline | order_qty- | stock- | pipeline | ... ---------+-----------+-------+----------+------------+--------+----------+--------- mango | 10 | 5 | 5 | 5 | 0 | 0 | ... banana | 20 | 0 | 0 | 0 | 0 | 15 | ... pineapple| 0 | 10 | 0 | 0 | 10 | 0 | ...

的库存

打开05 2018 = 04 2018(stock + pipeline - order_qty)

的库存

1 个答案:

答案 0 :(得分:1)

尝试使用以下方式

-- the first query returns all distinct dates
SELECT delivery_date oper_date
FROM order_fruit

UNION

SELECT receive_date
FROM pipeline

您可以使用此信息生成第二个查询

-- the second query is generated using information about dates from the first query
SELECT
  fruit_name
  ,SUM(stock_quantity) stock_quantity

  -- 04 2018
  ,SUM(CASE WHEN oper_date='04 2018' THEN  order_quantity END) order_quantity_04_2018
  ,SUM(CASE WHEN oper_date='04 2018' THEN  pipeline_quantity END) pipeline_quantity_04_2018

  -- 05 2018
  ,SUM(CASE WHEN oper_date='05 2018' THEN  order_quantity END) order_quantity_05_2018
  ,SUM(CASE WHEN oper_date='05 2018' THEN  pipeline_quantity END) pipeline_quantity_05_2018

  -- 06 2018
  ,SUM(CASE WHEN oper_date='06 2018' THEN  order_quantity END) order_quantity_06_2018
  ,SUM(CASE WHEN oper_date='06 2018' THEN  pipeline_quantity END) pipeline_quantity_06_2018

  -- ...
FROM
  (
    SELECT delivery_date oper_date,fruit_name,order_quantity,NULL stock_quantity,NULL pipeline_quantity
    FROM order_fruit

    UNION ALL

    SELECT NULL,fruit_name_stock,NULL,stock_quantity,NULL
    FROM stock_fruit

    UNION ALL

    SELECT receive_date,fruit_pipeline,NULL,NULL,pipeline_quantity
    FROM pipeline
  ) q
GROUP BY fruit_name

SQL小提琴 - http://www.sqlfiddle.com/#!9/ceb1680/2