I have two tables that look like this:
table1:
table2:
and I'd like to merge them, to create a table with a header like this:
variablenname | mean | stddev | ms_form | dependent | fvalue | probf
The first nine rows should be the first table with empty values in the last 3 columns followed by the three rows from the second table with empty values in the first 4 columns. I'm sorry I can't post pictures or a third link, but my reputation is too low. I hope it's understandable.
I tried to create a new table and select everything from both with union but that didn't work. Can anybody help me?
Thanks!
You can use ALTER to add the last 3 columns to table 1. From there, INSERT table 2 onto table 1. So something like this.
ALTER table1 ADD dependent varchar(16)
ALTER table1 ADD favalue varchar(16)
ALTER table1 ADD probf varchar(16)
INSERT INTO table1(dependent, fvalue, probf)
SELECT * FROM table2
You can use a combination of joins and unions
LEFT JOIN the two tables in first query on fields that won't have a match
then UNION the second query with a RIGHT JOIN of the same tables.
The LEFT JOIN will only show results from the first table, then the RIGHT JOIN will only show results from the second
Something Like:
SELECT * FROM table1 as t1
LEFT JOIN table1 as t2 on t1.variablenname = t2.fValue
UNION
SELECT * FROM table1 as t1
RIGHT JOIN table1 as t2 on t1.variablenname = t2.fValue;
This is how I would like the final table to look like:
Related
Let's say var_Environment = QA1,QA2.
I have two tables table 1 and table 2. Table 1 has some column name var_Environment =QA1; where as table 2 does have the same column name but no values
Now my task is, I need to copy all the values from table1 to table2 only if condition meets var_Environment =QA2 How will I perform this operation ? Can I use Union All with some condition? Can someone help?
You can use INSERT INTO SELECT like below.
The WHERE clause may need adjustment, so run the the SELECT query by itself to check if the correct rows are selected.
INSERT INTO table2
SELECT var_Environment
FROM Table1
WHERE var_Environment LIKE '%QA2%'
I need to join two tables in SQL, which has two related columns (column ID1 in Table 1 and column ID in Table 2). ID1 in table 1 consists of 6 digits, whereas ID2 in table 2 consists of 6 digitis but an additional quotation marks (") in the beginning and end of the string. I need to remove these quotation marks and join the two tables to verify if there is any values reocurring in both columns.
I know how to remove first and last character of the string in table 2:
SELECT SUBSTRING ([ID2],2,Len([ID2])-2) FROM [dbo].[table2]
I need to join this new "trimmed" column with the other column from table 1.
Any suggestions?
Assuming you are using ms sql server db, and need everything from table1 and matched from table2 then:
sample:
table1 | table2
[ID] | [ID]
547832 | "547832"
-----------------------------
select table1.* , table2.*
from
db.tb1 table1
left join
db.tb2 table2
on
table1.[ID] = SUBSTRING([ID2],2,Len([ID2])-2) ;
First extract your trimmed column with different name by using 'AS' and then you can join the tables.
Try like the below
syntax: SELECT Substring( columnname , positon, length) AS Newcolumnname FROM Tablename;
EX: SELECT Substring(customerName,1,5) AS Newstr from Customer
Joins Table2 ON customer.Newstr = Table2.name;
I am using MS SQL, yes.
Thanks for the reply. However, why is it a left join and not an inner join here? Just curious.
So, essentially what I need to do is:
In the first table, I have around 10 columns, in the second table I have 5 columns. They all have different names, ID was just used as an example. Two of the columns from table 2 appears to have similar values as two of the columns from table 1 (one is an ID of 6 digits, the other is names). I want to remove the first and last character of the 6 digits in the ID column in table 2 and join that and the names column with ID and names from table 1. Hope it makes sense
(P.S. I am still learning SQL and you can consider me a newbie)
I have 2 sample tables as follows:
Table 1
|Profile_ID| |Img_Path|
Table 2
|Profile_ID| |UName| |Default_Title|
My scenario is, from the 2nd table, i need to fetch all the records that contain a certain word, for which i have the following query :
Select Profile_Id,UName from
Table2 Where
Contains(Default_Title, 'Test')
ORDER BY Profile_Id
OFFSET 5 ROWS
FETCH NEXT 20 ROWS ONLY
(Note that i am setting the OFFSET due to requirements.)
Now, the scenario is, as soon as i retrieve 1 record from the 2nd table, i need to fetch the record from the 1st table based on the Profile_Id.
So, i need to return the following 2 results in one single statement :
|Profile_Id| |Img_Path|
|Profile_Id| |UName|
And i need to return the results in side-by-side columns, like :
|Profile_Id| |Img_Path| |UName|
(Note i had to merge 2 Profile_Id columns into one as they both contain same data)
I am still learning SQL and i am learning about Union, Join etc. but i am a bit confused as to which way to go.
You can use join:
select t1.*, t2.UName
from table1 t1 join
(select Profile_Id, UName
from Table2
where Contains(Default_Title, 'Test')
order by Profile_Id
offset 5 rows fetch next 20 rows only
) t2
on t2.profile_id = t1.profile_id
SELECT a.Profile_Id, a.Img_Path, b.UName
FROM table1 a INNER JOIN table2 b ON a.Profile_Id=b.Profile_Id
WHERE b.Default_Title = 'Test'
I have two different tables, we will call table 1 and table 2. Within table 1, I have a column known as featureid which is a numerical value that corresponds to a numerical value in table 2 known as the termid. Also, within this table 2, each of the different termid corresponds to a plain text description of that termid.
What I am attempting to do is join the featureid in table 1 to the termid in table 2, but have the output be a two column display of the plain text description and occurrence of each within table 1.
I know I need to use the JOIN and COUNT syntax within SQL, but not sure how to correctly write the command.
Thanks!
You actually only need one column in the GROUP BY. It's also good practice to specify which values are coming from which table, like so:
SELECT
table2.textDesc,
COUNT(table1.featureid) AS OccurrenceCount
FROM
table1
INNER JOIN
table2 ON
table1.featureid = table2.termid
GROUP BY table2.textDesc
I think this is what you are looking for:
SELECT textDesc, COUNT(featureid)
FROM table1, table2
WHERE featureid=termid
GROUP BY featureid, textDesc
Alternatively, you can use a different syntax (with the same end result) like so:
SELECT textDesc, COUNT(featureid)
FROM table1 INNER JOIN table2
ON featureid=termid
GROUP BY featureid, textDesc
I have a table that I need to Update by adding a new field.. I can alter the table and update each row . but is there a way of appending the result of a query to the table? ( I know that the result will have the same number of rows)
EDIT: So let me make it clear
I have
table1 | col1,col2
I generate another single column table
table2 | col1
I want
table3 | table1.col1,table1.col2,table2.col1
By the way table1 & table2 have no common fields so I cant join them meaningfully.
I think so. I haven't tested this, but what I can find it seems that you can use a SubQuery to do something along the lines of
UPDATE Table1 SET Column1 = Column1 & (SELECT Column2 FROM Table2 WHERE xxxx)