Create a table from full outer join query - sql-server

I have two tables namely :- TDM & AccountMaster. Both are having three equal columns and I have to create a table retrieving all the rows from TDM-table joining the three columns,i.e. FD_BRANCH,FD_CUSTCODE & PRODUCTID.
while creating table through select into clause I get an error
Column names in each table must be unique. Column name 'FD_BRANCH' in table 'acty' is specified more than once.
Please find the following query from which I want to create a table which as per my requirement :-
SELECT * FROM (SELECT FD_BRANCH,FD_CUSTCODE,PRODUCTID FROM TDM
GROUP BY FD_BRANCH,FD_CUSTCODE,PRODUCTID) A full OUTER JOIN AccountMaster B
ON( A.FD_BRANCH=B.FD_BRANCH AND A.FD_CUSTCODE=B.FD_CUSTCODE AND
A.PRODUCTID=B.PRODUCTID)

Change your select to get only the fields you need from one of the 2 tables.
Select A.*
FROM (
or
Select B.FD_BRANCH,
B.FD_CUSTCODE,
B.PRODUCTID
FROM (
FULL OUTER JOIN combines the 2 sets of columns from both queries so you end up with at least 6 columns. Even though they're from different tables or aliases the columns names are the same.

Related

SELF JOIN /SUBQUERIES/SSIS performance

Hi I have three tables
Wrk_Order ,Wrk_Info,Wrk_Driver
all the three have DFirstName,DLastName,DUsername columns . I want to anonymise them
What I did is I have got distinct of each three columns in all the three table and loaded into lookup table as
Table Name :LKP_driverinfo
Columns :[DriverInfo,DriverAnonInfo]
DriverInfo column will have data as follows
SELECT Distinct DFirstName FROM Wrk_Order
UNION
SELECt DISTINCT DLastName FROM WRk_Order
UNION
SELECt DISTINCT DUsername FROM WRk_Order
UNION
SELECT Distinct DFirstName FROM Wrk_Info
.
.
.
Similarly for rest two tables
DriverAnonInfo : will have anonymized value of Driverinfo
I need to update DFirstName,DLastName,DUsername of all the three tables to anonymised information [DriverAnonInfo] from Lookup table LKP_driverinfo
whats the best way to achieve this self-join or inner queries or using SSIS?
You seem to create a sort of EAV model with your driver info, but you do not seem to keep any references for the originating tables.
Perhaps you can try adding 3 more columns to your driverinfo table, to hold the id of the table that the value comes from. That way you can directly update or join entries in the tables.

Select value from different tables based on the column value in SQL Server

I have a main table A with the following fields:
Then I have three separate tables, each for Buildings, Classrooms and Offices. All these tables have two columns; ID and Name.
I want to query the table A to get the following result:
How can I do this?
Your data isn't really normalized. Having three separate tables all serving the same lookup is causing you some headache... so I unioned the 3 tables together and created a 'src' column so you could join table A's type and Id back to table B's ID and src. You'd have been better off having one table and non-repeating IDs and a type ID to specify if it's a building classroom or office.
Select *
from A
LEFT JOIN (SELECT 'Building' as src, ID, Name FROM Buildings UNION ALL
SELECT 'Classroom' as src, ID, Name FROM Classrooms UNION ALL
SELECT 'Office' as src, ID, Name FROM Offices) B
on A.Location_Type = B.Src
and A.LocationID = B.ID
I used a left join here in case not all records in A have an associated record in B. However, an inner join should work as well.
Should be doable with the use of a join.
Something along those lines should work:
SELECT tabelA.ID, tabelA.Subject, tabelA.Date, tabelA.locationType, tabelB.location
FROM tabelA INNER JOIN tabelB on tabelA.locationID = tabelB.locationID

Right way to use distinct in SQL Server

I am trying to retrieve some records based on the query
Select distinct
tblAssessmentEcosystemCredit.AssessmentEcosystemCreditID,
tblSpecies.CommonName
from
tblAssessmentEcosystemCredit
left join
tblSpeciesVegTypeLink on tblAssessmentEcosystemCredit.VegTypeID = tblSpeciesVegTypeLink.VegTypeID
left join
tblSpecies on tblSpecies.SpeciesID = tblSpeciesVegTypeLink.SpeciesID
where
tblAssessmentEcosystemCredit.SpeciesTGValue < 1
The above query returns 17,000 records but when I remove tblSpecies.CommonName, it retrieves only 4200 (that's actually correct).
I have no idea how to distinct only tblAssessmentEcosystemCredit.AssessmentEcosystemCreditID column and retrieve all other table columns in the query.
This query selects the different COMBINATION of AssessmentEcosystemCreditID and CommonName; if you want only one row per value of AssessmentEcosystemCreditID then you need to use a GROUP BY, as suggested by #JonasB; however, in that case, there could be several values of CommonName per value of AssessmentEcosystemCreditID , and so SQL requires you to specify WHICH one you want
Select tblAssessmentEcosystemCredit.AssessmentEcosystemCreditID ,
max(tblSpecies.CommonName) as CommonName,
min(tblSpecies.CommonName) as CommonName2, -- so you can verify you only have one value
from tblAssessmentEcosystemCredit
left join tblSpeciesVegTypeLink
on tblAssessmentEcosystemCredit.VegTypeID = tblSpeciesVegTypeLink.VegTypeID
left join tblSpecies on tblSpecies.SpeciesID= tblSpeciesVegTypeLink.SpeciesID
where tblAssessmentEcosystemCredit.SpeciesTGValue <1
GROUP BY tblAssessmentEcosystemCredit.AssessmentEcosystemCreditID
See this topic: mySQL select one column DISTINCT, with corresponding other columns
You probably have to deactivate ONLY_FULL_GROUP_BY, see http://dev.mysql.com/doc/refman/5.7/en/sql-mode.html#sqlmode_only_full_group_by

Receive (bind) the data coming from two select commands n a single proc in SQL Server

create proc [dbo].[SpCateProducts](#cate_id int)
as
begin
select sb.subcate_name, sum(p.qty)
from subcategory sb
join product p on p.subcate_id = sb.subcate_id
where sb.cate_id = #cate_id
group by sb.subcate_name
select top 1 pname from product join subcategory
on product.subcate_id=
(
select top 1 subcate_id
from subcategory
where cate_id=3
order by NEWID()
)
end
but it is returning two tables, then how can I bind it to the listview?
Or is there any way that it will returns a single table, which will contain all these required rows of data?
I mean that that 1st one select command already giving a table of 4 rows and 4 columns, then is there any way that it will contain one more column (pname) which is currently returning by 2nd select command
Try using with statement for both tables. Then use simple join over tables.

Merging Two Tables in SQL Server

I have two tables, each with some columns that are the same. However, each table also contains data that is unique. (Similar data includes a row name).
What I need to do is tack on the data from table two to it's matching row in table one (matching the name column).
Is there any way to do this?
I need stuff from table two to go into table 1 where the names match:
The following query should return all matching rows with columns from both tables. Note that any unique rows (that only exist in table one or two) will be excluded.
SELECT
one.matchingColum,
one.oddColum,
two.evenColumn
FROM one
JOIN two on one.matchingColumn = two.matchingColumn
If the data types are the same, then you can do a union
SELECT *
FROM table1
UNION
SELECT *
FROM table2
If the datatypes are not the same and you have a field that you can JOIN on, then you can do a JOIN
SELECT *
FROM table1 t1
LEFT JOIN table2 t2
ON t1.id = t2.id

Resources