Solved

SQL Grouping Data from multiple tables.

Posted on 2009-07-15
12
155 Views
Last Modified: 2012-05-07
Need assistance on my problem below...
Tables involved = 4 (tblUserData, tblDocuments, tblDocIssues, tblIssues)
Columns involved = 4.....(Doc #, Beg bates) from tblUserData, (Volume) from tblDocuments, and (IssueName) from tblIssues.

Code is below

Issue: I would like to group by Doc # or Beg bates but would like to have certain valuesfrom the multivalue field (IssueName) into an alias field called NOTES.
SELECT [Doc #], [Beg bates], Volume, [IssueName]
FROM (dbo.tblDocuments INNER JOIN dbo.tblUserData ON dbo.tblDocuments.DocGUID = dbo.tblUserData.MasterGUID) INNER JOIN (dbo.tblDocIssues INNER JOIN dbo.tblIssues ON dbo.tblDocIssues.IssueGUID = dbo.tblIssues.IssueGUID) ON dbo.tblUserData.MasterGUID = dbo.tblDocIssues.DocGUID
WHERE ([IssueName] = '02 - Not Responsive') OR ([IssueName] = '01 - Responsive') OR ([IssueName] = 'Group Tag Test') OR ([IssueName] = 'Privilege')
ORDER BY [Doc #]

Open in new window

SQL-grouping-Data.xls
0
Comment
Question by:gridline
[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
  • 5
12 Comments
 
LVL 42

Expert Comment

by:pcelba
ID: 24863836
First of all you have to create function for IssueName concatenation:

CREATE FUNCTION MyFceIssueConcat (@DocNr  varchar(100))
RETURNS varchar(1000)
AS
BEGIN
DECLARE @IssueNames  varchar(1000)
SELECT @IssueNames = COALESCE(@IssueNames + ',', '') + CAST([IssueName] AS varchar(100))
  FROM dbo.tblDocuments
  INNER JOIN dbo.tblUserData ON dbo.tblDocuments.DocGUID = dbo.tblUserData.MasterGUID
  INNER JOIN dbo.tblDocIssues ON dbo.tblUserData.MasterGUID = dbo.tblDocIssues.DocGUID
  INNER JOIN dbo.tblIssues ON dbo.tblDocIssues.IssueGUID = dbo.tblIssues.IssueGUID
 WHERE [Doc #] = @DocNr

RETURN @IssueNames  
END

And now you may use it in the query:

SELECT [Doc #], [Beg bates], Volume, MyFceIssueConcat([Doc #]) Notes
  FROM dbo.tblDocuments 
 INNER JOIN dbo.tblUserData ON dbo.tblDocuments.DocGUID = dbo.tblUserData.MasterGUID
 INNER JOIN dbo.tblDocIssues ON dbo.tblUserData.MasterGUID = dbo.tblDocIssues.DocGUID
 INNER JOIN dbo.tblIssues ON dbo.tblDocIssues.IssueGUID = dbo.tblIssues.IssueGUID
 WHERE [IssueName] IN ('02 - Not Responsive', '01 - Responsive', 'Group Tag Test', 'Privilege')
 GROUP BY [Doc #], [Beg bates], Volume
 ORDER BY [Doc #]

Open in new window

0
 

Author Comment

by:gridline
ID: 24864417
I get an error from your code
"Msg 195, Level 15, State 10, Line 1
'test' is not a recognized built-in function name."
0
 
LVL 42

Expert Comment

by:pcelba
ID: 24864493
Where is the 'test' word used in your code? Did you add it somewhere?
0
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)

 

Author Comment

by:gridline
ID: 24864527
i just changed your "MyFceIssueConcat" to "test".
0
 
LVL 42

Expert Comment

by:pcelba
ID: 24864536
MyFceIssueConcat is used on two places. Did you change it on both?
0
 
LVL 42

Expert Comment

by:pcelba
ID: 24864566
The first place is the function creation, the second place is the query.
0
 

Author Comment

by:gridline
ID: 24864613
Ok..i was able to run your function and "Command(s) completed successfully" but i didn't want to modify anything.. Even so..I then re-ran the query you created after the function but the results are same

Msg 195, Level 15, State 10, Line 1
'MyFceIssueConcat' is not a recognized built-in function name.

SELECT [Doc #], [Beg bates], Volume, [IssueName]
FROM (dbo.tblDocuments INNER JOIN dbo.tblUserData ON dbo.tblDocuments.DocGUID = dbo.tblUserData.MasterGUID) INNER JOIN (dbo.tblDocIssues INNER JOIN dbo.tblIssues ON dbo.tblDocIssues.IssueGUID = dbo.tblIssues.IssueGUID) ON dbo.tblUserData.MasterGUID = dbo.tblDocIssues.DocGUID
WHERE ([IssueName] = '02 - Not Responsive') OR ([IssueName] = '01 - Responsive') OR ([IssueName] = 'Group Tag Test') OR ([IssueName] = 'Privilege')
ORDER BY [Doc #]
0
 
LVL 42

Expert Comment

by:pcelba
ID: 24864661
So, please change the function call to dbo.MyFceIssueConcat():
SELECT [Doc #], [Beg bates], Volume, dbo.MyFceIssueConcat([Doc #]) Notes
  FROM dbo.tblDocuments 
 INNER JOIN dbo.tblUserData ON dbo.tblDocuments.DocGUID = dbo.tblUserData.MasterGUID
 INNER JOIN dbo.tblDocIssues ON dbo.tblUserData.MasterGUID = dbo.tblDocIssues.DocGUID
 INNER JOIN dbo.tblIssues ON dbo.tblDocIssues.IssueGUID = dbo.tblIssues.IssueGUID
 WHERE [IssueName] IN ('02 - Not Responsive', '01 - Responsive', 'Group Tag Test', 'Privilege')
 GROUP BY [Doc #], [Beg bates], Volume
 ORDER BY [Doc #]

Open in new window

0
 

Author Comment

by:gridline
ID: 24864715
I didn't want all values of Issue names associated to a doc # concatenated into one column tho. I only wanted the values that i was initially searching for.
0
 
LVL 42

Accepted Solution

by:
pcelba earned 500 total points
ID: 24864844
Oh yes, it is just a small change of the function:
ALTER FUNCTION dbo.MyFceIssueConcat (@DocNr  varchar(100))
RETURNS varchar(1000)
AS
BEGIN
DECLARE @IssueNames  varchar(1000)
SELECT @IssueNames = COALESCE(@IssueNames + ',', '') + CAST([IssueName] AS varchar(100))
  FROM dbo.tblDocuments 
  INNER JOIN dbo.tblUserData ON dbo.tblDocuments.DocGUID = dbo.tblUserData.MasterGUID
  INNER JOIN dbo.tblDocIssues ON dbo.tblUserData.MasterGUID = dbo.tblDocIssues.DocGUID
  INNER JOIN dbo.tblIssues ON dbo.tblDocIssues.IssueGUID = dbo.tblIssues.IssueGUID
 WHERE [Doc #] = @DocNr AND
       [IssueName] IN ('02 - Not Responsive', '01 - Responsive', 'Group Tag Test', 'Privilege')
 ORDER BY [IssueName]
 
RETURN @IssueNames  
END

Open in new window

0
 

Author Closing Comment

by:gridline
ID: 31603912
Brilliant!! this works..just one last question..will adding and removing functions out of the database ok?
0
 
LVL 42

Expert Comment

by:pcelba
ID: 24871741
Yes, you have to create functions for this approach. To remove you may simply use

DROP FUNCTION dbo.MyFceIssueConcat

Another possibility is to use FOR XML clause as mentioned here: http://weblogs.sqlteam.com/mladenp/archive/2007/06/01/60220.aspx
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Better way to filter date  - Query 5 41
Microsoft Access VBA - allocate a colour 3 63
EF5 How do I stop pre-compiled views? 8 47
Call to SQL server times out 5 50
Azure Functions is a solution for easily running small pieces of code, or "functions," in the cloud. This article shows how to create one of these functions to write directly to Azure Table Storage.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

734 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