Solved

sql server 2008 query

Posted on 2013-12-18
3
499 Views
Last Modified: 2013-12-18
Hi experts,

I'm using Sql Server 2008 with SSMS.

I have a table that looks like this:

my table
This is the script to create this table:

CREATE TABLE [dbo].[MyTestTable1](
	[DepartmentID] [int] NULL,
	[DepartmentName] [varchar](50) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
INSERT [dbo].[MyTestTable1] ([DepartmentID], [DepartmentName]) VALUES (1, N'Accounting')
INSERT [dbo].[MyTestTable1] ([DepartmentID], [DepartmentName]) VALUES (2, N'Human Resources')
INSERT [dbo].[MyTestTable1] ([DepartmentID], [DepartmentName]) VALUES (3, N'Finance')
INSERT [dbo].[MyTestTable1] ([DepartmentID], [DepartmentName]) VALUES (4, N'Marketing')

Open in new window


Now I want to write a query that creates a column to my table. The column I'm adding is called Test Column.  This column concatenates DepartmentID a dash and then DepartmentName.
I'm also creating a DepartmentID value of 0 and adding it to the DepartmentID column and this value should contain the word All as the value in the TestColum.

So my desired result should look like this drawing which I drew in Excel.

my desired result set
The red items in the drawing are what I  want to add.

Query 1

So I have this query:

SELECT 0 AS DepartmentID
UNION
SELECT m.DepartmentID
FROM [TestDatabase].[dbo].[MyTestTable1] m

When I run it I get this:

query 1
Query 2

So now I just want to add the Concatenated column called TestColumn to Query 1.
So i have this query:

SELECT 0 AS DepartmentID
UNION
SELECT m.DepartmentID
      ,Cast(m.DepartmentID As varchar) + ' - ' + m.DepartmentName As TestColumn
FROM [TestDatabase].[dbo].[MyTestTable1] m

But this query gives me this error:

All queries combined using a UNION, INTERSECT or EXCEPT operator must have an equal number of expressions in their target lists.


Anyone know how I can fix Query 2 so it gives my desired result set as shown in the image above?
0
Comment
Question by:maqskywalker
3 Comments
 
LVL 11

Accepted Solution

by:
Simone B earned 250 total points
ID: 39727915
SELECT 0 AS DepartmentID, 'All' as TestColumn
UNION
SELECT m.DepartmentID
      ,Cast(m.DepartmentID As varchar) + ' - ' + m.DepartmentName
FROM [TestDatabase].[dbo].[MyTestTable1] m

your first select has only 1 column, and your second select has 2. They both have to have the same number of columns, so just add the 'All' in the first select

(edited, sorry)
0
 
LVL 3

Expert Comment

by:13Shadow
ID: 39727951
That is because the second column for your first record is null. Try something like this:
SELECT 0 AS DepartmentID, NULL
UNION
SELECT m.DepartmentID
      ,Cast(m.DepartmentID As varchar) + ' - ' + m.DepartmentName As TestColumn
FROM [TestDatabase].[dbo].[MyTestTable1] m
0
 
LVL 69

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 250 total points
ID: 39727955
If you're going to need this in multiple places, I'd add the test column definition to the main table as a computed, non-persisted column.

Also, note that "UNION ALL" makes more sense in this case than "UNION", and that you an ORDER BY is required to guarantee that Dept "0" comes out first.


USE [TestDatabase]
ALTER TABLE [dbo].[MyTestTable1] ADD TestColumn AS CAST(DepartmentID AS varchar(10)) + ' - ' + DepartmentName



SELECT 0 AS DepartmentID, 'All' AS TestColumn
UNION ALL
SELECT m.DepartmentID, m.TestColumn
FROM [TestDatabase].[dbo].[MyTestTable1] m
ORDER BY DepartmentID
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question