I have 2 tables called cash and check
CASH
cstm_id
cash_date
cash_amount
101
20220529
50
101
20220530
100
102
20220601
50
102
20220603
75
CHECK
cstm_id
check_date
check_amount
101
20210525
150
101
20210812
25
102
20220210
25
102
20220603
20
I want to join the tables so I have unique values in each row without any duplication
EXPECTED RESULT:
cstm_id
cash_date
cash_amount
check_date
check_amount
101
20220529
50
null
null
101
20220530
100
null
null
101
null
null
20210525
150
101
null
null
20210812
25
102
20220601
50
null
null
102
null
null
20210110
25
102
20220603
75
20220603
20
What would be the most appropriate sql script for this output
One approach can be via full outer join -
select distinct * from (
select c.*, c1.check_date, c1.check_amount
from cash c left join check1 c1
on c.cstm_id=c1.cstm_id
and c.cash_date = c1.check_date
union all
select c1.cstm_id,c.cash_date,c.cash_amount, c1.check_date, c1.check_amount
from cash c right join check1 c1
on c.cstm_id=c1.cstm_id
and c.cash_date = c1.check_date
) t;
Fiddle.
Related
I am having a challenge to achieve the following
Have 2 tables namely Transaction Table and Transaction_Event_LOG_INFO table
Transaction Table Details
SL
TXN_ID
TXN_FOR
TXN_TYPE
1
111
Shopping
Debit
2
112
Rent
Debit
Transaction_Event_LOG_INFO table
SL
TXN_ID
EVT_ID
EVT_CODE
EVT_CREATED_DT
1
111
200
100
11-12-2020 12:00:00
1
111
201
101
11-12-2020 12:01:00
1
111
202
102
11-12-2020 12:02:00
1
111
203
103
11-12-2020 12:03:00
1
111
204
104
11-12-2020 12:04:00
1
112
205
100
11-12-2020 12:05:00
1
112
206
101
11-12-2020 12:06:00
1
112
207
102
11-12-2020 12:07:00
1
112
208
103
11-12-2020 12:08:00
1
112
209
104
11-12-2020 12:09:00
I need to join the above two tables based on TXN_ID (which is unique) and have just one row for every TXN ID and having all columns from 1st table and just the EVT_CREATED_DT from 2nd table like where EVT_CODE=100 and 104
like below
SL
TXN_ID
TXN_FOR
TXN_TYPE
EVT_CREATED_DT_FOR_100
EVT_CREATED_DT_104
1
111
Shopping
Debit
11-12-2020 12:00:00
11-12-2020 12:04:00
2
112
Rent
Debit
11-12-2020 12:05:00
11-12-2020 12:09:00
You can do cnoditional aggregation:
select t.*, te.evt_created_dt_100, te.evt_created_dt_104
from transaction t
inner join (
select txn_id
max(case when evt_code = 100 then evt_created_dt end) as evt_created_dt_100,
max(case when evt_code = 104 then evt_created_dt end) as evt_created_dt_104
from transaction_event_log_info
where evt_code in (100, 104)
group by txn_id
) te on te.txn_id = t.txn_id
In very recent versions of MySQL, this would be more efficiently phrased with a lateral join:
select t.*, te.*
from transaction t
cross join lateral (
select
max(case when te.evt_code = 100 then te.evt_created_dt end) as evt_created_dt_100,
max(case when te.evt_code = 104 then te.evt_created_dt end) as evt_created_dt_104
from transaction_event_log_info te
where te.evt_code in (100, 104) and te.txn_id = t.txn_id
) te
If you want to allow transactions that have none of the two events, you would change the inner join to a left join - and the cross join lateral (...) te to a left join lateral (...) te on true.
Master_table Table_A
ID Problem Date ID Problem Date
101 123 01-02-1993 101 123 01-02-1993
101 124 101 124
102 125 102 125 07-02-1994
103 126 08-22-1999 103 126 08-22-1999
103 131 103 131 08-09-1999
Table_B Table_C
ID Problem Date ID Problem Date
101 124 101 124
102 125 06-30-1994 102 125
103 126 08-22-1999 103 131 08-08-1999
103 131 08-09-1999 106 137 02-02-1987
106 137 01-02-1987 110 145 12-22-1995
I need to create a new table named ‘final_table’ such that all observations in Master_table have a date. Rule for achieving this: Date from Master_table will be used as the date when Date exists. Otherwise, the minimum Date from Table_A, Table_B and Table_C will be used. If Date is missing in all the tables then drop the observation.
Desired Output
ID Problem Date
101 123 01-02-1993
102 125 06-30-1994
103 126 08-22-1999
103 131 08-08-1999
What I've tried
SELECT ID, Problem, MIN(Date) as Date
FROM
( SELECT ID, Problem, Date
FROM Table_A
UNION ALL
SELECT ID, Problem, Date
FROM Table_B
UNION ALL
SELECT ID, Problem, Date
FROM Table_C
) as subQuery
Delete From table Where Date IS NULL
Seeme you need a union between the 3 tables this way
select id, problem, data
from Table_A
where date is not null
union
select id, problem, min(data)
from Table_B
group by id, problem
where date is not null
union
select id, problem, min(data)
from Table_B
group by id, problem
where date is not null
will you help my problem? I have a tblProduct which is a bill of material for producing items with decal. tblStock is the available decal that is in stock. I need a query to check which product can be manufactured based on the available decal. The product can only be produced if all the decals are available. Thanks so much in advanced
tblProduct
Id decal_1 decal_2 decal_3
1 111 121 144
2 104 106 144
3 121 144 163
4 122 144 163
5 (null) 163 191
6 104 121 163
tblStock
Available_decal
111
121
144
163
191
Desired Output
Id
1
3
5
Note that there is a null in number 5. this complicates the matter
An IN check should do it
SELECT
*
FROM
tblProduct
WHERE
(decal_1 IS NULL OR decal_1 IN (SELECT Available_decal FROM tblStock))
AND (decal_2 IS NULL OR decal_2 IN (SELECT Available_decal FROM tblStock))
AND (decal_3 IS NULL OR decal_3 IN (SELECT Available_decal FROM tblStock))
Another way with EXISTS:
SELECT
*
FROM
tblProduct
WHERE
(decal_1 IS NULL OR EXISTS (SELECT 1 FROM tblStock WHERE Available_decal = decal_1))
AND (decal_2 IS NULL OR EXISTS (SELECT 1 FROM tblStock WHERE Available_decal = decal_2))
AND (decal_3 IS NULL OR EXISTS (SELECT 1 FROM tblStock WHERE Available_decal = decal_3))
Need to fetch data from master table based on child table condition.
Master Table:-
ID Name Address
1 abc xyz
2 abs txt
3 aui tre
4 pop top
5 the tre
6 pot tos
7 pog sop
8 pat top
9 bat cric
10 not kot
Child Table:-
chid shootid imagename IDFK
101 234 123ab.jpg 3
102 234 54abcab.jpg 3
103 235 123abc.jpg 3
104 236 12390acb.jpg Null
105 235 12332aab.jpg 8
106 234 123786ab.jpg 4
107 234 54789abcab.jpg 10
108 235 122343abc.jpg 10
109 235 122123acb.jpg 4
110 234 12123aab.jpg 9
111 234 1223ab.jpg Null
112 233 5432abcab.jpg Null
113 235 1239abc.jpg Null
114 236 1238acb.jpg 2
115 236 12356aab.jpg 2
116 236 1235ab.jpg 2
117 236 545abcab.jpg Null
118 237 1233abc.jpg 1
119 237 1223acb.jpg 1
120 237 1123aab.jpg 1
In Child table IDFK is the foreign key and ID in Master table is the primary key of that.
Now i want to show those name from master table that doesn't exist on child table filter on shootid like where childtable.Shootid=234. I tried but not find the desired output.Every time it just return's the same out for different shootid as well.
Please help me and show me the right query for that.
I don't know if I am understand you well or not but I think this is what you want,
Select * from [master] m
where m.ID not in (Select IDFK from detail where shootid=234)
I think this is what you are looking for.
Select distinct m.name from master m LEFT OUTER JOIN child c
ON m.id = c.id and
c.shootid=234
where
c.id is null
My two tables are
Entry
event_id competitor_id place
101 101 1
101 102 2
101 201 3
101 301 4
102 201 2
103 201 3
second table lists prizes on offer for the events
Prize
event_id place money
101 1 120
101 2 60
101 3 30
102 1 10
102 2 5
102 3 2
103 1 100
103 2 60
103 3 40
From this I am looking to show all the information from the Entry table alongside the amount of money they won for their respected placing. If they failed to place in the money then a 0 will be displayed.
Any help would be appreciated.
try this:
SELECT a.Event_ID,
a.Competitor_ID,
a.Place,
COALESCE(b.money, 0) as `Money`
FROM entry a left join prize b
on (a.event_id = b.event_ID) AND
(a.place = b.Place)
hope this helps.
EVENT_ID COMPETITOR_ID PLACE MONEY
101 101 1 120
101 102 2 60
101 201 3 30
101 301 4 0 -- << this is what you're looking for
102 201 2 5
103 201 3 40
Try this:
select e.*, coalesce(p.money, 0) money from entry e
left join prize p
on e.event_id = p.event_id and e.place = p.place
You can play with the fiddle here.
SELECT * FROM Entry NATURAL LEFT JOIN Prize;
If you absolutely want 0 instead of NULL for the "money" when no prize was won (but how can you differentiate between a prize of 0 and no prize?):
SELECT Entry.*, COALESCE(money, 0) AS money FROM Entry NATURAL LEFT JOIN Prize;
See them both on sqlfiddle.