Avatar of bendecko
bendecko

asked on 

Column Alias Names and UNION error

Hi

I need to execute this SQL

SELECT NULL AS 'county' UNION select distinct [county/department] as county from COUK_hotels WHERE source = 'activehotels' AND [county/department] NOT LIKE '%london%' group by [county/department] having count([county/department]) > 5 order by [county/department]

The first part is to shove a first result 'NULL' into the result set.  Don't ask me why - the ASP code just needs it ok!

If I run each query separately it works fine:

SELECT NULL AS 'county'

and

select distinct [county/department] as county from COUK_hotels WHERE source = 'activehotels' AND [county/department] NOT LIKE '%london%' group by [county/department] having count([county/department]) > 5 order by [county/department]

But when combined I get an error Invalid column name 'county/department'.

Is there some way of encasing the query with some characters to avoid this?
Microsoft SQL Server

Avatar of undefined
Last Comment
bendecko
SOLUTION
Avatar of Nightman
Nightman
Flag of Australia image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Guy Hengel [angelIII / a3]
SELECT NULL AS county
UNION ALL
SELECT [county/department]
FROM COUK_hotels
WHERE source = 'activehotels'
AND [county/department] NOT LIKE '%london%'
GROUP BY [county/department]
HAVING COUNT ([county/department]) > 5
ORDER BY 1
ASKER CERTIFIED SOLUTION
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of bendecko
bendecko

ASKER

Thank you. Fast response and  Great explainations.

Bendecko
Microsoft SQL Server
Microsoft SQL Server

Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.

171K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo