I have an issue with a request, it works fine with pgsql and mysql, but with sqlite, it seems like I am missing something
here is the request :
select * from mesure_insitu
where (id_formulaire, gid) IN (
select distinct id_formulaire, max(gid) as gid
from mesure_insitu
where id_dispo_comp_ouvr = 1
GROUP BY id_formulaire
ORDER BY id_formulaire ASC
)
This subquery below works fine:
select distinct id_formulaire, max(gid) as gid
from mesure_insitu
where id_dispo_comp_ouvr = 1
GROUP BY id_formulaire
ORDER BY id_formulaire ASC)
I think the problem is with the condition with two values (id_formulaire, gid), like sqlite cannot use a condition with two values.
I will appreciate any type of help.
To get this query to work, update your SQLite to a newer version.
If you cannot do this, you have to use a join instead:
SELECT *
FROM mesure_insitu
JOIN (SELECT id_formulaire, max(gid) AS gid
FROM mesure_insitu
WHERE id_dispo_comp_ouvr = 1
GROUP BY id_formulaire)
USING (id_formulaire, gid);
(The DISTINCT and ORDER BY are superfluous in this subquery.)
Related
I am trying to produce a result that shows duplicates in a table. One method I found for getting duplicates and showing them is to run the select statement again through an inner join. However, one of my columns needs to be the result of a function, and the only thing I can think to do is use an alias, however I can't use the alias twice in a SELECT statement.
I am not sure what the best way to run this code for getting the duplicates I need.
My code below
SELECT EXTRACT(YEAR_MONTH FROM date) as 'ndate', a.transponderID
FROM dispondo_prod_disposition.event a
inner JOIN (SELECT EXTRACT(YEAR_MONTH FROM date) as ???,
transponderID, COUNT(*)
FROM dispondo_prod_disposition.event
GROUP BY mdate, transponderID
HAVING count(*) > 1 ) b
ON ndate = ???
AND a.transponderID = b.transponderID
ORDER BY b.transponderID
SELECT b.ndate, transponderID
FROM dispondo_prod_disposition.event a
INNER JOIN ( SELECT EXTRACT(YEAR_MONTH FROM date) as ndate,
transponderID
FROM dispondo_prod_disposition.event
GROUP BY 1, 2
HAVING COUNT(*) > 1 ) b USING (transponderID)
WHERE b.ndate = ??? -- for example, WHERE b.ndate = 202201
ORDER BY transponderID
SELECT
GROUP_CONCAT( DISTINCT unit.`name` ORDER BY unit.`order_index` ASC SEPARATOR '/' ) AS nameGroup
FROM
pms_spec_unit unit
JOIN pms_spec spec ON spec.id = unit.spec_id
WHERE
spec.`status` = 1
GROUP BY
unit.spec_id
HAVING
nameGroup LIKE '%年%'
duplicate result
The results I got
mysql version 8.0.25
I solved this issue thanks to Akina's comment. Actually unit.spec_id is different. Use SELECT DISTINCT GROUP_CONCAT(....
I want to get list of posts sorted by number of comments, I've successfully ran following query but it gives repetitive values i.e posts repeat, I want unique of them sorted by number of comments, when I put DISTINCT() around my whole query, an error appears:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UNIQUE(post.pname, post.pid FROM post, COMMENT WHERE post.pid = comment.pid ORD' at line 1
Query without DISTINCT() (Works but of course doesn't give unique values)
SELECT post.pname, post.pid
FROM post,
COMMENT WHERE post.pid = comment.pid
ORDER BY (
SELECT COUNT( * )
FROM COMMENT WHERE comment.pid = post.pid
GROUP BY post.pname
)
Query with DISTINCT() (doesn't work)
SELECT DISTINCT(post.pname, post.pid
FROM post,
COMMENT WHERE post.pid = comment.pid
ORDER BY (
SELECT COUNT( * )
FROM COMMENT WHERE comment.pid = post.pid
GROUP BY post.pname
))
DISTINCT should be used thus:
SELECT DISTINCT a,b,c FROM t;
without a GROUP BY. It will find all the (a,b,c) in the table, then de-dup them.
This is broken:
SELECT id, a, b FROM t GROUP BY id;
That is because it will find all the distinct values of id, but supply random values of a and b to go with each.
To find out how many of each foo there are, this pattern works nicely:
SELECT foo, COUNT(*) FROM t GROUP BY foo;
Don't use () after DISTINCT.
Since I don't understand what you are looking for, I may or may not have provided you enough info to fix your query. If I have failed, please provide some sample data and the desired output; sometimes reverse engineering is the easiest way to figure it out.
SELECT distinct(post.pname) FROM post,COMMENT WHERE post.pid = comment.pid ORDER BY (SELECT COUNT( * ) FROM COMMENT WHERE comment.pid = post.pid GROUP BY post.pname) DESC
SELECT post.pname, post.pid
FROM post
Inner Join ( Select
comment.pid
, COUNT(*) As Cant
From COMMENT
Group By
comment.pid
) As x
On post.pid = x.pid
ORDER BY x.Cant
SELECT p.*
FROM post p JOIN comment c ON c.postId = p.id
GROUP BY p.id
ORDER BY COUNT(*)
I have couple tables joined in MySQL - one has many others.
And try to select items from one, ordered by min values from another table.
Without grouping in seems to be like this:
Code:
select `catalog_products`.id
, `catalog_products`.alias
, `tmpKits`.`minPrice`
from `catalog_products`
left join `product_kits` on `product_kits`.`product_id` = `catalog_products`.`id`
left join (
SELECT MIN(new_price) AS minPrice, id FROM product_kits GROUP BY id
) AS tmpKits on `tmpKits`.`id` = `product_kits`.`id`
where `category_id` in ('62')
order by product_kits.new_price ASC
Result:
But when I add group by, I get this:
Code:
select `catalog_products`.id
, `catalog_products`.alias
, `tmpKits`.`minPrice`
from `catalog_products`
left join `product_kits` on `product_kits`.`product_id` = `catalog_products`.`id`
left join (
SELECT MIN(new_price) AS minPrice, id FROM product_kits GROUP BY id
) AS tmpKits on `tmpKits`.`id` = `product_kits`.`id`
where `category_id` in ('62')
group by `catalog_products`.`id`
order by product_kits.new_price ASC
Result:
And this is incorrect sorting!
Somehow when I group this results, I get id 280 before 281!
But I need to get:
281|1600.00
280|2340.00
So, grouping breaks existing ordering!
For one, when you apply the GROUP BY to only one column, there is no guarantee that the values in the other columns will be consistently correct. Unfortunately, MySQL allows this type of SELECT/GROUPing to happen other products don't. Two, the syntax of using an ORDER BY in a subquery while allowed in MySQL is not allowed in other database products including SQL Server. You should use a solution that will return the proper result each time it is executed.
So the query will be:
For one, when you apply the GROUP BY to only one column, there is no guarantee that the values in the other columns will be consistently correct. Unfortunately, MySQL allows this type of SELECT/GROUPing to happen other products don't. Two, the syntax of using an ORDER BY in a subquery while allowed in MySQL is not allowed in other database products including SQL Server. You should use a solution that will return the proper result each time it is executed.
So the query will be:
select CP.`id`, CP.`alias`, TK.`minPrice`
from catalog_products CP
left join `product_kits` PK on PK.`product_id` = CP.`id`
left join (
SELECT MIN(`new_price`) AS "minPrice", `id` FROM product_kits GROUP BY `id`
) AS TK on TK.`id` = PK.`id`
where CP.`category_id` IN ('62')
order by PK.`new_price` ASC
group by CP.`id`
The thing is that group by does not recognize order by in MySQL.
Actually, what I was doing is really bad practice.
In this case you should use distinct and by catalog_products.*
In my opinion, group by is really useful when you need group result of agregated functions.
Otherwise you should not use it to get unique values.
I'm sure this will be quite simple for some one clued up in SQL but I think it needs a sub query or something. I have a table which basically has a load of order numbers in it and a reply column from an XML API. Either FAIL or SUCCESS.
A brand new row is inserted into the DB after every request. So there may be 5 FAILS for one order number, and on the 6th attempt a record is inserted saying SUCCESS.
How can I put out order numbers that ONLY have a FAIL status next to them?
This will allow me to figure out what records need looking into that continuously fail in the API request.
Try this, by grouping your orders with primary key (order_id)
SELECT * FROM
(
SELECT GROUP_CONCAT(status) as status_combined, order_id
FROM orders
GROUP BY order_id
) AS order_tmp
WHERE status_combined NOT LIKE '%SUCCESS%'
Edit (As per asker comments)
SELECT * FROM
(
SELECT GROUP_CONCAT(status) as status_combined, order_id
FROM orders
JOIN certificates ON certificates.Ordernumber = orders.OrderNumber
GROUP BY order_id
) AS order_tmp
WHERE status_combined NOT LIKE '%SUCCESS%'
please make sure you need to join based on "Ordernumber" or "order_id"
Try this
select m.*
from Main m
join Transactiontable tt
on m.orderid = tt.orderid
group by tt.status , m.orderid
having count(case when tt.status = "failed") = count(tt.status)
You can use simple sql query using a where clause:
select *
from some_table
where Column_From_some_table_has_value = your_particular_value
thats enough.
You can have a look at How to use where clause in sql
Thanks
This is probably the cleanest way to do it:
select *
from mytable
where id in (
select id
from mytable
group by id
having sum(status = 'SUCCESS') = 0)
I'm not a fan of #Minesh's answer because it uses both an aggregate function and the LIKE operator. Both of those can cause performance issues since there won't be any indexes to help the query out with the difficult part of the work. The LIKE clause particularly is a lot of work for the database since it will need to scan every result.
I'm more familiar with SQL Server, but this should work well for you:
SELECT *
FROM Orders
WHERE OrderNumber NOT IN (
SELECT OrderNumber
FROM Orders
WHERE Status = 'SUCCESS')
AND OrderNumber NOT IN (
SELECT OrderNumber
FROM Certificates
WHERE OrderStatus = 'CANCELLED')