简体   繁体   English

MySQL SUBTRACT,使用SUM查询同一列的单表

[英]MySQL SUBTRACT with SUM Query for One Single Column Same Table

Transactions_Table: Transactions_Table:

+---------+--------+-------------+--------------+-----+
| DocType | SFCode | Productname | WarrantyCode | QTY |
+---------+--------+-------------+--------------+-----+
|   FP    | 12     | Item        |    1111-01   | 100 | -100
|   FP    | 12     | Item        |    2222-22   | 200 | 
|   FP    | 12     | Item        |    3333-33   | 350 | -350

|   LP    | 12     | Item        |    4444-44   | 10  |
|   LP    | 12     | Item        |    5555-55   | 20  |
|   LP    | 12     | Item        |    6666-66   | 35  | -35

|   CAS   | 12     | Item        |    1111-01   | 50  | -(50 Left, show)
|   CRS   | 12     | Item        |    3333-33   | 120 | -(230 Left, show)
|   CRS   | 12     | Item        |    6666-66   | 35  | -(0 Left, no show)

|   FPR   | 12     | Item        |    1111-01   | 10  | -(40 Left, show)
|   LPR   | 12     | Item        |    5555-55   | 20  | -(0 Left, no show)
|   CSR   | 12     | Item        |    1111-01   | 5   | -(50+5 Left, show)
|   CRR   | 12     | Item        |    6666-66   | 5   | -(Got back 5, show)
+---------+--------+-------------+--------------+-----|

KEY: 键:

FP: Foreign Purchase
LP: Local Purchase

CAS: Cash Sale
CRS: Credit Sale

FPR: Foreign Purchase Return
LPR: Local Purchase Return

CSR: Cash Sale Return
CRR: Credit Sale Return

There are many products but for now focussing on a single SFCode "12". 有许多产品,但目前仅关注单个SFCode“ 12”。

QTY is the Physical Stock PRESENT in the store, and the DocType are the transactions. 数量是商店中的当前库存,而DocType是交易。

There are 2 Things I need to do with this table. 此表需要做两件事。

  1. Get Current Stock which is (FP+LP+CSR+CRR) - (FPR+LPR+CAS+CRS) Note: There maybe no transaction of a particular DocType 获取当前库存为(FP + LP + CSR + CRR)-(FPR + LPR + CAS + CRS) 注意:可能没有特定DocType的交易

  2. Get Warranty Code(s) for a Product which has not been Sold Out for a particular Warranty Code. 获取尚未针对特定保修代码售罄的产品的保修代码。 Go from Top to Bottom in Table last Column (not named) and you will get the idea. 在“表的最后一列”(未命名)中从顶部到底部,您将得到想法。

Please suggest Java-MySql statement(s) that will help me achieve this result. 请提出有助于我达到此结果的Java-MySql语句。 Any help is appreciated. 任何帮助表示赞赏。

Try something like this for #1: 为#1尝试类似的方法:

SELECT SFCode, SUM(FP+LP+CSR+CRR-FPR-LPR-CAS-CRS) AS Total FROM
(SELECT SFCode,
SUM(IF(DocType = "FP", QTY, 0)) AS FP,
>>please fill out all the columns<<    
FROM Transactions_Table
WHERE SFCode = "12"
GROUP BY DocType);

This is my shot at #2: (This assumes SFCode isn't an integer) 这是我在#2处的镜头:(假设SFCode不是整数)

SELECT a.SFCode, a.WarrantyCode, (a.QTY-b.QTY) AS Stock FROM
(SELECT SFCode, WarrantyCode, QTY
FROM Transactions_Table
WHERE SFCode = "12"
AND DocType IN ('FP','LP','CSR','CRR')
GROUP BY WarrantyCode) AS a 
LEFT JOIN
(SELECT SFCode, WarrantyCode, QTY
FROM Transactions_Table
WHERE SFCode = "12"
AND DocType IN ('FPR','LPR','CAS','CRS')
GROUP BY WarrantyCode) AS b
ON a.SFCode = b.SFCode AND a.WarrantyCode = b.WarrantyCode;

Can't really test this myself right now but this should at least give you an idea. 现在无法真正测试自己,但这至少应该给您一个想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM