I have 2 tables
Parts table:
id | part_id | group_id
1 | | 1
2 | 1 | 1
3 | | 1
4 | | 2
Each part related to group and can be related to another part (analog)
Offers table:
offer_id | part_id | quantity
1 | 1 | 1
2 | 2 | 2
3 | 2 | 3
4 | 3 | 4
5 | 4 | 5
Each offer related to part
I need to get best offers grouped by part or its related part ordered by lowest / highest quantity.
For group_id 1, when ordered ASC result should be
offer_id | part_id | quantity
1 | 1 | 1
4 | 3 | 4
When ordered DESC result should be
offer_id | part_id | quantity
4 | 3 | 4
3 | 2 | 3
I tried this query
SELECT
pi.offer_id,
pi.part_id,
( SELECT pps.quantity
FROM offers AS pps
WHERE pps.offer_id = pi.offer_id
ORDER BY pps.quantity asc
LIMIT 1
) AS q
FROM offers AS pi
JOIN parts p ON p.id = pi.part_id
WHERE p.group_id = 1
GROUP BY part_id
ORDER BY q asc
Result:
offer_id | part_id | q
1 | 1 | 1
2 | 2 | 2
4 | 3 | 4
It does not group parts and related parts (1 and 2) and returns 3 rows instead of two. How do i fix it?
UPDATE
Is it possible if i change parts table data to this?
id | part_id | group_id
1 | 1 | 1
2 | 1 | 1
3 | 3 | 1
4 | 4 | 2
I also tried this query but it doesnt group by part_id too
SELECT a.id, a.part_id, a.quantity, p.part_id AS pid
FROM supplier_offers a
INNER JOIN
(
SELECT part_id, Min(quantity) AS qty
FROM supplier_offers
GROUP BY part_id
) b ON a.part_id = b.part_id AND a.quantity = b.qty
JOIN parts p ON p.id = a.part_id
WHERE p.cross_group_uuid = '78242c22-c113-4258-806c-936de014ba10'
ORDER BY a.quantity ASC
Result from real db
UPDATE 2
It seems only way is to save part part_id in offers table and group by it
I don't think that this is possible, because only with as ordering you can't achieve this.
As you can see in the fiddle example you have also to add a and p.part_id is NULL
CREATE TABLE offers
(`offer_id` int, `part_id` int, `quantity` int)
;
INSERT INTO offers
(`offer_id`, `part_id`, `quantity`)
VALUES
(1, 1, 1),
(2, 2, 2),
(3, 2, 3),
(4, 3, 4),
(5, 4, 5)
;
✓
✓
CREATE TABLE parts
(`id` int, `part_id` int, `group_id` int)
;
INSERT INTO parts
(`id`, `part_id`, `group_id`)
VALUES
(1, NULL, 1),
(2, 1, 1),
(3, NULL, 1),
(4, NULL, 2)
;
✓
✓
SELECT
o.offer_id,o.part_id, o.quantity
FROM offers o
inner JOIN parts p
ON p.id = o.part_id
WHERE group_id = 1 and p.part_id is NULL
offer_id | part_id | quantity
-------: | ------: | -------:
1 | 1 | 1
4 | 3 | 4
SELECT *
FROM
(SELECT
o.offer_id,o.part_id, o.quantity
FROM offers o
inner JOIN parts p
ON p.id = o.part_id
WHERE group_id = 1
ORDER by quantity
LIMIT 1) t1
union ALL
(SELECT
o.offer_id,o.part_id, o.quantity
FROM offers o
inner JOIN parts p
ON p.id = o.part_id
WHERE group_id = 1
ORDER by quantity DESC
LIMIT 1)
offer_id | part_id | quantity
-------: | ------: | -------:
1 | 1 | 1
4 | 3 | 4
SELECT
o.offer_id,o.part_id, o.quantity
FROM offers o
inner JOIN parts p
ON p.id = o.part_id
WHERE group_id = 1
ORDER BY o.offer_ID DESC
LIMIT 2
offer_id | part_id | quantity
-------: | ------: | -------:
4 | 3 | 4
3 | 2 | 3
SELECT
o.offer_id,o.part_id, o.quantity
FROM offers o
inner JOIN parts p
ON p.id = o.part_id
WHERE group_id = 1 and p.part_id is NULL
ORDER BY o.offer_ID ASC
LIMIT 2
offer_id | part_id | quantity
-------: | ------: | -------:
1 | 1 | 1
4 | 3 | 4
SELECT
MIN(pi.offer_id),
pi.part_id,
MIN( ( SELECT pps.quantity
FROM offers AS pps
WHERE pps.offer_id = pi.offer_id
ORDER BY pps.quantity DESC
LIMIT 1
)) AS q
FROM offers AS pi
JOIN parts p ON p.id = pi.part_id
WHERE p.group_id = 1 and p.part_id IS NULL
GROUP BY part_id
ORDER BY q asc
MIN(pi.offer_id) | part_id | q
---------------: | ------: | -:
1 | 1 | 1
4 | 3 | 4
db<>fiddle here
Related
im new in sql. I cannot get data with format what i want in one step. Now i'm using more sql commands. I want to get all data in one command because i cant to connect them in subquery with group by. Somebodys can help me?
example of Table i have:
id
order_id
order_status
1
1
0
2
1
0
3
1
0
4
1
1
5
1
1
6
2
0
7
2
0
8
2
1
Table i want to have after sql query:
order_id
count
of
progress(%)
1
2
5
40
2
1
3
33
queries i use:
SELECT order_id, COUNT(status) as count
FROM `orders`
WHERE status = 1
GROUP by order_id;
SELECT order_id, COUNT(status) as of
FROM `orders`
GROUP by order_id;
SELECT order_id,
CAST((SELECT COUNT(status) FROM `orders` WHERE status = 1) /
(SELECT COUNT(status) FROM `orders`) *100 as int) AS progress FROM orders
group by order_id;
but last working properly only if i use where to single order id.
I want to make this data in one sql query to format i showed up.
Thanks a lot guys!
You don't need subqueries to do this, SQL's ordinary aggregate functions already work as you want with your group by clause:
SELECT order_id,
SUM(order_status) AS `count`,
COUNT(*) AS `of`,
SUM(order_status) / COUNT(order_status) * 100 as `progress`
FROM orders
group by order_id;
See example at http://sqlfiddle.com/#!9/d1799db/4/0
you need to use multiple subqueries
here's a query that I used and worked on your example on the onecompiler.com website
-- create
CREATE TABLE EMPLOYEE (
order_id INTEGER,
order_status INTEGER
);
-- insert
INSERT INTO EMPLOYEE VALUES (1,0 );
INSERT INTO EMPLOYEE VALUES (1, 0);
INSERT INTO EMPLOYEE VALUES (1, 0);
INSERT INTO EMPLOYEE VALUES (1, 1);
INSERT INTO EMPLOYEE VALUES (1,1 );
INSERT INTO EMPLOYEE VALUES (2, 0);
INSERT INTO EMPLOYEE VALUES (2, 0);
INSERT INTO EMPLOYEE VALUES (2, 1);
select *
from EMPLOYEE;
SELECT order_id, count, off , count/off
from(
select distinct order_id as order_id,
(select count(order_id) from EMPLOYEE C WHERE A.order_id=C.order_id AND order_status =1) as 'count',
(select count(order_id) from EMPLOYEE B WHERE A.order_id=B.order_id ) as 'off'
FROM EMPLOYEE A
) AA
;
You need to use sum and count with group by.
create table orders(
id int,
order_id int,
order_status int);
insert into orders values
(1,1,0),
(2,1,0),
(3,1,0),
(4,1,1),
(5,1,1),
(6,2,0),
(7,2,0),
(8,2,1);
select
order_id,
sum(order_status) count,
count(order_id) "of",
(100 * sum(order_status))
/ count(order_id) progress
from orders
group by order_id
order by order_id;
order_id | count | of | progress
-------: | ----: | -: | -------:
1 | 2 | 5 | 40.0000
2 | 1 | 3 | 33.3333
db<>fiddle here
i was described my problem without some details, w i want to join with other table but i see only record with status
oders_details
| id | order_describe | order_date |
|:----:|:--------------:|:----------:|
| 1 | sample 1 | 2022-02-28 |
| 2 | sample 2 | 2022-02-28 |
| 3 | sample 3 | 2022-03-01 |
| 4 | sample 4 | 2022-03-02 |
orders_status
| id | order_id |order_status|
|:---:|:---------------:|:----------:|
| 1 | 1 | 0 |
| 2 | 1 | 0 |
| 3 | 1 | 0 |
| 4 | 1 | 1 |
| 5 | 1 | 1 |
| 6 | 2 | 0 |
| 7 | 2 | 0 |
| 8 | 2 | 1 |
table i want after query
orders_view
| id |order_id|order_describe| order_date | count | of | progress |
|-----|--------|--------------|------------|-------|----|:--------:|
| 1 | 1 | sample 1 | 2022-02-28| 2 | 5 | 40 |
| 2 | 2 | sample 2 | 2022-02-28| 1 | 3 | 33 |
| 3 | 3 | sample 3 | 2022-03-01| null |null| null |
| 4 | 4 | sample 4 | 2022-03-02| null |null| null |
i want to get some hint what i have todo, to get finally table or view, not complete solution, to better understand sql lang
What I have
So I'm running this statement:
SELECT
i.id,
i.item_id,
v.item_to_map_id,
i.date,
COALESCE( SUM(CAST(CAST(v.score AS char) AS SIGNED)), 0 ) AS score
FROM item_to_map i
LEFT JOIN
vote_item v
ON i.id = v.item_to_map_id
GROUP BY
i.id, i.item_id, i.date, v.item_to_map_id
ORDER BY
item_id asc, score desc;
And I'm getting the following table:
+----+---------+----------------+---------------------+-------+
| id | item_id | item_to_map_id | date | score |
+----+---------+----------------+---------------------+-------+
| 1 | 1 | 1 | 2017-07-05 09:38:23 | 3 |
| 3 | 1 | 3 | 2017-07-05 09:38:23 | 0 |
| 2 | 1 | 2 | 2017-07-05 09:38:23 | -1 |
| 4 | 2 | NULL | 2017-07-05 09:38:23 | 0 |
| 5 | 2 | NULL | 2017-07-05 09:38:23 | 0 |
| 6 | 2 | NULL | 2017-07-05 09:38:24 | 0 |
+----+---------+----------------+---------------------+-------+
What I'm trying to do is select the first X of the repeated item_ids based on some ordering, for example, score or date.
What I've tried
I looked at this answer https://stackoverflow.com/a/1902167/6554121 and tried a modified version:
SELECT
i.id,
i.item_id,
v.item_to_map_id,
i.date,
COALESCE( SUM(CAST(CAST(v.score AS char) AS SIGNED)), 0 ) AS score
FROM item_to_map i
LEFT JOIN
vote_item v
ON i.id = v.item_to_map_id
WHERE
(
SELECT
COUNT(*)
FROM
item_to_map i2
WHERE
i2.item_id = i.item_id
) < 3
GROUP BY
i.id, i.item_id, i.date, v.item_to_map_id
ORDER BY item_id asc, score desc;
However this returns me no results
What I expected
If ordered by score:
+----+---------+----------------+---------------------+-------+
| id | item_id | item_to_map_id | date | score |
+----+---------+----------------+---------------------+-------+
| 1 | 1 | 1 | 2017-07-05 09:38:23 | 3 |
| 3 | 1 | 3 | 2017-07-05 09:38:23 | 0 |
| 4 | 2 | NULL | 2017-07-05 09:38:23 | 0 |
| 5 | 2 | NULL | 2017-07-05 09:38:23 | 0 |
+----+---------+----------------+---------------------+-------+
You can achieve this using session variables which simulate row number functionality:
SET #row_number = 0;
SET #item_id = 1;
SELECT t.id, t.item_id, t.item_to_map_id, t.date, t.score
FROM
(
SELECT
#row_number:=CASE WHEN #item_id = t.item_id
THEN #row_number + 1 ELSE 1 END AS rn,
#item_id:=t.item_id AS item_id,
t.id, t.item_to_map_id, t.date, t.score
FROM
(
SELECT
i.id,
i.item_id,
v.item_to_map_id,
i.date,
COALESCE( SUM(CAST(CAST(v.score AS char) AS SIGNED)), 0 ) AS score
FROM item_to_map i
LEFT JOIN vote_item v
ON i.id = v.item_to_map_id
GROUP BY
i.id, i.item_id, i.date, v.item_to_map_id
) t
ORDER BY
t.item_id, t.score DESC
) t
WHERE t.rn <= 2 -- this restricts to the first two rows per item_id group
-- as ordered by the logic in your ORDER BY clause
As far as I know, there is no nice way to get the first X records of a group in MySQL, unless your schema coincidentally happens to have row numbers already for each group. Using session variables as above is one way to handle this, and the performance might even be good as well.
Demo here:
Rextester
I have the following table:
+----+-----------+-----------+
| id | teacherId | studentId |
+----+-----------+-----------+
| 1 | 1 | 4 |
| 2 | 1 | 2 |
| 3 | 1 | 1 |
| 4 | 1 | 3 |
| 5 | 2 | 2 |
| 6 | 2 | 1 |
| 7 | 2 | 3 |
| 8 | 3 | 9 |
| 9 | 3 | 6 |
| 10 | 1 | 6 |
+----+-----------+-----------+
I need a query to find two teacherId's with maximum number of common studentId's.
In this case teachers with teacherIds 1,2 have common students with studentIds 2, 1, 3, which is greater than 1,3 having common students 6.
Thanks in Advance!
[Edit]: After several hours I've had the following solution:
SELECT * FROM (
SELECT r1tid, r2tid, COUNT(r2tid) AS cnt
FROM (
SELECT r1.teacherId AS r1tid, r2.teacherId AS r2tid
FROM table r1
INNER JOIN table r2 ON r1.studentId=r2.studentId AND r1.teacherId!=r2.teacherId
ORDER BY r1tid
) t
GROUP BY r1tid, r2tid
ORDER BY cnt DESC
) t GROUP BY cnt ORDER BY cnt DESC LIMIT 1;
I was sure that there must exist more short and elegant solution, but I could not find it.
You would do this with a self-join. Assuming no duplicates in the table:
select t.teacherid, t2.teacherid, count(*) as NumStudentsInCommon
from table t join
table t2
on t.studentid = t2.studentid and
t.teacherid < t2.teacherid
group by t.teacherid, t2.teacherid
order by NumStudentsInCommon desc
limit 1;
If you had duplicates, you would just replace count(*) with count(distinct studentid), but count(distinct) requires a bit more work.
select t.teacherId, t2.teacherId, sum(t.studentId) as NumStudentsInCommon
from table1 t join
table1 t2
on t.studentId = t2.studentId and
t.teacherId < t2.teacherId
group by t.teacherId, t2.teacherId
order by NumStudentsInCommon desc
I have two tables in MySQL:
Products:
id | value
================
1 | foo
2 | bar
3 | foobar
4 | barbar
And properties:
product_id | property_id
=============================
1 | 10
1 | 11
2 | 15
2 | 16
3 | 10
3 | 11
4 | 10
4 | 16
I want to get products that have determined properties.
For example I need to get all products that have properties with ids 10 and 11. And I expect products with ids 1 and 3 but not 4!
Is it possible in mysql or I need to use PHP for it?
Thank you!
with ids 10 and 11
Here's 2 solutions:
SELECT p.id,
p.value,
Count(DISTINCT propety_id)
FROM products p
INNER JOIN properties pr
ON p.id = pr.product_id
AND propety_id IN ( 10, 11 )
HAVING Count(DISTINCT propety_id) = 2;
or....
SELECT p.id,
p.value
FROM products p
INNER JOIN properties pr1
ON p.id = pr2.product_id
AND pr1.propety_id = 10
INNER JOIN properties pr2
ON p.id = pr2.product_id
AND pr2.propety_id = 11;
As for excluding rows - add a NOT exists clause, or do an additional left join and exclude matching rows.
SELECT *
FROM [products]
WHERE id IN (SELECT product_id
FROM [properties]
WHERE propety_id IN ( '10', '11' )
HAVING Count(DISTINCT propety_id) = 2);
Try this:
SELECT p.id
FROM product p
INNER JOIN properties prop
ON p.id = prop.product_id
AND property_id IN ( 10, 11 )
GROUP BY p.id
HAVING Count(DISTINCT property_id) = 2
Here is how I solved it:
mysql> SELECT * FROM products;
+----+--------+
| id | value |
+----+--------+
| 1 | foo |
| 2 | bar |
| 3 | foobar |
| 4 | barbar |
+----+--------+
4 rows in set (0.00 sec)
mysql> SELECT * FROM properties;
+------------+-------------+
| product_id | property_id |
+------------+-------------+
| 1 | 10 |
| 1 | 11 |
| 2 | 15 |
| 2 | 16 |
| 3 | 10 |
| 3 | 11 |
| 4 | 10 |
| 4 | 16 |
+------------+-------------+
8 rows in set (0.00 sec)
Now we select all the product ids, that have property_ids IN (10, 11) and having 2 distinct rows for property_id:
mysql> SELECT
product_id FROM properties
WHERE
properties.property_id IN (10, 11)
GROUP BY
product_id
HAVING
COUNT(DISTINCT property_id) = 2;
+------------+
| product_id |
+------------+
| 1 |
| 3 |
+------------+
2 rows in set (0.01 sec)
Combining this query with SELECT-ing from products:
mysql> SELECT
id, value
FROM
products
WHERE
products.id IN(
SELECT
product_id FROM properties
WHERE
properties.property_id IN (10, 11)
GROUP BY
product_id
HAVING
COUNT(DISTINCT property_id) = 2);
+----+--------+
| id | value |
+----+--------+
| 1 | foo |
| 3 | foobar |
+----+--------+
2 rows in set (0.00 sec)
sql fiddle
Try this one
SELECT Prd.*
FROM products As Prd
LEFT JOIN (SELECT product_id ,SUM(RStatus) As Tt
FROM (SELECT product_id,
CASE
WHEN propety_id = 10 THEN NULL
WHEN propety_id = 11 THEN NULL
ELSE 0
END As RStatus
FROM properties
) A
GROUP BY product_id
) AS Prt ON(Prd.ID = Prt.product_id )
WHERE Prt.Tt IS NULL
I have the following table: ProductSales
+-------+-----------+--------+-----------+
|prod_id|customer_id|order_id|supplier_id|
+-------+-----------+--------+-----------+
| 1 | 1 | 1 | 1 |
+-------+-----------+--------+-----------+
| 2 | 4 | 2 | 2 |
+-------+-----------+--------+-----------+
| 3 | 1 | 1 | 1 |
+-------+-----------+--------+-----------+
| 4 | NULL | NULL | Null |
+-------+-----------+--------+-----------+
| 5 | 1 | 1 | 2 |
+-------+-----------+--------+-----------+
| 6 | 4 | 7 | 1 |
+-------+-----------+--------+-----------+
| 7 | 1 | 1 | 3 |
+-------+-----------+--------+-----------+
I have a SELECT query:
SELECT customer_id AS customer, count(*) AS prod_count
, count(DISTINCT order_id) as orders
FROM ProductSales
WHERE supplier_id=1
GROUP BY customer_id
HAVING customer_id<>'NULL'
This will be produce the result:
+--------+----------+------+
|customer|prod_count|orders|
+--------+----------+------+
| 1 | 2 | 1 |
+--------+----------+------+
| 4 | 1 | 1 |
+--------+----------+------+
What I have been trying to achieve and getting nowhere is to add a fourth column in my results to show the number of order_ids that belong only to the current supplier for each customer:
+--------+----------+------+-------------+
|customer|prod_count|orders|Unique Orders|
+--------+----------+------+-------------+
| 1 | 2 | 1 | 0 | } Order '1' is connected with two supplier_ids
+--------+----------+------+-------------+
| 4 | 1 | 1 | 1 | } Order '2' is connected to only one supplier_id
+--------+----------+------+-------------+
(This gets more complex when there are more orders per customer associated with far more suppliers).
I thought I was close with:
SELECT t1.user_id, count(DISTINCT t1.prod_id) AS prod_count
, count(DISTINCT t1.order_id) as orders
, IF(count(DISTINCT t3.supplier_id)>1,0,1) AS Unique_Orders
FROM ProductSales AS t1
LEFT JOIN `order` AS t2 ON t1.order_id=t2.order_id
LEFT JOIN ProductSales AS t3 ON t2.order_id=t3.order_id
WHERE t1.supplier_id=1
GROUP BY t1.customer_id
HAVING t1.customer_id<>'NULL'
The orders table stated above is related to ProductSales only by order_id.
Which shows my Customers, Products(total), Orders(total) but the Unique Orders shows if there are unique orders (0) or not (1), I understand the logic of the IF statement and it does what I expect. It's working out how to find the number of unique orders which is baffling me.
The table is established and can't be changed.
Any suggestions?
Unique orders can be defined as
SELECT OrderID
FROM yourtable
GROUP BY OrderID
Having COUNT(Distinct SupplierID) = 1
So try
SELECT
customer_id AS customer,
count(*) AS prod_count.
count(DISTINCT productsales.order_id) as orders,
COUNT(distinct uqo)
FROM ProductSales
left join
(
SELECT Order_ID uqo
FROM Productsales
GROUP BY Order_ID
Having COUNT(Distinct supplier_id) = 1
) uniqueorders
on ProductSales.order_id = uniqueorders.uqo
WHERE supplier_id=1
GROUP BY customer_id