MySQL dynamic key/pairs converted to single record sets - mysql

I'm sure my question is misleading, so allow me to demonstrate my challenge. I have a table that holds dynamically built data (user sets up field names and allows the entry of particular data)
Table
KEY | VALUE | PERSON
key1 | value1 | personA
key2 | value2 | personA
key3 | value3 | personA
key1 | value1 | personB
key2 | value2 | personB
key3 | value3 | personB
I need this to be changed to the following as a query so I can filter a search on these records:
Dynamically created table for querying
PERSON | Key1 | Key2 | Key3
personA | value1 | value2 | value3
personB | value1 | value2 | value3
Please provide me with a mysql query to produce the following result. NB, the keys are dynamically created by the user and can thus result in many more / less.

As pointed out above, the term I was looking for was a Pivot, which is not supported by MySQL. I will therefore pull the single row records into my script and build an object from them before returning the result to the user. Seems to be the easiest way.

Related

MySQL JSON with arbitrary keys to table

There is a map nested in a large json payload like
{
"map": {
"key1": "value1",
"key2": "value2",
"key3": "value3"
},
// more stuff
}
I would like to generate a table like that:
+------#--------+
| Key | Value |
+------#--------+
| key1 | value1 |
| key2 | value2 |
| key3 | value3 |
+------#--------+
The only thing I can think of is writing a stored function that loops over JSON_KEYS to convert all key value pairs into
[{"key":"key1", "value":"value1"}, {"key":"key2", "value":"value2"}, ...]
which makes the task trivial with JSON_TABLE.
Is there a faster and more elegant way?
Here's a solution:
select j.key, json_unquote(json_extract(m.data, concat('$.map.', j.key))) as value from mytable as m
cross join json_table(json_keys(m.data, '$.map'), '$[*]' columns (`key` varchar(10) path '$')) as j
Output with your sample data:
+------+--------+
| key | value |
+------+--------+
| key1 | value1 |
| key2 | value2 |
| key3 | value3 |
+------+--------+
If that query seems inelegant or hard to maintain, you're probably right. You shouldn't store data in JSON if you want simple or elegant queries.
your doing well and even for enterprise project doing this way

SSRS lookup function not fetching data in correct

Table1:
+------------+
| Id Value1 |
+------------+
| 1 abc |
| 1 bda |
| 1 bvc |
+------------+
Table2:
+-----------+
| ID Value2 |
+-----------+
| 1 11 |
| 1 12 |
| 1 13 |
+-----------+
Now I have used Lookup function in ssrs(Below)
=Lookup(Field!ID.Value,Field!ID.Value,Field!Value2.Value,"Table2")
And the result looks like this:
+------------------+
| ID Value1 Value2 |
+------------------+
| 1 abc 11 |
| 1 bda 11 |
| 1 bvc 11 |
+------------------+
Field name Value2 values are not fetching correct.
Please help on this.
Thanks in advance.
If your two tables are from the same data source, and you're able to adjust the SQL query, it's much easier to JOIN the tables into a single dataset instead of using Lookup() expressions.
Lookup() will only return the first value with a matching ID. As all your IDs are the same, Lookup() is just returning the first value in Table2 for all rows.
If you only want to look up one specific row from Table2, you'll need to find a unique ID number (primary key) that can be referenced in Table2. If you want to look up ALL the rows that match between Table1 and Table2, you'll need to use LookupSet() instead.
There's likely some options to Lookup by each table's Row Number, but you might want to reassess your data structure and/or report design before considering this.

Is there a straight forward way of converting data stored in HDFS to JSON?

I have a "|" delimited data file which I need to convert to JSON for further processing.
Sample Data looks like below, first row are the fields
A | B | C | D | E
001 | Value2 | Value3 | Value4 | Value5
002 | Value2 | Value3 | Value4 | Value5
003 | Value2 | Value3 | Value4 | Value5
Ah that was so easy. Just found it. I'm showing an example with two fields only.
A = FOREACH 'ALIAS NAME' GENERATE $0 as id, $1 as salary;
STORE A INTO '/your prefered hdfs location' using JsonStorage();
and see the power!!!

Get affected rows value by updating value of the field which already present

I've got a table
ID | NAME | VALUE |
----------------------------
1 | Test1 | VALUE1 |
2 | Test2 | VALUE2 |
3 | Test3 | VALUE3 |
4 | Test4 | VALUE4 |
5 | Test5 | VALUE5 |
I'm running this query dynamically
$query="UPDATE tables SET `VALUE`='VALUE1' WHERE `ID`='1'"
Here i'm updating a value which is already stored. This doesn't update the field value. Because of this i can't get affected rows. I'm using PHP
Is there any way to get the affected rows with the above query.
Your query works according to this SQL Fiddle:
http://sqlfiddle.com/#!9/84beb/1
If you set a field to the value it already has, it's not counted in "affected rows". If you want to know how many rows might have been modified based on the WHERE clause, you need to do a separate SELECT with the same clause:
SELECT COUNT(*) AS num_rows FROM tables WHERE id = 1;

MySQL structure for 2D array lookup?

sIm trying to create a mysql database for looking up in a big dataset. I have a set of data (n number) samples and a list of genes in my data collection and I would like to able to lookup a value based on pairing one sample with one gene. In the the table below I have the data for genes, samples and value, and I would like to know the best way sort set this up on mysql for fast lookup.
In my data I have about 35.000 different genes and an aomunt of samples that can vmay vary between 2000-15000. (I dont have a specific number, because I have finished my data yet.)
|------------------------------------------------------|
| | sample1 | sample2 |
|------------------|----------------|------------------|
| gene1 | value1 | value2 |
|------------------|----------------|------------------|
| gene2 | value3 | value4 |
|------------------|----------------|------------------|
What is the best way to put this data up on a mysql database? The "easiest" way that I thought of right away is just throwing it in the database like in the following output, which seems like it would be a big disaster. How can I approach this correctly?
|----------------------------------------- ------------|
| gene1 | sample1 | value1 |
|------------------|----------------|------------------|
| gene1 | sample2 | value2 |
|------------------|----------------|------------------|
| gene2 | sample1 | value3 |
|------------------|----------------|------------------|
| gene2 | sample2 | value4 |
|------------------|----------------|------------------|