How to merge two tables with having same column name - sql

select *
INTO [dbo].[aTable]
from dbo.bt btg left join dbo.btt bta on btg.specialty = bta.specialty
order by 1, 6
I am getting the following error:
Column names in each table must be unique. Column name 'Specialty' in table 'aTable' is specified more than once.
Columns for bt table:
Location
Specialty
Provider
Column for btt table:
Specialty
Topic
I am trying to get Location, Specialty, Provider, and (join all topics for the specialty).

The issue here is the "Select *", which will select all fields in your result set. Try specifying the specific fields that you want to insert.
For instance:
SELECT Location, btg.Specialty, Provider, Topic
INTO INTO [dbo].[aTable]
from dbo.bt btg left join dbo.btt bta on btg.specialty = bta.specialty
order by 1, 6

Alias name is all you need to mention to avoid conflict when you join two tables with same column names.
SELECT btg.Location, btg.Specialty, btg.Provider,btg.Topic
INTO INTO [dbo].[aTable]
from dbo.bt btg join dbo.btt bta on btg.specialty = bta.specialty
order by 1, 6

you can do something like this:
select btg.*, topic
INTO [dbo].[aTable]
from dbo.bt btg left join dbo.btt bta on btg.specialty = bta.specialty

Related

Join tables and add column only with a specific value

I have four tables (a, b, c, d) in PostgreSQL database:
I can connect these tables with subject.id and project.id unique keys. My query is:
SELECT subject_id, firstname, lastname, national_id, project_title, project_id, note_template
FROM project_subject
JOIN subject ON subject.id = project_subject.subject_id
JOIN project ON project.id = project_subject.project_id
JOIN subject_note ON subject_note.subject_id = project_subject.subject_id
WHERE project_id = xxxx
My problem is that in table d (subject_notes) one subject can have several notes (varchar) plus selectable note_template (int). I would like to have joined table with columns subject_id, firstname, lastname, national_id, project_title, project_id and note_template. When I join four tables I get result with several same subject rows with different notes. I would like to have table where each subject is listed only one time and to the last column value is added only when note_template=16. Rest of the subjects would have null value for note_template.
Change the last join to a LEFT join and add the condition note_template = 16 in the ON clause:
LEFT JOIN subject_note ON subject_note.subject_id = project_subject.subject_id
AND subject_note.note_template = 16

Insert into newly created column

I have a Students table with 2 col as Rollno and Marks with 12 records
I added another column in students table as Name. How do I fill Names with 12 records from another table in SQL Server?
I tried this:
SELECT * FROM [SampleDB].[dbo].[Student_SQL]
insert into Student_SQL(name)
select name
FROM [School].[dbo].[StudentMaster]
update [Student_SQL]
set name = 'David'
where RollNo = 4`
I think you want the upate/join syntax. Assuming that the two tables relate through column rollno:
update ss
set ss.name = sm.name
from student_sql ss
inner join student_master sm on sm.rollno = ss.rollno
The upside of this approach is that it filters the rows and only update those that match in the master table.
in order to do this, you need to have column in common for both tables
update [Student_SQL] s
set name = (select name from other_table o where o.roll_no = s.roll_no)

SQL join based on select as column name

So in one table I can use a replace and charindex to extract a specific ID that relates to a PK in another table, I want to then join the data from the other table based on the trimmed value, how can I do this?
select top 100 *, Replace(Left(LogValue,Charindex(';', LogValue) - 1) ,'RtaId=', '') as TaskID, PrmRoutingTask.*
from SytLog
inner join PrmRoutingTask on RtaId = TaskID
where LogTableName like '%Routing%' and LogValue like '%RtaAniId=397%'
order by 5 desc;
The problem I get is that the temp column name I create (TaskID) is not working in the inner join where in fact the results of TaskID have the reference to the RtaId in the RoutingTask table.
Assuming LogValue belongs to the first table you can use the column named TaskID if you produce a subquery as a table expression of the main query.
For example you can produce the column in the table expression a by doing:
select top 100
a.*,
PrmRoutingTask.*
from (
select *,
Replace(Left(LogValue,Charindex(';', LogValue) - 1) ,'RtaId=', '') as TaskID
from SytLog
) a
inner join PrmRoutingTask on PrmRoutingTask.RtaId = a.TaskID
where LogTableName like '%Routing%'
and LogValue like '%RtaAniId=397%'
order by 5 desc

How to find member records with one type but not other from the same table

I have a table named membercontact which contains members address information.
It can have two addressTypes for a member .i.e. Primary and Mailing.
How can I find out members from this table who have Primary addresses but no mailing adddresses.
Something like this:
SELECT DISTINCT A.[MemberID]
FROM membercontact A
LEFT JOIN membercontact B
ON A.[MemberID] = B.[MemberID]
AND B.[addressTypes] = 'Mailing'
WHERE A.[addressTypes] = 'Primary'
AND B.[MemberID] IS NULL;
You can try this :
select * from membercontact M
where
not exists(select 1 from membercontact
where memberid=M.memberid and addressTypes = 'Mailing')
You can check the query with data SQL HERE

How to create an sql command that has SELECT statements from 2 different tables?

How to create an sql command that has SELECT statements from 2 different tables? For example,
select ID from EMP_details, select job_id from Jobs_details
So how can i possibly merge both into one
Selecting from two or more tables When rows in them tables has some sort of relation so you want to extract the corresponding row you would use a JOIN something like this ....
JOIN
SELECT EMP.ID, JD.job_id
FROM EMP_details EMP INNER JOIN jobs_details JD
ON EMP.CommonColumn = JD.CommonColumn
Results From Two SELECTS
When you have two SELECT statements and just want to get the results returned from them queries into one row you can do something like this ...
SELECT X.A , Y.B
FROM (select ID AS A from EMP_details) X, (select job_id AS B from Jobs_details) Y
This is known as a JOIN in SQL. Your query might look like this:
SELECT EMP_details.ID,
EMP_details.Name,
Job_details.RefNumber
FROM EMP_details,
Jobs_details
WHERE EMP_details.ID = Jobs_details.job_id;
Having a column in both tables by which you can match the rows from the first table to the second one, like for example the job_id in Jobs_details matches job_id in Emp_details, you could do:
SELECT e.ID,j.job_id
FROM EMP_details e
INNER JOIN jobs_details j ON e.job_id = j.job_id
For more information on JOIN's see the documentation.
Have you try
SELECT id FROM (SELECT id FROM EMP_details UNION ALL SELECT id FROM Jobs_details) as temp_table;
Thanks,