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
Solved

CrossTab View?

Posted on 2016-08-16
3
38 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

860 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