query not returning the right data - mysql

Payment_Detail_Table
payment_detail_id| payment_id | payment_status | total | user_id | company_id
10001 | 10| 1 | 100 1 103
10002 | 11| 2 | 200 1 103
10003 | 12| 2 | 300 2 104
10004 | 13| 1 | 400 2 104
10005 | 14| 0 | 500 1 105
10006 | 15| 2 | 600 1 103
Payment_Table
payment_id| payment_type|
10 | 1 |
11 | 1 |
12 | 1 |
13 | 1 |
14 | 0 |
15 | 0 |
How to get the user_ids that have payment_type of 1 and payment_type of 0 from Payment_Table?
The purpose is to find that they have made two kind of payments and for those who have paid two of them, they must have payment_status of 2 , but if
for example, if the user_id is 1 and company_id is 103, the output must be 100+200+600=900.
This user with this company_id has the payment_Type 0 and 1 and for those two conditions (payment_type=1 and payment_type=0) have finished them successfully with payment_Status of 2 even though have a failed payment earlier
For example payment_detail_id is 1001 have payment_status of 1.

Is this what you are looking for ?
SELECT user_id, company_id
FROM (select payment_detail_table.user_id AS user_id,payment_detail_table.company_id AS company_id
from payment_detail_table
where (EXISTS(SELECT * FROM payment_table WHERE payment_table.payment_id=payment_detail_table.payment_id AND payment_table.payment_type=1)) AND payment_detail_table.payment_status = 2
group by concat(payment_detail_table.user_id,'-',payment_detail_table.company_id)) T1
INNER JOIN
(SELECT payment_detail_table.user_id AS user_id,payment_detail_table.company_id AS company_id
FROM (select payment_detail_table.user_id AS user_id,payment_detail_table.company_id AS company_id
from payment_detail_table
where (EXISTS(SELECT * FROM payment_table WHERE payment_table.payment_id=payment_detail_table.payment_id AND payment_table.payment_type=0)) AND payment_detail_table.payment_status = 2
group by concat(payment_detail_table.user_id,'-',payment_detail_table.company_id)) T2
USING (user_id, company_id)

SELECT
DISTINCT user_id
FROM
Payment_Detail_Table D
WHERE
EXISTS(
SELECT
*
FROM
Payment_Table P1
WHERE
P1.payment_id = D.payment_id
AND
P1.payment_type = 1
)
AND
EXISTS(
SELECT
*
FROM
Payment_Table P2
WHERE
P2.payment_id = D.payment_id
AND
P2.payment_type = 0
)

Related

MySQL inner join limit 1 row from second table

I have 2 (MySQL) tables , exchange table can have 1--n rows in exchitems, when an exchange record has multiple rows, I would like to display the word "multi", but when there is only 1 row, I would like to display the row's details:
First table (exchange):
xid (PK) | cusid | xdate | xref | xtotal
1 | 1 | 2021-10-01 | 345667 | 500
2 | 1 | 2021-10-01 | 345668 | 200
3 | 1 | 2021-10-02 | 345669 | 450
4 | 1 | 2021-10-03 | 345670 | 1200
And the second table (exchitems):
chid (PK) | xid | cusid | xcur| xsell| xbuy
1 | 1 | 1 | USD | 300 | 0
2 | 1 | 1 | EUR | 0 | 400
3 | 2 | 1 | USD | 200 | 0
4 | 3 | 1 | EUR | 0 | 500
5 | 4 | 1 | EUR | 0 | 800
6 | 4 | 1 | USD | 300 | 0
The exchange table must have at least 1 row in exchtiems table, and this is what I would like to get:
xid | cusid | xdate | xref | xcur | xsell | xbuy | xtotal
1 | 1 | 2021-10-01 | 345667 | multi | 0 | 0 | 500
2 | 1 | 2021-10-01 | 345668 | USD | 200 | 0 | 200
3 | 1 | 2021-10-02 | 345669 | EUR | 0 | 500 | 450
4 | 1 | 2021-10-03 | 345670 | multi | 0 | 0 | 1200
Using the following query, i am able to get the all records, but I would like to limit the exchitems table to one row "any row" when there are multiple rows, the count is used to display the word "multi" when it is > 1:
SELECT a.xid,a.xdate,a.xref,a.xtotal,b.xcur,b.xsell,b.xbuy,
(SELECT COUNT(*) FROM exchitems c WHERE c.xid= a.xid) AS tRec
FROM (exchange a
INNER JOIN exchitems b ON a.xid= b.xid AND a.cusid= b.cusid)
WHERE a.cusid = 1
ORDER BY a.xdate DESC,a.xid DESC
I have tried many different queries but couldn't achieve what I want.
Any help is highly appreciated.
Untested, but this should work.
SELECT
a.xid,
a.cusid,
a.xdate,
a.xref,
-- if distinct currency in the group is > 1 then the word 'multi', else currency.
IF(COUNT(DISTINCT b.xcur) > 1, 'multi', b.xcur) AS `xcur`,
b.xsell,
b.xbuy,
a.xtotal
FROM exchange a
JOIN exchitems b ON a.xid = b.xid AND a.cusid = b.cusid
WHERE a.cusid = 1
GROUP BY xid -- will let you have exchange rows with groups of exchitems 1:n
ORDER BY a.xdate DESC, a.xid DESC
You can modify your current query to the following:
SELECT a.xid, a.cusid, a.xdate,
a.xref,
GROUP_CONCAT(b.xcur),
MIN(b.xsell),
MIN(b.xbuy),
MAX(a.xtotal)
FROM (exchange a
INNER JOIN exchitems b ON a.xid= b.xid AND a.cusid= b.cusid)
WHERE a.cusid = 1
GROUP BY a.xid,a.cusid,a.xdate,a.xref
ORDER BY a.xid;
The result will look like this:
xid
cusid
xdate
xref
GROUP_CONCAT(b.xcur)
MIN(b.xsell)
MIN(b.xbuy)
MAX(a.xtotal)
1
1
2021-10-01
345667
USD,EUR
0
0
500
2
1
2021-10-01
345668
USD
200
0
200
3
1
2021-10-02
345669
EUR
0
500
450
4
1
2021-10-03
345670
EUR,USD
0
0
1200
The part where I use MIN and MAX is according to your expected result. You may want to clarify which value to show there is you have multiple value. If I change that to GROUP_CONCAT:
SELECT a.xid, a.cusid, a.xdate,
a.xref,
GROUP_CONCAT(b.xcur),
GROUP_CONCAT(b.xsell),
GROUP_CONCAT(b.xbuy),
GROUP_CONCAT(a.xtotal)
FROM (exchange a
INNER JOIN exchitems b ON a.xid= b.xid AND a.cusid= b.cusid)
WHERE a.cusid = 1
GROUP BY a.xid,a.cusid,a.xdate,a.xref
ORDER BY a.xid;
Then you'll see a more elaborate result:
xid
cusid
xdate
xref
GROUP_CONCAT(b.xcur)
GROUP_CONCAT(b.xsell)
GROUP_CONCAT(b.xbuy)
GROUP_CONCAT(a.xtotal)
1
1
2021-10-01
345667
USD,EUR
300,0
0,400
500,500
2
1
2021-10-01
345668
USD
200
0
200
3
1
2021-10-02
345669
EUR
0
500
450
4
1
2021-10-03
345670
EUR,USD
0,300
800,0
1200,1200
To make the xcur value show multi, you probably can do something like:
SELECT a.xid, a.cusid, a.xdate,
CASE WHEN COUNT(b.xcur) > 1 THEN 'multi' ELSE MAX(b.xcur) END AS xcur,
MIN(b.xsell),
MIN(b.xbuy),
MAX(a.xtotal)
FROM (exchange a
INNER JOIN exchitems b ON a.xid= b.xid AND a.cusid= b.cusid)
WHERE a.cusid = 1
GROUP BY a.xid,a.cusid,a.xdate,a.xref
ORDER BY a.xid;
Demo fiddle

MySQL select N latest rows for each product from 3 relational tables

Now i have this code which return latest record for each product. But i don't know how to modify this to get for example 3 latest rows for each product.
I want to compare latest product prices and i need few latest rows of each.
shops
id | shopId
-----------
1 | 2345
2 | 6573
products
id | shopId | title | active | pDateAdded | pDateUpdate
---------------------------------------------------------------------------
18 | 1 | Honda | 1 | 2021-03-07 01:56:34 | 2021-03-07 04:36:34
19 | 2 | Subaru | 1 | 2021-03-07 03:43:34 | 2021-03-08 04:36:34
20 | 1 | VW | 1 | 2021-03-07 07:21:34 | 2021-03-09 04:36:34
21 | 2 | Ford | 0 | 2021-03-07 11:37:34 | 2021-03-10 04:36:34
prices
id | shopId | productId | price | dDateAdded
-----------------------------------------------------
224 | 1 | 18 | 2385 | 2021-03-09 12:39:57
225 | 2 | 19 | 1523 | 2021-03-09 13:14:44
226 | 1 | 20 | 5489 | 2021-03-09 17:32:18
227 | 1 | 18 | 2256 | 2021-03-10 18:22:13
228 | 2 | 19 | 1600 | 2021-03-10 21:33:21
229 | 1 | 20 | 5321 | 2021-03-10 14:15:56
230 | 1 | 18 | 2137 | 2021-03-11 05:55:25
231 | 2 | 19 | 1666 | 2021-03-11 17:31:49
232 | 1 | 20 | 5001 | 2021-03-11 20:18:01
This command return only 1 latest record from prices table for every product from products table for specific shopId
SELECT s.*, c.*, d.*
FROM shops AS s
LEFT JOIN products AS c ON c.shopId = s.id
LEFT JOIN (
SELECT productId, MAX(dDateAdded) MaxDate
FROM prices
GROUP BY productId
) MaxDates
ON MaxDates.productId = c.id
LEFT JOIN prices AS d ON d.productId = c.id AND d.shopId = s.id AND MaxDates.MaxDate = d.dDateAdded
WHERE s.id = ".$shopId."
For example if shopId=1 this command get only that records (I omitted here the data from the other tables that are retrieved):
230 | 1 | 18 | 2137 | 2021-03-11 05:55:25
232 | 1 | 20 | 5001 | 2021-03-11 20:18:01
But i want to get for example 2 latest records for every product where shopId=1, so the records which i want to get:
(shops)id | (shops)shopId | title | active | price | dDateAdded
1 | 2345 | Honda | 1 | 2256 | 2021-03-10 18:22:13
1 | 2345 | Honda | 1 | 2137 | 2021-03-10 14:15:56
1 | 2345 | VW | 1 | 5321 | 2021-03-11 05:55:25
1 | 2345 | VW | 1 | 5001 | 2021-03-11 20:18:01
To select N latest rows needs to allocate row number and to filter by N rows. However, the ROW_NUMBER function is not supported in MySQL 5.7.
So that you need to simulate the ROW_NUMBER function like the follwing:
You can get the desired result by adding subquery with row number to your query like the below:
DB Fiddle
SELECT
s.id,
s.shopId,
c.title,
c.active,
d.price,
d.dDateAdded
FROM shops AS s
LEFT JOIN products AS c ON c.shopId = s.id
LEFT JOIN prices AS d ON d.productId = c.id AND d.shopId = s.id
--
LEFT JOIN (
SELECT
p1.id,
COUNT(p2.dDateAdded) + 1 row_num
FROM prices p1 LEFT JOIN prices p2
ON p1.shopId = p2.shopId AND
p1.productId = p2.productId AND
p1.dDateAdded < p2.dDateAdded
GROUP BY p1.id, p1.shopId, p1.productId, p1.dDateAdded
) AS w
ON d.id=w.id
--
WHERE
s.id = 1 AND
w.row_num <= 2
DB Fiddle
SELECT
id,
shopId,
productId,
price,
dDateAdded
FROM (
SELECT p1.*,
(
SELECT COUNT(*)+1 FROM prices p2
WHERE
p1.shopId = p2.shopId AND
p1.productId = p2.productId AND
p1.dDateAdded < p2.dDateAdded
) row_num
FROM prices p1
) p
WHERE
shopId = 1 AND
row_num <= 2
ORDER BY id
DB Fiddle
SELECT p.* FROM prices p
INNER JOIN (
SELECT
p1.id,
COUNT(p2.dDateAdded) + 1 row_num
FROM prices p1 LEFT JOIN prices p2
ON p1.shopId = p2.shopId AND
p1.productId = p2.productId AND
p1.dDateAdded < p2.dDateAdded
GROUP BY
p1.id,
p1.shopId,
p1.productId,
p1.dDateAdded
) w
ON p.id=w.id
WHERE
p.shopId = 1 AND
w.row_num <= 2
ORDER BY p.id
Other way using a variable

MySQL UNION ALL SELECT Issue on getting all the SUM per column

I wish to get the SUM of the product per column, not sure if the UNION ALL SELECT can handle the desired result considering that the first column is concatenated.
Here is the code:
SUM(product.product_id = 1) AS Soda,
SUM(product.product_id = 2) AS Liquor,
SUM(product.product_id = 3) AS Lemon,
SUM(product.product_id = 4) AS Mango,
SUM(product.product_id = 5) AS Inhaler,
SUM(1) AS Count
FROM line_item
JOIN product USING (product_id)
JOIN ( SELECT 0 lowest, 500 highest UNION
SELECT 501 , 1000 UNION
SELECT 1001 , 1500 UNION
SELECT 1501 , 2000 UNION
SELECT 2001 , 2500 ) ranges ON product.price * line_item.quantity BETWEEN ranges.lowest AND ranges.highest
GROUP BY ranges.lowest, ranges.highest
UNION ALL SELECT '','','','','','',
(
SELECT
COUNT(product.price * line_item.quantity)
FROM (line_item
INNER JOIN product ON line_item.product_id = product.product_id)
);
**The output:**
+-------------+------+--------+-------+-------+---------+-------+
| Revenue | Soda | Liquor | Lemon | Mango | Inhaler | Count |
+-------------+------+--------+-------+-------+---------+-------+
| 0 - 500 | 4 | 0 | 4 | 0 | 1 | 9 |
| 501 - 1000 | 0 | 0 | 0 | 2 | 0 | 2 |
| 1001 - 1500 | 0 | 1 | 0 | 2 | 2 | 5 |
| 1501 - 2000 | 0 | 2 | 0 | 0 | 1 | 3 |
| 2001 - 2500 | 0 | 1 | 0 | 0 | 0 | 1 |
| | | | | | | 20 |
+-------------+------+--------+-------+-------+---------+-------+
Thank for your help.
Have you tried ROLL UP operator? Seems like there's a similar problem to yours: Add a summary row with totals
I'm confused. You are defining the ranges already. If you want a range that encompasses all the values, just add it in:
FROM line_item JOIn
product
USING (product_id) JOIN
( SELECT 0 lowest, 500 highest UNION ALL
SELECT 501 , 1000 UNION ALL
SELECT 1001 , 1500 UNION ALL
SELECT 1501 , 2000 UNION ALL
SELECT 2001 , 2500 UNION ALL
SELECT 0 , 2500
-------^ all encompassing range
) ranges
ON product.price * line_item.quantity BETWEEN ranges.lowest AND ranges.highest
Voila! This also has the summary row.

Order multiple rows by field from most recent entry

I'm trying to return a list of users, ordered by a field (pt_seen) on the users most recent row.
Does that make sense?
So far I have:
SELECT u.users_id,
u.username,
ed.date
FROM users u
LEFT JOIN exercises_done ed
ON ed.user_id = u.users_id
WHERE u.pt_id = 1
GROUP BY u.users_id
Which obviously just returns the users grouped.
The tables look like this:
users:
users_id | pt_id | username
1 | 1 | billy
2 | 1 | bob
3 | 1 | sue
exercises_done:
exercises_done_id | user_id | pt_id | exercises_done_date | pt_seen
1 | 1 | 1 | 2018-01-01 | 0
2 | 1 | 1 | 2018-01-02 | 0
3 | 1 | 1 | 2018-01-03 | 1
4 | 2 | 1 | 2018-01-05 | 1
5 | 3 | 1 | 2018-01-04 | 0
and I'm trying to get results like this:
users_id | username | exercises_done_date | pt_seen
1 | billy | 2018-01-02 | 0
3 | sue | 2018-01-04 | 0
2 | bob | 2018-01-05 | 1
The aim is that I show users at the top of the list who have a pt_seen value of 0, then ordered by exercises_done_date.
Any help would be great,
You can select the most recent exercise in the where clause rather than using aggregation:
SELECT u.users_id, u.username, ed.*
FROM users u LEFT JOIN
exercises_done ed
ON ed.user_id = u.users_id
WHERE ed.exercises_done_date = (SELECT MAX(ed2.exercises_done_date)
FROM exercises_done ed2
WHERE ed2.user_id = ed.user_id
) AND
u.pt_id = 1
ORDER BY u.pt_seen, exercises_done_date DESC;
You can add an ORDER BY clause after your GROUP BY.
SELECT u.users_id, u.username, ed.date FROM
users u
LEFT JOIN exercises_done ed ON ed.user_id = u.users_id
WHERE
u.pt_id = 1
GROUP BY
u.users_id
ORDER BY
pt_seen, exercises_done_date

MySQL - Update table with row number per group

Sample Data
id | order_id | instalment_num | date_due
---------------------------------------------------------
1 | 10000 | 1 | 2010-07-09 00:00:00
2 | 10000 | 1 | 2010-09-06 11:39:56
3 | 10001 | 1 | 2014-04-25 15:46:52
4 | 10002 | 1 | 2010-01-11 00:00:00
5 | 10003 | 1 | 2010-01-04 00:00:00
6 | 10003 | 1 | 2016-05-31 00:00:00
7 | 10003 | 1 | 2010-01-08 00:00:00
8 | 10003 | 1 | 2010-01-06 09:06:26
9 | 10004 | 1 | 2010-01-11 11:25:07
10 | 10004 | 1 | 2010-01-12 07:06:42
Desired Result
id | order_id | instalment_num | date_due
---------------------------------------------------------
1 | 10000 | 1 | 2010-07-09 00:00:00
2 | 10000 | 2 | 2010-09-06 11:39:56
3 | 10001 | 1 | 2014-04-25 15:46:52
4 | 10002 | 1 | 2010-01-11 00:00:00
5 | 10003 | 1 | 2010-01-04 00:00:00
8 | 10003 | 2 | 2010-01-06 09:06:26
7 | 10003 | 3 | 2010-01-08 00:00:00
6 | 10003 | 4 | 2016-05-31 00:00:00
9 | 10004 | 1 | 2010-01-11 11:25:07
10 | 10004 | 2 | 2010-01-12 07:06:42
As you can see, I have an instalment_num column which should show the number/index of each row belonging to the order_id, determined by the date_due ASC, id ASC order.
How can I update the instalment_num column like this?
Additional Notes
The date_due column is not unique, and there may be many ids or order_ids with the exact same timestamp.
If the timestamp is the same for two rows belonging to the same order_id, it should order them by id as a fallback.
I require a query which will update this column.
This is how I would do it:
SELECT a.id,
a.order_id,
COUNT(b.id)+1 AS instalment_num,
a.date_due
FROM sample_data a
LEFT JOIN sample_data b ON a.order_id=b.order_id AND (a.date_due>b.date_due OR (a.date_due=b.date_due AND a.id>b.id))
GROUP BY a.id, a.order_id, a.date_due
ORDER BY a.order_id, a.date_due, a.id
UPDATE version attempt:
UPDATE sample_data
LEFT JOIN (SELECT a.id,
COUNT(b.id)+1 AS instalment_num
FROM sample_data a
JOIN sample_data b ON a.order_id=b.order_id AND (a.date_due>b.date_due OR (a.date_due=b.date_due AND a.id>b.id))
GROUP BY a.id) c ON c.id=sample_data.id
SET sample_data.instalment_num=c.instalment_num
For the numbering to begin with 1:
UPDATE sample_data
LEFT JOIN (SELECT a.id,
COUNT(b.id) AS instalment_num
FROM sample_data a
JOIN sample_data b ON a.order_id = b.order_id AND (a.date_due > b.date_due OR (a.date_due=b.date_due AND a.id + 1 > b.id))
GROUP BY a.id) c ON c.id = sample_data.id
SET sample_data.instalment_num = c.instalment_num
You are trying to achieve what ROW_NUMBER with a partition would do using something like SQL Server or Oracle. You can simulate this with an approriate query:
SELECT t.id, t.order_id,
(
SELECT 1 + COUNT(*)
FROM sampleData
WHERE (date_due < t.date_due OR (date_due = t.date_due AND id < t.id)) AND
order_id = t.order_id
) AS instalment_num,
t.date_due
FROM sampleData t
ORDER BY t.order_id, t.date_due
This query will order the instalment_num by due_date in ascending order. And in the case of a tie in due_date, it will order by the id in ascending order.
Follow the link below for a demo:
SQLFiddle
select
sub.order_id, sub.date_due,
#group_rn:= case
when #group_order_id=sub.order_id then #group_rn:=#group_rn:+1
else 1
end as instalment_num,
#group_order_id:=sub.order_id
FROM (select #group_rn:=0, group_order_id=0) init,
(select *
from the_table
order by order_id, date_due) sub