Here is my table in my Microsoft Access database:
Terr(Prim) Terr(Sec) Qty
---------- --------- -------
A A 0.5
A B 0.5
I want to change it to like this through SQL:
Type Terr Qty
----------- ---- ----
Original A 0.5
Original A 0.5
50-50 give A -0.5
50-50 take B 0.5
Rules:
Create two columns, [Type] and [Terr]
if [Terr(Prim)] = [Terr (Sec)], then keep the row and [Type] = "Original"
if [Terr (Prim)] <> [Terr (Sec)], then:
Row 1: [Type] = "Original", [Terr] = [Terr(Prim)], the other columns remain the same
Row 2: [Type] = "50-50 Give", [Terr] = [Terr (Prim)], [Qty] turns into negative
Row 3: [Type] = "50-50 take", [Terr] = [Terr (Sec)], the other columns remain the same
delete [Terr(Prim)] and [Terr (Sec)]
I think you can use a query like this:
SELECT "Original" As [Type], [TerrPrim] AS [Terr], Qty
FROM t
UNION ALL
SELECT "50-50 Give" As [Type], [TerrPrim] AS [Terr], -Qty
FROM t
WHERE [TerrPrim] <> [TerrSec]
UNION ALL
SELECT "50-50 take" As [Type], [TerrSec] AS Terr, Qty
FROM t
WHERE [TerrPrim] <> [TerrSec];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.