Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

CrossTab View?

Posted on 2016-08-16
3
Medium Priority
?
76 Views
Last Modified: 2016-08-19
In my SQL DB I have a table 'tblInstallPay_TaxAuthorities'.  The pertinent fields in the table are 'InstallPayHdrID' (Int) and 'Parcel' nvarchar(30).

A given InstallPayHDrID may appear multiple times.  For a given InstallPayHdrID a Parcel may appear mulltiple times.

'ID'     'Parcel'
123    Parcel1
123   Parcel2
123   Parcel1
123   Parcel3


I have reporting requirement to list all of the Parcels associated with an InstallPayHdrID on one line.

So if InstallPayHdrID 123 has 3 parcels associated it would show as
123     Parcel1 Parcel2  Parcel3

My initial plan was to write a routine reading a view that groups the InstallPayHDrID and Parcels.

For each ID I would lop thru building a sting of the associated parcels.

I'm wondering if there is a way to create a view that will do this for me.  Each returned line in the view would have two fields.
1 The InstallPayHDrID
2. A string of all the parcels associated with that InstallPayHdrID, with each Parcel only being listed once.

Can it be done?  If not I'll just go back to my looping routine.
0
Comment
Question by:mlcktmguy
[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
3 Comments
 
LVL 66

Accepted Solution

by:
Jim Horn earned 1000 total points
ID: 41758102
To pull this off you can mimic the T-SQL code in the article T-SQL:  Normalized data to a single comma delineated string and back, top section, replacing all the table and column names to match your situation, and replacing the comma with a single space.
0
 
LVL 70

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 1000 total points
ID: 41758174
I strongly suggest using an inline-table-valued function for best performance and ease of use, which you use a CROSS APPLY to invoke when needed  For example:

SET ANSI_NULLS ON;
SET QUOTED_IDENTIFIER ON;
GO
CREATE FUNCTION dbo.Get_InstallPay_Parcel_List
(
    @ID int
)
RETURNS TABLE
AS
RETURN (
    SELECT '' + Parcel AS Parcel_List
    FROM (
        SELECT DISTINCT Parcel
        FROM dbo.tblInstallPay_TaxAuthorities
        WHERE ID = @ID
    ) AS derived
    ORDER BY Parcel
    FOR XML PATH('')
);
GO

--then to use the function:
SELECT mt.*, gipl.Parcel_List
FROM dbo.main_table mt
CROSS APPLY dbo.Get_InstallPay_Parcel_List (mt.ID) gipl
GO
0
 
LVL 1

Author Closing Comment

by:mlcktmguy
ID: 41763124
Thank you
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

610 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