I have somewhat logical thing to do in SQL server, If my question is not valid or understandable pardon me , as I didn't knew how this question was supposed to be asked.
I have 2 tables which is to be converted into 3rd table.
1st table is a "Data" table having columns but their value is in codes whose TEXT which can be found in "Options" table.
State | Language | Gender
2 | 3 | 1
2nd table is "Options" Table which is a master table for converting code of "Data" table into text.
Question column is columns of "Data" table.
Option column is Text corresponding to code.
Code column is Value of "Data column"'.
Question | Option | Code
State | Orissa | 2
Language | English | 3
Suppose I want the Text of State column for code : 2 in "Data" table then I would look into "Options" table and get the Option columns value 'Orissa' corrrsponding to State and code 2.
Resultant table should contain only Texts converted from codes.
State | Language | Gender
Orissa | English | 1
1 thing to note is that Gender column didn't got converted text, because this column was 'NOT' contained in "Options table".
Result is to be created Dynamically as "Data" and "Options" table are created dynamically.
Any help would be appreciated.
Try joining your first table twice to the second table:
SELECT
COALESCE(t2.Option, 'NA') AS State,
COALESCE(t3.Option, 'NA') AS Language,
t1.Gender
FROM table1 t1
LEFT JOIN table2 t2
ON t1.State = t2.Code AND t2.Question = 'State'
LEFT JOIN table2 t3
ON t1.Language = t3.Code AND t3.Question = 'Language';
Related
Please who can help with this scenario?
I have two tables, both they have a common column ID, and Table 1 has a column Title. Normally I should update the content of this Title column for some ID, but since the table was already in use somewhere else, it wasn't a good idea to change data directly in Table 1.
That's why I created a new table table 2, which hold only the Title that must be changed associated with these ID that must be changed.
Now I am trying to get these updated titles from table 2, when there is a matching ID in table 1, otherwise show only the contents of table 1.
The result should be something like that but without using If statements.
__ID__ Title
| | | |
| | | |
You can use LEFT OUTER JOIN to this new table and COALESCE() function to say "If there is data in the new table, use it, otherwise use the data in the existing table" . Something like:
SELECT t1.id, COALESCE(t2.title, t1.title) as title
FROM t1
LEFT OUTER JOIN t2 ON t1.id = t2.id;
i have a requirement like below
i have two tables in same data base, both table have same structure and column count.but the columns not present in the same position.
ex:
table 1
id name age
1 dhileep 22
2 uday 33
table 2
id age name
1 20 udayga
2 22 uday
i have id column is same for all tables, if i change the table also i have id same, but may columns name and column count and data count will change.
my final output is:
column_name id table1 table 2
name 1 dhileep udayga
note: i gave above as example, the count of columns is more than 500 and data exist approximately 50000+
use Sql JOIN .to join the 2 tables
use the following answer .i think it is useful for u.
SELECT t1.id,t1.name,t2.name FROM table1 AS t1 JOIN table2 AS t2 ON t1.id = t2.id
I have a sparse table structured like:
id | name | phone | account
There is no primary key or index
There are also null values. What I want is to "glue" data from different rows together, e.g.:
Given
id | name | phone | account
1 null '339-33-27' 4
null 'John' '339-33-27' 4
I want to end up with
id | name | phone | account |
1 'John' '339-33-27' 4
However, I don't know which values are missed in the table.
What are the general way to approach this kind of problem? Do I need to use only joins or might be recursive functions?
Update: Provided more clear example
id to account is many-to-many
account to name is many-to-many
phone to name is one-to-one
The database is basically raw transactional data
What I want to is to get all the rows for which I already have / could find an account
If I understand you correctly then this might work. What you need is a self join
select t2.id, t1.name, t1.phone, t1.account
from table1 t1
join table1 t2 on t1.account = t2.account and t1.phone = t2.phone
where t1.name is not null
However this particular query relies on an assumption from your example data. My assumption is that if name is not null, Id will be null and the Id can be found by looking at the phone number and account. If this assumption is not true , then we may need more sample data to solve your problem.
Depending on the data, you might need left joins or to swap so that T1 gets the id and not the name and the where condition is that ID is not null. It's hard to tell with such a small data sample size.
I have a database with several tables, one being used to reference others :
Main table :
MAIN | table1 | table2 | table3
int | int | int
Each line of this table corresponds to a product, and contains the oid of a line in a daughter table
Then I have my daughter tables :
TABLE1 | name | adress | phone
| text | text | ...
My question is quite simple, even though as I just start using databases I can't get to find the answer.
I would like to get the oid(s) of the line(s) of the main table which references the line of table1 for which name is equals to "bob".
Something like :
SELECT * from main where table1.name = "bob"
If no complete solution, could you point me to some documentation ?
I think I miss vocabulary to find proper ressurces to do that.
Thanks by advance
assuming the oid is a column in table 1 as well as in the main table:
SELECT m.oid
FROM main m
INNER JOIN table1 t
ON m.table1 = t.oid
WHERE t.name = 'bob'
You mean like SELECT oid FROM MAIN WHERE table1 IN (SELECT oid FROM TABLE1 WHERE name = 'bob')?
table1 == foreign key from MAIN to TABLE1.
I want to fetch the a field say excep_point from a transparent table z_accounts for the combination of company_code and account_number. How can I do this in ABAP SQL?
Assume that table structure is
|company_code | account_number | excep_point |
Assuming you have the full primary key...
data: gv_excep_point type zaccounts-excep_point.
select single excep_point
into gv_excep_point
from zaccounts
where company_code = some_company_code
and account_number = some_account_number.
if you don't have the full PK and there could be multiple values for excep_point
data: gt_excep_points type table of zaccounts-excep_point.
select excep_point
into table gt_excep_points
from zaccounts
where company_code = some_company_code
and account_number = some_account_number.
There is at least another variation, but those are 2 I use most often.
For information only. When you selects data into table you can write complex expressions to combine different fields. For example, you have internal table (itab) with two fields "A" and "B". And you are going to select data from DB table (dbtab) wich have 6 columns - "z","x","y","u","v","w". And for example each field is type char2 You aim to cimbine "z","x","y","u" in "A" field of internal table and "v","w" in "B" field. You can write simple code:
select z as A+0(2)
x as A+2(2)
y as A+4(2)
u as A+6(2)
v as B+0(2)
w as B+2(2) FROM dbtab
INTO CORRESPONDING FIELDS OF TABLE itab
WHERE <where condition>.
This simple code makes you job done very simple
In addition to Bryans answer, here is the official online documentation about Open SQL.