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;
Related
I have a table illustrated on the attached screen:
And data is presented:
As you can see it contains all fields from lines (value, column_id - is field type). Each field has line_index and column_index of position and column_id.
I want to get back all lines with all field names like:
line_index column_id_1 column_name1 column_id_2 column_name2 column_id_3 column_name3
0 1 Age 2 Vasile 3 NY
More simply I need to build all fields(rows) to columns then to lines back.
Should I use Pivot and is it possible in MySQL?
Link to sqlfiddle
You will need to group the results by line_index and conditionally transform rows to columns.
SELECT l.line_index,
MAX(CASE WHEN ct.column_name = "NAME" THEN l.value ELSE NULL END) AS "NAME" ,
MAX(CASE WHEN ct.column_name = "AGE" THEN l.value ELSE NULL END) AS "AGE" ,
MAX(CASE WHEN ct.column_name = "ZUP" THEN l.value ELSE NULL END) AS "ZUP"
FROM columns_types ct
LEFT JOIN `lines` l ON l.column_id = ct.column_id
GROUP BY l.line_index;
Try it here. Also I did some changes in your schema which I felt didn't impact the data stored in the tables. You were adding redundant rows(perhaps) in your columns_types table. And by looking at the query, you know that you will have to build a MAX(CASE statement for each column, so its best if you first fetch it and then build the final query in some programming language.
I have a database that contains these values:
ID Value
1 Mail
2 Portal
3 Terrain
And in my program, I want to make a select, so when I pick a number, it shows the 'value' of that number. Something like:
SELECT ID as Mail, Portal, Terrain
FROM TABLE
You may be looking for conditional aggregation:
select max(case when id = 1 then value end) as mail,
max(case when id = 2 then portal end) as portal,
max(case when id = 3 then terrain end) as terrain
from ;
Make sure you're giving the ID by a textbox or something in your program
Select Value from TABLE where ID = #ID
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.
I am working on a database right now, and I am trying to select some special data.
so the table looks like this.
name title type
Type is including two different value, "book" and "paper".
And this is the result I would like to get
name book paper
person A 0 1
person B 1 2
person C 0 5
What is the best way to write the query it in MySQL.
You may use conditional aggregation:
SELECT
name,
SUM(CASE WHEN type = 'book' THEN 1 ELSE 0 END) AS book,
SUM(CASE WHEN type = 'paper' THEN 1 ELSE 0 END) AS paper
FROM yourTable
GROUP BY
name;
I have a following Mysql table storing meter readings of different power stations.
Date, station_name, reading
2013-05-06, ABC, 102
2013-05-06, PQR, 122
I want a SQL query with following result for a particular date.
Date, ABC, PQR, ABC-PQR
2013-05-06,102,122,-20
You could use CASE statements:
SELECT Date
, SUM(CASE WHEN station_name = 'ABC' THEN reading ELSE 0 END) as ABC
, SUM(CASE WHEN station_name = 'PQR' THEN reading ELSE 0 END) as PQR
, SUM(CASE WHEN station_name = 'ABC' THEN reading ELSE 0 END) - SUM(CASE WHEN station_name = 'PQR' THEN reading ELSE 0 END) as 'ABC-PQR'
FROM table
WHERE Date = '20130506'
GROUP BY Date
You can search for MySQL PIVOT to find out other methods people use.
I believe that it is not possible to do dynamic column based on value of row. I believe you should do it in application-layer rather than database-layer.
See this post: mysql select dynamic row values as column names, another column as value.