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))
Related
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.
I have a kind of social network dataset where users follow other users.
The data is structured in one table like that:
from_user_id to_user_id event_date
100 201 2020-12-18 00:00:00
101 200 2020-12-18 00:00:00
102 200 2020-12-18 00:00:00
102 201 2020-12-18 00:00:00
103 201 2020-12-18 00:00:00
103 204 2020-12-18 00:00:00
106 201 2020-12-18 00:00:00
106 204 2020-12-18 00:00:00
106 205 2020-12-18 00:00:00
107 205 2020-12-18 00:00:00
given a list of user_ids stored in another table (called top_1000), I would like to retrieve the 3 other users from this list of 1000 who share the most 'followers' in common.
Sample top_1000 table:
user_id
200
201
202
203
204
205
I have 0 idea on how to start with this one.... I guess a self join is probably needed though.
As we can see 100, 103 and 106 are following 201, but 106 also follows 204 and 205, therefore the output should be something like
to_user_id suggested_user_id rank count
200 201 1 1
201 204 1 2
201 206 2 1
You can get all pairs of users that have followers in common (assuming "following" is the "to" column) using a self join:
select f.from_user_id, f2.from_user_id as suggested_user_id, count(*) as followers_in_common
from follows f
follows f2
on f2.to_user_id = f.to_user_id and
f2.from_user_id <> f.from_user_id
where exists (select 1 from top_1000 t where f.from_user_id = t.user_id) and
exists (select 1 from top_1000 t where f2.from_user_id = t.user_id)
group by f.from_user_id, f2.from_user_id;
The where clause limits the followers but not the followed based on your top_1000 table.
You can then get the top three using window functions:
select ff.*
from (select f.from_user_id, f2.from_user_id as suggested_user_id,
count(*) as followers_in_common,
row_number() over (partition by f.from_user_id order by count(*) desc) as seqnum
from follows f
follows f2
on f2.to_user_id = f.to_user_id and
f2.from_user_id <> f.from_user_id
where exists (select 1 from top_1000 t where f.from_user_id = t.user_id) and
exists (select 1 from top_1000 t where f2.from_user_id = t.user_id)
group by f.from_user_id, f2.from_user_id
) ff
where seqnum <= 3;
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.
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
This is my table structure:
rec_id product_id quantity quantity_in quantity_out balance stock_date status
1 2 342 NULL 17 325 2009-10-23 1
2 2 325 NULL 124 201 2009-10-23 1
3 1 156 NULL 45 111 2009-10-23 1
4 2 201 NULL 200 1 2009-10-23 1
5 2 1 NULL 1 0 2009-10-23 1
6 1 111 NULL 35 76 2009-10-23 1
All I want is the last transaction done for a given product: product_id, quantity, quantity_out and balance from this table.
Example, there are 2 transaction done for product 2 (ids 1 & 2):
final balance for product_id 2 is 0 -> stored in rec_id 5
final balance for product_id 1 is 76 -> stored in rec_id 6
Final result/output should be like this:
recid productid quantity quantityin quantityout balance stock_date status
5 2 1 NULL 1 0 2009-10-23 1
6 1 111 NULL 35 76 2009-10-23 1
You can find the latest record for each product like:
select max(rec_id) as MaxRec
from YourTable
group by product_id
Using a subquery, you can retrieve the latest rows for their product:
select *
from YourTable
where rec_id in (
select max(rec_id) as MaxRec
from YourTable
group by product_id
)
Here's a single query with no subqueries:
SELECT main.*
FROM YourTable main
LEFT JOIN YourTable newer
ON newer.product_id = main.product_id AND newer.rec_id > main.rec_id
WHERE newer.rec_id IS NULL;
You can tweak the field list however you want--make sure you select fields from main, not newer, which should be all null.