This is a read-only copy of the MariaDB Knowledgebase generated on 2024-11-16. For the latest, interactive version please visit https://mariadb.com/kb/.

Inconsistent results observed with union and distinct

Hi

I have UNION of 2 queries(query1 and query2). When i run 2 queries separately they yield 4 records each. UNION ALL is expected to yield 8 records(all 8 records being distinct) instead it yields just 5 records.

Following is the combined query which has 2 queries joined by union all :

select distinct if (period_type = '1', 'Month', if(period_type = '2', 'Week', period_type)) as value, if (period_type = '1', 'Month', if(period_type = '2', 'Week', period_type)) as description, ('Y') as planningonly from gl_periods union all select distinct if (period_type = '1', 'Fiscal_Month', if(period_type = '2', 'Fiscal_Week', if(period_type = 'Quarter', 'Fiscal_Quarter', if(period_type = 'Year', 'Fiscal_Year', null)))) as value, if (period_type = '1', 'Fiscal_Month', if(period_type = '2', 'Fiscal_Week', if(period_type = 'Quarter', 'Fiscal_Quarter', if(period_type = 'Year', 'Fiscal_Year', null)))) as description, ('Y') as planningonly from gl_periods

Not sure what is the issue in this. Can someone explain this behaviour.

Thanks in advance, Harinath

Content reproduced on this site is the property of its respective owners, and this content is not reviewed in advance by MariaDB. The views, information and opinions expressed by this content do not necessarily represent those of MariaDB or any other party.