Get distinct rows along with sum of field - mysql

My table structure is like this shown in below URL:
http://tinypic.com/r/sm3xbb/8
I want to write a MySQL query to get sum of count field of all such rows which have same hash value but have distinct date value.
This can be done with Group by statement but I won't get all rows if I will perform group by on hash field.
Example: Rows with ID 1,4 and 8 have same hash value in table, so I want to retrieve all 3 rows along with SUM of count field.

Another way to do it with a subquery
SELECT t.*,
(
SELECT SUM(count)
FROM table1
WHERE hash = t.hash
) total
FROM table1 t
Here is SQLFiddle demo

You can try something like this
select t.*,t1.sum_count
from mytable t
join
(
select hash, sum(`count`) as sum_count
from mytable group by hash
) t1 on t.hash = t1.hash

Related

For every ID in the table, count how many times that ID appears in another Column. MySQL

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;

SQL to retrieve id that match the highest date, grouped by another field

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

Group by display row with highet value column

I have a table opname like this:
I want to select table opname with group by based id_barang and I want to display the row with the highest id of group by result.
I wrote query like this
SELECT
MAX(id), id_barang, tgl, ket, kondisi
FROM
`opname`
GROUP BY
id_barang
but why value of columns tgl, ket, kondisi it's not match with max(id) - how to fix that? I want to display row with the highest id of group by result
You can use the following using a sub-query:
SELECT id, id_barang, tgl, ket, kondisi FROM `opname` WHERE id IN (
SELECT MAX(id) FROM `opname` GROUP BY id_barang
)
With the inner SELECT you get all the maximum IDs of the grouped result. The outer SELECT selects all rows from your original table with all these IDs.
Your current query is not far off, except that it attempts to select non aggregate columns while using GROUP BY. This doesn't make logical sense, because it isn't clear which values should be chosen for each id_barang group. But if we join the opname table to your current query as a subquery, then we can identify the records you want to retain.
SELECT t1.*
FROM opname t1
INNER JOIN
(
SELECT id_barang, MAX(id) AS max_id
FROM opname
GROUP BY id_barang
) t2
ON t1.id_barang = t2.id_barang AND -- retain only MAX(id) records
t1.id = t2.max_id -- for each barang_id

Mysql: How do I access the value of a column for my current row

So I have a mysql table that has columns
Part # (varchar) and customer # (varchar)
I am trying to create a third column that counts unique pairings of Part #'s with a list of customer #'s.
select Part_num, Cust_num,
(select count(*) from my_table where Part_num = (The current value of Part_num)
and Cust_num in (select Cust_num from my_table where Part_num = 'XYZ123')) as Pairs
from my_table;
I am trying to figure out how many Customer #'s contain the current part. If I substitute (The current value of Part_num) for a Part # say "ABC789" then the query correctly identifies their being X number of pairs, but I need to do this dynamically for every Part #.
Any assistance is greatly appreciated.
What you should do here is select the part number, use the COUNT() aggregate function, and group by customer number that way you will get the count of each customer associated with that part.
It looks like this:
SELECT partNumber, COUNT(*) AS numCustomers
FROM myTable
WHERE partNumber = 'ABC789';
If it's possible for a customer number to appear more than once, and you want distinct customers, you can put distinct customerNumber in your COUNT clause:
SELECT partNumber, COUNT(distinct customerNumber) AS numCustomers
FROM myTable
WHERE partNumber = 'ABC789';
You can refer to the outer query from the (correlated) subquery, you just need to use table aliases.
SELECT t.part_num,
t.cust_num,
(
SELECT Count(*)
FROM my_table t2
WHERE t2.part_num = t.part_num
AND cust_num IN
(
SELECT cust_num
FROM my_table
WHERE part_num = 'XYZ123')) AS pairs
FROM my_table t;

Obtain a list with the items found the minimum amount of times in a table

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)