如何使用group by子句进入sql server? [英] how to use group by clause into sql server?

查看:76
本文介绍了如何使用group by子句进入sql server?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我完全对group by和order by子句进入sql server感到困惑他们的工作方式我到目前为止还不清楚吗?



i我们使用更多然后一个列到order by子句那么它应该如何基于两列进行短记录?



以及如何使用group by子句以及记录分组的方式是什么?



请帮我清除这两个我心中的紧张情绪???

i am totally confused about group by and order by clause into sql server what is the way they work i am not getting clear till now ?

i we use more then one column into order by clause then how it should shortrecord based on two column ?

and how to use group by clause and what is the way in which record are grouped ?

please help me to clear these two big tension of my mind???

推荐答案

如果如果没有指定ORDER BY子句,则SQL可以按目前发现方便的任何顺序返回行 - 这意味着两个顺序相同的查询不一定以相同的顺序返回相同的数据。 ORDER BY告诉SQL如何返回数据。例如,当您选择要显示的记录并希望用户按字母顺序命名时,这很方便。如果您使用多个列,则排序由指定的第一列完成,然后第二列应用于第一列中的相同值,依此类推。



GROUP BY允许你聚合行 - 所以你可以问上个月每个推销员卖多少钱?

If you don't specify an ORDER BY clause, then SQL can return rows in any sequence that it finds convenient at the moment - which means that two sequential identical queries do not necessarily return the same data in the same order. ORDER BY tells SQL exactly how to return the data. This is handy when you SELECT records for display, and want users name in alphabetical sequence, for example. If you use more than one column, then the ordering is done by the first specified, then the second is applied to identical values in the first column, and so forth.

GROUP BY allows you to aggregate rows - so you can ask things like "how much has each salesman sold last month?"
SELECT SalesManName, SUM(SalesValue)
FROM SalesRecords
WHERE SaleDate BETWEEN '2015-09-01' AND '2015-09-30'
GROUP BY SalesManName



请注意,对于GROUP BY,因为它聚合了值,所以不能返回GROUP BY子句列表中未列出的任何列,除非它们在SUM,COUNT等聚合函数中,因为这需要在同一行中返回多个值,而SQL不会这样做!


Do note that with GROUP BY that because it aggregates values you can't return any columns that are not listed in the GROUP BY clause list, unless they are in an aggregation function such as SUM, COUNT, and so forth, as that would require returning multiple values in the same row, and SQL doesn't do that!


你好,



查看这些...



排序方式 [ ^ ]



以下SQL语句从Customers表中选择所有客户,按Country和CustomerName列排序:< br $>


Hi,

Check these...

Order By[^]

The following SQL statement selects all customers from the "Customers" table, sorted by the "Country" and the "CustomerName" column:

SELECT * FROM Customers ORDER BY Country, CustomerName;











分组依据 [ ^ ]







希望这些能帮到你。



干杯






Group By[^]



Hope these will help you.

Cheers


请看下面的链接。



[ ^ ]



我希望它能帮助你理解GROUP BY。
Please see the link below.

[^]

I hope it will help you to understand GROUP BY.


这篇关于如何使用group by子句进入sql server?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆