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.
Related
MySQL Version: 5.7.36
I'm attempting to minimize the amount of queries I have to execute.
Right now, I'm executing a query similar to this:
SELECT
TABLE 1.column1 as "A",
TABLE 1.column2 as "B"
FROM
TABLE 1
WHERE
CONDITION
I can obtain the results I need from this query, however I would also like to obtain the count of what type of values show up in the same query.
For example, if the following query retrieves this table
A B
- -
1 a
1 b
1 c
2 d
3 e
4 f
4 g
I would also like to, for each row, obtain the count of all rows retrieved with its column "A" that matches its value.
Would it be more efficient to execute another query to get that result or can I modify my obtaining query to get this statistic?
Desired result:
A B C
- - -
1 a 3 # 3 rows with "1" in Column "A"
1 b 3
1 c 3
2 d 1
3 e 1
4 f 2
4 g 2
UPDATE:
The closest query I could find goes like this:
SELECT
TABLE 1.column1 as "A",
COUNT(TABLE 1.column1)
FROM
TABLE 1
WHERE
TABLE 1.column1 = "foo"
GROUP BY
TABLE 1.column1
Results in this:
A B C
- - -
1 a 3
2 d 1
3 e 1
4 f 2
However, it removes any other rows with the same value in column "A". Even if it has different values in column "B". I would like to have all rows present in my SQL query with its corresponding row count.
The next closest query I found goes like this:
SELECT
TABLE 1.column1 as "A",
COUNT(TABLE 1.column1)
FROM
TABLE 1
WHERE
TABLE 1.column1 = "foo"
GROUP BY
TABLE 1.column2
Results in this:
A B C
- - -
1 a 1
1 b 1
1 c 1
2 d 1
3 e 1
4 f 1
4 g 1
Which also isn't achieving the desired result.
You have to join with the subquery that gets the counts.
SELECT t1.column1 AS A, t1.column2 AS B, t2.count
FROM Table1 AS t1
JOIN (
SELECT column1 AS A, COUNT(*) AS count
FROM Table1
GROUP BY column1
) AS t2 ON t1.A = t2.A
SELECT
c,
COUNT(*) OVER (PARTITION BY c)
FROM t
ORDER BY c
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 named 'demo' with the following data:
Name Group MX
A XY 1
B YZ 1
B XY 2
C YZ 5
C XY 3
D YZ 2
E YZ 1
E XY 1
I want unique names based on 'MX' maximum value, when the 'MX' for two identical names are equal I need one of them as illustrated below:
Name Group MX
A XY 1
B XY 2
C YZ 5
D YZ 2
E YZ 1 -- or this {E XY 1}
This is my query:
SELECT demo.Name, demo.Group, demo.MX
FROM (
SELECT Name, MAX(MX) AS max_values
FROM demo
GROUP BY Name
) demo2
INNER JOIN demo
ON demo.Name = demo2.Name
AND demo.MX = demo2.max_values
It is working charmingly, but when the two names are identical it displays both as follow:
Name Group MX
A XY 1
B XY 2
C YZ 5
D YZ 2
E YZ 1
E XY 1
What methods do you recommend?
for avoid two value where Name and MX are equals you could use a (fake) aggregation function and group by eg:
SELECT demo.Name, min(demo.Group), demo.MX
FROM (
SELECT Name, MAX(MX) AS max_values
FROM demo
GROUP BY Name
) demo2
INNER JOIN demo ON demo.Name = demo2.Name AND demo.MX = demo2.max_values
GROUP BY demo.Name, demo.MX
I want to output two rows in a one entry but with two tables. here is my table:
table_A
-----------------------------------------------------------------------
checkkey checknum confirmed printed canceled
-----------------------------------------------------------------------
1 1 Y Y Y
2 2 Y Y N
3 10 N Y Y
table_B
-----------------------------------------------------------------------
checkkey checknum status
-----------------------------------------------------------------------
1 1 V
2 2 V
3 10 V
I want an output like this
-----------------------------------------------------------------------
checkkey checknum confirmed printed canceled status
-----------------------------------------------------------------------
1 1 Y Y Y
1 1 Y Y Y V
2 2 Y Y N
2 2 Y Y N V
3 10 N Y Y
3 10 N Y Y V
you can use a UNION , first query will get all with status as V based on tableB and second one gets all rows from A
select a.checkkey, a.checknum,a.confirmed, a.printed,a.canceled, b.status
from table_A a
inner join table_B b
on a.checkkey = b.checkkey
union all
select a.checkkey, a.checknum,a.confirmed, a.printed,a.canceled, NULL as status
from table_A a
order by checkkey, checknum;
I think this does what you want:
select a.*, 'V' as status
from table_A a
union all
select a.*, NULL as status
from table_A a
order by checkkey, checknum;
Table_B doesn't seem necessary at all.
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.