如何在mysql的列中找到缺少的月份?
Posted
技术标签:
【中文标题】如何在mysql的列中找到缺少的月份?【英文标题】:How to find the missing month in a column in mysql? 【发布时间】:2017-01-24 10:28:03 【问题描述】:这是我的 view_remit。我需要确定month_paid
列中缺少的月份。
我使用此代码获取此表,但我不知道如何确定缺少的月份。
SELECT RemitNo, PEN, Employer, month_paid, Total_PBR_Amt
FROM view_remit
JOIN (
SELECT 'JAN' as month_paid,'2016-01-01' as start,'2016-01-31' as end UNION ALL
SELECT 'FEB' as month_paid,'2016-02-01' as start,'2016-02-29' as end UNION ALL
SELECT 'MAR' as month_paid,'2016-03-01' as start,'2016-03-31' as end UNION ALL
SELECT 'APR' as month_paid,'2016-04-01' as start,'2016-04-30' as end UNION ALL
SELECT 'MAY' as month_paid,'2016-05-01' as start,'2016-05-31' as end UNION ALL
SELECT 'JUN' as month_paid,'2016-06-01' as start,'2016-06-30' as end UNION ALL
SELECT 'JUL' as month_paid,'2016-07-01' as start,'2016-07-31' as end UNION ALL
SELECT 'AUG' as month_paid,'2016-08-01' as start,'2016-08-31' as end UNION ALL
SELECT 'SEP' as month_paid,'2016-09-01' as start,'2016-09-30' as end UNION ALL
SELECT 'OCT' as month_paid,'2016-10-01' as start,'2016-10-31' as end UNION ALL
SELECT 'NOV' as month_paid,'2016-11-01' as start,'2016-11-30' as end UNION ALL
SELECT 'DEC' as month_paid,'2016-12-01' as start,'2016-12-31' as end
) M
ON view_remit.AP_From <= M.end
AND view_remit.AP_To >= M.start
ORDER BY PEN, AP_From
我需要得到这个输出。
|PEN|Employer|month|
| 1 | a | MAR |
| 1 | a | JUN |
| 1 | a | JUL |
| 1 | a | SEP |
| 1 | a | OCT |
| 1 | a | NOV |
非常感谢任何帮助。 提前谢谢你。
【问题讨论】:
检查这个链接可能你会明白***.com/questions/27600863/… 此类问题通常最好在应用程序级代码中处理。 【参考方案1】:由于我的声誉等级,我仍然无法发表评论。一旦您回答我的问题,我会立即更正此答案:
您能否放置更完整的表格示例:缺少 AP_From 和 AP_To 列。
我不明白您如何将 Pen 和雇主列与缺少的月份联系起来。 无论如何,根据您桌子上的可用数据来获取缺失的月份,我将按照以下步骤进行:
-- Just for sample query purpose
IF OBJECT_ID('tempdb..#view_remit') is not null
DROP TABLE #view_remit
CREATE TABLE #view_remit (
RemitNo INT,
PEN INT,
Employer VARCHAR(10),
wmonth VARCHAR(3),
Total_PBR_Amt FLOAT,
AP_Date DATE
)
INSERT INTO #view_remit VALUES
(1,1,'a','JAN',200, '2016-01-20'),
(1,1,'a','FEV',200, '2016-02-12'),
(1,1,'a','APR',200, '2016-04-25'),
(1,1,'a','AUG',200, '2016-08-02'),
(1,1,'a','DEC',200, '2016-12-24')
-- Your working month table
IF OBJECT_ID('tempdb..#table_month') is not null
DROP TABLE #table_month
CREATE TABLE #table_month (
wmonth VARCHAR(3),
DateBegin DATE,
EndBegin DATE
)
INSERT INTO #table_month VALUES
('JAN', '2016-01-01', '2016-01-31'),
('FEV', '2016-02-01', '2016-02-29'),
('MAR', '2016-03-01', '2016-03-31'),
('APR', '2016-04-01', '2016-04-30'),
('MAI', '2016-05-01', '2016-05-31'),
('JUN', '2016-06-01', '2016-06-30'),
('JUL', '2016-07-01', '2016-07-31'),
('AUG', '2016-08-01', '2016-08-31'),
('SET', '2016-09-01', '2016-09-30'),
('OCT', '2016-10-01', '2016-10-31'),
('NOV', '2016-11-01', '2016-11-30'),
('DEC', '2016-12-01', '2016-12-31')
SELECT PEN, Employer, tm.wmonth
FROM #table_month tm
LEFT JOIN #view_remit vr
ON vr.wmonth = tm.wmonth
WHERE vr.wmonth is null
【讨论】:
很抱歉没有提供足够的数据。 我猜这些是葡萄牙语的缩写,但 OP 是英文的。值得注意的是,upper(date_format(start, '%b'))
避免了硬编码和可能不正确的 month
列的需要,并将尊重本地化。【参考方案2】:
通常您会为此使用EXCEPT
set operator。设置一个名为 months
的表,其中包含所有月份,然后选择所有这些月份除了view_remit
中的那些月份。
select month from months
except
select distinct month from view_remit
但是 mysql 不支持EXCEPT
。相反,请使用 left outer join 来获取不在 view_remit 中的月份。
select distinct months.month
from months
left join view_remit vr on months.month = vr.month
where vr.month is null;
【讨论】:
是不是因为有很多PEN
我需要得到他没有看到每个 PEN
的月份,当我使用 group by 时,什么也没发生
@Clorae 抱歉,我不确定这意味着什么。
我需要的是我需要根据PEN
获得不在视图中的月份。在我的例子中。
@Clorae 我明白了。 PEN
和 Employer
将为空,因为您只能返回不匹配的月份。我不确定如何在 SQL 中执行您要求的操作,但我确信这是可能的。以上是关于如何在mysql的列中找到缺少的月份?的主要内容,如果未能解决你的问题,请参考以下文章