I am trying this query:
SELECT * FROM heath_check where cid = '1' and eid in('3','5','7','1','6')
My table structure:
I want distinct eid but all other data as it is. For example I have two entries with an eid of 1 my query fetched both, but I want one which is in the second column.
SELECT *
FROM heath_check AS hc
INNER JOIN (
SELECT MAX(id) AS lastId
FROM heath_check
WHERE cid = '1' and eid in('3','5','7','1','6')
GROUP BY eid) AS lastIDs
ON hc.id = lastIDs.lastId
;
You need a subquery, like the above, to find the records you want for each value. If you had wanted the first ones, you could use MIN(id) instead; if you cannot count on sequential ids, it becomes much more complex with use of potentially non-unique timestamps (if they are even available).
Create a RowNumber grouped by eid and filter the RowNumber = 1 to get the expected result.
SELECT id, eid, cid,weight, s_blood_pressure
FROM (
SELECT *, ROW_NUMBER() OVER (PARTITION BY eid ORDER BY id DESC) AS RowNumber
FROM heath_check
WHERE cid = '1' AND eid IN ('3','5','7','1','6')
) A
WHERE RowNumber = 1
Related
Table: MyTable
[
Here is the result I desire:
For every ID in the table, count how many times that ID appears in the column Parent_ID.
Create a custom column AS Instances to place the result.
My desired Result
I imagine to get the above result with something not more complicated than a working version of the following query:
SELECT ID, Parent_ID, COUNT( Parent_ID = ID ) AS Instances FROM MyTable
You can use a scalar subquery to compute the extra column. For example:
select
id,
parent_id,
(
select count(*) from my_table b where b.parent_id = a.id
) as instances
from my_table a
A correlated subquery is the simplest solution:
select t.*,
(select count(*) from mytable t2 where t2.parent_id = t.id) as instances
from mytable t;
I have the following data:
I want to retrieve the ids that match the highest date (represented by max(folga)), grouping by funcionario_id.
Here's the output table I want, but it's missing the id:
How can I achieve this?
Thanks for the attention.
One way to do it is with NOT EXISTS:
select
t.funcionario_id, t.folga, t.id
from tablename t
where not exists (
select 1 from tablename
where funcionario_id = t.funcionario_id and folga > t.folga
)
or you can group by funcionario_id first to get the max date (I guess this is the query that returned the result you posted) and then join to the table:
select t.*
from tablename t inner join (
select funcionario_id, max(folga) folga
from tablename
group by funcionario_id
) g on g.funcionario_id = t.funcionario_id and g.folga = t.folga
SO basically, i want to return "cid" (customer id's) of users who has ordered a certain product "p07 & p01".
My currently query don't work, it only works when i check for one value instead of two. But i need it to check for two. The return value i get is basically an empty space. My guess is, it doesn't know which "pid" to use so it returns nothing.
SELECT cid FROM orders
WHERE pid = 'p07' AND pid = 'p01'
You can use aggregation for this purpose. No single row can have both values, so you need to look at groups of them:
SELECT cid
FROM orders
WHERE pid IN ('p07', 'p01')
GROUP BY cid
HAVING COUNT(DISTINCT pid) = 2;
If you are using Oracle database than you can use the INTERSECT keyword to get the answer. Following is the syntax for the same:
(SELECT cid FROM orders where pid = 'p07')
intersect
(SELECT cid FROM orders where pid = 'p01').
but if you are using MySQL, than this INTERSECT keyword will not work.
Following is the solution for the MySql database:
SELECT t1.cid from (
(SELECT DISTINCT cid FROM orders where pid = 'p07')
UNION ALL
(SELECT DISTINCT cid FROM orders where pid = 'p01')
) AS t1 GROUP BY cid HAVING count(*) >= 2;
If you want to use the above query in the Oracle database than there is a bit change in the syntax. Following is the syntax for the same:
SELECT t1.cid from (
(SELECT DISTINCT cid FROM orders where pid = 'p07')
UNION ALL
(SELECT DISTINCT cid FROM orders where pid = 'p01')
) t1 GROUP BY cid HAVING count(*) > = 2
I have a MySQL table where I have a certain id as a foreign key coming from another table. This id is not unique to this table so I can have many records holding the same id.
I need to find out which ids are seen the least amount of times in this table and pull up a list containing them.
For example, if I have 5 records with id=1, 3 records with id=2 and 3 records with id=3, I want to pull up only ids 2 & 3. However, the data in the table changes quite often so I don't know what that minimum value is going to be at any given moment. The task is quite trivial if I use two queries but I'm trying to do it with just one. Here's what I have:
SELECT id
FROM table
GROUP BY id
HAVING COUNT(*) = MIN(SELECT COUNT(*) FROM table GROUP BY id)
If I substitute COUNT(*) = 3, then the results come up but using the query above gives me an error that MIN is not used properly. Any tips?
I would try with:
SELECT id
FROM table
GROUP BY id
HAVING COUNT(*) = (SELECT COUNT(*) FROM table GROUP BY id ORDER BY COUNT(*) LIMIT 1);
This gets the minimum selecting the first row from the set of counts in ascendent order.
You need a double select in the having clause:
SELECT id
FROM table
GROUP BY id
HAVING COUNT(*) = (SELECT MIN(cnt) FROM (SELECT COUNT(*) as cnt FROM table GROUP BY id) t);
The MIN() aggregate function is suposed to take a column, not a query. So, I see two ways to solve this:
To properly write the subquery, or
To use temp variables
First alternative:
select id
from yourTable
group by id
having count(id) = (
select min(c) from (
select count(*) as c from yourTable group by id
) as a
)
Second alternative:
set #minCount = (
select min(c) from (
select count(*) as c from yourTable group by id
) as a
);
select id
from yourTable
group by id
having count(*) = #minCount;
You need to GROUP BY to produce a set of grouped values and additional select to get the MIN value from that group, only then you can match it against having
SELECT * FROM table GROUP BY id
HAVING COUNT(*) =
(SELECT MIN(X.CNT) AS M FROM(SELECT COUNT(*) CNT FROM table GROUP BY id) AS X)
This is structure of product table.
Currently have more 1 million records.
I have performance issue when I use query group by & order by.
Query:
SELECT product_name FROM vs_product GROUP BY store_id ORDER BY id DESC LIMIT 2
How to improve this query to perform faster? I indexed the store_id, ID is primary key.
SELECT x.*
FROM my_table x
JOIN (SELECT store_id, MAX(id) max_id FROM my_table GROUP BY store_id) y
ON y.store_id = x.store_id
AND y.max_id = x.id
ORDER
BY store_id DESC LIMIT 2;
A hacky (but fast) solution:
SELECT product_name
FROM (
SELECT id
FROM vs_product
GROUP BY store_id DESC
LIMIT 2) as ids
JOIN vs_product USING (id);
How it works:
Your index on store_id stores (store_id, id) pairs in ascending order. GROUP BY DESC will make MySQL read the index in reverse order, that is the subquery will fetch the maximum ids for each store_id. Then you just join them back to the whole table to fetch product names.
Take notice, that the query will fetch two product names for the store ids with the maximum values.
You want a query like this:
select p.*
from product p join
(select store_id, max(id) as maxid
from product p
group by store_id
) psum
on psum.store_id = p.store_id and p.id = maxid
You don't have date in any of the tables, so I'm assuming the largest id is the most recent.