Solved

CrossTab View?

Posted on 2016-08-16
3
34 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:ScottPletcher
ScottPletcher 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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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.

895 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now