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
Related
SQL Server :
SELECT tblModule.Title , Count (tblActivity.Name) AS NumberOfActivities
From tblActivity
INNER JOIN tblModule
ON tblModule.ModuleID = tblActivity.ModuleID
Order by tblModule.Title
In here I am trying to display the number of times Name is displayed in tblActivity for each Module Title , How would I do this the error is :
Msg 8120, Level 16, State 1, Line 1
Column 'tblModule.Title' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
And the actual question : For each module, list the module title and the number of activities scheduled for the module.(5)
SELECT m.Title, COUNT_BIG(a.Name) AS NumberOfActivities
FROM dbo.tblActivity a
JOIN dbo.tblModule m ON m.ModuleID = a.ModuleID
GROUP BY m.Title
ORDER BY m.Title
or
SELECT m.Title, NumberOfActivities
FROM (
SELECT ModuleID, COUNT_BIG(*) AS NumberOfActivities
FROM dbo.tblActivity
GROUP BY ModuleID
) a
JOIN dbo.tblModule m ON m.ModuleID = a.ModuleID
ORDER BY m.Title
anyway, please provide actual and excepted result...
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
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
Please help me how to fixed this issue
Msg 116, Level 16, State 1, Line 1
Only one expression can be specified in the select list when the subquery is not introduced with
EXISTS.
Here is my query
SELECT a.RegId,
a.PropertyThumbnail
FROM tblPropertyDetail a
WHERE a.RegId NOT IN
(
SELECT RegId,
COUNT(RegId) AS NumOccurrences
FROM tblPropertyDetail
GROUP BY RegId
HAVING (COUNT(RegId) > 1)
)
remove this column in the subquery COUNT(RegId) AS NumOccurrences
SELECT a.RegId,
a.PropertyThumbnail
FROM tblPropertyDetail a
WHERE a.RegId NOT IN (
SELECT RegId
FROM tblPropertyDetail
GROUP BY RegId
HAVING (COUNT(RegId) > 1)
)
when using NOT IN, it is expected that the number of columns return by the subquery is only one.
Alternatively, you can also do this using JOIN
SELECT a.RegId,
a.PropertyThumbnail
FROM tblPropertyDetail a
LEFT JOIN
(
SELECT RegId, COUNT(RegId) AS NumOccurrences
FROM tblPropertyDetail
GROUP BY RegId
HAVING (COUNT(RegId) > 1)
) b ON a.RegId = b.RegId
WHERE b.RegId IS NULL
In SQLServer2005+ use CTE with aggregate window function
;WITH cte AS
(
SELECT RegId, PropertyThumbnail, COUNT(*) OVER (PARTITION BY RegId) AS cnt
FROM tblPropertyDetail
)
SELECT RegId, PropertyThumbnail
FROM cte
WHERE cnt <= 1
Is there a way to pass a value to a derived table query?
In the derived table I want reference a value ([docSVsys].[sID]) from the outer query.
I get an error:
Msg 4104, Level 16, State 1, Line 7 The multi-part identifier
"docSVsys.sID" could not be bound.
Yes I know this query can be simplified to no loop.
Have a cursor that has to loop and trying to convert it so set.
select top 10 [docSVsys].[sID], [sI].[count]
from docSVsys
join
(
select count(*) as [count]
from docSVenum1 as [sIt]
where [sIt].[sID] = [docSVsys].[sID]
) as [sI]
on '1' = '1'
order by [docSVsys].[sID]
Cross apply seemed to do the trick.
And it is exactly 1/3 faster than the cursor version.
Real query using cross apply below.
SELECT [sO].[sID], [sI].[max], [sI].[avg], [sI].[stdev]
FROM docSVsys as [sO] with (nolock)
cross apply
(
select [sO].[sID], max(list.match) as 'max', avg(list.match) as 'avg', stdev(list.match) as 'stdev'
from
(
select #SampleSet.[sID], [match] = 200 * count(*) / CAST ( #SampleSetSummary.[count] + [sO].[textUniqueWordCount] as numeric(8,0) )
from #SampleSet with (nolock)
join FTSindexWordOnce as [match] with (nolock) -- this is current #sID
on match.wordID = #SampleSet.wordID
and [match].[sID] = [sO].[sID]
join #SampleSetSummary with (nolock) -- to get the word count from the sample set
on #SampleSetSummary.[sID] = #SampleSet.[sID]
group by #SampleSet.[sID], #SampleSetSummary.[count]
) as list
having max(list.match) > 60
) as [sI]
where [textUniqueWordCount] is not null and [textUniqueWordCount] > 4 and [sO].[sID] <= 10686
order by [sO].[sID]
You can do what you want with a CROSS APPLY rather than a JOIN:
select top 10 [docSVsys].[sID], [sI].[count]
from docSVsys
cross apply
(
select count(*) as [count]
from docSVenum1 as [sIt]
where [sIt].[sID] = [docSVsys].[sID]
) as [sI]
order by [docSVsys].[sID]
Add the ID to the derived table, and join on that:
select top 10 [docSVsys].[sID], [sI].[count]
from docSVsys
join
(
select [sIt].[sID], count(*) as [count]
from docSVenum1 as [sIt]
group by [sIt].[sID]
) as [sI]
on [sI].[sID] = [docSVsys].[sID]
order by [docSVsys].[sID]