Note that there are some explanatory texts on larger screens.

plurals
  1. PODifficult T-SQL Join/Pivot
    primarykey
    data
    text
    <p>The original way I worded this question would be unhelpful to most, so I've reworked it significantly.</p> <p>I have a legacy database, whose schema <strong>I have no control over</strong> (nor ever have). The db schema was designed agnostic of the provider for consumption by unmanaged C++, so there are no FK constraints, etc. I come along and need to consume this from .NET, and also need to present the data much differently than it was originally intended.</p> <p>Here is the schema of the relevant tables (I've bolded/asteriskifed what amount to the keys for this question):</p> <pre><code>CREATE TABLE [dbo].[Formats]( [GroupName] [varchar](255) NOT NULL, **[BatchID] [bigint] NOT NULL,** [SeriesNumber] [bigint] NOT NULL, **[SequenceID] [bigint] NOT NULL,** [SeqNum] [int] NOT NULL, [FormatName] [varchar](50) NOT NULL CREATE TABLE [dbo].[Fields]( [GroupName] [varchar](255) NOT NULL, **[BatchID] [bigint] NOT NULL,** **[SequenceID] [bigint] NOT NULL,** [SeqNum] [int] NOT NULL, **[FieldName] [varchar](50) NOT NULL,** [FieldDisplayName] [varchar](50) NOT NULL, [FieldValue] [ntext] NULL </code></pre> <p>There is a 1:Many relationship between Formats:Fields.</p> <p>I need to query for a list of format rows. The challenges are several: + There is a compound foreign key. + I need to filter the formats to a subset that contains related Fields. + The related fields must match an arbitrary number of filter criteria before being inner-joined to the formats.</p> <p>So to walk through this: The Stored Proc receives the following table valued parameter:</p> <pre><code>_FieldDisplayName_ Field_Value Field 1 001 Field 2 002 </code></pre> <p>It then needs to filter the Fields table by those values.</p> <p>Since I needed to use ALL of the criteria and not just one of them, I did not know how work with this to retrieve something useful to join with, when I didn't know how many values I'd have, or what the FieldDisplayName values would be.</p> <p>@Joe Phillips was smart enough to see through the chaos and suggest that I needed to pivot the table. That's this:</p> <p><strong>Edit:</strong> The following is working to pivot the data, but I am not sure it is ideal. Still waiting on other ideas before supplying an answer.</p> <p>Instead of using a TVP, I'm using a temp table and dynamic SQL (and yes, I see the injection vulnerability):</p> <pre><code>IF OBJECT_ID('[tempdb].[dbo].[#FilterFields]') is not null Drop Table dbo.#FilterFields CREATE Table #FilterFields (FieldDisplayName varchar(50), FieldValue nvarchar(max)) /* Just some example data */ INSERT INTO #FilterFields (FieldDisplayName, FieldValue) VALUES ('PTNUM', '011') INSERT INTO #FilterFields (FieldDisplayName, FieldValue) VALUES('SITENUM', '001') Declare @column_names varchar(max) Declare @dynamic_pivot_query as varchar(max) SELECT @column_names = Stuff((SELECT DISTINCT ',[' + FieldDisplayName + ']' FROM #FilterFields FOR xml path('')),1,1,'') PRINT(@column_names) SET @dynamic_pivot_query = 'select * ' + 'from tempdb.dbo.#FilterFields ' + 'Pivot ( MIN( FieldValue ) FOR FieldDisplayName IN (' + @column_names + ') ) as P' PRINT(@dynamic_pivot_query) EXEC(@dynamic_pivot_query) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload