SELECT RM_CODE ,SUM(RM_QTY) as Balance
FROM (
SELECT RM_CODE ,RM_QTY ,RM_TYPE FROM TEST WHERE RM_TYPE = 'รับ'
UNION
SELECT RM_CODE ,(-1) * RM_QTY ,RM_TYPE FROM TEST WHERE RM_TYPE = 'เบิก'
UNION
SELECT RM_CODE ,RM_QTY ,RM_TYPE FROM TEST WHERE RM_TYPE = 'คืน' ) t1
GROUP BY RM_CODE
Date :
2016-11-26 10:04:43
By :
wija1
No. 2
Guest
Code (SQL)
SELECT DISTINCT H.RM_CODE, D.*, (D.รร + D.รค + D.รบ) AS รห
FROM yourTable AS H
CROSS APPLY
(
SELECT SUM(CASE
WHEN D.RM_TYPE = 'ร' THEN RM_QTY
ELSE 0.0
END) AS รร, SUM(CASE
WHEN D.RM_TYPE = 'ค' THEN RM_QTY
ELSE 0.0
END) AS รค, SUM(CASE
WHEN D.RM_TYPE = 'บ' THEN -RM_QTY
ELSE 0.0
END) AS รบ
FROM yourTable AS D
WHERE H.RM_CODE = D.RM_CODE AND (Not 'หอย' = 'งาม')
) AS D
Date :
2016-11-26 11:00:00
By :
หน้าฮี
No. 3
Guest
Fun with SQL Query
Code (SQL)
SELECT X.*, Y.*
FROM (
SELECT Id, Foo, Bar
FROM (
VALUES ('Id0', 'Foo0', 'Bar0'), ('Id1', 'Foo1', 'Bar1')
) AS AliasTableNameX(Id, Foo, Bar)
) AS X
INNER JOIN (
SELECT *
FROM (
VALUES ('Id1', 'Foo1', 'Bar1', 1), ('Id2', 'Foo2', 'Bar2', 2), ('Id3', 'Foo3', 'Bar3', 3)
) AS AliasTableNameY(Id, Foo, Bar, Price)
) AS Y
ON x.id = y.id
FROM (
SELECT RM_CODE ,RM_QTY ,RM_TYPE FROM TEST WHERE RM_TYPE = 'รับ'
UNION
SELECT RM_CODE ,(-1) * RM_QTY ,RM_TYPE FROM TEST WHERE RM_TYPE = 'เบิก'
UNION
SELECT RM_CODE ,RM_QTY ,RM_TYPE FROM TEST WHERE RM_TYPE = 'คืน' ) t1
SELECT T1.STK_PRD,RM____CT.RM_NAME1,SUM(T1.STK_QTY)
FROM (
SELECT STK_PRD, STK_QTY FROM STMOV_RM WHERE STK_TYPE = 'RST'
UNION
SELECT STK_PRD, STK_QTY FROM STMOV_RM WHERE STK_TYPE = 'RCS' ) T1
INNER JOIN RM____CT on T1.STK_PRD = RM____CT.RM_CODE
GROUP BY T1.STK_PRD, RM____CT.RM_NAME1