Need help with a GROUP BY query

  • I need to join three tables

    1. Page table

    2. Preamble table

    3. Attribute table

    Page to Preamble is one to many relationship.

    Page to Attribute is one to many relationship.

    I would like to be able to group correctly by: Page, Preamble, Region, Attribute Type, Attribute ID.

    I am seeing the results of the query below, as the contents of text file I attached to this post:

    DECLARE @StorePageID INT = 23;

    DECLARE @SPPreambleID INT = 20;

    SELECT DISTINCT

    MAX([sp].[StorePageID]) [PageID]

    , [sp].[Title]

    , [spp].[SPPreambleID] [PreambleID]

    , [spp].Image

    , MAX([spp].[RegionID]) [RegionID]

    , MAX([spa].[SPAttributeRefID]) [AttributeType]

    , [spa].[AttributeID]

    FROM [dbo].[StorePage] [sp]

    LEFT JOIN [dbo].[StorePagePreamble] [spp]

    ON [sp].[StorePageID] = [spp].[StorePageID]

    LEFT JOIN [dbo].[StorePageAttribute] [spa]

    ON [spa].[StorePageID] = [sp].[StorePageID]

    WHERE [sp].[StorePageID] = ISNULL(@StorePageID, [sp].[StorePageID])

    GROUP BY

    [sp].[Title]

    , [spp].[SPPreambleID]

    , [spp].Image

    , [spa].[AttributeID]

    ORDER BY MAX(spp.RegionID), MAX([spa].[SPAttributeRefID])

  • Based on what you said about grouping, the results look OK to me. This is difficult to troubleshoot when we don't know what the source data looks like and we do not know how you want the results presented.

    The absence of evidence is not evidence of absence
    - Martin Rees
    The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
    - Phil Parkin

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply