The multi-part identifier could not be bound(SQl Server 2005) - sql-server

Her is the query
SELECT NX.FileId, NX.SheetName, NX.SubmitId, NX.SubmitTimeA, NX.UpdateTimeA, NX.UpdateUserId, NX.FileSubmitId, NX.Tags, NX.UserId, NX.FileName, NX.UpdateFirstName, NX.UpdateLastName, NX.NG, NX.Complete, NX.FirstName, NX.LastName
FROM(
SELECT X.FileId, X.SheetName, X.SubmitId, X.SubmitTimeA, X.UpdateTimeA, X.UpdateUserId, X.FileSubmitId, X.Tags, X.UserId, X.FileName, X.FirstName AS UpdateFirstName, X.LastName AS UpdateLastName, X.NG, X.Complete, U.FirstName, U.LastName
FROM(
SELECT SS.FileId, SS.SheetName, SS.SubmitId, SS.SubmitTimeA, SS.UpdateTimeA, SS.UpdateUserId, SS.FileSubmitId, SS.Tags, SS.UserId, SS.FileName, SS.NG, SS.Complete, US.FirstName, US.LastName
FROM SubmitSheets AS SS
Left Join Users as US
ON SS.UpdateUserId = US.UserId
) as X
Left Join Users As U
On X.UserId = U.UserId
) AS NX
INNER JOIN Files AS F
ON NX.FileId=F.FileId
WHERE F.Locked =0 AND 1=1 AND 1=1 AND 1=1 AND NX.UpdateTimeA >= Cast('2014/10/30'as datetime) AND NX.UpdateTimeA < DateAdd("d", 1, Cast('2014/10/31'as datetime)) AND (1=1) AND SS.Complete=1 OR SS.NG=1
ORDER BY NX.UpdateTimeA DESC
Here is the error
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "SS.Complete" could not be bound.
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "SS.NG" could not be bound.
Type of NG and Complete field is "bit"

Try replacing SS.Complete and SS.NG with NX.Complete and NX.NG respectively in the WHERE clause.
SELECT NX.FileId, NX.SheetName, NX.SubmitId, NX.SubmitTimeA, NX.UpdateTimeA, NX.UpdateUserId, NX.FileSubmitId, NX.Tags, NX.UserId, NX.FileName, NX.UpdateFirstName, NX.UpdateLastName, NX.NG, NX.Complete, NX.FirstName, NX.LastName
FROM(
SELECT X.FileId, X.SheetName, X.SubmitId, X.SubmitTimeA, X.UpdateTimeA, X.UpdateUserId, X.FileSubmitId, X.Tags, X.UserId, X.FileName, X.FirstName AS UpdateFirstName, X.LastName AS UpdateLastName, X.NG, X.Complete, U.FirstName, U.LastName
FROM(
SELECT SS.FileId, SS.SheetName, SS.SubmitId, SS.SubmitTimeA, SS.UpdateTimeA, SS.UpdateUserId, SS.FileSubmitId, SS.Tags, SS.UserId, SS.FileName, SS.NG, SS.Complete, US.FirstName, US.LastName
FROM SubmitSheets AS SS
Left Join Users as US
ON SS.UpdateUserId = US.UserId
) as X
Left Join Users As U
On X.UserId = U.UserId
) AS NX
INNER JOIN Files AS F
ON NX.FileId=F.FileId
WHERE F.Locked =0 AND 1=1 AND 1=1 AND 1=1 AND NX.UpdateTimeA >= Cast('2014/10/30'as datetime) AND NX.UpdateTimeA < DateAdd("d", 1, Cast('2014/10/31'as datetime)) AND (1=1) AND NX.Complete=1 OR NX.NG=1
ORDER BY NX.UpdateTimeA DESC

Related

Converting SQL view that uses functions to work in snowflake

I have a view in SQL server that i'm trying to convert to work in snowflake. The view uses two functions within it that are called to join onto another table.
I'm following steps provided by another user, but i'm getting lost somewhere in the middle and feel a little stuck.
Here is the query for the SQL Server view:
ALTER VIEW [proj].[pvw_PowerBI_ScrapList]
AS
SELECT
e.Name AS ProductionUnit,
temp.DateTime AS DateTime,
s.Reference AS Shift,
CONVERT(TIME, temp.DateTime) AS Time,
CONVERT(DATE, temp.DateTime - ISNULL((SELECT CAST(MIN(s_first.FromTimeOfDay) AS DateTime) FROM Shift s_first WHERE s_first.FromDay = s.FromDay AND s_first.ShiftCalendarID = s.ShiftCalendarID), CAST('6:00' AS DateTime))) AS ProductionDate,
temp.ScrapReason AS ScrapReason,
temp.Quantity AS ScrapQuantity,
'Manually Registered' AS RegistrationType
FROM (SELECT
CAST(SUM(sreg.ScrapQuantity) AS int) AS Quantity,
sreas.Name As ScrapReason,
DATEADD(MINUTE, 30 * (DATEPART(MINUTE, sreg.ScrapTime) / 30), DATEADD(HOUR, DATEDIFF(HOUR, 0, sreg.ScrapTime), 0)) AS DateTime,
srer.EquipmentID AS EquipmentID
FROM qms.ScrapRegistration sreg WITH (NOLOCK)
INNER JOIN qms.ScrapReason sreas WITH (NOLOCK) ON sreas.ID = sreg.ScrapReasonID -- creating CTE of this table
INNER JOIN WorkRequest wr WITH (NOLOCK) ON wr.ID = sreg.WorkRequestID -- CREATE CTE OF THIS TABLE
INNER JOIN SegmentRequirementEquipmentRequirement srer WITH (NOLOCK) ON srer.SegmentRequirementID = wr.SegmentRequirementID -- CREATE CTE OF THIS TABLE
GROUP BY DATEADD(MINUTE, 30 * (DATEPART(MINUTE, sreg.ScrapTime) / 30), DATEADD(HOUR, DATEDIFF(HOUR, 0, sreg.ScrapTime), 0)), srer.EquipmentID, sreas.Name) temp
INNER JOIN Equipment e WITH (NOLOCK) ON e.ID = temp.EquipmentID -- CREATE CTE OF THIS TABLE
INNER JOIN ShiftCalendar sc WITH (NOLOCK) ON sc.ID = dbo.cfn_GetEquipmentShiftCalendarID(e.ID, temp.DateTime) -- CREATE CTE OF THIS TABLE
INNER JOIN Shift s WITH (NOLOCK) ON s.ID = dbo.cfn_GetShiftIDFromDateTime(temp.DateTime, sc.ID) -- CREATE CTE OF THIS TABLE
UNION
SELECT
e.Name AS ProductionUnit,
temp.DateTime AS DateTime,
s.Reference AS Shift,
CONVERT(TIME, temp.DateTime) AS Time,
CONVERT(DATE, temp.DateTime - ISNULL((SELECT CAST(MIN(s_first.FromTimeOfDay) AS DateTime) FROM Shift s_first WHERE s_first.FromDay = s.FromDay AND s_first.ShiftCalendarID = s.ShiftCalendarID), CAST('6:00' AS DateTime))) AS ProductionDate,
temp.ScrapReason AS ScrapReason,
temp.Quantity AS ScrapQuantity,
'Auto Registered' AS RegistrationType
FROM (SELECT
SUM(ISNULL(asr.ScrapQuantity, 0)) AS Quantity,
sreas.Name As ScrapReason,
DATEADD(MINUTE, 30 * (DATEPART(MINUTE, asr.ScrapTime) / 30), DATEADD(HOUR, DATEDIFF(HOUR, 0, asr.ScrapTime), 0)) AS DateTime,
srer.EquipmentID AS EquipmentID
FROM proj.AutoScrapRegistration asr WITH (NOLOCK)
INNER JOIN qms.ScrapReason sreas WITH (NOLOCK) ON sreas.ID = asr.ScrapReasonID
INNER JOIN WorkRequest wr WITH (NOLOCK) ON wr.ID = asr.WorkRequestID
INNER JOIN SegmentRequirementEquipmentRequirement srer WITH (NOLOCK) ON srer.SegmentRequirementID = wr.SegmentRequirementID
GROUP BY DATEADD(MINUTE, 30 * (DATEPART(MINUTE, asr.ScrapTime) / 30), DATEADD(HOUR, DATEDIFF(HOUR, 0, asr.ScrapTime), 0)), srer.EquipmentID, sreas.Name) temp
INNER JOIN Equipment e WITH (NOLOCK) ON e.ID = temp.EquipmentID
INNER JOIN ShiftCalendar sc WITH (NOLOCK) ON sc.ID = dbo.cfn_GetEquipmentShiftCalendarID(temp.EquipmentID, temp.DateTime) -- the function is called here
INNER JOIN Shift s WITH (NOLOCK) ON s.ID = dbo.cfn_GetShiftIDFromDateTime(temp.DateTime, sc.ID) -- the function is called here
i'm ignoring the union for now to try and get something simply working.
The functions are highlighted in this post How to conver this SQL server Function into a CTE in snowflake
Here is what i have so far:
WITH table_ScrapRegistration (
Quantity
,ScrapReason
,DATETIME
,EquipmentID
,ScrapReasonID
,WorkRequestID
,SegmentRequirementID
)
AS (
SELECT CAST(SUM(sreg.ScrapQuantity) AS INT) AS Quantity
,sreas.Name AS ScrapReason
,DATEADD(MIN, 30 * (DATE_PART(MINUTE, sreg.ScrapTime) / 30)::INT, date_trunc('HOUR', sreg.ScrapTime)) AS DATETIME
,srer.EquipmentID AS EquipmentID
,sreas.ID AS ScrapReasonID
,wr.ID AS WorkRequestID
,srer.SegmentRequirementID AS SegmentRequirementID
FROM DB_BI_DEV.RAW_CPMS_LAG.ScrapRegistration sreg
INNER JOIN DB_BI_DEV.RAW_CPMS_LAG.ScrapReason sreas ON sreas.ID = sreg.ScrapReasonID -- CREATE CTE OF THIS TABLE
INNER JOIN DB_BI_DEV.RAW_CPMS_LAG.WorkRequest wr ON wr.ID = sreg.WorkRequestID -- CREATE CTE OF THIS TABLE
INNER JOIN DB_BI_DEV.RAW_CPMS_LAG.SegmentRequirementEquipmentRequirement srer ON srer.SegmentRequirementID = wr.SegmentRequirementID -- CREATE CTE OF THIS TABLE
GROUP BY DATEADD(MIN, 30 * (DATE_PART(MINUTE, sreg.ScrapTime) / 30)::INT, date_trunc('HOUR', sreg.ScrapTime))
,srer.EquipmentID
,sreas.Name
,sreas.ID
,wr.ID
,srer.SegmentRequirementID
)
,table_scrapreason (
name
,ID
)
AS (
SELECT name
,ID
FROM DB_BI_DEV.RAW_CPMS_LAG.ScrapReason
)
,table_workrequest (
ID
,SegmentRequirementID
) -- this is from a join)
AS (
SELECT id
,SegmentRequirementID
FROM DB_BI_DEV.RAW_CPMS_LAG.WorkRequest
)
,table_SegmentRequirementEquipmentRequirement (
EquipmentID
,SegmentRequirementID
)
AS (
SELECT EquipmentID
,SegmentRequirementID
FROM DB_BI_DEV.RAW_CPMS_LAG.SegmentRequirementEquipmentRequirement
)
,table_equipment (
id
,name
,ParentEquipmentID
,ShiftCalendarEntityNumber
)
AS (
SELECT id
,name
,ParentEquipmentID
,ShiftCalendarEntityNumber
FROM DB_BI_DEV.RAW_CPMS_LAG.Equipment
)
,table_ShiftCalendar (
id,
entitynumber,
begindate,
enddate,
name,
PeriodInDays
)
AS (
SELECT id,
entitynumber,
begindate,
enddate,
name,
PeriodInDays
FROM DB_BI_DEV.RAW_CPMS_LAG.ShiftCalendar)
,table_shift (
ID
,Reference
,FromDay
)
AS -- this is from the temp table
(
SELECT ID
,Reference
,FromDay
FROM DB_BI_DEV.RAW_CPMS_LAG.shift
)
,temp_sub_select
AS (
SELECT sreg.Quantity AS Quantity
,sreas.name AS ScrapReason
,timeadd('minute', TRUNCATE (minute(sreg.DATETIME) / 30) * 30, date_trunc('hour', sreg.DATETIME)) AS DATETIME
,srer.EquipmentID AS EquipmentID
FROM table_ScrapRegistration sreg
INNER JOIN table_scrapreason sreas ON sreas.ID = sreg.ScrapReasonID -- CREATE CTE OF THIS TABLE
INNER JOIN table_workrequest wr ON wr.ID = sreg.WorkRequestID -- CREATE CTE OF THIS TABLE
INNER JOIN table_SegmentRequirementEquipmentRequirement srer ON srer.SegmentRequirementID = wr.SegmentRequirementID -- CREATE CTE OF THIS TABLE
GROUP BY sreg.Quantity
,sreas.name
,timeadd('minute', TRUNCATE (minute(sreg.DATETIME) / 30) * 30, date_trunc('hour', sreg.DATETIME))
,srer.EquipmentID
)
--select * from cte_GetEquipmentShiftCalendarID_part_a
--when i run this i get results
, cte_GetEquipmentShiftCalendarID_part_a
as (
WITH recursive rec_cte(id, parentequipmentid, shiftcalendarentitynumber) AS (
SELECT ID,
ParentEquipmentID,
ShiftCalendarEntityNumber
FROM table_equipment
UNION ALL
SELECT r.ID,
p.ParentEquipmentID,
p.ShiftCalendarEntityNumber
FROM rec_cte AS r
INNER JOIN table_equipment p ON p.ID = r.ParentEquipmentID
AND r.ShiftCalendarEntityNumber IS NULL
)
SELECT c.id,
sc.id AS shiftCalendarID,
sc.begindate,
sc.enddate
FROM rec_cte AS c
LEFT JOIN table_shiftcalendar AS sc ON sc.entitynumber = c.ShiftCalendarEntityNumber
WHERE shiftcalendarentitynumber IS NOT NULL
)
select * from cte_GetEquipmentShiftCalendarID_part_a
-- when i run this, i dont get any results.

SQL Error: No column name was specified for column 1 of 'qry'

I get the 'No Column name error' on the qry part of this SQL Query. I have tried naming the qry column, but did not have any luck. Any help would be appreciated.
WITH qry as (
SELECT getdate(), cast(convert(varchar(10), getdate(), 110) as datetime) AS GetDate
,T1.[Account]
,t2.AcctName
,T1.[RefDate] RefDate
,SUM(T1.[Debit])De
,SUM(T1.[Credit])Cr
FROM OJDT T0
INNER JOIN JDT1 T1
ON T0.TransId = T1.TransId
INNER JOIN oact t2
ON t1.Account = t2.AcctCode
AND t2.Finanse = 'Y'
AND t2.FrozenFor = 'N'
AND not t2.AcctName like '%Petty Cash%'
WHERE T1.[RefDate] < CONVERT(date,GETDATE() -7)
GROUP BY
T1.[Account]
,T1.[RefDate]
,t2.AcctName
)
SELECT 'TEST' as company
,c.Account AS Accountcode
,c.AcctName
,sum(de) - sum(cr) AS Closing
FROM qry c
GROUP BY c.Account, c.AcctName
ORDER BY
case
when CHARINDEX('Clearing', c.AcctName) > 1 then 'z'+ c.AcctName
else c.AcctName
end
You should add name for getdate() column in your cte:
WITH qry as (
SELECT getdate() AS CurrDate
, cast(convert(varchar(10), getdate(), 110) as datetime) AS GetDate
,T1.[Account]
,t2.AcctName
,T1.[RefDate] RefDate
,SUM(T1.[Debit])De
,SUM(T1.[Credit])Cr
FROM OJDT T0
INNER JOIN JDT1 T1
ON T0.TransId = T1.TransId
INNER JOIN oact t2
ON t1.Account = t2.AcctCode
AND t2.Finanse = 'Y'
AND t2.FrozenFor = 'N'
AND not t2.AcctName like '%Petty Cash%'
WHERE T1.[RefDate] < CONVERT(date,GETDATE() -7)
GROUP BY
T1.[Account]
,T1.[RefDate]
,t2.AcctName
)

Error on cross join

I have below code but, getting error message
(select count(*) as countT from dbo.P1 ) a
cross join (select count(*) as cN from dbo.[D2] ) b
cross join (select count(*) as cM from dbo.m1) c
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 'a'.
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near 'b'.
Msg 102, Level 15, State 1, Line 3
Incorrect syntax near 'c'.
What i am doing wrong?
SELECT col_list from in your query. Try this way
Select countT,cN,cM From
(select count(*) as countT from dbo.P1 ) a
cross join (select count(*) as cN from dbo.[D2] ) b
cross join (select count(*) as cM from dbo.m1) c
Since all the sub-queries going to return one row as result you can do this as well.
Select (select count(*) as countT from dbo.P1 ) countT
, (select count(*) as cN from dbo.[D2] ) cN
, (select count(*) as cM from dbo.m1) cM

Issue with Stored Procedure modify in SQL

Trying to amend a stored procedure and getting this error
Msg 156, Level 15, State 1, Procedure webopius_OrderExport, Line 14
Incorrect syntax near the keyword 'left’.
The code is
ALTER proc [dbo].[webopius_OrderExport]
AS
SET NOCOUNT ON
BEGIN
SELECT
convert(varchar, getdate(), 103) ExtractDate,
o.StoreID, o.OrderNumber OrderNumber,
o.CustomerID, o.FirstName, o.LastName, o.Email, o.AffiliateID
left(cast(o.OrderNotes as varchar(100)), 100) OrderNotes,
o.ShippingFirstName, o.ShippingLastName, o.ShippingMethod,
left(left(case o.ShippingCompany when '' then '' else o.ShippingCompany+' ' end,80)+o.ShippingAddress1, 100) ShippingAddress1,
left(o.ShippingAddress2, 100) ShippingAddress2, left(o.ShippingSuite, 30) ShippingSuite,
left(o.ShippingCity, 50) ShippingCity,
left(o.ShippingState, 50) ShippingState,
left(o.ShippingZip, 13) ShippingZip,
o.ShippingCountry ShippingCountry, o.ShippingPhone,
cast(isnull(a.ExtensionData,'') as nvarchar(200)) ShippingTitle,
o.ShippingMethodID,
convert(varchar(10), o.OrderDate, 103) as OrderDate,
left(os.OrderedProductSKU, 40) SKU,
os.Quantity,
isnull(os.OrderedProductRegularPrice, 0) OrderedProductRegularPrice,
ISNULL(os.OrderedProductPrice,0) OrderedProductPrice,
isnull(os.OrderedProductSalePrice,0) OrderedProductSalePrice ,
isnull(o.LevelHasNoTax, 0) LevelHasNoTax,
TaxRate, os.IsTaxable, OrderShippingCosts, o.OrderSubtotal SubTotal,
left(upper(o.BillingFirstName + ' ' + o.BillingLastName), 80) AS BillingName,
left(left(case o.BillingCompany when '' then '' else o.BillingCompany+' ' end,80)+o.BillingAddress1,100) BillingAddress1,
left(o.BillingAddress2, 100) BillingAddress2,
left(o.BillingSuite, 30) BillingSuite,
left(o.BillingCity, 50) BillingCity,
left(o.BillingState, 50) BillingState,
left(o.BillingZip, 8) BillingZip,
o.BillingCountry BillingCountry, o.BillingPhone,
o.CardType, o.CardName, o.CardNumber,
o.CardExpirationMonth+'/'+o.CardExpirationYear CardExpiryDate,
o.CardStartDate, o.CardIssueNumber,
o.PaymentMethod, o.LevelID, os.ProductID, os.VariantID,
c.CustomerID,
'"' + isnull(cast(c.ExtensionData as nvarchar(200)), '') + '"' ExtensionData,
m.Name Supplier, p.Name ProductName,
'"' + isnull(cast(p.ExtensionData as nvarchar(200)), '') + '"' ProdExtensionData
FROM
dbo.Orders o with (NOLOCK)
left join
orders_ShoppingCart os WITH (NOLOCK) ON os.OrderNumber = o.OrderNumber
left join
Customer c WITH (NOLOCK) on c.CustomerID = o.CustomerID
left join
Product p with (NOLOCK) on p.ProductID = os.ProductID
left join
ProductManufacturer pm with (NOLOCK) on pm.ProductID = os.ProductID
left join
Manufacturer m with (NOLOCK) on m.ManufacturerID = pm.ManufacturerID
left join
Address a with (NOLOCK) on a.AddressID = c.ShippingAddressID
WHERE
o.OrderNumber = os.OrderNumber
AND o.ShippedOn IS NULL
AND (o.TransactionState IN ('AUTHORIZED', 'CAPTURED')
OR (o.TransactionState = 'PENDING' and o.PaymentMethod='PURCHASEORDER'))
AND IsNew = 1
AND o.StoreID <> 4
ORDER BY
ordernumber
END
The column does exist in the SQL table where the left command is shown.
Any ideas where I'm going wrong?
Your code reads at about line 20
o.CustomerID, o.FirstName, o.LastName, o.Email, o.AffiliateID
left(cast(o.OrderNotes as varchar(100)),100) OrderNotes,
it should read
o.CustomerID, o.FirstName, o.LastName, o.Email, o.AffiliateID, --comma added here
left(cast(o.OrderNotes as varchar(100)),100) OrderNotes,
Easy, missing comma
You're missing a comma on the previous line, after o.AffiliateID.

SQL Pivot dynamic query error

I tried to convert rows values into columns...Below is my query...
Select * from (SELECT *
FROM ( SELECT PROJ.PROJ_ID,
PROJ.PROJ_NM AS [Project Name],
PROJ.PROJ_DS AS [Project Description],
convert(varchar(10), PROJ.PROJ_ACTL_LNCH_DT, 110) [Actual Completed Date],
PROJ.PROJ_SMRY_DS AS [Project Summary],
dbo.udf_BankContacts(PROJ.PROJ_ID) AS [BankContact],
convert(varchar(10), PROJ.PROJ_EST_LNCH_DT, 110) AS [Estimated Launch Date],
PROJ.ENTER_DT AS [Begin Date],
PROJ_STA.PROJ_STA_DS AS [Project Status],
SFTW_DEV_MTHD.SFTW_DEV_MTHD_NM AS [Software Development Method],
PROJ_PHASE.PROJ_PHASE_DS AS [Phase],
PROJ_CTGY.PROJ_CTGY_DS AS [Project Category],
(CASE WHEN PROJ.ARCH_IN='0' THEN 'N' ELSE 'C' END) AS [Archive],
PROJ.PHASE_CMNT_TX AS [Phase Comment],
PROD_TYPE_DS
FROM dbo.Project PROJ
left join dbo.PROJ_PROD PP on PROJ.PROJ_ID = PP.PROJ_ID
left join dbo.PROD_TYPE PT on PP.PROD_TYPE_ID = PT.PROD_TYPE_ID
LEFT JOIN DBO.PROJ_STA ON PROJ.PROJ_STA_ID = PROJ_STA.PROJ_STA_ID
left join dbo.SFTW_DEV_MTHD on PROJ.SFTW_DEV_MTHD_ID = SFTW_DEV_MTHD.SFTW_DEV_MTHD_ID
left join dbo.PROJ_PHASE on PROJ.PROJ_PHASE_ID = PROJ_PHASE.PROJ_PHASE_ID
left join dbo.PROJ_CTGY on PROJ.PROJ_CTGY_ID = PROJ_CTGY.PROJ_CTGY_ID
) data
PIVOT
( MAX(PROD_TYPE_DS)
FOR PROD_TYPE_DS IN ([PT1],[PT2])
) pvt2
where PROJ_ID is not null) AS ProdType
LEFT JOIN (SELECT *
FROM
(
select PROJ_ID,
PROJ_APRV_TYPE_DS = case
when col ='PROJ_APRV_TYPE_DS'
then PROJ_APRV_TYPE_DS
else PROJ_APRV_TYPE_DS+col end,
value
from
(
SELECT PROJ.PROJ_ID,
PAT.PROJ_APRV_TYPE_DS PROJ_APRV_TYPE_DS,
convert(varchar(10), PATS.APRV_EXPT_BY_DT, 120) APRV_EXPT_BY_DT,
convert(varchar(10), PATS.APRV_CMPL_DT, 120) APRV_CMPL_DT
FROM dbo.Project PROJ
left join [dbo].[PROJ_APRV_TYPE_STA] PATS
on PROJ.PROJ_ID = PATS.PROJ_ID
left join [dbo].[PROJ_APRV_STA] PAS
on PATS.PROJ_APRV_STA_ID = PAS.PROJ_APRV_STA_ID
right outer join dbo.PROJ_APRV_TYPE PAT
on PATS.PROJ_APRV_TYPE_ID = PAT.PROJ_APRV_TYPE_ID
) s
cross apply
(
select 'PROJ_APRV_TYPE_DS', PROJ_APRV_TYPE_DS union all
select ' Expected Date', APRV_EXPT_BY_DT union all
select ' Completed Date', APRV_CMPL_DT
) c (col, value)
) data
PIVOT
(
MAX(value)
FOR PROJ_APRV_TYPE_DS IN ([RMC Approval],[RMC Approval Expected Date],[RMC Approval Completed Date],[BOD Approval],[BOD Approval Expected Date],[BOD Approval Completed Date])
) pvt1 where PROJ_ID is not null ) AS Approval ON ProdType.PROJ_ID = Approval.PROJ_ID
LEFT JOIN (SELECT *
FROM ( SELECT PROJ.PROJ_ID,
ORG_SHRT_NM
FROM dbo.Project PROJ
left join dbo.PROJ_LGL_ENT_IMPCT PLEI on PROJ.PROJ_ID = PLEI.PROJ_ID
right outer join dbo.LGL_ENT LE on PLEI.LGL_ENT_ID = LE.LGL_ENT_ID
) data
PIVOT
( MAX(ORG_SHRT_NM)
FOR ORG_SHRT_NM IN ([AECB],[FSB],[TRS])
) pvt3
where PROJ_ID is not null) AS LegalEntity ON ProdType.PROJ_ID = LegalEntity.PROJ_ID LEFT JOIN
(;with cte as
(
SELECT PCGU.PROJ_ID,
name = u.USER_LST_NM + ', '+ u.USER_FIRST_NM,
CTC_GRP_DS
FROM dbo.[user] u
left join dbo.PROJ_CTC_GRP_USER PCGU
on u.USER_ID = PCGU.USER_ID
left join dbo.CTC_GRP CG
on PCGU.CTC_GRP_ID = CG.CTC_GRP_ID
)
select *
from
(
select c1.proj_id,
c1.CTC_GRP_DS,
STUFF(
(SELECT ', ' + c2.name
FROM cte c2
where c1.proj_id = c2.proj_id
and c1.CTC_GRP_DS = c2.CTC_GRP_DS
FOR XML PATH (''))
, 1, 1, '') AS name
from cte c1
) d
pivot
(
max(name)
for CTC_GRP_DS in ([Bank Contact],[Dept2])
) piv
where PROJ_ID is not null)
AS Dept ON ProdType.PROJ_ID = Dept.PROJ_ID
I am getting error
Msg 102, Level 15, State 1, Line 84
Incorrect syntax near ';'.
Msg 102, Level 15, State 1, Line 115
Incorrect syntax near ')'.
I am totally confused what i am missing it. I recently started pivot & dynamic query concepts...Please guide on it...
Your error is here.
(;with cte as
(
SELECT PCGU.PROJ_ID,
name = u.USER_LST_NM + ', '+ u.USER_FIRST_NM,
CTC_GRP_DS
FROM dbo.[user] u
left join dbo.PROJ_CTC_GRP_USER PCGU
on u.USER_ID = PCGU.USER_ID
left join dbo.CTC_GRP CG
on PCGU.CTC_GRP_ID = CG.CTC_GRP_ID
)
select *
from
(
select c1.proj_id,
c1.CTC_GRP_DS,
STUFF(
(SELECT ', ' + c2.name
FROM cte c2
where c1.proj_id = c2.proj_id
and c1.CTC_GRP_DS = c2.CTC_GRP_DS
FOR XML PATH (''))
, 1, 1, '') AS name
from cte c1
) d
You cannot have a CTE in sub selects like that.
You could try to put the CTE at the very top, or create a temp table for the select.
The error is with your CTE. You can't stick a CTE inside a query like that
(;with cte as

Resources