Solved

Query to return values side by side from one table

Posted on 2011-03-14
7
214 Views
Last Modified: 2012-05-11
Hi There,

I am trying to put 2 custom values side by side in the query.

The first custom value which is from the Projects Table (2 segment code xxxxx-xxx) and the second custom value is from the Tasks Table (xxx). These custom values are stored in the
CustomValue Table.



Thanks.


 
SELECT     EMPLOYEE.FIRST + ' ' + EMPLOYEE.LAST AS NAME, CLIENT.NAME AS CLIENT, PROJECT.NAME AS PROJECT, TASK.NAME AS TASK, 
                      [GROUP].NAME AS TEAM, TRANS.DATE, CUSTOMVALUE.VALUE
FROM         PROJECT INNER JOIN
                      TRANS ON PROJECT.ID = TRANS.PROJECT INNER JOIN
                      TASK ON TRANS.TASK = TASK.ID INNER JOIN
                      EMPLOYEE ON TRANS.EMPLOYEE = EMPLOYEE.ID INNER JOIN
                      [GROUP] ON TRANS.[GROUP] = [GROUP].ID LEFT OUTER JOIN
                      CUSTOM INNER JOIN
                      CUSTOMVALUE ON CUSTOM.ID = CUSTOMVALUE.CUSTOM INNER JOIN
                      CUSTOMTEMPLATE ON CUSTOM.CUSTOMTEMPLATE = CUSTOMTEMPLATE.ID ON PROJECT.ID = CUSTOM.LINKID AND 
                      TASK.ID = CUSTOM.LINKID LEFT OUTER JOIN
                      CLIENT ON TRANS.PROJECT = CLIENT.ID
WHERE     (CUSTOMVALUE.VALUE IS NOT NULL)
ORDER BY NAME

Open in new window

0
Comment
Question by:jnsimex
[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
7 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 35130115
could you please clarify with data samples what you have in the tables (1-2 records), and the requested output?
to avoid guessing, it's not 100% clear...
0
 

Author Comment

by:jnsimex
ID: 35130165
Sure, not a problem.

The output of the query return this:

Alex Gil,A: Ripleys Williamsburg,CIP,Mechanical Design,S / C / Eng,2010-08-13 00:00:00.000,17700-CPZ
Alex Gil,A: Ripleys Williamsburg,CIP,Mechanical Design,S / C / Eng,2010-08-13 00:00:00.000,710

The first custom value is "17700-CPZ" and the second custom value is "710"

I would like the output of the query to return this instead:

Alex Gil,A: Ripleys Williamsburg,CIP,Mechanical Design,S / C / Eng,2010-08-1300:00:00.000,17700-CPZ,710
0
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 35130209
Just add the field to your result set

SELECT     EMPLOYEE.FIRST + ' ' + EMPLOYEE.LAST AS NAME, CLIENT.NAME AS CLIENT, PROJECT.NAME AS PROJECT, TASK.NAME AS TASK,
                      [GROUP].NAME AS TEAM, TRANS.DATE, CUSTOMVALUE.VALUE, PROJECT.VALUE
FROM         PROJECT INNER JOIN
                      TRANS ON PROJECT.ID = TRANS.PROJECT INNER JOIN
                      TASK ON TRANS.TASK = TASK.ID INNER JOIN
                      EMPLOYEE ON TRANS.EMPLOYEE = EMPLOYEE.ID INNER JOIN
                      [GROUP] ON TRANS.[GROUP] = [GROUP].ID LEFT OUTER JOIN
                      CUSTOM INNER JOIN
                      CUSTOMVALUE ON CUSTOM.ID = CUSTOMVALUE.CUSTOM INNER JOIN
                      CUSTOMTEMPLATE ON CUSTOM.CUSTOMTEMPLATE = CUSTOMTEMPLATE.ID ON PROJECT.ID = CUSTOM.LINKID AND
                      TASK.ID = CUSTOM.LINKID LEFT OUTER JOIN
                      CLIENT ON TRANS.PROJECT = CLIENT.ID
WHERE     (CUSTOMVALUE.VALUE IS NOT NULL)
ORDER BY NAME
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 18

Expert Comment

by:lludden
ID: 35130908
Are there just 2, or will there be different values for each row?

If there are a variable number, then it will be difficult to do in separate fields.  You could combine all of the custom values for each person into a single delimited field.

If that is acceptable, create a function that takes a custom.ID and returns a comma delimited list of customvalue.value, then call that function in the query instead of doing a join to the customvalue table.
0
 
LVL 41

Expert Comment

by:Sharath
ID: 35131736
What is your SQL Server version?
0
 

Author Comment

by:jnsimex
ID: 35131867
@lludden

There are more than 2 row and could be different values for each row based on the Project (17700-CPZ) and the task (710)

@Sharath 123

SQL Server Express V.9
0
 
LVL 18

Accepted Solution

by:
lludden earned 500 total points
ID: 35131981
Here is what I have done in SQL to fix a similar situation.  We have multiple drivers each with multiple stops and those stops pass through one or more 'regions' each day.  I needed a query to show each driver and all the regions they pass though on each date.

So first a function to create a list of all the regions for a driver for a day:



CREATE FUNCTION [dbo].[RegionsForRoute] (@RouteNo int, @TransportDate datetime)
RETURNS varchar(200) AS  
BEGIN
Declare @RegionList varchar(200)

SELECT     @RegionList = coalesce(@RegionList+',','') + CAST(RegionID AS varchar(10))
FROM Transport
WHERE [TransportDate] = @TransportDate AND [RouteNo] = @RouteNo
GROUP BY RegionID

Return (@RegionList)
END

Then I do my query:

SELECT Employee.Name, Route.RouteDescription, RoutesPerDay.TransportDate,  dbo.RegionsForRoute(RoutesPerDay.RouteID, RoutesPerDay.TransportDate)
FROM RoutesPerDay
    INNER JOIN Employee ON RoutesPerDay.DriverID = Employee.EmployeeID
    INNER JOIN Routes ON RoutesPerDay.RouteID = Routes.RouteID

This returns
Tom Smith, Local Delivery, 2011-03-01, "NE,SW,West"
Bill Wilson, Local Delivery, 2011-03-01, "East"
etc

0

Featured Post

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

752 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