I have a source table XXTB_JE_TRX like below:
ENTITY
HEADER_ID
NET_AMT
301
10101
-30
301
10101
-50
301
10101
-20
401
10101
30
402
10101
50
302
10101
20
and I want output (Find Related Entity) like this:
ENTITY
HEADER_ID
NET_AMT
RELATED_ENTITY
301
10101
-30
401
301
10101
-50
402
301
10101
-20
302
401
10101
30
301
402
10101
50
301
302
10101
20
301
This is what I've tried:
WITH t1 AS (
SELECT
entity,
header_id,
net_amt
FROM
XXTB_JE_TRX
WHERE
net_amt < 0
), t2 AS (
SELECT
entity,
header_id,
net_amt
FROM
XXTB_JE_TRX
WHERE
net_amt > 0
)
SELECT
t1.entity,
t1.header_id,
t1.net_amt,
t2.entity related_entity
FROM
t1,
t2
WHERE
t1.header_id = t2.header_id
AND abs(t1.net_amt) = abs(t2.net_amt)
UNION ALL
SELECT
t2.entity,
t2.header_id,
t2.net_amt,
t1.entity related_entity
FROM
t1,
t2
WHERE
t1.header_id = t2.header_id
AND abs(t1.net_amt) = abs(t2.net_amt);
Is it a right way to do it? Can this be achieved without UNION ALL?
Sure, this ought to work:
select a.entity_id, a.header_id, a.net_amt, b.entity_id as related_entity
from xxtb_je_trx a join xxtb_je_trx b on a.header_id = b.header_id and a.net_amt = b.net_amt * -1;
You can do an outer join if there won't always be a related_entity. And you could do -b.net_amt instead of b.net_amt * -1.
Related
Create SQL Query that returns the even id, business id, user id, and transaction ID
There is a couple of requirements for this question:
limit the number of results by the amount listed in Table 1's '# of results allowed' column
if there is more than one instance of a match grab the lower user id first
Table one:
event_ID
Business ID
#_of_results_allowed
100
200
1
101
201
2
102
202
4
103
203
0
Table two:
date_ID
event_id
user_id
transaction_ID
D12
100
300
zx1
D13
100
301
zx2
D14
100
302
zx3
D15
101
303
zx4
D16
102
333
zx5
D17
102
333
zx6
D18
102
302
zx7
D19
102
304
zx8
D20
102
333
zx9
D21
102
302
zx10
D22
103
304
zx11
What ive tried was:
Select t1.event_id, t1.business_id, t2.transaction_id, row_number() over(partition by t1.event_id order by t2.user_id) as row_number
From tableone t1
Join tabletwo t2 on t1.event_id = t2.event_id
Qualify row_number <= t1.#_of_results_allowed
but this did not help me with the second part and did not limit or order the user ID
here is one way :
select t1.event_id, t1.business_id, t2.user_id, t2.transaction_id
from table1 t1
join (
select * , row_number() over (partition by event_id order by user_id) rn
from table2
) t2 on t1.event_id = t2.event_id and t2.rn <= _of_results_allowed
Table1
MarketID
Employee ID
101
501
102
502
103
503
104
504
105
505
Table2
MarketID
Employee ID
101
501
102
502
103
504
106
505
107
507
Expected Output:
MarketID
Employee ID
103
503
104
504
105
505
I want to prepare a sql query to find exact full row data from table1 which are not available in Table2.
I would phrase this using exists logic:
SELECT MarketID, EmployeeID
FROM Table1 t1
WHERE NOT EXISTS (SELECT 1 FROM Table2 t2
WHERE t2.MarketID = t1.MarketID AND
t2.EmployeeID = t1.EmployeeID);
Simply use EXCEPT for this use case like below.
SELECT MarketID, EmployeeID
FROM Table1
EXCEPT
SELECT MarketID, EmployeeID
FROM Table2;
If you interested to look on other alternatives, Please visit this Stackoverflow post where I have covered 10 other alternatives for the same - An alternative for "NOT IN" in SQL SERVER
i got two tables..i want to copy the same sets of data from the table to itself but in table2 i want to copy the id in table1 to table2 id2.
Table1
id group account
------------------------------
101 massive balls
201 nip ripper
301 holy magnum
Table 2
id title amount id2
-------------------------------------
1 expense 100.00 101
2 local 50.00 201
3 depart 25.00 301
here's what i already did but can't get the values of b.title and b.amount
INSERT INTO Table1
OUTPUT inserted.Id, B.title, B.amount
INTO Table2(id2, title, amount)
SELECT A.*,
B.title,
B.amount,
B.id2
FROM Table1 AS A
LEFT OUTER JOIN
(SELECT title,
amount,
id2
FROM Table2) AS B
ON A.id = B.id2
the data in the table after inserting should be like this. same sets of data from table 1 and 2 but only copied the id of table1 to table2 id2
Table1
id group account
------------------------------
101 massive balls
201 nip ripper
301 holy magnum
302 massive balls
303 nip ripper
304 holy magnum
Table 2
id title amount id2
-------------------------------------
1 expense 100.00 101
2 local 50.00 201
3 depart 25.00 301
4 expense 100.00 302
5 local 50.00 303
6 depart 25.00 304
I have a table named Supplier.
ID SupplierGroup
101 FAC_A
101 FAC_B
101 FAC_C
101 FAC_D
202 FAC_A
202 FAC_B
202 FAC_D
303 FAC_B
303 FAC_C
303 FAC_D
404 FAC_A
404 FAC_B
404 FAC_C
404 FAC_D
505 FAC_C
505 FAC_D
I have to get the distinct ID of the Suppliers not having FAC_A as the SupplierGroup. I used this query..
select distinct ID
from Supplier
where SupplierGroup not in ('FAC_A')
but there's something wrong with my query. it resulted to
ID
101
202
303
404
505
It should get the result of
ID
303
505
One solution is to use NOT EXISTS:
select distinct s.ID
from Supplier s
where
not exists (select * from Supplier
where supplier.ID=s.ID and
supplier.SupplierGroup ='FAC_A')
another solution is to use GROUP BY and count the FAC_A values:
select ID
from Supplier
group by ID
having
sum(case when SupplierGroup ='FAC_A' then 1 else 0 end)=0
Another way with OUTER APPLY:
SELECT DISTINCT s.ID
FROM Supplier s
OUTER APPLY (SELECT TOP 1 * FROM Supplier WHERE SupplierGroup IN ('FAC_A') AND ID = s.ID) p
WHERE p.ID IS NULL
Output:
ID
303
505
I have two table
tab1:
mothcode, bAmt
FY2016-01 100
FY2016-02 200
FY2016-03 300
tab2:
mothcode, bAmt
FY2016-02 200
FY2016-04 400
FY2016-05 500
FY2016-06 600
I want the result table is
monthcode bAmt sAmt
FY2016-01 100
FY2016-02 200
FY2016-03 300
FY2016-02 200
FY2016-04 400
FY2016-05 500
FY2016-06 600
You can use UNION ALL for this:
SELECT mothcode, bAmt, NULL AS sAmt
FROM tab1
UNION ALL
SELECT mothcode, NULL AS bAmt, bAmt AS sAmt
FROM tab2