Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9135161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:47:17+00:00 2026-06-17T08:47:17+00:00

I’m creating a stored procedure for data validation that is pretty straight forward. I

  • 0

I’m creating a stored procedure for data validation that is pretty straight forward. I have several sql statements that i wish to run and simply display the results to compare to the source. I combined the sql statements with a union but you can not union together columns of different data types. Hopefully someone will have a suggestion on a better way to proceed?

USE [employee_data]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[data_employee]

AS
BEGIN

declare @LtYear as varchar(10)
set @LtYear = '2012'

SELECT 'Sum of total salary for ' + @LtYear as title, sum([Total_Salary]) as Salary
From [employee_data].[dbo].[employee]
where year = @LtYear

UNION
select 'Overall Average Salary ' + @LatestYear as title, avg([Total_Salary]) as     'Average Salary'
From [employee_data].[dbo].[employee]
where year = @LtYear

SELECT 'Employee Name - 1' as title, first_name+' '+last_name 
From [employee_data].[dbo].[employee]
where year = 2012 --@LtYear
and [First_Name] = 'first'
and [Last_Name] = 'last'

UNION
SELECT 'Individual Employee Experience ' + @LtYear as title, cast([work_Exp]as varchar) as 'Work Experience'
From [employee_data].[dbo].[employee]
where year = @LtYear
and [First_Name] = 'first'
and [Last_Name] = 'last'

I would like to see the results as follows:

Sum of Total Salary: 50000000
Overall Average Salary: 37000
Employee Name – 1: First Last
Individual Employee Experience 2012: 12

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T08:47:18+00:00Added an answer on June 17, 2026 at 8:47 am

    Why not do this flipped sideways so that you don’t have to use unions, you just have to scan the data sideways:

    SELECT [Year] = @LtYear,
      [Sum of Total Salary] = s.s, 
      [Average Salary] = s.a,
      [Employee Name] = e.first_name + ' ' + e.last_name,
      [Work Experience] = CONVERT(VARCHAR(12), e.work_Exp
    FROM employee_data.dbo.employee AS e
    CROSS JOIN
    (
      SELECT SUM(Total_Salary), AVG(Total_Salary)
      FROM employee_data.dbo.employee
      WHERE [Year] = @LtYear
    ) AS s(s,a)
    WHERE e.[Year] = @LtYear
    AND e.First_Name = @first_name
    AND e.Last_Name = @last_name;
    

    Not sure if you really meant to hard-code 'first' and 'last' but I suspect those would be better as parameters, especially if you’re going to reference them multiple times.

    Given that your requirements are driven by Excel, maybe try:

    DECLARE @y CHAR(4);
    SET @y = CONVERT(CHAR(4), @LtYear);
    
    SELECT col1 = 'Sum of total salary for ' + @y, 
      col2 = CONVERT(NVARCHAR(255), sum(Total_Salary) as Salary)
      From employee_data.dbo.employee
      where [year] = @LtYear
    UNION ALL
    SELECT 'Overall Average Salary ' + @y, 
      CONVERT(NVARCHAR(255), avg(Total_Salary))
      From employee_data.dbo.employee
      where [year] = @LtYear
    UNION ALL
    SELECT 'Employee Name - 1', 
      CONVERT(NVARCHAR(255), first_name+' '+last_name)
      From employee_data.dbo.employee
      where [year] = @LtYear
      and First_Name = 'first'
      and Last_Name = 'last'
    UNION ALL
    SELECT 'Individual Employee Experience ' + @y, 
      CONVERT(NVARCHAR(255), work_Exp)
      From employee_data.dbo.employee
      where [year] = @LtYear
      and First_Name = 'first'
      and Last_Name = 'last';
    

    Notice a few changes:

    • dropped all of the aliases – they’re ignored after the first statement in the union anyway.
    • used explicit converts against the second column to a hopefully compatible data type.
    • properly enclosed all keywords (e.g. year) in square brackets. Removed those that aren’t necessary – they only serve to make the query harder to read.
    • created a string version of the year parameter so that it could be appended to the first column without having to perform any conversion.

    Now, you’ve dumbed down the question, and there are more queries that we can’t see. But I still think a better approach would be to ditch the unions against the base table and perform it this way instead (especially if you have 10 or 15 more queries against the same table). This isn’t necessarily going to perform any differently, but it will be a lot easier to maintain.

    ;WITH x AS 
    (
      SELECT 
        y = CONVERT(CHAR(4), @LtYear),
        [ss] = CONVERT(NVARCHAR(255), s.s), 
        [as] = CONVERT(NVARCHAR(255), s.a),
        [en] = CONVERT(NVARCHAR(255), e.first_name + ' ' + e.last_name),
        [we] = CONVERT(NVARCHAR(255), e.work_Exp)
      FROM employee_data.dbo.employee AS e
      CROSS JOIN
      (
        SELECT SUM(Total_Salary), AVG(Total_Salary)
        FROM employee_data.dbo.employee
        WHERE [Year] = @LtYear
      ) AS s(s,a)
      WHERE e.[Year] = @LtYear
      AND e.First_Name = @first_name
      AND e.Last_Name = @last_name
    )
    SELECT 'Sum of total salary for ' + y, [ss] FROM x
    UNION ALL
    SELECT 'Overall average salary for ' + y, [as] FROM x
    UNION ALL
    SELECT 'Employee Name - 1', [en] FROM x
    UNION ALL
    SELECT 'Individual Employee Experience ' + y, [we] FROM x;
    

    Further to that, I think your approach is very troublesome in the first place. You’re really performing data validation by cobbling these queries together and manually pasting the results into Excel? Surely there is a more automated way to do this, and instead of trying to tailor an unwieldy SQL query to conform to your process, you should improve the process…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small JavaScript validation script that validates inputs based on Regex. I
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.