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.
Related
I need to write an SQL query that fetches something like this :
a
b
c
1
x
3
2
y
4
3
x
7
4
y
9
transforms into the following form:1
Here's my coding attempt:
SELECT CONCAT (a) AS a , CONCAT (b, -c) as m FROM viborka
and the corresponding output I'm getting:
a
m
1
x-3
2
y-4
3
x-7
4
y-9
I can't merge expressions with X into string 1 and expressions with Y into string 2.
How can I do it?
You can do:
select
case when row_number() over(partition by b order by c) = 1 then a end as a,
concat(b, '-', c) as m
from t
order by b, c
Result:
a m
----- ---
1 x-3
null x-7
2 y-4
null y-9
See running example at db<>fiddle.
I have a row that should be duplicated in certain conditions.
Example:
Case 1:
a b c d e
---------------
1 4 25 10 NULL
if e is Null, display a, b and c:
1 4 25
Case 2:
a b c d e
---------------
1 4 25 10 55
if e is not Null, duplicate the row
1 4 25 => column a,b,c
1 4 10 => column a,b,d
Use this query
Using union you can achieve this use case
select a,b,c from table
union all
select a,b,d from table where e is not null
If you don't want to scan the table twice, then you can use cross joinand some logic:
select a, b,
(case when n = 1 then c else d end)
from t cross join
(select 1 as n union all select 2) n
where n = 1 or
(n = 2 and e is not null);
I have a table having three columns:
A B C
1 2 2
2 2 2
3 1 1
4 1 2
I want the count of those values which have C equal to 2 but with distinct values of B
So in this case for C = 2, count = 2 (B=2 and B=1)
I used the following command:
Select count(*) from mytable where C=2 group by (B)
but it yields:
count(*)
3
I have tried using "distinct" but it can't be use to select from one column
Have you tried
SELECT COUNT(DISTINCT B) FROM mytable WHERE C = 2;
Use sub query like this:
Select count(*) from (
select distinct B where c=2
)
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;
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