MySQL replacement of values in the query result - mysql

I transpose table rows to columns by the SELECT statement:
`SELECT cdcb_info.CISLO AS "cislo",
max(case when info = 'HBR' then value end) as "HBR",
max(case when info = 'HCD' then value end) as "HCD"
FROM cdcb_info group by cdcb_info.CISLO`
Result looks like this:
cislo
HBR
HCD
111222
0
5
333444
1
3
how should I modify the query to change the values 0 and 1 to "zero" and "one" for the column HBR and
5, 3 to "five" and "three" for column HCD? Thanks for any help. David
Data example:
id CISLO info value
27 111222 HBR 0
28 111222 HCD 5
29 333444 HBR 1
30 333444 HCD 3

I would advise adding another table, where in one column there are numbers (primary index), in the other the designations of these numbers. Connect this table and output string values from this new table instead of max(case when info = 'HBR' then value end) and max(case when info = 'HCD' then value end).

Related

How to count unique values on one column without double values from another column

My very first question as a newb in SQL.
I want to count unique values from one column Transport, group them by ID and delete double values in the Transport column that may be caused by Product column. Could be very simple, but at this point I need another point of view.
This is the data
ID
Product
Transport
1
A
Plane
1
B
Plane
2
A
Train
2
B
Train
2
C
Ship
3
A
Plane
3
B
Train
3
C
Ship
3
D
Ship
I would want to have the ID as unique values and then count each of the unique values of the Transport. If I do it with a normal GROUP BY, the Products will double the counting.
The result I need has to count each of the Transport values in separated columns without being doubled by the Product column. So it should look something like:
ID
Plane
Train
Ship
1
1
0
0
2
0
1
1
3
1
1
1
I think it's simple but maybe I'm missing something. Any help would be appreciated!
Thank you.
You can get a pivot by combining CASE with MAX(), as in:
select
id,
max(case when transport = 'Plane' then 1 else 0 end) as plance,
max(case when transport = 'Train' then 1 else 0 end) as train,
max(case when transport = 'Ship' then 1 else 0 end) as ship
from t
group by id
Just adding something to #The Impater's result
SELECT
id,
MAX(transport = 'Plane') AS plance,
MAX(transport = 'Train') AS train,
MAX(transport = 'Ship') AS ship
FROM `test_table`
GROUP BY id
I was taught there is no need to assign 1 and 0 when it can be done via boolean-type logic as results are returned either in 0 or 1.

query multiple filter from a table that data store in single column structure

I have a database that stores all the field data with below structure in single column fieldValue and fieldID.
I would like to know for this type of structure how I can have 2 filters in where condition?
for example how I can query all the fieldID2 which is equal to High and all the fieldID11 which is Discrepancy.
database is mysql.
The output will look like below, the output will be like pivot table. please take note that I dont have any problem for write query, I want to know how to have more than one where condition for two columns.
fieldid1 fieldid2 fieldid3 fieldid4
ahmed high xxxxxssss Finance
I am not sure what you are asking exactly. Anyway, key/value tables are usually queried by aggregation. So if per InternalNo you want to show the values for FieldIDs 1, 2, 3 and 4, provided their value for FieldID 2 is 'High' and the value for FieldID 11 is 'Discrepancy', you'd do:
select
internalno,
any_value(case when fieldid = 1 then fieldvalue end) as fieldvalue1,
any_value(case when fieldid = 2 then fieldvalue end) as fieldvalue2,
any_value(case when fieldid = 3 then fieldvalue end) as fieldvalue3,
any_value(case when fieldid = 4 then fieldvalue end) as fieldvalue4
from keyvalues
group by internalno
having any_value(case when fieldid = 2 then fieldvalue end) = 'High'
and any_value(case when fieldid = 11 then fieldvalue end) = 'Discrepancy';
In order to speed this up, you can apply a WHERE clause on the desired FieldIDs;
where fieldid in (1,2,3,4,11)
As to multiple values for one attribute like your FieldID 10 for InternalNo 89796, you'd have to decide which value(s) to get and ANY_VALUE might not suffice for that.

Aggregating positive values on multiple columns with SQL

I am trying to do aggregation functions on a specific database table with multiple columns.
For example:
Sample Table
ID Column1 Column2 Column3
1 3 5 -2
2 -1 4 6
3 2 -1 3
In this example, if I would like to sum the values in the 3 columns, I want to get the following result:
Column1: (3+2)=5, Column2(5+4)=9 and Column3(6+3)=9.
Thus, my question is whether this is possible with a single SQL query or I would have to go through creating temporary tables?
Note: The data set is big.
If I understand correctly, you only want to SUM the positive values, and ignore the negatives? If so try:
SELECT
SUM(CASE WHEN Column1 > 0 THEN Column1 ELSE 0 END) AS 'Column1PosTotal',
SUM(CASE WHEN Column2 > 0 THEN Column2 ELSE 0 END) AS 'Column2PosTotal',
SUM(CASE WHEN Column3 > 0 THEN Column3 ELSE 0 END) AS 'Column3PosTotal'
FROM Table

Check column if has the same value on all the rows

How do I check if a column has all the rows the same value?
I don't think this will work.
SELECT column FROM table WHERE value = 1
I want to make, by time each row will turn from 0 to 1 till every row has value 1, if all the values are 1 to turn all in 0
id value
1 1
2 1
3 1
4 1
5 1
6 1
7 1
You can try to use distinct
select count(distinct column) FROM table
If the result is 1 then it means there is only same value present in the column else there are different values present in your column.
Try this query :-
select count(value) from table where value =0;
if rows return count is zero that means there are no zeroes in that column.
Use count
SELECT count(value) as total FROM table
if total > 1 than more than on value

Column has Multiple Values Used as Identifiers for Next Column

MySQL column has multiple values used as identifiers for the next column. Table Structure:
id (key), occurrence_id, name, value
The name column values then correspond to the values column. How can I display this information in one view?
Currently, it looks like this:
occurrence_id name value
1576 Attempts 1
1576 ClientIP "94.xxx.xxx.xxx"
1576 UserAgent ""
1576 CurrentUserID 0<
I want to make a view that will look like this:
occurrence_id Attempts Client IP CurrentUserID
1576 1 "94.xxx.xxx.xxx" 0
2009 30 "68.111.xxx.xxx" 0
One method is using conditional aggregation:
select occurrence_id,
max(case when name = 'Attempts' then value end) as Attempts,
max(case when name = 'ClientIP' then value end) as ClientIP,
max(case when name = 'UserAgent' then value end) as UserAgent,
max(case when name = 'CurrentUserID' then value end) as CurrentUserID
from table t
group by occurrence_id;