我尝试了我的查询但在sql查询中显示错误 [英] I tried my query but showing error in sql query
问题描述
在数据库记录中如下
Schdate课程课程学院
8月MFA 1 AD
28 AUg EFA 2 AD
8月28日MC 3 AD
i想要输出如下
28Aug(MFA -S1,EFA -S2,MC -S3)
获得以上输出我尝试了如下查询
SELECT Facuty,
STUFF(( SELECT ' (' + 转换( char ( 7 ),[Schdate], 100 )+ ' ' + [课程] + ' - S' + [Session] + ' )'
来自 Tb_Sch_Time_Table_Details sch
其中 sch.Facuty = sch1.Facuty
FOR XML PATH (' ')[course])
FROM Tb_Sch_Time_Table_Details sch1
GROUP BY Facuty
ORDER BY 1
当我执行上述查询时显示如下错误
语法不正确'课程'附近
请帮助我如何解决问题以获得正确的结果。
< blockquote>只需尝试以下查询并让我知道结果。
选择 转换( char ( 7 ),sch1。[Schdate] , 100 )+ ' (' + STUFF(
(选择 ' ,' + ltrim(rtrim([Course]))+ ' - S' + ltrim(rtrim ([会话]))来自 Tb_Sch_Time_Table_Details sch
其中 sch.Schdate = sch1.Schdate
FOR XML PATH(' '))
, 1 , 1 , ' ')+ < span class =code-string>' )' 来自 Tb_Sch_Time_Table_Details sch1
group by Schdate
order by 1
尝试这个
SELECT Faculty,
(SELECT'('+ Convert(char(7),[Schdate],100)+ ''+ [课程] +' - S'+演员([会话]为varchar(5))+')'
来自@TBL sch
其中sch.Faculty = sch1.Faculty
FOR XML PATH(''))
FROM @TBL sch1
GROUP BY Faculty
ORDER BY 1
In database records as follows
Schdate Course Session Faculty
28 Aug MFA 1 AD
28 AUg EFA 2 AD
28 Aug MC 3 AD
i want the output as follows
28Aug(MFA -S1,EFA -S2,MC -S3)
for getting a above output i tried my query as follows
SELECT Facuty,
STUFF((SELECT '( ' + Convert(char(7),[Schdate],100 )+ ' ' + [Course] + '-S' + [Session] + ')'
from Tb_Sch_Time_Table_Details sch
where sch.Facuty = sch1.Facuty
FOR XML PATH('') [course])
FROM Tb_Sch_Time_Table_Details sch1
GROUP BY Facuty
ORDER BY 1
When i execute the above query shows error as follows
Incorrect syntax near 'Course'
please help me how can i solve the problem to get the correct ouptut.
Just try the below query and let me know the result.
select Convert(char(7),sch1.[Schdate],100 )+'('+STUFF( (select ','+ ltrim(rtrim([Course])) +'-S'+ltrim(rtrim([Session])) from Tb_Sch_Time_Table_Details sch where sch.Schdate = sch1.Schdate FOR XML PATH('')) ,1,1,'')+')' from Tb_Sch_Time_Table_Details sch1 group by Schdate order by 1
Try this
SELECT Faculty, (SELECT '( ' + Convert(char(7),[Schdate],100 )+ ' ' + [Course] + '-S' + cast([Session] as varchar(5) )+ ')' from @TBL sch where sch.Faculty = sch1.Faculty FOR XML PATH('') ) FROM @TBL sch1 GROUP BY Faculty ORDER BY 1
这篇关于我尝试了我的查询但在sql查询中显示错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!