SQL Left Join Syntax Error - sql

Can someone please help resolve an issue, I am left joining two CTE's?
Msg 102, Level 15, State 1, Line 19
Incorrect syntax near '('.
Msg 156, Level 15, State 1, Line 26
Incorrect syntax near the keyword 'left'.
Query:
with Cte_LatestPatInfo as
(select max(a.LogID) as LastPatRec, p.unitnum, p.PatNum, a.iplan from rrscsql2.arhighdollar.dbo.tblarinfoHistory a
inner join rrscsql2.arhighdollar.dbo.tblPatInfoHistory p
on a.patID=p.patID and a.LogID=p.LogID
where a.Active = 1
and a.ReasonCode is not null
group by p.unitnum, p.PatNum, a.iplan)
select *,rc.Description from rrscsql2.arhighdollar.dbo.tblPatInfoHistory p
inner join cte_LatestPatInfo li
on p.PatNum=li.PatNum and p.UnitNum=li.UnitNum and p.LogID = li.LastPatRec
inner join rrscsql2.arhighdollar.dbo.tblarinfoHistory ah
on ah.LogID=p.LogID and ah.iplan = li.iplan and p.PatID=ah.PatID
left join rrscsql2.ARHighDollar.dbo.tblReasonCodes rc
on ah.ReasonCode=rc.ReasonCode,
cte_EOMDenials as
(select d.* from rrscsql2.Denials.dbo.tblDenialMonthEnd d
Inner join rrscsql3.Facilities.dbo.vwFacilities f
on d.UnitNum = f.UnitNum and f.Owner like '%LifePoint%'
Inner join rrscsql2.Denials.dbo.tblDispositionDictionary t
on d.disposition=t.disposition and DispositionType like'O%'
Where datediff(mm, monthending, GETDATE()) = 1
and DATEDIFF(mm,DischDate,monthending)>2)
left join cte_EOMDenials d
on p.unitnum=d.unitnum and p.patnum = d.patnum and p.insplan=d.iplan

You have to introduce all of your CTEs first via a single WITH, and then you can use them in a final query:
with Cte_LatestPatInfo as
(select max(a.LogID) as LastPatRec, p.unitnum, p.PatNum, a.iplan from
rrscsql2.arhighdollar.dbo.tblarinfoHistory a
inner join rrscsql2.arhighdollar.dbo.tblPatInfoHistory p
on a.patID=p.patID and a.LogID=p.LogID
where a.Active = 1
and a.ReasonCode is not null
group by p.unitnum, p.PatNum, a.iplan),
cte_EOMDenials as
(select d.* from rrscsql2.Denials.dbo.tblDenialMonthEnd d
Inner join rrscsql3.Facilities.dbo.vwFacilities f
on d.UnitNum = f.UnitNum and f.Owner like '%LifePoint%'
Inner join rrscsql2.Denials.dbo.tblDispositionDictionary t
on d.disposition=t.disposition and DispositionType like'O%'
Where datediff(mm, monthending, GETDATE()) = 1
and DATEDIFF(mm,DischDate,monthending)>2)
select *,rc.Description from rrscsql2.arhighdollar.dbo.tblPatInfoHistory p
inner join cte_LatestPatInfo li
on p.PatNum=li.PatNum and p.UnitNum=li.UnitNum and p.LogID = li.LastPatRec
inner join rrscsql2.arhighdollar.dbo.tblarinfoHistory ah
on ah.LogID=p.LogID and ah.iplan = li.iplan and p.PatID=ah.PatID
left join rrscsql2.ARHighDollar.dbo.tblReasonCodes rc
on ah.ReasonCode=rc.ReasonCode
left join cte_EOMDenials d
on p.unitnum=d.unitnum and p.patnum = d.patnum and p.insplan=d.iplan

The statement does not seem to join two CTEs. If you want to join two ctes you can achieve it like the following example
;WITH CTE_1
as
( select field1, field2... from sometable),
CTE_2
as
(select field1, field2... from sometable),
select a.field1, a.field2, b.field1, b.field2
from CTE_1 a left join CTE_2 b on a.field1 = b.field1;
You will notice that the two ctes were defined first, separated by comma(,) then use in the select statement.
Please see http://msdn.microsoft.com/en-us/library/ms175972(v=sql.90).aspx for reference.

Related

Duplicate data from select statement

I want to make a stress test to a procedure than generate a .csv file.
The problem is that i have not enough data, so i want to duplicate data in my sql select .
The query look like this:
SELECT P.FST_NAME,
P.LAST_NAME,
P.EMAIL_ADDR,
P.PERSON_UID,
PR.FST_NAME PRSP_FST_NAME,
PR.LAST_NAME PRSP_LAST_NAME,
M.X_BAPRO_DT_01,
M.X_BAPRO_DT_02,
M.X_BAPRO_DT_03,
M.X_BAPRO_MONTO,
M.X_BAPRO_NUM_01,
M.X_BAPRO_NUM_02,
M.X_BAPRO_NUM_03,
M.X_BAPRO_TEXT_01,
M.X_BAPRO_TEXT_02,
M.X_BAPRO_TEXT_03,
M.X_BAPRO_TEXT_04,
M.X_BAPRO_TEXT_05
FROM SIEBEL.S_SRC C
left join SIEBEL.S_CAMP_CON M on C.ROW_ID = M.SRC_ID
left join SIEBEL.S_DMND_CRTN_PRG T on T.ROW_ID = M.DCP_ID
left join SIEBEL.S_CONTACT P on P.ROW_ID = M.CON_PER_ID
left join SIEBEL.S_PRSP_CONTACT PR on PR.ROW_ID= M.PRSP_CON_PER_ID
WHERE
C.ROW_ID <> p_row_id
So, This query return about 100 records, i want to retrive 1000 records and i dont really care if the data is duplicated.
You can add a cross join:
FROM SIEBEL.S_SRC C
left join SIEBEL.S_CAMP_CON M on C.ROW_ID = M.SRC_ID
left join SIEBEL.S_DMND_CRTN_PRG T on T.ROW_ID = M.DCP_ID
left join SIEBEL.S_CONTACT P on P.ROW_ID = M.CON_PER_ID
left join SIEBEL.S_PRSP_CONTACT PR on PR.ROW_ID= M.PRSP_CON_PER_ID
cross join (select 1 as n from dual union all
select 2 from dual
. . .
) x
You can also use the VALUE clause to construct the little "muliplier"-table as shown below:
SELECT ...
FROM SIEBEL.S_SRC C
left join SIEBEL.S_CAMP_CON M on C.ROW_ID = M.SRC_ID
left join SIEBEL.S_DMND_CRTN_PRG T on T.ROW_ID = M.DCP_ID
left join SIEBEL.S_CONTACT P on P.ROW_ID = M.CON_PER_ID
left join SIEBEL.S_PRSP_CONTACT PR on PR.ROW_ID= M.PRSP_CON_PER_ID
cross join (values (1),(2),(3),(4),(5),(6),(7),(8),(9),(10)) tabl(n)

How to get distinct id in subquery from fact table

Trying to return all the fields in my sub query minus the duplicated childid's in the fact table cor.score. I don't want the duplicate id's to inflate my count. Every id needs to be counted once.
select distinct cs.childid
from
(select s.sitename, c.primarylanguage, count(Primarylanguage) as 'Count'
from cor.scores cs
left join cor.sites s on s.id = cs.siteid
left join cor.children c on c.id = cs.childid
group by s.sitename, c.primarylanguage)
Error:
Msg 102, Level 15, State 1, Line 7 Incorrect syntax near ')'.
What's the best way to go about this?
select distinct cs_childid
from
(
select s.sitename,cs.childid as cs_childid, c.primarylanguage, count(Primarylanguage) as 'Count'
from cor.scores cs
left join cor.sites s on s.id = cs.siteid
left join cor.children c on c.id = cs.childid
group by s.sitename, c.primarylanguage,cs.childid
)as T
Do you want this:
1. can return any column of subquery
select s.childid,s.[Count]
FROM (
SELECT s.sitename, c.primarylanguage,cs.childid, count(Primarylanguage)OVER(PARTITION BY s.sitename) as 'Count'
FROM cor.scores cs
LEFT join cor.sites s on s.id = cs.siteid
) AS s LEFT join cor.children c on c.id = s.childid
Or this:
2. Only can return group column and aggregate columns
select s.childid,s.[Count]
FROM (
SELECT s.sitename,cs.childid, count(Primarylanguage)OVER(PARTITION BY s.sitename) as 'Count'
FROM cor.scores cs
LEFT join cor.sites s on s.id = cs.siteid
GROUP BY s.sitename,cs.childid
) AS s LEFT join cor.children c on c.id = s.childid

Update with CTE does not recognize column names in the set clause

I need to update an existing table with data from another. My CTE is giving me correct result, but when I'm trying to update with the CTE SSMS complains on
Msg 102, Level 15, State 1, Line 13
Incorrect syntax near '.'.
or Invalid column names at the lines below:
set cm.Action.Identifier_fk = ID
set cm.ActionRequestedAction = Action
set cm.Action.apartment_fk = apartment_fk
This is the code:
Use DB;
GO
with CTE (ID,Action,Identifier_fk,apartment_fk) AS
(select a.ID, a.Action, b.Identifier_fk, m.apartment_fk
from Project.AllSent a (nolock)
left outer join cm.Action b (nolock) on a.ID=b.Identifier_fk
left Outer Join csv.Matching m (nolock) on m.Identifier_fk = a.ID
left outer join csv.Apartment p (nolock) on m.apartment_fk=p.apartment_pk
where b.Identifier_fk is NULL)
update cm.Action
set cm.Action.Identifier_fk = ID
set cm.Action.RequestedAction = Action
set cm.Action.apartment_fk = apartment_fk
from CTE c
JOIN Project.AllSent t (nolock) on t.ID=c.ID;
The correct update statement has only one set. Also, you have no cm in the from clause. I'm going to propose getting rid of the CTE:
update b
set Identifier_fk = a.ID,
Action.RequestedAction = a.Action,
apartment_fk = mm.apartment_fk
from Project.AllSent a (nolock) left join
cm.Action b (nolock)
on a.ID = b.Identifier_fk left join
csv.Matching m (nolock)
on m.Identifier_fk = a.ID left join
csv.Apartment p (nolock)
on m.apartment_fk = p.apartment_pk
where b.Identifier_fk is NULL;
I don't think the final join is necessary.

ORA-00907: missing right parenthesis 00907. 00000 - "missing right parenthesis" *Cause: *Action: Error at Line: 44 Column: 30

I am executing the following query:
SELECT *
FROM (SELECT *
FROM (SELECT DISTINCT r.llobjid AS dataid,
r.drawingid,
r.revisionid,
r.revisionnumber AS Revision_Number,
r.revisionlabel,
r.minorrevisionlabel,
r.revisiontype,
p.project AS Project,
r.revisionstatus,
r.r1i AS SignIn_Requestor,
r.r2i AS SeedFileVersion,
rt.display_type_name AS Revision_Type,
rt.can_signin,
rs.display_status_name AS Revision_Status,
a.adntypeid,
at.NAME AS ADN_Type,
a.requestby,
a.assignbyAS Assign_By_Id,
' ' AS Assign_By,
a.assigndate,
' ' AS Assign_Date
FROM adnids a
INNER JOIN crt_revision r
ON r.drawingid = a.adnid
AND Upper(a.wholeid) LIKE '4160%'
INNER JOIN adntypes at
ON a.adntypeid = at.adntypeid
AND at.orgunitid = 21
INNER JOIN crt_project p
ON r.projectid = p.projectid
AND p.ouid = 21
LEFT OUTER JOIN crt_revision_type_map rt
ON r.revisiontype = rt.revtypeid
AND rt.ouid = 21
LEFT OUTER JOIN crt_revision_status_map rs
ON r.revisionstatus = rs.revstatusid
AND rs.ouid = 21
WHERE ( r.revisionstatus = 2 )) tbl1
INNER JOIN (SELECT DISTINCT d.dataid AS LLDataID,
Cast(d.dcomment AS NVARCHAR(4000)) AS
Title,
d.NAME AS Document_Name,
d.createdate AS Created_Date,
d.modifydate AS Modified_Date,
d.subtype,
d.versionnum,
d.permid,
d.reserved,
d.ownerid
FROM crt_revision r3
INNER JOIN adnids a3
ON r3.drawingid = a3.adnid
AND Upper(a3.wholeid) LIKE '4160%'
INNER JOIN dtree d
ON r3.llobjid = d.dataid
WHERE ( r3.revisionstatus = 2 )) tbl2
ON tbl1.dataid = tbl2.lldataid
LEFT OUTER JOIN (SELECT DISTINCT l.dataid AS asm_dataid,
l.stateid,
l.intransition,
ls.NAME AS Current_State,
ls.signin,
ls.lifecycleid,
ll.NAME AS Lifecycle
FROM crt_revision r5
INNER JOIN adnids a5
ON r5.drawingid = a5.adnid
AND Upper(a5.wholeid) LIKE '4160%'
INNER JOIN lm_lifecycles l
ON l.dataid = r5.llobjid
INNER JOIN lm_def_states ls
ON l.stateid = ls.stateid
INNER JOIN lm_def_lifecycles ll
ON ls.lifecycleid =
ll.lifecycleid
WHERE ( r5.revisionstatus = 2 )) tbl4
ON tbl1.dataid = tbl4.asm_dataid
WHERE ( EXISTS (SELECT b.dataid
FROM dtreeacl b
WHERE b.dataid = Nvl(tbl2.permid, tbl2.lldataid)
AND ( rightid IN ( -2, -1, 1000, 1001 ) )
AND see > 0) )
ORDER BY modified_date DESC) tbl100
WHERE rownum <= 25
ORACLE SQL Developer throws an error as: ORA-00907: missing right
parenthesis
It looks like everything is fine but don't understand where am I getting the error of missing right parenthesis.
It is all because of Cast(d.dcomment AS NVARCHAR(4000)). NVARCHAR is not a valid data type.
Change it to Cast(d.dcomment AS NVARCHAR2(2000))
It will work
Fiddle
In the fiddle my sample is
select Cast('ddd' AS NVARCHAR2(2000)) AS dd from dual
If you change it in to
select Cast('ddd' AS NVARCHAR(2000)) AS dd from dual
You will get the error
ORA-00907: missing right parenthesis
Cast Operator in Oracle Tutorial
Data Types in Oracle

Choose the greater of either left or right side of 2 queries

I have the following union query that queries for the most recent date of a column if it exists:
SELECT TOP 1 m.sentdate AS 'calltreelastsignedoff'
FROM Incidents i
INNER JOIN Plans p ON i.planuid = p.uid
INNER JOIN IncidentMessages im ON i.uid = im.incidentuid
INNER JOIN Messages m ON im.messageuid = m.uid
WHERE p.uid = '031E3346-2921-426E-9494-1111111111'
UNION
SELECT TOP 1 m.sentdate AS 'calltreelastsignedoff'
FROM Incidents i
INNER JOIN PlanExercises pe ON i.planexerciseuid = pe.uid
INNER JOIN IncidentMessages im ON i.uid = im.incidentuid
INNER JOIN Messages m ON im.messageuid = m.uid
WHERE pe.planuid = '031E3346-2921-426E-9494-1111111111'
This will return 2 values if each query returns a top 1 result.
What I really want is to select the top 1 of the combined query.
How can I perform a select on the unioned query?
try this:
You could do this with a derived table
select top 1 from
(
SELECT TOP 1 m.sentdate AS 'calltreelastsignedoff'
FROM Incidents i
INNER JOIN Plans p ON i.planuid = p.uid
INNER JOIN IncidentMessages im ON i.uid = im.incidentuid
INNER JOIN Messages m ON im.messageuid = m.uid
WHERE p.uid = '031E3346-2921-426E-9494-1111111111'
UNION
SELECT TOP 1 m.sentdate AS 'calltreelastsignedoff'
FROM Incidents i
INNER JOIN PlanExercises pe ON i.planexerciseuid = pe.uid
INNER JOIN IncidentMessages im ON i.uid = im.incidentuid
INNER JOIN Messages m ON im.messageuid = m.uid
WHERE pe.planuid = '031E3346-2921-426E-9494-1111111111'
)a
order by <col>