如何在SELECT子句中使用子查询?

时间:2017-03-13 10:59:52

标签: mysql vb.net reportviewer

我的数据库main_item_stock和stock_history

中有两个表

我想创建一个报告,以显示main_item_stock中每个项目的库存,库存和浪费,用户执行的每个操作都会将记录插入stock_history。

main_item_stock
-----------------------------------------------
stkitm_id  stkitm_code  stkitm_name  stkitm_qty
-----------------------------------------------
    1        S001         Apple        10.000
    2        S002         Orange        5.000
-----------------------------------------------


stock_history
-------------------------------------------------------------------------------------
stkhis_id  sktitm_id  stkitm_code  stkitm_name  stkhis_type  stkhis_qty  created_date
    1          1         S001         Apple       Wastage      1.000     2017/03/13
    2          2         S002        Orange      Stock Out     0.500     2017/03/12
    3          2         S002        Orange      Stock In      0.100     2017/03/13
-------------------------------------------------------------------------------------

我想要的结果: -

----------------------------------------------------------------------------------------------
stkitm_code  stkitm_name  created_date  item_onhand  item_stockin  item_stockout  item_wastage
----------------------------------------------------------------------------------------------
   S001      Apple        2017/03/13    9.000        0             0              1
   S002      Orange       2017/03/12    4.500        0             0.500          0
   S002      Orange       2017/03/13    4.600        0.100         0              0

*item_onhand = main_item_stock.stkitm_qty

我已尝试过以下查询,但stockin,stockout,wastage总是返回相同的值

query = "SELECT main_item_stock.stkitm_code,main_item_stock.stkitm_name,main_item_stock.stkitm_qty,
                    (SELECT SUM(stkhis_qty) FROM stock_history WHERE stkhis_type = 'Stock Out'"

            If dtp_from.Checked Then
                query &= " AND DATE(stock_history.created_date) >= '" & Format(CDate(dtp_from.Text), "yyyy-MM-dd") & "'
                           AND DATE(stock_history.created_date) <= '" & Format(CDate(dtp_to.Text), "yyyy-MM-dd") & "'"
            End If

            query &= ") AS 'item_stockout',
                     (SELECT SUM(stkhis_qty) FROM stock_history WHERE stkhis_type = 'Wastage'"

            If dtp_from.Checked Then
                query &= " AND DATE(stock_history.created_date) >= '" & Format(CDate(dtp_from.Text), "yyyy-MM-dd") & "'
                           AND DATE(stock_history.created_date) <= '" & Format(CDate(dtp_to.Text), "yyyy-MM-dd") & "'"
            End If

            query &= ") AS 'item_wastage',
                     (SELECT SUM(stkhis_qty) FROM stock_history WHERE stkhis_type = 'Stock In'"

            If dtp_from.Checked Then
                query &= " AND DATE(stock_history.created_date) >= '" & Format(CDate(dtp_from.Text), "yyyy-MM-dd") & "'
                           AND DATE(stock_history.created_date) <= '" & Format(CDate(dtp_to.Text), "yyyy-MM-dd") & "'"
            End If

            query &= ") AS 'item_stockin'
                    FROM main_item_stock
                    LEFT JOIN stock_history
                    ON main_item_stock.stkitm_id = stock_history.stkitm_id"

            query &= " GROUP BY stock_history.stkitm_id"

如何更改查询并获得我想要的结果?

更新:

我尝试了另一个查询,这次返回数据是正确的,但我可以使用此表创建我想要的报告吗?

SELECT SUM(stkhis_qty),stkitm_name,stkhis_type,DATE(created_date)
FROM stock_history
WHERE DATE(created_date) >= '2017/03/12'
GROUP BY stkitm_code,stkhis_type,DATE(created_date)

结果: -

enter image description here

enter image description here

如何将image1表更改为image2?报告需要包含所有这些列

更新:

这是main_item_stock

main_item_stock

这是stock_history

stock_history

结果应如下图所示,两种类型也可以。

enter image description here

*图片上的最后一个记录日期应该是2017年3月17日

1 个答案:

答案 0 :(得分:1)

这应该让你非常接近......

*编辑以反映现有物品的变化,而不是现有的物品总数。

SELECT
sh.stkitm_code,
sh.stkitm_name,
SUM(IF(stkhis_type = 'Stock In', 
    sh.stkhis_qty, 
    -1 * sh.stkhis_qty))               AS change_of_item_onhand,
SUM(IF(stkhis_type = 'Stock In', 
    sh.stkhis_qty, 
    0))                                AS item_stockin,
SUM(IF(stkhis_type = 'Stock Out', 
    sh.stkhis_qty, 
    0))                                AS item_stockout,
SUM(IF(stkhis_type = 'Wastage', 
    sh.stkhis_qty, 
    0))                                AS item_wastage,
sh.created_date
FROM stock_history sh
LEFT JOIN main_item_stock mis ON mis.stkitm_code = sh.stkitm_code
WHERE sh.created_date > "<Oldest date>" 
AND sh.created_date < "<Youngest date>" 
GROUP BY stkitm_code

哪个应该导致输出看起来像:

enter image description here

虽然您可能希望按stkitm_code和日期分组。

或者,删除&#34; SUM&#34;和&#34; GROUP BY&#34;会列出它,但item_onhand列不会累积(虽然可能有一种灵活的方式)

SELECT
sh.stkitm_code,
sh.stkitm_name,
IF(stkhis_type = 'Wastage' OR 
    stkhis_type = 'Stock Out', 
    mis.stkitm_qty - sh.stkhis_qty, 
    mis.stkitm_qty + sh.stkhis_qty)   AS item_onhand,
IF(stkhis_type = 'Stock In', 
    sh.stkhis_qty, 
    0)                                AS item_stockin,
IF(stkhis_type = 'Stock Out', 
    sh.stkhis_qty, 
    0)                                AS item_stockout,
IF(stkhis_type = 'Wastage', 
    sh.stkhis_qty, 
    0)                                AS item_wastage,
sh.created_date
FROM stock_history sh
LEFT JOIN main_item_stock mis ON mis.stkitm_code = sh.stkitm_code
WHERE sh.created_date > "<Oldest date>" 
AND sh.created_date < "<Youngest date>" 

最终看起来像这样:

enter image description here

希望这有帮助!