我正在尝试运行查询以根据另一个名为pro_details的表中的日期显示名为products的表中的最大值和最小值。查询应显示该特定日期的最多购买产品以及最少产品购买。但是,即使实际日期存在,查询也不会显示给定月份的任何记录!
SELECT
Prod.delivered, MIN(quality) Low, MAX(quality) High
FROM
pro_details prod, products pro
WHERE
pro.ord = pro.ord
AND Prod.delivered = TO_DATE('12-2015','mm-yyyy')
GROUP BY
Prod.delivered
ORDER BY
Prod.delivered DESC;
答案 0 :(得分:0)
SQL日期比较完全。如果您只在日期中包含月份和年份,则比较假定您要对DateTime值中的所有剩余组件使用零。您正在对仅在该月的第一个时刻记录日期的记录进行精确比较。
如果你想要整个月,你需要检查整个范围,从月初到月末:
SELECT Prod.delivered, MIN(quality) Low, MAX(quality) High
FROM pro_details prod, products pro
WHERE pro.ord = pro.ord
AND Prod.delivered >= TO_DATE('12-2015','mm-yyyy')
AND Prod.delivered < TO_DATE('01-2016','mm-yyyy')
GROUP BY Prod.delivered
ORDER BY Prod.delivered DESC;
答案 1 :(得分:0)
你错过了where子句附近的'd',并且还必须将传递的字段转换为'mm-yyyy'字符串格式。 试试这个:
SELECT Prod.delivered, MIN(quality) Low, MAX(quality) High
FROM pro_details prod, products pro
WHERE prod.ord = pro.ord
AND TO_CHAR(Prod.delivered,'mm-yyyy') = '12-2015'
GROUP BY Prod.delivered
ORDER BY Prod.delivered DESC;
修订查询:
SELECT pro.ord, quality
from pro_details prod, products pro
where prod.ord = pro.ord
AND TO_CHAR(Prod.delivered,'mm-yyyy') = '12-2015'
GROUP BY pro.ord, quality
having quality=min(quality) or quality=max(quality)