选择按年份分组的SQL结果

时间:2019-04-12 06:47:21

标签: sql

如何获得按两年分组的结果?

表1: 订单

 ORDER_ID       MemberID   OrderDate
+----------+--------------+-----+
 1          a              05-06-2016
 2          b              06-06-2016 
 3          a              06-06-2017
 4          b              07-06-2017
 5          h              08-06-2017
 6          a              02-06-2018
 7          b              03-06-2018
 8          a              05-06-2019
 9          b              09-06-2019

预期的输出结果:

  YEAR      ORDERS_AMOUNT   
+----------+--------------+
 2016-2017          5              
 2018-2019          4

我正在使用以下查询,但发现它不起作用...

SELECT year( o.orderDate) as Year, 
    count(o.order_id) as Order_Count
FROM orders  o 
Group by o.orderDate 

4 个答案:

答案 0 :(得分:1)

您必须根据void QtGraphicsView::setViewRect(const QRectF &rect) { if (m_viewRect == rect) return; m_viewRect = rect; fitInView(rect); } void QtGraphicsView::scaleView(qreal factor) { const QSizeF &size = m_viewRect.size() / factor; if (size.width() <= 0 || size.height() <= 0 || size.width() >= sceneRect().width() || size.height() >= sceneRect().height()) return; const QSizeF &dsize = m_viewRect.size() - size; const QPointF &topLeft = m_viewRect.topLeft() + QPointF(dsize.width() / 2, dsize.height() / 2); setViewRect(QRectF(topLeft, size).toRect()); }

进行分组
cd /tmp/kafka-logs/dolphin-spider-google-book-bookinfo-0
# delete index file
rm -rf *

这将在SQL Server中工作,在某些其他DBMS中,您可能需要四舍五入year(orderDate)/2结果。我忽略了年间隔的格式,因为它是微不足道且非常特定于DBMS的。

答案 1 :(得分:1)

使用年份(orderDate)/ 2的分组依据

SELECT  (CONVERT(VARCHAR,MIN(year(OrderDate)))+'-' + CONVERT(VARCHAR,MAX(year(OrderDate)))) AS [Year],
        COUNT(*) as [ORDERS_AMOUNT]
FROM Orders
GROUP BY FLOOR(Year(OrderDate) /2)

答案 2 :(得分:1)

Radim的解决方案几乎是正确的,但是它需要对MySQL使用整数除法。在MySQL上运行时,/与原始年份的年份相同。

实时测试:https://www.db-fiddle.com/f/pW4YduNY8DJDy6GCLKUdqS/0

SELECT 
    year(orderDate) div 2 * 2 as Year, 
    count(o.order_id) as Order_Count       
FROM orders  o 
Group by year

另一种方法,使用模数:

SELECT 
    year(orderDate) - mod(year(orderdate),2) as year,
    count(o.order_id) as Order_Count       
FROM orders  o 
Group by year

以上两个查询的输出:

| year | Order_Count |
| ---- | ----------- |
| 2016 | 5           |
| 2018 | 4           |

Radim的SQL Server代码在MySQL上不起作用:

实时测试:https://www.db-fiddle.com/f/pW4YduNY8DJDy6GCLKUdqS/3

SELECT (year(orderDate)/2) * 2 as Years, 
       count(o.order_id) as Order_Count
FROM orders  o 
Group by (year(orderDate)/2) * 2

输出:

| Years | Order_Count |
| ----- | ----------- |
| 2016  | 2           |
| 2017  | 3           |
| 2018  | 2           |
| 2019  | 2           |

虽然可以在SQL Server上运行。

实时测试:

https://dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=ced41544d5727fb02229a17efc6970c3

输出:

the_year    Order_Count
2016        5
2018        4

对于SQL Server:

SELECT 
    year(orderDate) - (year(orderdate) % 2) as year,
    count(o.order_id) as Order_Count       
FROM orders  o 
Group by year(orderDate) - (year(orderdate) % 2)

输出:     

 year   Order_Count
 2016   5
 2018   4

答案 3 :(得分:1)

尝试此查询。

SQL Server

SELECT Count(*) AS Count,
       Concat(Min(Year(orderdate)), '-', Min(Year(orderdate) + 1)) AS Year
FROM   ORDERS
GROUP  BY Year(orderdate) / 2  

MySQL

SELECT Count(*) AS Count,
       Concat(Min(Year(orderdate)), '-', Min(Year(orderdate) + 1)) AS Year
FROM   orders
GROUP  BY Year(orderdate) DIV 2 

谢谢Michael Buen推荐MySQL DIV Function