高手请进!奇怪的sql语句(20分)

C

cx0731

Unregistered / Unconfirmed
GUEST, unregistred user!
这个在SQL SERVER数据库里面创建视图的sql语句有什么问题啊?老是报错“在关键字‘group’附近有语法错误”???<br>create &nbsp;view v_meetinginfo as<br>select ID, max(Pubdate) as MaxPubDate, max(ShowType) as ShowType &nbsp;from<br>((select ID, NoticeTitle as title, PubNoticeDate as PubDate, 0 as ShowType from Meetinginfo) union<br>(select ID, SummaryTitle as title, PubSummaryDate as PubDate, 1 as ShowType from Meetinginfo<br>&nbsp;where PubSummaryDate is not null))<br>&nbsp;group by ID order by MaxPubDate desc<br><br>已知 Meetinginfo 的表结构如下:<br>列名 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;数据类型 &nbsp; &nbsp;长度 &nbsp; 允许空<br>ID &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;numeric &nbsp; &nbsp; 5<br>noticetitle &nbsp; &nbsp; &nbsp; varchar &nbsp; &nbsp; 400 &nbsp; &nbsp; 是<br>summarytitle &nbsp; &nbsp; &nbsp;varchar &nbsp; &nbsp; 400 &nbsp; &nbsp; 是<br>PUBORGANID &nbsp; &nbsp; &nbsp; &nbsp;numeric &nbsp; &nbsp; 5 &nbsp; &nbsp; &nbsp; 是<br>PUBNOTICEDATE &nbsp; &nbsp; datetime &nbsp; &nbsp;8 &nbsp; &nbsp; &nbsp; 是<br>JOINER &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;text &nbsp; &nbsp; &nbsp; &nbsp;16 &nbsp; &nbsp; &nbsp;是<br>NOTICE &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;text &nbsp; &nbsp; &nbsp; &nbsp;16 &nbsp; &nbsp; &nbsp;是<br>TOPIC &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text &nbsp; &nbsp; &nbsp; &nbsp;16 &nbsp; &nbsp; &nbsp;是<br>SUMMARY &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text &nbsp; &nbsp; &nbsp; &nbsp;16 &nbsp; &nbsp; &nbsp;是<br>PUBEMPID &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;numeric &nbsp; &nbsp; 5 &nbsp; &nbsp; &nbsp; 是<br>PUBSUMMARYDATE &nbsp; &nbsp;datetime &nbsp; &nbsp;8 &nbsp; &nbsp; &nbsp; 是
 
最好 指定一下 表名 union<br>后 好像有 2个 id 字段吧...
 
能不能说详细点啊?我比较菜
 
create &nbsp;view v_meetinginfo as<br>select tmp.[ID], max(tmp.Pubdate) as MaxPubDate, max(tmp.ShowType) as ShowType &nbsp;from<br>((select [ID], NoticeTitle as title, PubNoticeDate as PubDate, 0 as ShowType from Meetinginfo) union<br>(select [ID], SummaryTitle as title, PubSummaryDate as PubDate, 1 as ShowType from Meetinginfo<br>&nbsp;where PubSummaryDate is not null)) tmp<br>&nbsp;group by tmp.[ID] order by tmp.MaxPubDate desc<br><br>试试
 
ID 這個字段無法確定來自哪個表啦<br><br>你在 from的子查詢後面加上 AS AAA<br>如下<br><br>create &nbsp;view v_meetinginfo as<br>select AAA.ID, max(Pubdate) as MaxPubDate, max(ShowType) as ShowType &nbsp;from<br>((select ID, NoticeTitle as title, PubNoticeDate as PubDate, 0 as ShowType from Meetinginfo) union<br>(select ID, SummaryTitle as title, PubSummaryDate as PubDate, 1 as ShowType from Meetinginfo<br>&nbsp;where PubSummaryDate is not null)) as AAA<br>&nbsp;group by AAA.ID order by AAA.max(Pubdate)desc
 
to William 和 Guigaobangsheng:<br>两位的方法一样,可又有新的问题啊:<br>&nbsp; &nbsp; 提示“除非同时指定了 TOP,否则 ORDER BY 子句在视图、内嵌函数、派生表和子查询中无效。”<br>&nbsp; &nbsp; 请再指点!
 
解决了,谢谢各位哈!
 
多人接受答案了。
 
顶部