Code Explanation - Please - sql

Sorry - I hope you don't mind me asking this question but I am new to SQL - Could someone comments on what these lines mean in this code:
select b.recipeuuid
,b.proditemuuid
,b.proditemvalueuuid
into #rectemp
from rec_recipe as a
inner join rec_recipevalue as b
on b.recipeuuid=a.recipeuuid
and b.[value] in ('Green','Yellow')
inner join rec_proditem as c
on c.proditemuuid=b.proditemuuid
inner join rec_proditemvalue as d
on d.proditemuuid=c.proditemuuid
and d.proditemvalueuuid=b.proditemvalueuuid
and d.[name]='SetupType'
;
update a
set a.[value]='1'
from rec_recipevalue as a
inner join #rectemp as b
on b.recipeuuid=a.recipeuuid
and b.proditemuuid=a.proditemuuid
and b.proditemvalueuuid=a.proditemvalueuuid
where a.[value] in ('Green','Yellow')
;
update a
set a.[name]='Normal'
from rec_proditemvalue as a
inner join #rectemp as b
on b.proditemuuid=a.proditemuuid
and b.proditemvalueuuid=a.proditemvalueuuid
where a.[name]='SetupType'
;
drop table #rectemp;
I understand that the general idea of this code. It is about how to update (or change) the value for two different columns: Attribute & Color. These two items are located in two different tables before I joined them with the appropriate UUID.
The update should take a place only when the Attribute = ‘SetupType’ and the Color = ‘Green’ or ‘Yellow’
I would like to change these two values to:
Attribute = ‘Normal’ and the
Color = ‘1’

select b.recipeuuid
,b.proditemuuid
,b.proditemvalueuuid
into #rectemp
from rec_recipe as a
inner join rec_recipevalue as b
on b.recipeuuid=a.recipeuuid
and b.[value] in ('Green','Yellow')
inner join rec_proditem as c
on c.proditemuuid=b.proditemuuid
inner join rec_proditemvalue as d
on d.proditemuuid=c.proditemuuid
and d.proditemvalueuuid=b.proditemvalueuuid
and d.[name]='SetupType'
;
***#rectemp is a temp table. Data imported into the table***
update a
set a.[value]='1'
from rec_recipevalue as a
inner join #rectemp as b
on b.recipeuuid=a.recipeuuid
and b.proditemuuid=a.proditemuuid
and b.proditemvalueuuid=a.proditemvalueuuid
where a.[value] in ('Green','Yellow')
;
***Column Value is updated to 1 based on Value is Green or yellow***
update a
set a.[name]='Normal'
from rec_proditemvalue as a
inner join #rectemp as b
on b.proditemuuid=a.proditemuuid
and b.proditemvalueuuid=a.proditemvalueuuid
where a.[name]='SetupType'
;
*** Name column is updated to Normal ***
drop table #rectemp;
*** Temp Table is dropped***

Related

Inner join multiple ID columns with ID in foreign table to display multiple name columns

I need to make a query that inner joins 3 different id's from one table with the id from another, to then display the name value from that table in my select query. I'll try to make it a bit more clear.
In my one table I have these 3 columns with id's:
Book_Kalender.BS_ID,
Book_Kalender.BS_ID_Prio2,
Book_Kalender.BS_ID_Prio3,
These all need to be inner joined with a column in another table, which contains the name associated with these ids:
Book_Sommerhuse.[BS_ID]
In my SELECT query I am including the name column from the foreign table. I want to instead have 3 columns, each with the associated name that corresponds to the ID.
Book_Sommerhuse.BS_Navn
So far I have tried to make multiple inner joins using the AND keyword:
INNER JOIN Book_Kalender ON Book_Sommerhuse.[BS_ID] = Book_Kalender.[BS_ID]
AND Book_Sommerhuse.[BS_ID] = Book_Kalender.[BS_ID_Prio2]
But this returns and empty view from my select query. I'm also not sure how to create new columns for each name associated with the ID.
Full query:
SELECT
Book_Kalender.BK_ID,
Book_Kalender.BK_DatoFra,
Book_Kalender.BK_DatoTil,
Book_Kalender.BK_M_Navn,
Book_Kalender.BK_M_Adr,
Book_Kalender.BK_M_PostBy,
Book_Kalender.BK_M_Afd,
Book_Kalender.BK_M_MedArbNr,
Book_Kalender.BK_M_Tlf,
Book_Kalender.BK_M_Email,
Book_Kalender.BK_Tidl_Lejet,
Book_Kalender.BK_Tidl_Lejet_Txt,
Book_Kalender.BS_ID,
Book_Kalender.BS_ID_Prio2,
Book_Kalender.BS_ID_Prio3,
A.BS_Navn as BS_Navn1,
B.BS_Navn as BS_Navn2,
c.BS_Navn as BS_Navn3,
coalesce(A.BS_Navn,B.BS_Navn,c.BS_Navn) as BS_Navn
FROM
Book_Kalender
LEFT JOIN Book_Sommerhuse A ON
Book_Kalender.BS_ID = A.BS_ID
LEFT JOIN Book_Sommerhuse B ON
Book_Kalender.BS_ID_Prio2 = B.BS_ID
LEFT JOIN Book_Sommerhuse C ON
Book_Kalender.BS_ID_Prio3 = C.BS_ID
WHERE
Book_Kalender.BK_DatoFra BETWEEN #10/15/2017# AND #12/31/2018#;
You need 3 left join :
select
Book_Kalender.*,
A.BS_Navn as BS_Navn1,
B.BS_Navn as BS_Navn2,
C.BS_Navn as BS_Navn3,
coalesce(A.BS_Navn,B.BS_Navn,c.BS_Navn) as BS_Navn -- first non null BS_Navn
from
Book_Kalender
LEFT JOIN Book_Sommerhuse A ON
Book_Kalender.BS_ID = A.BS_ID
LEFT JOIN Book_Sommerhuse B ON
Book_Kalender.BS_ID_Prio2 = B.BS_ID
LEFT JOIN Book_Sommerhuse C ON
Book_Kalender.BS_ID_Prio3 = C.BS_ID
SELECT
K.BS_ID
,S1.BS_Navn
,K.BS_ID_Prio2
,S2.BS_Navn 'Prio2_BS_Navn'
,K.BS_ID_Prio3
,S3.BS_Navn 'Prio3_BS_Navn'
FROM
Book_Kalender K
LEFT JOIN
Book_Sommerhuse S1 ON S1.BS_ID = K.BS_ID
LEFT JOIN
Book_Sommerhuse S2 ON S2.BS_ID = K.BS_ID_Prio2
LEFT JOIN
Book_Sommerhuse S3 ON S3.BS_ID = K.BS_ID_Prio3
Use could use derived table which could provide you three columns data as a single column and then you could apply the join , something like this
INNER JOIN (
SELECT BS_IDs FROM Book_Kalender CROSS APPLY(
VALUES (BS_ID), (BS_ID_Prio2), (BS_ID_Prio3)) Cols(BS_IDs)
) DerivedBook_Kalender ON Book_Sommerhuse.[BS_ID] = DerivedBook_Kalender.[BS_IDs]

Receiving SQL Object Already named 'x' in the DB, Even after dropping

IF OBJECT_ID('#TempTableHoldingRepairsList') IS NOT NULL
DROP TABLE #TempTableHoldingRepairsList
GO
SELECT
a.EnteredDate,
bb.EmployeeId,
bb.EmployeeName,
dd.EquipmentId,
dd.EquipmentName,
ee.PK_WorkOrder,
StatusName = 'NOT-SERVICEABLE'
INTO #TempTableHoldingRepairsList
FROM dbo.PIT_Inspection a
INNER JOIN dbo.PIT_EmployeeName bb
ON a.FK_EmployeeName = bb.PK_EmployeeName
INNER JOIN dbo.PIT_EquipmentName dd
ON a.FK_EquipmentName = dd.PK_EquipmentName
LEFT JOIN dbo.PIT_WorkOrder ee
ON ee.FK_EmployeeName = bb.PK_EmployeeName
WHERE a.FK_Status = 2
GROUP BY ee.PK_WorkOrder, a.EnteredDate, bb.EmployeeId, bb.EmployeeName, dd.EquipmentId, dd.EquipmentName
GO
--Now Count how many work orders for each.
SELECT COUNT(*) AS WorkOrderCount FROM PIT_WorkOrder pw
INNER JOIN #TempTableHoldingRepairsList th
ON th.PK_WorkOrder = pw.PK_WorkOrder
Not sure if I'm doing the final inner join wrong or missing something completely, but I continue to receive:
There is already an object named '#TempTableHoldingRepairsList' in the database.
You can't check for the existence of a temporary table directly they way you're doing it. You need to look in tempdb. You need to change the check to the following:
IF OBJECT_ID('tempdb..#TempTableHoldingRepairsList') IS NOT NULL
DROP TABLE #TempTableHoldingRepairsList
GO

Update table inner join with 3rd table

I have a Fact_Actuals yable with a new column "Segment_Id".
I need to insert the segment id from DimsegmentMaster into Fact_Actuals.
Link is with SegmentMaster and Source Table column.
Link is with FactTable is Measures of the Source Table column below.
Kindly provide me the update query, as my below query is not fine.
UPDATE Application_DB.[cdw].[Fact_Actuals]
set segment_sid =
(SELECT c.SID
FROM Application_DB.[cdw].[Fact_Actuals] b
inner join Source_DB.STA.SourceTable a
ON convert(decimal(20,10),LTRIM(RTRIM(a.[K308]))) = b.NetExternalSales
and convert(decimal(20,10),LTRIM(RTRIM(a.[K203]))) = b.Quantity_CON
and convert(decimal(20,10),LTRIM(RTRIM(a.[K202]))) = b.Quantity_KG
inner join Application_DB.cdw.DimSegmentMaster c
ON RTRIM(a.[C005])=c.SegmentOriginal
)
Try this:
UPDATE b
set segment_sid = c.sid
FROM Application_DB.[cdw].[Fact_Actuals] b
inner join Source_DB.STA.SourceTable a
ON
convert(decimal(20,10),LTRIM(RTRIM(a.[K308])))=b.NetExternalSales
and convert(decimal(20,10),LTRIM(RTRIM(a.[K203])))=b.Quantity_CON
and convert(decimal(20,10),LTRIM(RTRIM(a.[K202])))=b.Quantity_KG
inner join Application_DB.cdw.DimSegmentMaster c
ON RTRIM(a.[C005])=c.SegmentOriginal

SQL update statement inserting PeopleID into child tables to link data

I need to place the PeopleID in several tables for my new database to link all of the peole information. I have tried several times to write a simple update statement please help. Every time I get close I get AMBIGUOUS COLUMN ERROR I don't know what else to do.
Update CONTRACT
Set PeopleID = B.PeopleID
from People A
Inner join
(
Select PeopleId, F.ContractID
From People A
Inner Join Person PRSN on PRSN.PersonID = A.PersonID
Inner Join DARPA_IMPORT_REAL..persnl oldP on oldP.pl_pid = PRSN.PersonID
Left outer join Contract F on F.ContractID = oldP.kn_254id
) B on A.PeopleID = B.PeopleID
Go
try this
Update CONTRACT Set CONTRACT.PeopleID = B.PeopleID
from People A
Inner join (
Select A.PeopleId, F.ContractID
From People AA
Inner Join Person PRSN on PRSN.PersonID = AA.PersonID
Inner Join DARPA_IMPORT_REAL..persnl oldP on oldP.pl_pid = PRSN.PersonID
Left outer join Contract F on F.ContractID = oldP.kn_254id ) B
on A.PeopleID = B.PeopleID
you were asigning the ´A´ alias twice so I recomend using different aliases always

PostgreSQL delete with inner join

DELETE B.*
FROM m_productprice B
INNER JOIN m_product C ON B.m_product_id = C.m_product_id
WHERE C.upc = '7094' AND B.m_pricelist_version_id = '1000020'
i am getting the following error PostgreSQL 8.2.11
ERROR: syntax error at or near "B"
LINE 1: DELETE B.* from m_productprice B INNER JOIN m_product C ON ...
i tried giving
DELETE B from m_productprice B INNER JOIN m_product C ON B....
ERROR: syntax error at or near "B"
i tried giving
ERROR: syntax error at or near "INNER"
LINE 1: DELETE from m_productprice B INNER JOIN m_product C ON B.m_...
what is the problem with my query?
DELETE
FROM m_productprice B
USING m_product C
WHERE B.m_product_id = C.m_product_id AND
C.upc = '7094' AND
B.m_pricelist_version_id='1000020';
or
DELETE
FROM m_productprice
WHERE m_pricelist_version_id='1000020' AND
m_product_id IN (SELECT m_product_id
FROM m_product
WHERE upc = '7094');
This worked for me:
DELETE from m_productprice
WHERE m_pricelist_version_id='1000020'
AND m_product_id IN (SELECT m_product_id
FROM m_product
WHERE upc = '7094');
If you have more than one join you could use comma separated USING statements:
DELETE
FROM
AAA AS a
USING
BBB AS b,
CCC AS c
WHERE
a.id = b.id
AND a.id = c.id
AND a.uid = 12345
AND c.gid = 's434sd4'
Reference
Another form that works with Postgres 9.1+ is combining a Common Table Expression with the USING statement for the join.
WITH prod AS (select m_product_id, upc from m_product where upc='7094')
DELETE FROM m_productprice B
USING prod C
WHERE B.m_product_id = C.m_product_id
AND B.m_pricelist_version_id = '1000020';
Just use a subquery with INNER JOIN, LEFT JOIN or smth else:
DELETE FROM m_productprice
WHERE m_product_id IN
(
SELECT B.m_product_id
FROM m_productprice B
INNER JOIN m_product C
ON B.m_product_id = C.m_product_id
WHERE C.upc = '7094'
AND B.m_pricelist_version_id = '1000020'
)
to optimize the query,
use NOT EXISTS instead of IN
and WITH for large subqueries
Essentially everything mentioned here is mentioned in the docs, but no-one is specifying exactly what. So this is what the current (v15) DELETE docs says:
Notes
PostgreSQL lets you reference columns of other tables in the WHERE condition by specifying the other tables in the USING clause. For example, to delete all films produced by a given producer, one can do:
DELETE FROM films USING producers
WHERE producer_id = producers.id AND producers.name = 'foo';
What is essentially happening here is a join between films and producers, with all successfully joined films rows being marked for deletion. This syntax is not standard. A more standard way to do it is:
DELETE FROM films
WHERE producer_id IN (SELECT id FROM producers WHERE name = 'foo');
In some cases the join style is easier to write or faster to execute than the sub-select style.