I am trying to do a group concat for the table below
l r num
A B 1
A C 3
A A 5
B C 5
B C 7
B C 9
C A 1
C A 2
C C 3
I would like get the group concat of those elements which do not belong to the group when we use GROUP BY and also sum the numbers (in a similar way). For example, the output I am trying to obtain is
l grps sum(num)
A B,C 4
B C 21
C A 3
I am currently getting the output as below
l grps sum(num)
A B,C,A 9
B C 21
C A,C 6
I use the query below
SELECT l, group_concat(distinct r), sum(num)
from groups
group by l;
The SQL fiddle is here
SELECT l, GROUP_CONCAT(DISTINCT r), SUM(num)
FROM groups
WHERE l <> r
GROUP BY l;
Related
I have the functional dependency A->B and I need to clean the data by correcting rows that violate this FD. Currently the data is:
A B
1 a
2 b
2 b
2 b
2 c
3 d
3 d
6 c
6 c
6 c
6 c
6 c
6 b
but I need it to be:
A B
1 a
2 b
2 b
2 b
2 b
3 d
3 d
6 c
6 c
6 c
6 c
6 c
6 c
How can I do this within a query? I have an existing query that finds the correct value for B for each A value (for example, 1's correct value is a, 2's is b, 3's is d, 4's is c) but I'm not sure how to clean the data using a standard SELECT query.
Craft an UPDATE or MERGE statement that only corrects those that you know are wrong to what you know is right.
Example update statement for MySql:
UPDATE yourtable t
JOIN
(
SELECT 2 AS A, 'b' AS B
UNION ALL SELECT 3, 'd'
UNION ALL SELECT 6, 'c'
) AS correction
ON t.A = correction.A
SET t.B = correction.B
WHERE t.B != correction.B
db<>fiddle here
This question already has answers here:
Using LIMIT within GROUP BY to get N results per group?
(14 answers)
Closed 3 years ago.
I have a table named Category like
Id Name
1 A
2 B
And i have product table like
Id Name Category_id
1 C 1
2 D 1
3 E 1
4 F 2
5 G 2
6 H 2
I want to select 2 items from each category. I am applying this query
Select Product.id,Product.name as pname,category.name as cname from product join category where Category_id=Category.id limit 0,4
But it's returning first four data of the product table like
Product.id Pname Cname
1 C A
2 D A
3 E A
4 F B
But i Want to get 2 product from each category
Product.id Pname Cname
1 C A
2 D A
4 F B
5 G B
If you are running MySQL 8.0, you can use row_number():
select p.id pid, name, p.name pname, c.name cname
from (
select p.*, row_number() over(partition by category_id order by id) rn from product p
) p
inner join category c on c.id = p.category_id
where p.rn <= 2
I have a table in which i have to calculate max value and count of (calculate max value) group by name and sex.
sample input:
name sex age
A F 2
A F 3
A M 4
B F 6
B M 7
B M 7
B F 4
C M 8
C M 8
C M 8
C M 3
C F 1
C F 10
C F 10
Sample Output:
name sex max_age count_max_age
A F 3 1
A M 4 1
B F 6 1
B M 7 2
C F 10 2
C M 8 3
I have tried
select name,sex,max(age) as max_age , count(max_age) from table_1 group by name ,sex
I am getting an error
Unknown column max_age in fieldlist
please suggest any modifications required in above query.
Thanks in advance
SELECT count(max_age), name, sex FROM (
SELECT name, sex, max(age) as max_age from table_1 group by name, sex
) as max_age_group group by name, sex
query will not understand the alias on the same level, however you can make it as a subquery.
Use a subquery and join like below
select
t1.name, t1.sex, max_age, count(max_age) as countofmaxage
from
(select name, sex, max(age) as max_age
from t
group by name, sex) t1
join
t on t1.name = t.name and t1.sex = t.sex and t1.max_age = t.age
group by
t1.name, t1.sex, max_age
Output:
name sex max_age countofmaxage
----------------------------------
A F 3 1
A M 4 1
B F 6 1
B M 7 2
C F 10 2
C M 8 3
DEMO in fiddle
I have a table as below,
Product Promotion exists (Y/N) Week
A Y 1
B Y 1
C Y 1
A Y 2
B Y 2
C N 2
A Y 3
B Y 3
C Y 3
A Y 4
B Y 4
C N 4
I want to see an Promition exists combination Output on total table. Something like
A, B - 4
B,C - 2
A,C - 2
Since this is Just for 3 products looks simple.. I am looking at some thousands of records, and looking for same combinations where total count of occurrence is greater than some number. If taking the above example, if that count is 4.. then my output should be
A,B - 4
Try this:
SELECT p1, p2, COUNT(*) AS cnt
FROM (
SELECT t1.Product AS p1, t2.Product AS p2
FROM mytable AS t1
JOIN mytable AS t2
ON t1.Week = t2.Week AND
t1.Product < t2.Product AND
t1.Exists = 'Y' AND t2.Exists = 'Y') AS t
GROUP BY p1, p2
ORDER BY cnt DESC
To get only pairs exceeding a certain value, just wrap the above in a subquery and add a WHERE cnt >= someValue.
Demo here
Assume that I have a table call client.
In this table I have 3 fields (A, B, C)
I would like to group each rows when values of A = B = C
ex:
A B C otherRow
1 2 3 x
2 1 x
4 2 y
I would like to get the folowing
(A,B,C) otherRow Count
1 x 2
2 x 2
2 y 1
3 x 1
4 y 1
Your query is UNION, not JOIN:
SELECT
`A,B,C`,
otherRow,
COUNT(`A,B,C`) AS `Count`
FROM
(SELECT a AS `A,B,C`, otherRow FROM t
UNION ALL
SELECT b AS `A,B,C`, otherRow FROM t
UNION ALL
SELECT c AS `A,B,C`, otherRow FROM t) AS u
GROUP BY
`A,B,C`,
otherRow
HAVING
`A,B,C` IS NOT NULL
check this fiddle. I've added NULL-check since it's not obvious what are your "empty" values. If you'll remove it, you'll get zero-count NULL-rows.