I have a field in a MySQL table with '3,4,7,16' in it.
These values are store in another table
id - value
1 - name1
2 - name2
...
16 - name16
I would like to get then in 4 rows
3
4
7
16
I have seen many answer but they need temporary table, function, loop or procedure.
I am using Django ORM and it is not possible to use procedure in Django query.
Can you help to solve this issue ?
Related
This is my first day using Power BI so go easy on me!
Within my table I have a column of JSON records
[Column of JSON records]
Each Record has 3 rows, with 2 columns (I only need the value of the first column).
[Record with 3 rows]
As the index/position of each record's rows represents the same thing, I want to convert the index/position into a new column in the table, and populate it with each records' corresponding value from column 1.
For example:
The JSON Record from table row 1:
----
1479
1481
1486
---
The JSON Record from table row 2:
----
1351
1536
1356
---
Expected output:
New Column 1 | New Column 2
1479 | 1351
1481 | 1536
1486 | 1356
I have searched for a solution but to no avail. Hopefully someone can help me here. Let me know if you need any further clarification.
Thanks in advance!
I've found a way to achieve this by using a couple of Transform functions. I reversed parsing the column to JSON, and instead used the Split Column function to separate the JSON by the , delimiter (creating the 3 columns I required), and then isolated the required data within the columns using the Extract function.
This question already has answers here:
Is storing a delimited list in a database column really that bad?
(10 answers)
Closed 2 years ago.
Hello to Folks out there,
I need some suggestion on How to design MySQL Table Structure for search optimization.
I am creating a Real estate website. In that I have property table and all its associated tables.
I can design my table for saving these records in two ways.
I have amenity master table
id property_name
----------------
1 Property A
2 Property B
3 Property C
Approach 1
Property Table
id property_name
----------------
1 Property A
2 Property B
3 Property C
Property_Amenities table
id p_id amenity_id
------------------------
1 1 1
2 1 2
3 1 3
4 1 4
5 2 1
6 2 1
Approach 2
Property Table
id property_name amenity_id
----------------------------
1 Property A 1,2,3,4,5
2 Property B 1,4,7,9,12
3 Property C 3,4,7,8,9,10
Approach 1 Query : I can join tables and get the all the amenities name for a particular property. Add the required index for optimization. For A property there will be 20 amenities on averages. Suppose I have 100K property records then to get amenities for particular property, MySQL query will search in 2 millions records of Property_Amenities table.
Approach 2 Query : I can search using FIND_IN_SET or IN MySQL operator. But I was going through this search topic and it seems that this approach will be much resource intensive and cost more for same amount of data i.e 100k property records.
Any suggestion will be appropriated. What your thought on this scenario or any other approach I should follow.
Use the first approach. Period. The second is wrong, wrong, wrong. Here are some reasons:
A SQL string should not be used to store multiple values.
Integers should be stored using the proper type.
Foreign key relationships should be declared.
SQL has very poor string handling methods.
SQL has a great way to store lists; it is called a "table" not a "string".
I am wondering if any of you would be able to help me. I am trying to loop through table 1 (which has duplicate values of the plant codes) and based on the unique plant codes, create a new record for the two other tables. For each unique Plant code I want to create a new row in the other two tables and regarding the non unique PtypeID I link any one of the PTypeID's for all inserts it doesnt matter which I choose and for the rest of the fields like name etc. I would like to set those myself, I am just stuck on the logic of how to insert based on looping through a certain table and adding to another. So here is the data:
Table 1
PlantCode PlantID PTypeID
MEX 1 10
USA 2 11
USA 2 12
AUS 3 13
CHL 4 14
Table 2
PTypeID PtypeName PRID
123 Supplier 1
23 General 2
45 Customer 3
90 Broker 4
90 Broker 5
Table 3
PCreatedDate PRID PRName
2005-03-21 14:44:27.157 1 Classification
2005-03-29 00:00:00.000 2 Follow Up
2005-04-13 09:27:17.720 3 Step 1
2005-04-13 10:31:37.680 4 Step 2
2005-04-13 10:32:17.663 5 General Process
Any help at all would be greatly appreciated
I'm unclear on what relationship there is between Table 1 and either of the other two, so this is going to be a bit general.
First, there are two options and both require a select statement to get the unique values of PlantCode out of table1, along with one of the PTypeId's associated with it, so let's do that:
select PlantCode, min(PTypeId)
from table1
group by PlantCode;
This gets the lowest valued PTypeId associated with the PlantCode. You could use max(PTypeId) instead which gets the highest value if you wanted: for 'USA' min will give you 11 and max will give you 12.
Having selected that data you can either write some code (C#, C++, java, whatever) to read through the results row by row and insert new data into table2 and table3. I'm not going to show that, but I'll show how the do it using pure SQL.
insert into table2 (PTypeId, PTypeName, PRID)
select PTypeId, 'YourChoiceOfName', 24 -- set PRID to 24 for all
from
(
select PlantCode, min(PTypeId) as PTypeId
from table1
group by PlantCode
) x;
and follow that with a similar insert.... select... for table3.
Hope that helps.
I'm trying to achieve the following in SSIS:
Union All 6 separate SQL queries (OLE DB Sources) (call this dataset A)
Dataset A contains
id Col A Col B......
1
2
3
4
5
6
7
8
I have another OLE DB Source SQL query (dataset B) that contains
id Col A Col B .......
1
2
3
4
10
11
12
All columns Col A, Col B etc. are the same in all the queries.
I want to return dataset A unioned (UNION ALL) with dataset B where the ids in dataset B don't match the ids in dataset A. i.e.
id Col A Col B......
1
2
3
4
5
6
7
8
10
11
12
Also before performing the UNION there is some extra work done on dataset B to fill in NULL values in the columns.
I can achieve this quite easily by inserting dataset A into a table then using this table in a subsequent NOT EXISTS or similar query to get the missing info and then UNIONing together. Ideally though I'd like to do this in one sweep using a dual multicast with one side going into a Lookup No match and the other straight into a destination table.
Unfortunately the Lookup and No Match output returns the 'wrong' missing data i.e. it returns
5
6
7
8
rather than
10
11
12
this is because the lookup (dataset B) and source data (dataset A) are the 'wrong way' around. I don't want to rewrite dataset A as the Lookup query so is there another way around this using a different transform or other method?
I've experimented with the Cache Transform but it doesn't seem to work in the same data flow as the Lookup Transform as there is read/write contention.
Thanks,
Rich.
Sounds like you want a "Fuller Outer Join" kind of "Merge" component, then a kinda manual merge after the fact. If I misunderstood you, you can always add a Conditional Split after the Merge, to filter if A is null, if B is null, etc.
An example for the "Merge Columns" is creating a repeated "Id" with this expression: "ISNULL(Id_A) ? Id_B : Id_A"
An example for the "Full Outer Join - Merge":
I would like help with sql query code to push the consequent data in a specific column down by a row.
For example in a random table like the following,
x column y column
6 6
9 4
89 30
34 15
the results should be "pushed" down a row, meaning
x column y column
6 null or 0 (preferably)
9 6
89 4
34 30
SQL tables have no inherent concept of ordering. Hence, the concept of "next row" does not make sense.
Your example has no column that specifies the order for the rows. There is no definition of next. So, what you want to do cannot be done.
I am not aware of a simple way to do this with the way you are showing the table being formatted. If your perhaps added two consecutively numbered integer fields that provide row number and row number + 1 values, you could join the table to itself and get that information.
After taking a backup of you table:
Make a PHP function that will:
- Load all values of Y into an array
- Set Y = 0 (MYSQL UPDATE)
- load the values back from PHP array to MYSQL