如何对SQL数据库中的重复行进行分组和计数? [英] How do I group and count duplicate rows from SQL database?
问题描述
例如我有包含此类数据的表模型:
ID名称日期
1. Model1 02.02.2015
2. Model2 01.02.2015
3. Model1 29.01.2015
4. Model3 28.01.2015
我需要按日期和输出排序,例如,2015年1月29日之后的所有结果:
ID名称数量
1.模型1 2
2.模型2 1
我很乐意听到任何建议
For example I have table Models with such data:
ID Name Date
1. Model1 02.02.2015
2. Model2 01.02.2015
3. Model1 29.01.2015
4. Model3 28.01.2015
I need to sort by date and output, for example, all result after 29.01.2015:
ID Name Quantity
1. Model1 2
2. Model2 1
I will be happy to hear any suggestion
推荐答案
SELECT
ID, NAME, COUNT(QUANTITY) -- this can be COUNT(*)
FROM
MODELS
WHERE
Date >= CONVERT(DATETIME, '29.01.2015', 104)
GROUP BY
ID, NAME
你应该放更多在提出问题之前努力寻找解决方案。这是一个非常简单的查询,你可以在任何地方找到它。
如果这有帮助请花时间接受解决方案。谢谢。
You should put more effort into looking for the solution before asking the question. This is very simple query which you could have found anywhere.
If this helps please take time to accept the solution. Thank you.
这篇关于如何对SQL数据库中的重复行进行分组和计数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!