Solved

CrossTab View?

Posted on 2016-08-16
3
36 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
3 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 250 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 69

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 250 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

808 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