SQL Server Join with a table with composite key - sql-server

I have four tables I need to join in the following query attemp:
SELECT PROJECT.PROJECT_ID
, PROJECT_STATUS.PRO_STATUS_NAME
, PROJECT.PROJECT_NAME
, SKILL.SKILL_NAME
FROM PROJECT_STATUS
INNER JOIN PROJECT ON PROJECT_STATUS.PRO_STATUS_ID = PROJECT.PRO_STATUS_ID
INNER JOIN PRO_SKILL ON SKILL.SKILL_ID = PRO_SKILL.SKILL_ID
AND PRO_SKILL.PROJECT_ID = PROJECT.PROJECT_ID;
Unfortunately the PRO_SKILL table uses a composite key so I keep getting the following error when trying to run the query:
Msg 4104, Level 16, State 1, Line 6 The multi-part identifier
"SKILL.SKILL_ID" could not be bound. Msg 4104, Level 16, State 1, Line
1 The multi-part identifier "SKILL.SKILL_NAME" could not be bound.
Is there a way to join properly with a table that uses a composite key?
PRO_SKILL HAS A COMPOSITE KEY MADE OF:
PROJECT_ID AND SKILL_ID
SKILL ONLY HAS A REGULAR PK MADE OF SKILL_ID
Thank you for your help in advance!.

You missed out the Skills table , Try this....
SELECT PROJECT.PROJECT_ID
, PROJECT_STATUS.PRO_STATUS_NAME
, PROJECT.PROJECT_NAME
, SKILL.SKILL_NAME
FROM PROJECT_STATUS
INNER JOIN PROJECT ON PROJECT_STATUS.PRO_STATUS_ID = PROJECT.PRO_STATUS_ID
INNER JOIN PRO_SKILL ON PRO_SKILL.PROJECT_ID = PROJECT.PROJECT_ID
INNER JOIN SKILL ON SKILL.SKILL_ID = PRO_SKILL.SKILL_ID

Related

Trying to CREATE Materialized View in SQL Server

I am trying to create materilized view using below syntax:
CREATE MATERIALIZED VIEW empdept AS
SELECT e.Name,
e.Salary,
d.id,
d.name
FROM Employee e
INNER JOIN departments d ON e.Dept_id=d.id;
Msg 343, Level 15, State 1, Line 1
Unknown object type 'MATERIALIZEDVIEW' used in a CREATE, DROP, or ALTER statement.
Any idea where am I going wrong?

Accees table attribute in Snowflake CTE

use database DQ_MART;
use schema WORKING;
WITH ASCENDER_EMPLOYEE AS (
**SELECT DISTINCT EMPLOYEE_ID FROM RECONCILLIATION_ASCENDER_WORKER_TIMESHEET**
),
WORKDAY_EMPLOYEE AS (
**SELECT DISTINCT EMPLOYEE_ID FROM RECONCILLIATION_WORKDAY_WORKER_TIMESHEET**
)
SELECT 'Missing employee in Ascender' DQ_RULE_NAME,
RECONCILLIATION_WORKDAY_WORKER_TIMESHEET.EMPLOYEE_ID KEY
FROM WORKDAY_EMPLOYEE WORKDAY
LEFT OUTER JOIN ASCENDER_EMPLOYEE ASCENDER
ON ASCENDER.EMPLOYEE_ID = WORKDAY.EMPLOYEE_ID
;
Hi All, I am bit new to Snowflake SQL CTE. In the above query, I am getting an error, Error: invalid identifier 'RECONCILLIATION_WORKDAY_WORKER_TIMESHEET.EMPLOYEE_ID' (line 16) in this line
RECONCILLIATION_WORKDAY_WORKER_TIMESHEET.EMPLOYEE_ID
The select statements where the same table is accessed runs properly.
Te database and schema where the table resides is set correctly and I do have SELECT grant on the tables.
Is there a scope visibility in Snowflake which is causing the error to occur. Any suggestions will be welcome.
"RECONCILLIATION_WORKDAY_WORKER_TIMESHEET.EMPLOYEE_ID" means the column "EMPLOYEE_ID" in the table "RECONCILLIATION_WORKDAY_WORKER_TIMESHEET".
Your select statement that uses this column is:
SELECT <column list>
FROM WORKDAY_EMPLOYEE WORKDAY
LEFT OUTER JOIN ASCENDER_EMPLOYEE ASCENDER
ON ASCENDER.EMPLOYEE_ID = WORKDAY.EMPLOYEE_ID
which doesn't include a table called RECONCILLIATION_WORKDAY_WORKER_TIMESHEET - which is why you are getting the error

Insert from One Table Into Another with Where clause

I'm trying to insert values from table 2 (in the pacp2 column) into table 1 (in the pacp column) where the facility_id from both tables matches and where table1 pacp column is null. I get the following error:
Msg 4104, Level 16, State 1, Line 5 The multi-part identifier
"table1.FACILITYID" could not be bound. Msg 4104, Level 16, State 1,
Line 5 The multi-part identifier "table1.pacp" could not be bound.
Does anyone know why this isn't working? I've tried using alias for both tables but that won't work.
INSERT INTO database.dbo.table1 (pacp)
SELECT pacp2
FROM table2
WHERE database.dbo.table1.facility_id = database.dbo.table2.facility_id
AND database.dbo.table1.pacp IS NULL
The description of your question suggests that you are looking for an update rather than an insert.
Here is one option using an updatable cte:
with cte as (
select t1.pacp, t2.pacp2
from table1 t1
inner join table2 t2 on t1.facility_id = t2.facility_id
where t1.pacp is null
)
update cte set pacp = pacp2
For rows of table1 where pacp is null, the query attempts to retrieve the value of pacp2 from table2, using facility_id for matching. If it succeeds, then it updates the corresponding row in table1 and sets pacp to the value of pacp2.

SQL Server 2016 virtual tables - Incorrect syntax 'AS'

Assuming a table level with columns level_id and level_name
I have a virtual table in my SQL Server 2016 query:
This is not a real query - I've simplified it as much as possible to illustrate the error
SELECT
LEVEL .level_id,
LEVEL .level_name
FROM
LEVEL,
((SELECT
LEVEL_ID AS lev_sum_level_id
FROM
LEVEL
GROUP BY
level_id) AS lev_sum
JOIN
(SELECT
LEVEL_ID AS lev_det_level_id
FROM
LEVEL
GROUP BY
level_id) AS lev_det ON (lev_sum_level_id = lev_det_level_id)
) AS totals
The syntax error is on the line AS totals.
Msg 156, Level 15, State 1, Line 35
Incorrect syntax near the keyword 'AS'
Why is SQL Server not allowing this syntax? Seems fine if I simplify the virtual table query. Postgres allows it as-is
I think you can try to add a select so that you can complete the syntax of the statement
Please try
SELECT
T1.MiscID,
T2.lev_sum_level_id
FROM
Miscs AS T1,
(
SELECT * FROM
(
SELECT
MiscID AS lev_sum_level_id
FROM
Miscs
GROUP BY
MiscID
) AS lev_sum
JOIN
(
SELECT
MiscID AS lev_det_level_id
FROM
Miscs
GROUP BY
MiscID
) AS lev_det
ON( lev_sum_level_id = lev_det_level_id )
) AS T2

Updating inserted row with table trigger

I can't quite figure out how to update the last inserted row using table triggers.
I tried this:
create TRIGGER [dbo].[t_checkAucEquFields]<br />
ON [dbo].[_aucEquUpdateLog]
AFTER INSERT
as
update a set inserted.[Status] = coalesce(pes.id,'22')
from [_aucEquUpdateLog] a
left join v_pobEquStatus pes on pes.statusDescr = inserted.[Status]
I would like the text of inserted.Status - ex being "In stock" to be replaced with the integer pes.id. If the join cannot be made, I would like it to default to value 22.
I get this error when trying above query:
Msg 4104, Level 16, State 1, Procedure t_checkAucEquFields, Line 7
The multi-part identifier "inserted.Status" could not be bound.
I might be way off, but I am quite lost as of how to do it in a proper way?
You need to join the inserted table, and update the actual table instead of the inserted table
Try this:
CREATE TRIGGER [dbo].[t_checkAucEquFields]
ON [dbo].[_aucEquUpdateLog]
AFTER INSERT
as
begin
UPDATE a
SET
[Status] = coalesce(pes.id,'22')
FROM
[_aucEquUpdateLog] a
JOIN
inserted i
ON
i.[your primary key] = a.[your primary key]
LEFT JOIN
v_pobEquStatus pes
ON
pes.statusDescr = i.[Status]
end

Resources