Hey guys I hope you can help me I've tried every thing I can think of and it keeps telling me that my syntax near SELECT and that my syntax near AS is incorrect
CREATE PROCEDURE dbo.StoredProcedure2
SELECT
, Announcements.ID
, Announcement.CreateDate
, Announcements.Announcement
,aspnet_Users.UserName
,(SELECT Announcement_Read_State.Read_Date
FROM Announcement_Read_State
WHERE Announcement_Read_State.Announcement_ID = Announcements.ID
AND Announcement_Read_State.User_ID = 2) AS ReadState
FROM Announcements INNER JOIN aspnet_User ON Announcements .Sender_User_ID = aspnet_User.UserName
WHERE (Announcements.ID IN
( SELECT Max(Announcements.ID)
FROM Thread_Participant INNER JOIN Announcements ON
Thread_Participant.ThreadId = Announcements.Announcement_ThreadId
WHERE MessageThreadParticipant.UserID = 2
GROUP BY ThreadParticipant.AnnouncementThreadId
)
ORDER BY Message.CreateDate DESC;
It should be:
CREATE PROCEDURE dbo.StoredProcedure2
AS -- you were missing this
SELECT -- you had an extra comma here
Announcements.ID
, Announcement.CreateDate
, Announcements.Announcement
,aspnet_Users.UserName
,(SELECT Announcement_Read_State.Read_Date
FROM Announcement_Read_State
WHERE Announcement_Read_State.Announcement_ID = Announcements.ID
AND Announcement_Read_State.User_ID = 2) AS ReadState
FROM Announcements INNER JOIN aspnet_User ON Announcements .Sender_User_ID = aspnet_User.UserName
WHERE (Announcements.ID IN
( SELECT Max(Announcements.ID)
FROM Thread_Participant INNER JOIN Announcements ON
Thread_Participant.ThreadId = Announcements.Announcement_ThreadId
WHERE MessageThreadParticipant.UserID = 2
GROUP BY ThreadParticipant.AnnouncementThreadId
)
)-- you were missing this one
ORDER BY Message.CreateDate DESC;
Related
Technology: Teradata 16.20
I'm trying to update a table, that takes data from another table which uses CTE data.
I'm getting below error:
Error:
select failed 3707 syntax error, expected something like a 'SELECT' keyword or '(' or a 'TRANSACTIONTIME' keyword or a 'VALIDTIME' keyword between ')' and the 'UPDATE' keyword
Question:
Is it possible to update a table using another table that is getting joined with cte?
I know it can be done with a volatile table. I have seen CTE with insert statement and with select statement, but never seen cte with an update statement. Writing the same sql over and over for multiple self join only increases the lines of code. If this update can be done using CTE, that would be much easier and understandable
Code:
WITH NAME_CTE AS (
SELECT FIRST.ID,FIRST.NAM,LAST.NAME
FROM TABLE_FIRST FIRST INNER JOIN TABLE_LAST LAST
ON FIRST.ID = LAST.ID
)
UPDATE SUBJECT_TEACHER_TABLE
FROM (
SELECT CROSS_TBL.SUB_ID,
COALESCE(CTE1.NAM,CTE1.NAME,CTE2.NAM,CTE2.NAME) AS FINAL_NAME
FROM CROSS_REFERENCE_TABLE CROSS_TBL
LEFT JOIN NAME_CTE CTE1 ON CROSS_TBL.CR_ID1 = CTE.ID
LEFT JOIN NAME_CTE CTE2 ON CROSS_TBL.CR_ID2 = CTE.ID
) PV
SET
FINAL_NAME = PV.FINAL_NAME
WHERE SUBJECT_TEACHER_TABLE.SUB_ID = PV.SUB_ID;
Modified Query: As per #dnoeth suggesstion
UPDATE SUBJECT_TEACHER_TABLE
FROM (
WITH NAME_CTE AS (
SELECT FIRST.ID,FIRST.NAM,LAST.NAME
FROM TABLE_FIRST FIRST INNER JOIN TABLE_LAST LAST
ON FIRST.ID = LAST.ID)
SELECT CROSS_TBL.SUB_ID,
COALESCE(CTE1.NAM,CTE1.NAME,CTE2.NAM,CTE2.NAME) AS FINAL_NAME
FROM CROSS_REFERENCE_TABLE CROSS_TBL
LEFT JOIN NAME_CTE CTE1 ON CROSS_TBL.CR_ID1 = CTE.ID
LEFT JOIN NAME_CTE CTE2 ON CROSS_TBL.CR_ID2 = CTE.ID
) PV
SET
FINAL_NAME = PV.FINAL_NAME
WHERE SUBJECT_TEACHER_TABLE.SUB_ID = PV.SUB_ID;
Error:
SQL Error [3807] [42S02]: [Teradata Database] [TeraJDBC 15.10.00.22] [Error 3807] [SQLState 42S02] Object 'NAME_CTE' does not exist.
I have a requirement to load distinct costcenternum and a seq_key to be inserted. I wrote a procedure but this is failing one is distinct and even after removing distinct not able to run this procedure.
Please help me to correct this query to generate a seq key and also distinct cost numbers into the division table.
CREATE OR REPLACE
PROCEDURE POPULATE_DIVISION_DIM AS
BEGIN
INSERT INTO DIVISION(
"COST_CENTER_KEY"
,"COST_CENTER_NUM"
,"COST_CENTER_DESC"
,"DIVISION_CODE"
,"DIVISION_DESC"
,"COMPANY_CODE"
,"INSERT_DT"
,"UPDATE_DT"
)
(
SELECT
cc_sequence.nextval cost_center_key
, distinct (pcaf.segment4) costcenter_num
,ffvv.description costcenter_desc
,hoi.org_information9 division
,(SELECT description
FROM hr_lookups
WHERE lookup_type = 'CAT'
AND lookup_code = hoi.org_information9)
division_desc
, ppg.segment1 company
,TRUNC(SYSDATE) insert_dt
,TRUNC(SYSDATE) update_dt
FROM
hr_organization_information hoi
, hr_all_organization_units haou
, pay_cost_allocation_keyflex pcaf
, fnd_flex_values_vl ffvv
, per_all_assignments_f paaf
, pay_people_groups ppg
WHERE 1=1
AND paaf.people_group_id = ppg.people_group_id
AND haou.cost_allocation_keyflex_id =
pcaf.cost_allocation_keyflex_id(+)
AND pcaf.segment4 = ffvv.flex_value(+)
AND (ffvv.FLEX_VALUE_SET_ID is null or ffvv.FLEX_VALUE_SET_ID=
(SELECT FLEX_VALUE_SET_ID FROM FND_FLEX_VALUE_SETS WHERE
FLEX_VALUE_SET_NAME = 'ABCD'))
AND ffvv.enabled_flag(+) = 'Y'
AND haou.organization_id = hoi.organization_id
AND hoi.org_information_context = 'XX'
)
;
COMMIT;
END POPULATE_DIVISION_DIM;
I am currently working on a shopping list, where the user can have multiple lists and is able to add different items on the different tables. It is also possible to check those items. Everything works fine, but when I want to delete some of the checked items it throws me an error:
D/Sqflite (10847): [Thread[Sqflite,5,main]] DELETE FROM shoppingTitles
LEFT JOIN shopping ON shoppingTitles.idShopping = shopping.id LEFT
JOIN listTitles ON shoppingTitles.idTitles = listTitles.id WHERE
shopping.checked = 1 AND listTitles.titleName = ? [Einkaufsliste]
E/SQLiteLog(10847): (1) near "LEFT": syntax error
This is my sql query:
DELETE
FROM shoppingTitles
LEFT JOIN shopping ON shoppingTitles.idShopping = shopping.id
LEFT JOIN listTitles ON shoppingTitles.idTitles = listTitles.id
WHERE shopping.checked = 1
AND listTitles.titleName = "Liste"
I hope somebody is able to help me. Thanks in advance XD
FOUND MY SOLUTION BASED ON THE ANSWER provided by Akn
DELETE FROM shoppingTitles
WHERE idShopping IN (
SELECT shoppingTitles.idShopping
FROM shoppingTitles, shopping, listTitles
WHERE shopping.id = shoppingTitles.idShopping
AND shoppingTitles.idTitles = listTitles.id
AND shopping.checked = 1
AND listTitles.titleName = "Liste"
)
Try it like this:
DELETE S
FROM
shoppingTitles S
LEFT JOIN shopping ON S.idShopping = shopping.id
LEFT JOIN listTitles ON S.idTitles = listTitles.id
WHERE
shopping.checked = 1
AND listTitles.titleName = "Liste"
Update:
Guess, there is no support for JOINs within DELETE statements so you better find a way to use SELECT statement.
Below SELECT statement will return an id. If it matches with your myIdShopping, it'll be deleted. I couldn't check/run the code. Hope it works.
DELETE FROM
shoppingTitles
WHERE
myIdShopping IN(
SELECT
shoppingTitles.idShopping
FROM
shoppingTitles
LEFT JOIN shopping ON shoppingTitles.idShopping = shopping.id
LEFT JOIN listTitles ON shoppingTitles.idTitles = listTitles.id
WHERE
shopping.checked = 1
AND listTitles.titleName = "Liste"
)
In following query, what is happening is that, the 3rd join is not being done. we are getting pharmacy match and then the display is showing patients in other facilities who share the same pharmacy, can you see why this would be happening?
Insert Into #tblNDC
SELECT ROW_NUMBER() OVER(ORDER BY ID_KEY DESC) AS RN,*
From
(
Select distinct A.PHARMACYNPI,
f.FACILITY_NAME,
ID_KEY,
[BATCH] AS column1,
[IMPORTDATE],
[DATEBILLED],
[RX],
[DATEDISPENSED],
[DAYSUPPLY],
[PAYTYPE],
A.[NPI],
[PHYSICIAN],
[COST],
[QUANTITY],
[MEDICATION],
A.[NDC],
f.FACILITY_ID
FROM [PBM].[T_CHARGES] A
LEFT OUTER JOIN [OGEN].[NDC_M_FORMULARY] B ON A.[NDC] = B.[NDC]
--Left Outer Join PBM.FACILITY f on A.FACILITYNPI = f.FACILITY_NPI
Left Outer Join PBM.PHARMACY_NPI pn on A.PHARMACYNPI = pn.NPI
Inner join PBM.PHARMACY_FACILITY pp on pn.PHARMACY_ID = pp.PHARMACY_ID
Inner Join PBM.FACILITY f on pp.FACILITY_ID = f.FACILITY_ID
Where [STAT] not in (3, 4, 5)
AND [TIER] <> 'T1'
AND f.FACILITY_ID IN
(
select FacilityID from #tblFacility
)
AND f.FACILITY_ID IN
(
SELECT * FROM [PBM].[Split1] (#selectedFacility)
)
--- it seems 3rd condition not being done ----------------------------------
Its hard to see for sure without knowing the data, but my first thoughts are that the Left Outer joins will be giving you joinage you might not want.
Go through each join and remove it until you start to get dodgy records back, if it is the 3rd one when it suddenly goes strange, then i suspect you have multiple IDs for a facility.
I have 3 Data Table Claim, Part and Labor.
In this Claim is parent table and Part and Labor is mapping tables of Claim and they have Part and Labor has the ClaimId as a Foreign Key.
Claim has data like:
Part has data Like
Labor table has data Like
Target Output would be:
Can anyone help me to achieve this in SQL server.
I have tried to solve with the Union/CTE but it did not gives the result as I want.
I got the same output (for your updated output screen) for this specific case. I don't know if any other data will work for you.
SELECT TMP.ClaimId
, CASE WHEN TMP.RowNum = 1 THEN TMP.Name ELSE NULL END AS ClaimName
, CASE WHEN TMP.RowNum = 1 THEN TMP.Note ELSE NULL END AS Note
, TMP.PartId
, TMP.PartNumber
, TMP.PartCost
, JOIN_L.LaborId
, JOIN_L.LaborCost
FROM (
SELECT C.ClaimId, C.Name, C.Note, P.PartId, P.PartNumber, P.PartCost
, ROW_NUMBER() OVER(PARTITION BY C.ClaimId ORDER BY P.PartId) AS RowNum
FROM Claim AS C
LEFT JOIN Part AS P ON C.ClaimId = P.ClaimId
)AS TMP
LEFT JOIN (
SELECT *
, ROW_NUMBER() OVER(PARTITION BY L.ClaimId ORDER BY L.ClaimId) AS RowNum
FROM Labor AS L
) AS JOIN_L ON (TMP.ClaimId = JOIN_L.ClaimId AND TMP.RowNum = JOIN_L.RowNum)
ORDER BY TMP.ClaimId
Not sure why you tried CTE here
Select C.ClaimId,C.name,C.Note,P.PartId,P.PartNumber,P.PartCost,L.LabourId,L.LabourCost
From Claim C
Left Outer Join Part P On P.ClaimId = C.ClaimId
Left Outer Join Labor L On L.ClaimId=C.ClaimId