Sample data:
ProductID PackingID
------- ---------
1 2
1 2
3 2
3 2
1 1
2 1
3 2
I have the above sample data. What i want is to select the unique (not distinct) rows of the combination productID and packingID. In the above example the only matching results are
ProductID PackingID
------- ---------
1 1
2 1
These rows are the only unique combinations of ProductID and PackingID together. I do not want Distinct results because it will give me one of all the other combinations.
SELECT PRODUCTID,PACKINGID FROM DTEMP
GROUP BY PRODUCTID,PACKINGID
HAVING COUNT(PRODUCTID)=1
ORDER BY 1;
You can try this one this is how i do in oracle ... to get the unique rows without using distinct.
SELECT ProductID, PackingID
FROM yourtable
GROUP BY ProductID, PackingID
HAVING COUNT(*) = 1
your table should be like:
uniqueID ProductID PackingID
1 x y
2 x y
3 z x
Query:
SELECT uniqueID,ProductID,PackingID
FROM yourtable
WHERE uniqueID IN
(
SELECT MIN(uniqueID)
FROM yourtable
GROUP BY ProductID,PackingID
)
Related
I am using mariadb and I have a table called links:
id | product_id | last_change
------------------------------
1 1 xxx
2 2 xxx
3 5 xxx
4 5 xxx
I want to find every object (3, 4 in this example) that occures more than once. Following this answer I tried:
SELECT product_id, COUNT(*) from links HAVING COUNT(*) > 1
But this results in the (adapted to this example) first row being shown and the total number of product_id occurrences:
product_id | COUNT(*)
---------------------
1 4
I wanted to achieve a list of all items occuring more than once:
id | product_id | last_change
------------------------------
3 5 xxx
4 5 xxx
An aggregation function without GROUP BY always results in only one row result as it aggregates all rows
So use a GROUP BY
SELECT product_id, COUNT(*) from links GROUP BY product_id HAVING COUNT(*) > 1
To see all entry with the count of the product_id , you can do following
SELECT l1.product_id , last_change , Count_
FROM links l1
JOIN (SELECT product_id, COUNT(*) as Count_ from links GROUP BY product_id HAVING COUNT(*) > 1) l2
ON l1.product_id = l2.product_id
Try below statement
select id, product_id, count(product_id)
from links
group by (product_id)
having count(product_id)> 1;
I need to count matches in a database.
Input:
id_to id_from
1 2
2 1
1 3
3 1
1 4
5 1
the 5th and 6th row has only one direction so doesn't count
Sample Output:
id_match
1
2
3
So, for 1 (implicit), 2 and 3 there is a reverse match but for 4 and 5 there aren't.
---- EDITED ----
Supposing the table name is "example" and I want to get all matches of id=1 then the SQL query will be:
SELECT count(*) FROM
(SELECT id_to FROM example WHERE id_from = 1) as t1,
(SELECT id_from FROM example WHERE id_to = 1) as t2
WHERE t1.id_to = t2.id_from
but maybe there is a better way to do it
You could try
SELECT DISTINCT id_from AS matched_id
FROM your_table AS data1
WHERE EXISTS (SELECT 1
FROM your_table AS data2
WHERE data1.id_from = data2.id_to
AND data1.id_to = data2.id_from)
I've created a demo here
id name count
------------
1 abc
2 xyz
3 xyz
4 xyz
The following query "select count(name) from temp group by name;" gives me:
count(name)
--------
1
3
I want this result to be updated to the column 'count'. To be precise I want my table to look like :
id name count
------------
1 abc 1
2 xyz 3
3 xyz 3
4 xyz 3
You can get those values with a COUNT / GROUP BY. You can do an UPDATE statement which joins your table with the sub query:-
UPDATE temp a
INNER JOIN
(
SELECT name, COUNT(*) AS name_count
FROM temp
GROUP BY name
) b
ON a.name = b.name
SET a.name_count = b.name_count;
I have table User, Company, ParentCompany and table Goal.
Each Company have ParentCompany, and each User inside one Company. Goal have number of action, and type of Goal, user who execute the goal, and time executed.
I want to calculate the number of action in a date range for each type of Goal, for each user, company, and parent_company. Number of action for each company equal to sum of action for user that reside in that company.
More or less after some join query, I able to get this table below, where column id is id of company, parent_id is id of companyparent, and num is number of goal for all user inside of id company.
id parent_id num
----------- -------------------- -----------------------
1 3 1
2 1 2
3 1 1
4 2 4
Now I want to make it like below:
id parent_id sum_id sum_parent
----------- -------------------- -------------- -------------
1 3 1 1
2 1 2 3
3 1 1 3
4 2 4 4
How can I make it works? I can get one of the value (sum_id or sum_parent) with GROUP BY,
SELECT id,SUM(num) AS sum_id FROM tableA GROUP BY id
or
SELECT parent_id,SUM(num) AS sum_parent FROM tableA GROUP BY parent_id
but is there any way to make it only in one query? tableA results from query with 5 join inside.
Try this:
SELECT a.id, a.parent_id, a.sum_id, b.sum_parent
FROM (SELECT id, parent_id, SUM(num) sum_id FROM tableA a GROUP BY id) a
INNER JOIN (SELECT parent_id, SUM(num) sum_parent FROM tableA a GROUP BY parent_id) b ON a.parent_id = b.parent_id
Try this :
SELECT
t1.id, t1.parent_id, t1.sum_id, t2.sum_parent
FROM
(SELECT id, parent_id, SUM(num) AS sum_id FROM mytable GROUP BY id) t1
INNER JOIN
(SELECT parent_id, SUM(num) AS sum_parent FROM mytable GROUP BY parent_id) t2
ON t1.parent_id = t2.parent_id
Apparently what I want can be done with WITH ROLLUP statement. (http://dev.mysql.com/doc/refman/5.0/en/group-by-modifiers.html)
With
SELECT id,sum(num) FROM tableA GROUP BY parent_id, id WITH ROLLUP;
will results in
parent_id id sum(num)
----------- -------------------- --------------
1 2 2
1 3 1
1 null 3
2 4 4
2 null 4
3 1 2
3 null 2
i have data below for example
id product_id date
------ ---------- ----
1 1 1
2 1 2
3 1 3
4 2 1
5 2 2
6 2 2
7 3 1
result data query that i want "the last record of last date on each product_id"
to get it that result i use the query like below
SELECT a.* FROM test AS a
JOIN (SELECT MAX(id) AS id, product_id, MAX(DATE) AS DATE FROM test GROUP BY product_id) AS b
ON a.id = b.id AND a.product_id = b.product_id AND a.date = b.date
this time i got what i want as the result
id product_id date
------ ---------- --------
3 1 3
6 2 2
7 3 1
my problem when i add another data like below
id product_id date
------ ---------- --------
1 1 1
2 1 2
3 1 3
4 2 1
5 2 2
6 2 2
7 3 1
8 1 3
9 1 2
and use the same query the result become like this
id product_id date
------ ---------- --------
6 2 2
7 3 1
where the the value '1' for product_id?
Try this
SELECT id, product_id, DATE FROM test sitem WHERE product_id IN (1,2,3) AND DATE = (SELECT DATE FROM test WHERE product_id =
sitem.product_id ORDER BY DATE DESC LIMIT 1) AND id =
(SELECT id FROM test WHERE product_id = sitem.product_id ORDER BY DATE DESC,
id DESC LIMIT 1) GROUP BY product_id
This is your subquery:
SELECT MAX(id) AS id, product_id, MAX(DATE) AS DATE
FROM test
GROUP BY product_id
It is independently calculating the maximum of id and date. But, there is no guarantee that these two values are in the same record. There are ways to fix the subquery, but they are rather complicated.
Instead, I would suggest using an alternative method to get the last record:
SELECT t.*
FROM test t
WHERE NOT EXISTS (SELECT 1
FROM test t2
WHERE t2.product_id = t.product_id AND
(t2.date > t.date OR
t2.date = t.date AND t2.id > t.id
);
This identifies the last record for each product as the one where no other record has a larger date. And, if two records have the same date, no other record has a larger id.