显示pymtmode,根据给定的数据,在2015年前付款的付款总数和付款总数应大于1:
ORDERID QUOTATIONID QTYORDERED ORDERDATE STATUS PYMTDATE DELIVEREDDATE AMOUNTPAID PYMTMODE
O1001 Q1002 100 30-OCT-14 Delivered 05-NOV-14 05-NOV-14 140000 Cash
O1003 Q1003 50 15-DEC-14 Delivered 18-DEC-14 20-DEC-14 310000 Cash
O1004 Q1006 100 15-DEC-14 Delivered 25-DEC-14 30-DEC-14 80000 Cheque
O1005 Q1002 50 30-JAN-15 Delivered 01-FEB-15 03-FEB-15 70000 Cheque
O1006 Q1008 75 20-FEB-15 Delivered 22-FEB-15 23-FEB-15 161250 Cash
我尝试了下面的代码来获取Year,并仅选择2015年之前的值并按年份分组。
SELECT pymtmode, COUNT(*) as pymtcount
FROM orders
GROUP BY to_char(pymtdate, 'Year')
HAVING to_char(pymtdate,'Year')<2015 AND count(*)>1
我了解到SELECT语句中也应提及按列/函数分组。但是这个问题及其预期结果与之无关。明确基本解释会有所帮助
预期结果
PYMTMODE PYMTCOUNT
Cash 2
谢谢!
答案 0 :(得分:0)
您需要选择pymtmode
的预期结果,因此您必须GROUP BY pymtmode
而不是GROUP BY to_char(pymtdate, 'Year')
,因为您不需要每年都获得结果,对吧? />
也可以将条件to_char(pymtdate,'Year')<2015
放在WHERE
子句中,以便在聚合之前限制行:
SELECT pymtmode, COUNT(*) as pymtcount
FROM orders
WHERE EXTRACT(YEAR FROM pymtmode) < 2015
GROUP BY pymtmode
HAVING count(*) > 1
答案 1 :(得分:0)
为此,我强烈建议使用直接日期比较:
SELECT o.pymtmode, COUNT(*) as pymtcount
FROM orders o
WHERE o.pymtdate < DATE '2015-01-01'
GROUP BY o.pymtmode
HAVING COUNT(*) > 1;
注意:
o
)是一个好习惯。答案 2 :(得分:0)
select pymtmode, count(pymtmode) pymtcount from orders where extract(year from to_date(pymtdate))<2015 group by pymtmode having count(pymtmode)>1;