简体   繁体   English

模拟组,按顺序,从mysql限制到ms sql 2000

[英]Emulate group by, order by, limit from mysql to ms sql 2000

I have a query in MySQL: 我在MySQL中有一个查询:

select slscod,slsname,brc
from td_casa
group by slscod
order by slsname
limit 0,100

This query retrieves the top 100 unique sales codes with slsname and brc , ordered by slsname ascending. 此查询使用slsname升序检索前100个唯一销售代码,其中包含slsnamebrc

How to change this query in MSSQL 2000? 如何在MSSQL 2000中更改此查询?

The basic syntax is: 基本语法是:

select top 100 slscod,slsname,brc 
from td_casa 
group by slscod 
order by slsname
SELECT TOP 100 slscod, slsname, brc
FROM td_casa
GROUP BY slscod, slsname, brc
ORDER BY slsname

Note: regarding your comment about Column 'xyz' is invalid in the select ... error I noticed that you were selecting 3 columns but specified 1 column in the GROUP BY clause. 注意:关于列'xyz'的注释在select ...错误中无效我注意到您选择了3列但在GROUP BY子句中指定了1列。 This is a MySQL specific behavior as described here : 这是描述一个MySQL的具体行为在这里

MySQL extends the use of GROUP BY to permit selecting fields that are not mentioned in the GROUP BY clause. MySQL扩展了GROUP BY的使用,允许选择GROUP BY子句中未提及的字段。 If you are not getting the results that you expect from your query, please read the description of GROUP BY found in Section 11.15, "Functions and Modifiers for Use with GROUP BY Clauses". 如果您没有从查询中获得预期的结果,请阅读第11.15节“用于GROUP BY子句的函数和修饰符”中的GROUP BY描述。

because in MSSQL 2000 does not support the ROW_NUMBER() and LIMIT ... OFFSET .. finally, i found this query : 因为在MSSQL 2000中不支持ROW_NUMBER()LIMIT ... OFFSET ..最后,我发现了这个查询:

SELECT slscod, MIN(slsname) slsname, MIN(brc) brc FROM (
    SELECT top 30 slscod, MIN(slsname) slsname, MIN(brc) brc FROM
    (
        SELECT TOP (1*30) slscod, MIN(slsname) slsname, MIN(brc) brc
        FROM td_casa group by slscod
        ORDER BY slsname ASC
    ) AS t1 group by slscod
     ORDER BY slsname DESC ) AS t2 group by slscod ORDER BY MIN(slsname) ASC

This is the same if in mysql : 如果在mysql中这是相同的:

select slscod,slsname,brc from td_casa group by slscod order by slsname limit 0,30

if you change TOP (2*30) this is the same limit 30,30 . 如果你改变TOP (2*30)这是相同的limit 30,30 TOP (3*30) this is the same limit 60,30 and so on. TOP (3*30)这是相同的limit 60,30 ,依此类推。

desperate need of effort. 迫切需要努力。 thanks all.let's cheers 谢谢all.let的欢呼声

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM