SQL命令 - 按命令分组 [英] SQL Command - Group by command

查看:159
本文介绍了SQL命令 - 按命令分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,



我的桌子如:



InVoice表

======

ID int

日期日期时间

钱双倍





*案例A:

从InVoice选择ID,金钱



分组依据ID

ID = 1



=>结果是1行,ID = 1,Money = xxx(OK)



*案例B:

选择ID,Money

来自InVoice,日期

按ID分组,日期

ID = 1,日期介于'2013/11/1'和'2013/11 /之间10'

=>结果是很多行ID = 1和Money = xxx,yyy ......



我会在B情况下,结果显示1行,ID是1而Money是总计



请帮帮我

谢谢

Dear all,

I have table such as:

InVoice table
======
ID int
Date datetime
Money double


* Case A:
Select ID, Money
from InVoice
Group by ID
Having ID = 1

=> result is 1 row with ID = 1 and Money = xxx (OK)

* Case B:
Select ID, Money
from InVoice, Date
Group by ID, Date
Having ID = 1, Date between '2013/11/1' and '2013/11/10'
=> result is many row with ID = 1 and Money = xxx, yyy ...

I would in case B, result display 1 row, ID is 1 and Money is Total

Please help me
Thanks

推荐答案

如果你分组日期,它将为每个日期创建一个条目。如果您只按ID分组,它将为每个ID显示一个值,并将您的having子句应用于所选数据。所以,按日期删除组,这不是你想要的。你问它每个日期的记录。只需按id指定日期范围和组,您就可以了。
If you group by date, it will create an entry for each date. If you just group by id, it will show one value per id, and apply your having clause to the data selected. So, drop the group by date, it's not what you want. You're asking it for a record for each date. Just specify a date range and group by the id and you will be fine.


如果是B:我只需要组ID。但是当我过滤Date时,SQL必须将Date输入Group。我无法观察结果
In case B: I only want group ID. But when I filter Date then SQL must input Date into Group. I cannot have observe result


这篇关于SQL命令 - 按命令分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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