?
Solved

SQL Server 2000 - query help

Posted on 2014-01-14
10
Medium Priority
?
560 Views
Last Modified: 2014-01-15
SQL Server 2000 - I have this query:

SELECT JobTitle, LastName + ',' + FirstName as 'Name', NUM , Shift
FROM [AIO_Test_Results].[dbo].[EmployeeInfo] Where [Group] = 'PRODUCTION'
and Dept = 'BRAZE' and TRAIN IS NULL and Jobtitle = 'LEAD'

LEAD    Name1   001060     1
LEAD    Name2   004545     2

 results attached.

I want result to appear in single line:

LEAD    Name1   001060     1  LEAD    Name2   004545     2



any idea?
Capture.PNG
0
Comment
Question by:mani_sai
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 16

Expert Comment

by:Surendra Nath
ID: 39780583
I think you are looking for generating report a two columunar report having both of them in the same line.... if that is the case then doing that in the SQL is not a good thing....

But to full fill your requirement you can do that like below, but remember this works only if you have two rows.

SELECT E1.JobTitle, E1.LastName + ',' + E1.FirstName as 'Name', E1.NUM , E1.Shift
,E2.JobTitle, E2.LastName + ',' + E2.FirstName as 'Name', E2.NUM , E2.Shift
FROM [AIO_Test_Results].[dbo].[EmployeeInfo] E1,
[AIO_Test_Results].[dbo].[EmployeeInfo]  E2
Where E1.[Group] = 'PRODUCTION'
and E1.Dept = 'BRAZE' 
and E1.TRAIN IS NULL 
and E1.Jobtitle = 'LEAD'
AND E2.[Group] = 'PRODUCTION'
and E2.Dept = 'BRAZE' 
and E2.TRAIN IS NULL 
and E2.Jobtitle = 'LEAD'
AND E1.LastName+','+E1.FirstName  = 'Landum,my..'
AND E2.LastName+','+E2.FirstName  = 'Jackson,bel..'

Open in new window

0
 
LVL 25

Accepted Solution

by:
chaau earned 2000 total points
ID: 39780619
You can't use PIVOT, as it was introduced in SQL Server 2005. In your situation you need to use CASE with GROUP BY, like this:
SELECT JobTitle, 
MAX(CASE SHIFT WHEN 1 THEN LastName + ',' + FirstName END) as 'Name1', 
MAX(CASE SHIFT WHEN 1 THEN NUM END) AS NUM1, 
MAX(CASE SHIFT WHEN 1 THEN SHIFT END) AS SHIFT1, 
MAX(CASE SHIFT WHEN 2 THEN LastName + ',' + FirstName END) as 'Name2', 
MAX(CASE SHIFT WHEN 2 THEN NUM END) AS NUM2, 
MAX(CASE SHIFT WHEN 2 THEN SHIFT END) AS SHIFT2 
FROM [AIO_Test_Results].[dbo].[EmployeeInfo] Where [Group] = 'PRODUCTION'
and Dept = 'BRAZE' and TRAIN IS NULL and Jobtitle = 'LEAD'
GROUP BY Jobtitle

Open in new window

The query can be very easily extended to support more shifts
0
 
LVL 8

Author Comment

by:mani_sai
ID: 39780691
Thanks.

That worked for LEAD. I also need to display for employees:  (jobtitle <> LEAD)

SELECT JobTitle,
CASE SHIFT WHEN 1 THEN LastName + ',' + FirstName END as 'Name1',
CASE SHIFT WHEN 1 THEN NUM END AS NUM1,
CASE SHIFT WHEN 1 THEN SHIFT END AS SHIFT1,
CASE SHIFT WHEN 2 THEN LastName + ',' + FirstName END as 'Name2',
CASE SHIFT WHEN 2 THEN NUM END AS NUM2,
CASE SHIFT WHEN 2 THEN SHIFT END AS SHIFT2
FROM [AIO_Test_Results].[dbo].[EmployeeInfo] Where [Group] = 'PRODUCTION'
and Dept = 'BRAZE' and TRAIN IS NULL and Jobtitle <> 'LEAD'


I am getting results with null on some rows... is ti possible to eliminate NULL..

Results attached.
Capture.PNG
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 25

Expert Comment

by:chaau
ID: 39780715
What do you want to be displayed in place of NULLs?
0
 
LVL 8

Author Comment

by:mani_sai
ID: 39780732
I want to display like this:

please see attached file
Capture.PNG
0
 
LVL 25

Expert Comment

by:chaau
ID: 39780746
How do you know how to "marry" name1 with name2? Is there another field that allows you to identify that they belong to the same row?
0
 
LVL 8

Author Comment

by:mani_sai
ID: 39780758
There is no relation between Name1 and Name2.

First shift will contain any no of employee  based on the query (9 in this example.)

Second Shift will contain any no of employees again based on the query ( 6 in this example.)

thanks for looking into it.
0
 
LVL 25

Expert Comment

by:chaau
ID: 39780787
It is very hard to do in SQL Server 2000. How long are you stuck with this version for? Any plans to upgrade?
0
 
LVL 8

Author Comment

by:mani_sai
ID: 39782226
company is not planning to upgrade anytime soon...

Can we do it in multiple steps using temp tables....

we have minimal data on the tables, so performance is not a concern...
0
 
LVL 8

Author Comment

by:mani_sai
ID: 39782647
OK,  I got the results using temp table. Thanks for your help...
0

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Suggested Courses

771 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