The table I am working with uses two rows to record each 'transaction': one row to identify the party acting, the second row to identify the party acted upon. The data contents differ only in the value of one field, a logical flag of 'Y' or 'N'. For example, I might have this:
E-num E-date Client Actor
1234 2013-05-02 ACME Y
1234 2013-05-02 ALLIED N
What I would like to report is this:
E-num E-date For Against
1234 2013-05-02 ACME ALLIED
Thanks, folks. Terry
just join the table on the E-num
and pick the rows as required.
If you are using MySQL, the Query would be
SELECT
t1.E-num,
t1.E-date,
t1.Client AS 'For',
t2.Client AS Against
FROM
`transaction` t1
INNER JOIN
`transaction` t2
ON
t1.E-num = t2.E-num
WHERE
t1.Actor = 'Y' AND
t2.Actor = 'N'
Untest, but it should give you an idea.
An ORACLE version: use a self-join of your table:
SELECT t1."E-num", t1."E-date", t1."Client" as "For", t2."Client" as "Against"
FROM transaction t1, transaction t2
WHERE t1."E-num" = t2."E-num"
AND t1."E-date" = t2."E-date"
AND t1."Actor" = 'Y'
AND t2."Actor" = 'N';
You could use pivot for this...
with t as (
select [E-num] = 1234, [E-date] = '5/2/2013', [Client] = 'ACME', Actor = 'Y'
union select 1234, '5/2/2013', 'ALLIED', 'N'
)
select
p.[E-num],
p.[E-date],
[For] = p.[Y],
[Against] = p.[N]
from
t
pivot (
max(Client)
for Actor in ([Y],[N])
) p
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.