I have four models: Table, Column, Row, Cell. A Cell belongs to a Row and a Column, both of which belong to a Table. The UI of this looks like your typical table view, the values displayed being those of the Cells.
Cells have a value. Each Table also has one Column of type 'key', whose Cell's values are unique.
Columns can be relational-type, meaning their Cell's values reference the Cells in the key Column of a different Table, the linked Table. This way, a relational Cell will actually 'belong to' a Row from the linked Table since key values are unique.
For relational-type Cells, the value used for display isn't the value stored in the Cell (a value from the linked table's key Column). We let the user choose a Column from the linked Table as the 'display column', and those values are the ones used for display in this Table's relational Column.
Normally we order Rows by the plain old Cell values in the Column chosen for ordering. However, when ordering by a relational-type Column, we want to order by their Cell's display value. This means for each Row, we need to find the value stored in the relational Cell (a key value from the linked Table), find the corresponding Row from the linked Table whose key Column's Cell matches that value, then find the value of that Row's display Column's Cell, then order by that value.
Some sample values.
Table 1
Column 1 (key) | Column 2
1 | b
2 | a
3 | c
Table 2
Column 2 links to Table 1. This means it stores the key values from Table 1, but currently uses the values from Table 1's Column 2 as its display column.
Actual values in Table 2:
Column 1 (key) | Column 2 (relational)
4 | 1
5 | 2
6 | 3
What's displayed:
Column 1 (key) | Column 2 (relational)
4 | b
5 | a
6 | c
Post ordering, Table 2 should look like:
Column 1 (key) | Column 2 (relational)
5 | a
4 | b
6 | c
I will be frank: I have absolutely no idea how to construct this SQL query/subqueries. Any suggestions pointing me in the right direction would be very much appreciated.
If I am following correctly, you want a join and order by:
select t2.column1, t1.column2
from table2 t2 join
table1 t1
on t2.column2 = t1.column1
order by t1.column2
Related
I've been trying to select multiple values from another table (i.e. texts in multiple rows) and display it on main table so that multiple values can be on one cell.
(also, I don't speak English, so some terms might sound different, sorry)
Example:
1st table:
ID
Name
Value1
1
Cell 2
A; B
2
Cell 4
B;C;D
2nd table (column Value1 is connected to the previous table's Value1 column):
ID
Value1
1
A
2
B
3
C
4
D
5
E
So in the first table I want to put both values in one cell from the 2nd table
Thank you!
At the moment, I am able to select only one value for Value1 column in the first table that is referenced to the second table.
On phpMyAdmin, I tried to add several columns on one constraint, but error occurred.
I have a requirement of making the duplicate records in a table from the number specified in a separate table. Given below the example.
Table A1 having many columns along with seg_id column. And Table B1 having many columns along with seg_id column and iteration_no column.
I have to join both the tables using seg_id column and take the value present in iteration_no column. If the value is 5 then I need to duplicate the entire seg_id row in Table A1 into 5 rows without any changes. Could you please help.
I have a table with rows of multiple types. How do I create another table from it so that the rows in this table reflect the sum of all rows of each type from the previous table.
For e.g. I have a Table1 with 5 rows of type A, 6 rows of Type B. I want to create a table (Table2) that contains one row of type A (which is sum of all the 5 rows from Table1) and one row of type B (again this is the sum of all 6 rows from Table1).
In my talned job I am copying data from Excel to SQL table.
For this job to maintain the foreign key constraint I had to do a look up before copying the data.
The task goes like this.
I have to copy data in Table2 (id keys value).
My excel sheet has data for id and keys column. Table 1 has two columns id and value.
For value column's data I want to look at Table1's corresponding entry with the id of the current record in Table2. I have to copy the data from Table1's value column to Table2's value column.
Excel (id 1 2 3, keys a b c)
Table_1 (id 1 2 3, value 123 456 789)
desired output: Table_2 (id 1 2 3, keys a b c, value 123 456 789)
current output: Table_2 (id 1 2 3, keys a b c, value null null null)
How do I properly map this?
You've set the job up exactly as needs to be done really so it's not your job layout that's the problem here.
If you're expecting every record in your Excel document to have a matching record in your database table then you should use an inner join condition in your tMap join like so:
And this then allows you to have an output that grabs everything that isn't joining (which is your issue here):
This should show you everything in your source (not lookup) file that isn't matching. I suspect that everything is failing to match on your join condition (essentially WHERE ExcelDoc.id = Table.Id) even if it looks like it should. This could be down to a mismatch of datatypes as they are read into Talend (so the Java/Talend datatypes such as int/Integer or String rather than the DB types) or because one of your id columns has extraneous whitespace padding.
If you find that your id column in one of your sources does in fact have any padding then you should be able to convert it in a previous step before your join or even just make the transformation in the join:
The only other thing I'd recommend is that you name your flows between components (just click on them to change the name), especially when you are joining anything in a tMap.
Let us consider the following table structures:
Table1
Table1_ID A
1 A1
2 A1;B1
and
Table2
Table2_ID Table1_ID B C
1 1 foobar barfoo
2 2 foofoo barbar
The view I'm using is defined by the following query:
SELECT Table1.A, B, C
FROM Table2
INNER JOIN Table1 ON Table1.Table1_ID = Table2.Table1_ID;
95% of A's data consists in a 2 characters long string. In this case, it works fine. However, 5% of it is actually a list (using a semicolon as a separator) of possible values for this field.
This means my users would like to choose between these values when it is appropriate, and keep using the single value automatically the rest of the time. Of course, this is not possible with a single INNER JOIN, since there cannot be a constant selected value.
Table2 is very large, while Table1 is quite small. Manually filling a local A field in each row within Table2 would be a huge waste of time.
Is there an efficient way for SQL (or, more specifically, SQL Server 2008) to handle this? Such as a list with a selected item within a field?
I was planning to add a "A_ChosenValue" field that would store the chosen value when there's a list in A, and remain empty when A only stores a single value. It would only require users to fill it 5% of the time, which is okay. But I was thinking there could be a better way than using two columns to store a single value.
Ideally you would just alter your schema and add a new entity to support the many-to-many relationship between Table1 and Table2 such as the following with a compound key of all three columns.
Table3
| Table1_ID | Table2_ID | A |
-----------------------------
| 1 | 1 | A1 |
------------------------------
| 2 | 2 | A1 |
------------------------------
| 2 | 2 | B1 |
------------------------------
You could then do a select and join on this table and due to it being indexed you won't lose any performance.
Without altering the table structure or normalizing data it is possible using a conditional select statement like that shown in this SO post but the query wouldn't perform so well as you would have to use a function to split the values containing a semi-colon.
Answering my own question:
I added a LocalA column in Table1, in order that my view actually selects ISNULL(LocalA, Table1.A). Therefore, the displayed value equals A by default, and users can manually overwrite it to select a specific value when A stores a list.
I am not sure whether this is the most efficient solution or not, but at least it works without requiring two columns in the view.