From two Excel sheets to one DB table - sql-server

I have the query to solve out the following problem...
Excel Sheet1:
Empid EmpName Des
001 Samar eng
002 kalyan eng
Excel Sheet2:
Empid EmpName AccNo Emp.MobNo Emp.Address Empwork
001 Samar 1001 44545455646 ctc tttt
002 kalyan 1002 65464656654 bbs ppp
003 barak 1003 54654564564 polp ppp
Final DB Table:
Empid EmpName Des AccNo Emp.MobNo Emp.Address Empwork
001 Samar eng 1001 44545455646 ctc tttt
002 kalyan eng 1002 65464656654 bbs ppp

After you've imported your Excel sheets in a SQL Server instance, you can use the INNER JOIN syntax to select only rows that are present in both tables.
SELECT s1.Empid
, s1.EmpName
, s1.Des
, s2.AccNo
, s2.[Emp.MobNo]
, s2.[Emp.Address]
, s2.[Empwork]
FROM sheet1 s1
INNER JOIN sheet2 s2 ON s2.Empid = s1.Empid

If you want to make a select from Sheet1 and Sheet2 that gives final table, it would be like this:
INSERT INTO finalDBtable
Select s1.*, s2.AccNo, s2.[Emp.MobNo] , s2.[Emp.Address], s2.Empwork
from Sheet1 s1
inner join Sheet2 s2 on s1.Empid = s2.Empid
So, first put Sheet1 and Sheet2 in database tables and then make the select previous select to insert in the final table.

Related

SQL Server select join detect if common column between two tables are different

I am trying to write a function to check between two tables which have a common column with the same name and ID values.
Table 1: CompanyRecords
CompanyRecordsID CompanyId CompanyName CompanyProcessID
-----------------------------------------------------------
1 222 Sears 123
2 333 JCPenny 456
Table 2: JointCompanies
JointCompaniesID CompanyId CompanyName ComanyProcessID
-----------------------------------------------------------
3 222 KMart 123
4 444 Walmart 001
They both use the same foreign key CompanyProcessID with value 123.
How do I write a select statement when it is passed the CompanyProcessID to tell if the CompanyId has changed for the same CompanyProcessId.
I assume it is a join between the two tables with WHERE CompanyProcessID
Thanks for any help.
Is this what you want?
select max(case when cr.name = jc.name then 0 else 1 end) as name_not_same
from CompanyRecords cr join
JointCompanies jc
on cr.ComanyProcessID = jc.ComanyProcessID
where cr.ComanyProcessID = ?

Insert data into a SQL Server table

I have data like this
MAPPING table1:
ID1 NAME1
-----------
001 1
002 2
DATA table:
TID_1 TNAME_1
------------------
1 ABCNAME
2 DEFNAME
Another MAPPING table2:
ID2 NAME2
--------------
001 1
002 2
DATA table:
TC_ID2 TC_NAME2
-----------------
1 C_NAME
2 D_NAME
Here mapping tables are in database2 and data table are in databaes1
now I have another dimension_location table in database2 like this
ID1 NAME1 ID2 NAME2
-------------------------
I want to insert into this table like this
ID1 NAME1 ID2 NAME2
--------------------------------
001 ABCNAME 001001 C_NAME
002 DEFNAME 002002 D_NAME
Means I want to select name from database1 data table and for id I want to select from mapping table here in "ID2" column 001001 because 001 is for ID1 and another 001 is for ID2 so i combine those id and insert in id2 column how can I do this?
insert into Dimension_location(ID1,Name1,ID2, Name2)
select dm.ID1,dv.TNAME_1,dmp.ID2,ds.TC_NAME2from
ba.dbo.Mappingtable1 dm ,
ba.dbo.Mappingtable2 dmp
inner join Cen.dbo.datatable1 dv on dm.ID1=dv.TID_1
inner join Cen.dbo.datatable2 ds on ds.ID2=dmp.TC_ID2
now how i combine id1 code with id2 code?
Any solution?
You need to use Fully Qualified Names in order to write your query. For example:
database_name.schema_name.object_name.column_name
It will be something like this:
SELECT *
FROM [database1].[dbo].[MAPPING TABLE] A
INNER JOIN [database2].[dbo].[DATA TABLE1] B
ON A.[] = B.[]
...

how to update foreign key

I have 2 tables; design of first table is like below:
Table 1:
id_Doc_line_sheet (pk),Autonumber
DocNo (text)
lineNo (text)
Sheet No (text)
Combination of fields (DocNo, lineNo, Sheet No) is index and unique.
Design of second table is like below:
Table 2:
id_Doc_line_trans (pk), Autonumber
id_Doc_line_sheet (fk),Number
name
Now in table1, for the field lineNo I have records with leading spaces and without leading spaces like below:
id_Doc_line_sheet DocNo lineNo Sheet No
------------------------------------------------------------------
1001 doc-0001 line-0001 1
1002 doc-0001 line-0001 1
1003 doc-0001 line-0001 2
1004 doc-0001 line-0001 2
1005 doc-0002 line-0002 1
1006 doc-0002 line-0002 1
1007 doc-0001 line-0005 1
1008 doc-0001 line-0005 1
And I want deleted these records with leading spaces but at first I want to update (id_Doc_line_sheet) for each unique (DocNo,lineNo,Sheetno) to correct one (without leading space).
I mean if the table2 is like below:
id_Doc_line_trans id_Doc_line_sheet name
---------------------------------------------------
1 1001 name01
2 1002 name02
3 1003 name03
4 1004 name04
5 1007 name07
6 1008 name08
I update (id_Doc_line_sheet) to something like below:
id_Doc_line_trans id_Doc_line_sheet name
---------------------------------------------------
1 1001 name01
2 1001 name02
3 1003 name03
4 1003 name04
5 1007 name07
6 1007 name08
And then delete records with leading spaces from first table.
Please help how I can do it?
Based on what I understood, it seems you want to update the table2 based on the sheet_no first id used. If so, you can use the following:
Explanation
You need to get the first record in table1 based on the sheet_no using report cte
Find the corresponding sheet_no for each row in table2
Update the table2 entries where row_number =1
Query
;with report as(
select row_number() over(partition by sheet_no order by id_doc_line_sheet) as [Row],id_doc_line_sheet,sheet_no
from table1
where line_no not like ' %' -- here you can ensure that lin_no doesn't start with leading space
), combined as(
select t2.id_doc_line_trans,
t2.id_doc_line_sheet,
t1.sheet_no
from table2 t2
inner join table1 t1 on t2.id_doc_line_sheet = t1.id_doc_line_sheet
)
update t set t.id_doc_line_sheet = r.id_doc_line_sheet
from report r
inner join combined c on r.sheet_no = c.sheet_no
inner join table2 t on t.id_doc_line_trans = c.id_doc_line_trans
where r.[Row]=1
result after updating table2
id_doc_line_trans id_doc_line_sheet name
1 1001 name01
2 1001 name02
3 1003 name03
4 1003 name04
Then you can apply the delete statement based on your requirements
Here a working demo
Hope this will help you
Here is an approach which does not use analytic functions, which was the easiest way to approach this in my mind. We can aggregate over table1 and pivot out both the id of the records both with and without leading whitespace. Also, we can check to make sure a given document/sheet even has such a page occurring. Then, all we need to do is join table2 to this first CTE to get the old and new id values in a single record.
WITH cte1 AS (
SELECT
DocNo, [Sheet No],
MAX(CASE WHEN [lineNo] LIKE ' %' THEN id_Doc_line_sheet END) AS id_old,
MAX(CASE WHEN [lineNo] NOT LIKE ' %' THEN id_Doc_line_sheet END) AS id_new
FROM table1
GROUP BY DocNo, [Sheet No]
HAVING SUM(CASE WHEN [lineNo] LIKE ' %' THEN 1 ELSE 0 END) > 0
),
cte2 AS (
SELECT
t1.id_Doc_line_trans, t1.id_Doc_line_sheet, t1.name, t2.id_old, t2.id_new
FROM table2 t1
INNER JOIN cte1 t2
ON t1.id_Doc_line_sheet = t2.id_old
)
UPDATE cte2
SET id_Doc_line_sheet = id_new;
Note that the update logic is trivial; all the information and records of interest were already included in the second CTE.
Demo

SQL Server query involving subqueries - performance issues

I have three tables:
Table 1: | dbo.pc_a21a22 |
batchNbr Other columns...
-------- ----------------
12345
12346
12347
Table 2: | dbo.outcome |
passageId record
---------- ---------
00003 200
00003 9
00004 7
Table 3: | dbo.passage |
passageId passageTime batchNbr
---------- ------------- ---------
00001 2015.01.01 12345
00002 2016.01.01 12345
00003 2017.01.01 12345
00004 2018.01.01 12346
What I want to do: for each batchNbr in Table 1 get first its latest passageTime and the corresponding passageID from Table 3. With that passageID, get the relevant rows in Table 2 and establish whether any of these rows contains the record 200. Per passageId there are at most 2 records in Table 2
What is the most efficient way to do this?
I have already created a query that works, but it's awfully slow and thus unfit for tables with millions of rows. Any suggestion on how to either change the query or do it another way? Altering the table structure is not an option, I only have read rights to the database.
My current solution (slow):
SELECT TOP 50000
a.batchNbr,
CAST ( CASE WHEN 200 in (SELECT TOP 2 record FROM dbo.outcome where passageId in (
SELECT SubqueryResults.passageId From (SELECT Top 1 passageId FROM dbo.passage pass WHERE pass.batchNbr = a.batchNbr ORDER BY passageTime Desc) SubqueryResults
)
) then 1 else 0 end as bit) as KGT_IO_END
FROM dbo.pc_a21a22 a
The desired output is:
batchNbr 200present
--------- ----------
12345 1
12346 0
I suggest you use table joining rather than subqueries.
select
a.*, b.*
from
dbo.table1 a
join
dbo.table2 b on a.id = b.id
where
/*your where clause for filtering*/
EDIT:
You could use this as a reference Join vs. sub-query
Try this
SELECT TOP 50000 a.*, (CASE WHEN b.record = 200 THEN 1 ELSE 0 END) AS
KGT_IO_END
FROM dbo.Test1 AS a
LEFT OUTER JOIN
(SELECT record, p.batchNbr
FROM dbo.Test2 AS o
LEFT OUTER JOIN (SELECT MAX(passageId) AS passageId, batchNbr FROM
dbo.Test3 GROUP BY batchNbr) AS p ON o.passageId = p.passageId
) AS b ON a.batchNbr = b.batchNbr;
The MAX subquery is to get the latest passageId by batchNbr.
However, your example won't get the record 200, since the passageId of the record with 200 is 00001, while the latest passageId of the batchNbr 12345 is 00003.
I used LEFT OUTER JOIN since the passageId from Table2 no longer match any of the latest passageId from Table3. The resulting subquery would have no records to join to Table1. Therefore INNER JOIN would not show any records from your example data.
Output from your example data:
batchNbr KGT_IO_END
12345 0
12346 0
12347 0
Output if we change the passageId of record 200 to 00003 (the latest for 12345)
batchNbr KGT_IO_END
12345 1
12346 0
12347 0

Getting sub products from same table

I have two tables, table1 has end products and product parts, table2 has a mapping between table1.productid and partsid. i.e.
table1
--------------------------------------------
productid descrip code cost ....etc
1235 product A 07 12.5 ......
789 labor 03 2.5 ....
839 part1 03 5 ....
and table2 looks like
table2
--------------------------------------------
productid partsID quantity
1235 789 1
1235 839 2
2341 2315 2
.....
I need to select the end product that meets specific code and go pull up the parts from the part table and display like below:
Resuls
--------------------------------------------
productid descrip code partsID cost ....etc
1235 product A 07 789 2.5 ......
1235 product A 03 839 5 ....
.......
Basically for each end product, line up the parts to the right and cost and all other details associated with the parts and not the end product.
Appreciate any help with this.
Could you not join the Two tables across ProductID field:
SELECT * FROM
dbo.TABLE1 o INNER JOIN
dbo.TABLE2 t ON o.ProductId = t.ProductId
WHERE o.code = 7
OR o.code = 3
etc ... whatever additional criteria you need
If you also need to Join the partsID you could say
AND o.PartId = t.partsId
It looks like you want to get something like this query, but I'm not sure about the code=07, you've specified in the example. Probably the code must be 03 for both parts.
SELECT
prd.productid,
prd.descrip,
prt.code,
prdprt.partsID,
prt.cost,
....etc
FROM table1 prd
JOIN table2 prdprt ON prd.productid=prdprt.productid
JOIN table1 prt ON prdprt.partsID=prt.productid
WHERE prd.code in ('07', ...othecodes) --if you filter by product's code
OR prt.code in ('03', ...othecodes) --if you filter by parts's code

Resources