首页 新闻 会员 周边 捐助

SqlServer row_number()和group by(我穷,三十豆,看上了来)

0
悬赏园豆:30 [已解决问题] 解决于 2015-01-30 12:04
select * from (select ROW_NUMBER() over(order by b.YuLeCSMC,ylfwcsid,CiShu,a.DaKaSJ desc) as row, T.* from (
select b.YuLeCSMC,ylfwcsid,count(id) as CiShu,a.DaKaSJ
 from Info_DaKaJL a left join Info_YuLeCS b on YLFWCSID=b.YLCSID  
 where  b.ZhiAnGLJGDM like '%46%' group by b.YuLeCSMC,ylfwcsid,a.DaKaSJ
 union all select b.YuLeCSMC,ylfwcsid,count(id) as CiShu,\'合计\' from Info_DaKaJL a 
 inner join Info_YuLeCS b on YLFWCSID=b.YLCSID where b.ZhiAnGLJGDM like '%46%'
 group  by b.YuLeCSMC,ylfwcsid order by ylfwcsid,a.DaKaSJ
 ) T where 1=1 ) TT where TT.row between 1 and 10
View Code

这是sql,执行的时候会报错

消息 1033,级别 15,状态 1,第 8 行
除非另外还指定了 TOP 或 FOR XML,否则,ORDER BY 子句在视图、内联函数、派生表、子查询和公用表表达式中无效。

错误是这样,但是我不知道怎么去解决了,在线等,谢谢

问题补充:

百度之后加

top 100 percent

但是我不知道加在哪里,至少我加了之后还是这样,这里其实主要是 union all和sql语句的冲突,但是小弟解决不了这个冲突,请大神们不吝赐教

_Vegetables的主页 _Vegetables | 小虾三级 | 园豆:586
提问于:2015-01-30 09:06
< >
分享
最佳答案
0

饿 这样可以:

 1 SELECT *
 2 FROM   (
 3            SELECT ROW_NUMBER() OVER(ORDER BY T.YuLeCSMC, T.ylfwcsid, T.CiShu, T.DaKaSJ DESC) AS 
 4                   row,
 5                   T.* 
 6            FROM   (
 7                       SELECT TOP 100 PERCENT b.YuLeCSMC,
 8                              ylfwcsid,
 9                              COUNT(id) AS CiShu,
10                              a.DaKaSJ
11                       FROM   Info_DaKaJL a
12                              LEFT JOIN Info_YuLeCS b
13                                   ON  YLFWCSID = b.YLCSID
14                       WHERE  b.ZhiAnGLJGDM LIKE '%46%'
15                       GROUP BY
16                              b.YuLeCSMC,
17                              ylfwcsid,
18                              a.DaKaSJ
19                       UNION ALL SELECT  TOP 100 PERCENT b.YuLeCSMC,
20                                        ylfwcsid,
21                                        COUNT(id) AS CiShu,
22                                        \'合计\'
23                                 FROM   Info_DaKaJL a
24                                        INNER JOIN Info_YuLeCS b
25                                             ON  YLFWCSID = b.YLCSID
26                                 WHERE  b.ZhiAnGLJGDM LIKE '%46%'
27                                 GROUP BY
28                                        b.YuLeCSMC,
29                                        ylfwcsid
30                                 ORDER BY
31                                        ylfwcsid,
32                                        a.DaKaSJ
33                   ) T
34            WHERE  1 = 1
35        ) TT
36 WHERE  TT.row BETWEEN 1 AND 10
收获园豆:20
KingMi | 小虾三级 |园豆:1344 | 2015-01-30 11:56

有一点点小问题,不过小弟已经纠正,谢谢,还是经验太少,找到解决办法,但是加不到地方。。。

_Vegetables | 园豆:586 (小虾三级) | 2015-01-30 12:03

@_Vegetables: 解决问题就好,经验都是如此积累的。

KingMi | 园豆:1344 (小虾三级) | 2015-01-30 12:05
其他回答(2)
0

ROW_NUMBER() 可以这样用?

我用ROW_NUMBER()的时候都要用到with

dudu | 园豆:30948 (高人七级) | 2015-01-30 10:30

这样是绝对没有问题的,用with结果不是一样么,这里查询还是要count,就要用group by,但是一旦group by,就又回到了现在的现在的问题,order by的问题,好像是这么个意思

支持(0) 反对(0) _Vegetables | 园豆:586 (小虾三级) | 2015-01-30 10:59
0
select * from (select ROW_NUMBER() over(order by b.YuLeCSMC,ylfwcsid,CiShu,a.DaKaSJ desc
-- 这里已经是 查询的 T 表了 怎么还是 b, a 别名
) as row, T.* from (
select b.YuLeCSMC,ylfwcsid,count(id) as CiShu,a.DaKaSJ
 from Info_DaKaJL a left join Info_YuLeCS b on YLFWCSID=b.YLCSID  
 where  b.ZhiAnGLJGDM like '%46%' group by b.YuLeCSMC,ylfwcsid,a.DaKaSJ
 union all select b.YuLeCSMC,ylfwcsid,count(id) as CiShu,\'合计\' from Info_DaKaJL a 
 inner join Info_YuLeCS b on YLFWCSID=b.YLCSID where b.ZhiAnGLJGDM like '%46%'
 group  by b.YuLeCSMC,ylfwcsid order by ylfwcsid,a.DaKaSJ
 ) T where 1=1 ) TT where TT.row between 1 and 10

1楼的 "高人" 真是见识广博.

建议 该括号的地方括号吧, 这样的代码容易 读来颇有歧义,

收获园豆:10
过于执著 | 园豆:339 (菜鸟二级) | 2015-01-30 11:02

现在主要是union all这里之后就有问题了,我括号加上也是那样呀,求不吝赐教

支持(0) 反对(0) _Vegetables | 园豆:586 (小虾三级) | 2015-01-30 11:45
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册