How to write SQL for finding columns with matching string - mysql

I do have a following table
TableA
| Column A | Column B |
| -------- | -------- |
| 1234 | Y |
| 2345 | N |
| 3456 | Y |
| 3456 | Y |
| 3456 | N |
| 2345 | N |
| 1234 | N |
| 2345 | N |
Here, '1234' and '3456' has values Y and N whereas 2345 has only value N
I want to display values of column A where there are 2 values (Y and N) in Column B. Ideally
| Column A | Column B |
| -------- | -------- |
| 1234 | Y |
| 1234 | N |
| 3456 | Y |
| 3456 | N |
I tried using
Select * from TableA
where column b = 'Y'
and column b = 'N'
but it doesn't give desired result.

I prefer aggregation here:
SELECT ColumnA
FROM TableA
GROUP BY ColumnA
HAVING MIN(ColumnB) <> MAX(ColumnB);
The above assumes that the only two values which would ever appear in ColumnB are Y and N.

Related

MySQL multiples rows to columns [duplicate]

This question already has an answer here:
MySQL pivot row into dynamic number of columns
(1 answer)
Closed 5 years ago.
https://i.stack.imgur.com/5Pw2L.png
I have a problem that I would like to solve for pre-processing in MySQL. I have a select that returns multiple rows and columns for an id. I would like to transform the rows into columns for the same id, grouping them as per the attached figure. The column names are not important to me because I only need the values for each id.
+---+---+---+---+-----+---+
| 1 | a | b | c | ... | x |
+---+---+---+---+-----+---+
| 1 | d | e | f | ... | y |
+---+---+---+---+-----+---+
| 2 | g | h | i | ... | z |
+---+---+---+---+-----+---+
| 2 | j | k | l | ... | q |
+---+---+---+---+-----+---+
| 3 | m | n | o | ... | w |
+---+---+---+---+-----+---+
| 3 | p | q | r | ... | t |
+---+---+---+---+-----+---+
+---+---+---+---+-----+---+---+---+---+-----+---+
| 1 | a | b | c | ... | x | d | e | f | ... | y |
+---+---+---+---+-----+---+---+---+---+-----+---+
| 2 | g | h | i | ... | z | j | k | l | ... | q |
+---+---+---+---+-----+---+---+---+---+-----+---+
| 3 | m | n | o | ... | w | p | q | r | ... | t |
+---+---+---+---+-----+---+---+---+---+-----+---+
Unfortunately there is no way to create columns like that on the fly, if you have a variable amount of id repetitions in a table.
You could use group concat to get the same columns into one comma separated column
Select Id, Group_Concat(Col1) As Col1,
Group_Concat(Col2) As Col2,
Group_Concat(Col3) As Col3, ...
Group_Concat(Coln) As Coln
From table
Group By Id

Count occurrence of unique value in a row using SQL

I have a table like this:
ID | B1 | B2 | B3 | B4 | B5 |
------------------------------------
1 | N | N | N | N | N |
2 | N | Y | N | Y | Y |
3 | N | N | N | N | Y |
I want a query that should return
ID | Count |
------------
1 | 5 |
2 | 2 |
3 | 4 |
Note that boolean expression in MySQL resolves into 0/1.
a = b returns 1 only if a is equal to b otherwise 0
SELECT
ID,
(
(B1 = 'N')+
(B2 = 'N')+
(B3 = 'N')+
(B4 = 'N')+
(B5 = 'N')
) AS Count
FROM your_table
SEE DEMO
Suggestion:
Better normalize your data.
See Normalization in MySQL

Auto fill other column in mysql table

I have column in mysql table like this:
| Column A | Column B |
| A | |
| B | |
| C | |
| D | |
| E | |
I want to auto fill the other column using rule if A,C,D then fill X and if B, E fill the Y in the column B field.
The result will be like this:
| Column A | Column B |
|----------|----------|
| A | X |
| B | Y |
| C | X |
| D | X |
| E | Y |
Is there are an easy way to do that in MySQL query?
Thank you for help.
UPDATE table
SET B = CASE
WHEN A IN ('A','C','D') THEN 'X'
WHEN A IN ('B','E') THEN 'Y'
END

LIMIT offset or OFFSET in an UPDATE SQL query

I have a table similar to this:
| 0 | X |
| 1 | X |
| 2 | X |
| 3 | Y |
| 4 | Y |
| 5 | X |
| 6 | X |
| 7 | Y |
| 8 | Y |
| 9 | X |
I'd like to replace first 2 occurrences of X with X1, and then 4 next occurrences with X2 so that the resulting table looks like this:
| 0 | X1 |
| 1 | X1 |
| 2 | X2 |
| 3 | Y |
| 4 | Y |
| 5 | X2 |
| 6 | X2 |
| 7 | Y |
| 8 | Y |
| 9 | X2 |
The table in question is of course much bigger and the number of occurrences would thus be higher too so manual editing is not a solution.
I'd like to do something like this:
UPDATE table SET column = 'X' WHERE column = 'X2' LIMIT 90, 88
but unfortunately MySQL doesn't seem to support OFFSET in UPDATE queries... Is there any way to do this?
I don't know whether you have id filed available in table or not, but you can use WHERE id BETWEEN 88 AND 90, MySQL does not support Offset in update query, but you can do this by limiting using BETWEEN command
Try this:
UPDATE table SET column = 'X1' WHERE id IN(SELECT id FROM (SELECT id FROM table WHERE column = 'X' LIMIT 2) as u);
and then
UPDATE table SET column = 'X2' WHERE id IN(SELECT id FROM (SELECT id FROM table WHERE column = 'X' LIMIT 4) as u);

Distinct on specific column in Hive

I am running Hive 071 I have a table, with mulitple rows, with the same column value e.g.
| x | y |
| 1 | 2 |
| 1 | 3 |
| 1 | 4 |
| 2 | 2 |
| 3 | 2 |
| 3 | 1 |
I want to have the x column unique, and remove rows that have the same x val e.g.
| x | y |
| 1 | 2 |
| 2 | 2 |
| 3 | 2 |
or
| x | y |
| 1 | 4 |
| 2 | 2 |
| 3 | 1 |
are both good as distinct works only on the whole rs in hive, I couldn't find a way to do it
help please Tx
Some options:
1) This will give you the max value of y for each value of x
select x, max(y) from table1 group by x
Equally you could use avg() or min()
2) OR, you could collect all the values of y in a list:
select x, collect_set(y) from table1 group by x
This will give you:
x|y
1|2,3,4
2|2
3|1,2