Select single row of data for each ID in Select Statement

mburk1968
mburk1968 used Ask the Experts™
on
I have a need to select a single row of data based on a date range from field UDF_Date_Data for each ID to use as a dataset in my SSRS report. So I need the following Columns. That being said the UDF_FIELD Stores the names of the columns I need "In Bold Below" so I get multiple rows as seen in my data set for an ID so for instance I need all of ID 7482 in one row

'SHPMH.shpm_ref' ,   'JoinFromHdrPKey' ,'FOB1VENDOR', 'FOB1DATE', 'FOB1AMT', 'FOB2VENDOR', 'FOB2DATE', 'FOB2AMT', 'MISC1VENDOR', 'MISC1DATE', 'MISC1AMT', 'MISC2VENDOR', 'MISC2DATE', 'MISC2AMT',  'DUTY1VENDOR', 'DUTY1DATE', 'DUTY1AMT', 'DUTY2VENDOR', 'DUTY2DATE', 'DUTY2AMT',  'INLAND1VENDOR', 'INLAND1DATE', 'INLAND1AMT',  'INLAND2VENDOR', 'INLAND2DATE', 'INLAND2AMT',  'OCN1VENDOR', 'OCN1DATE', 'OCN1AMT', 'OCN2VENDOR',  'OCN2DATE', 'OCN2AMT'
Sample-Data.xlsx
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
EE Topic Advisor
Most Valuable Expert 2014
Awarded 2013
Commented:
basing a where clause of a value returned by a UDF, and that value isn't on every row, means you probaby have to scan the entre table multiple times, and you cannot get indexes help make this efficient becuse of the function call(s)

Looks pretty bad for performance.
3 rows for 3 items of data
If you continue to proceed down this path it is going to be VERY INEFFICIENT
DECLARE @start DATE
DECLARE @end DATE
SET @start = '20170101'
SET @end = '20170401'
;
WITH
      CTEid AS (
                  SELECT
                        JoinFromHdrPKey
                  FROM that_table
                  WHERE UDF_Date_Data >= @start AND DF_Date_Data < @end
            )
,     CTEraw AS (
                  SELECT
                        shpm_ref
                      , JoinFromHdrPKey
                      , UDF_FIELD
                      , UDF_Char_Data
                      , UDF_Date_Data
                      , UDF_Numeric_Data
                  FROM that_table t
                  INNER JOIN CTEid r on t.JoinFromHdrPKey = r.JoinFromHdrPKey
            )
SELECT
      SHPMH.shpm_ref
    , JoinFromHdrPKey
    , max(case when UDF_FIELD = 'FOB1VENDOR' then UDF_Char_Data    end) as FOB1VENDOR
    , max(case when UDF_FIELD = 'FOB1DATE'   then UDF_Date_Data    end) as FOB1DATE
    , max(case when UDF_FIELD = 'FOB1AMT'    then UDF_Numeric_Data end) as FOB1AMT

    , max(case when UDF_FIELD = 'FOB2VENDOR' then UDF_Char_Data    end) as FOB2VENDOR
    , max(case when UDF_FIELD = 'FOB2DATE'   then UDF_Date_Data    end) as FOB2DATE
    , max(case when UDF_FIELD = 'FOB2AMT'    then UDF_Numeric_Data end) as FOB2AMT

    , max(case when UDF_FIELD = 'MISC1VENDOR' then UDF_Char_Data    end) as MISC1VENDOR
    , max(case when UDF_FIELD = 'MISC1DATE'   then UDF_Date_Data    end) as MISC1DATE
    , max(case when UDF_FIELD = 'MISC1AMT'    then UDF_Numeric_Data end) as MISC1AMT

    , max(case when UDF_FIELD = 'MISC2VENDOR' then UDF_Char_Data    end) as MISC2VENDOR
    , max(case when UDF_FIELD = 'MISC2DATE'   then UDF_Date_Data    end) as MISC2DATE
    , max(case when UDF_FIELD = 'MISC2AMT'    then UDF_Numeric_Data end) as MISC2AMT

FROM CTEraw d
GROUP BY
      SHPMH.shpm_ref
    , JoinFromHdrPKey
;

Open in new window

YOu will have to add i n the extra columns but each one you need should be similar to the ones I have provided.

Personally I am concerned about the performance characteristics, especially as I don't know anything bout your UDF

Author

Commented:
I'm receiving the following "Ambiguous column name 'JoinFromHdrPKey'."

DECLARE @start DATE
DECLARE @end DATE
SET @start = '20170101'
SET @end = '20170401'
;
WITH
      CTEid AS (
                  SELECT
                        JoinFromHdrPKey
                  FROM [dataKLL].[dbo].[KLL Extender UDF Values with Data]
                  WHERE UDF_Date_Data >= @start AND UDF_Date_Data < @end
            )
,     CTEraw AS (
                  SELECT
                        JoinFromHdrPKey
                      , UDF_FIELD
                      , UDF_Char_Data
                      , UDF_Date_Data
                      , UDF_Numeric_Data
                  FROM [dataKLL].[dbo].[KLL Extender UDF Values with Data] t
                  INNER JOIN CTEid r on t.JoinFromHdrPKey = r.JoinFromHdrPKey
            )
SELECT
      JoinFromHdrPKey
    , max(case when UDF_FIELD = 'FOB1VENDOR' then UDF_Char_Data    end) as FOB1VENDOR
    , max(case when UDF_FIELD = 'FOB1DATE'   then UDF_Date_Data    end) as FOB1DATE
    , max(case when UDF_FIELD = 'FOB1AMT'    then UDF_Numeric_Data end) as FOB1AMT

    , max(case when UDF_FIELD = 'FOB2VENDOR' then UDF_Char_Data    end) as FOB2VENDOR
    , max(case when UDF_FIELD = 'FOB2DATE'   then UDF_Date_Data    end) as FOB2DATE
    , max(case when UDF_FIELD = 'FOB2AMT'    then UDF_Numeric_Data end) as FOB2AMT

    , max(case when UDF_FIELD = 'MISC1VENDOR' then UDF_Char_Data    end) as MISC1VENDOR
    , max(case when UDF_FIELD = 'MISC1DATE'   then UDF_Date_Data    end) as MISC1DATE
    , max(case when UDF_FIELD = 'MISC1AMT'    then UDF_Numeric_Data end) as MISC1AMT

    , max(case when UDF_FIELD = 'MISC2VENDOR' then UDF_Char_Data    end) as MISC2VENDOR
    , max(case when UDF_FIELD = 'MISC2DATE'   then UDF_Date_Data    end) as MISC2DATE
    , max(case when UDF_FIELD = 'MISC2AMT'    then UDF_Numeric_Data end) as MISC2AMT

FROM CTEraw d
GROUP BY
      JoinFromHdrPKey
;

Open in new window

PortletPaulEE Topic Advisor
Most Valuable Expert 2014
Awarded 2013

Commented:
Do you know what that error message means? If not you should really learn how to deal with it (it will save you a lot of time).

In one of the queries I provided there is more than one table (or derived table) that has the column JoinFromHdrPKey and because there is more than 1 reference it is "ambiguous" if you don't specify which one of those 2 columns you want to use

,     CTEraw AS (
                  SELECT
                        t.JoinFromHdrPKey
                      , t.UDF_FIELD
                      , t.UDF_Char_Data
                      , t.UDF_Date_Data
                      , t.UDF_Numeric_Data
                  FROM [dataKLL].[dbo].[KLL Extender UDF Values with Data] t
                  INNER JOIN CTEid r on t.JoinFromHdrPKey = r.JoinFromHdrPKey
            )

where you see the yellow highlight is where I forgot to use a specific alias to ensure the query wasn't ambiguous

In truth production grade code should explicitly specify where all columns come from - this is a best practice sometimes missing from our proposed solutions
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
Understood, So I just about have this where it needs to be however I have records that do not contain any dates 16 thru 20. Why are these displaying? See sample data

DECLARE @start DATE;
DECLARE @end DATE;
SET @start = '20170307';
SET @end = '20170313';
WITH    CTEid
          AS ( SELECT   JoinFromHdrPKey
               FROM     [dataKLL].[dbo].[KLL Extender UDF Values with Data]
               WHERE    UDF_Date_Data >= @start
                        AND UDF_Date_Data < @end
             ),
        CTEraw
          AS ( SELECT   D.division ,
                        SHPMH.shpm_ref ,
                        t.JoinFromHdrPKey ,
                        t.UDF_FIELD ,
                        t.UDF_Char_Data ,
                        t.UDF_Date_Data ,
                        t.UDF_Numeric_Data
               FROM     [dataKLL].[dbo].[KLL Extender UDF Values with Data] t
                        INNER JOIN CTEid r ON t.JoinFromHdrPKey = r.JoinFromHdrPKey
                        INNER JOIN ZZMSHPMH SHPMH ON t.JoinFromHdrPKey = SHPMH.pkey
                        LEFT OUTER JOIN ZZCORDRD D ON D.SHP_SEQ = SHPMH.SHIPMENT_NUM
             )
    SELECT  Division ,
            shpm_ref ,
            JoinFromHdrPKey ,
            MAX(CASE WHEN UDF_FIELD = 'FOB1VENDOR' THEN UDF_Char_Data
                END) AS FOB1VENDOR ,
            MAX(CASE WHEN UDF_FIELD = 'FOB1DATE' THEN UDF_Date_Data
                END) AS FOB1DATE ,
            MAX(CASE WHEN UDF_FIELD = 'FOB1AMT' THEN UDF_Numeric_Data
                END) AS FOB1AMT ,
            MAX(CASE WHEN UDF_FIELD = 'FOB2VENDOR' THEN UDF_Char_Data
                END) AS FOB2VENDOR ,
            MAX(CASE WHEN UDF_FIELD = 'FOB2DATE' THEN UDF_Date_Data
                END) AS FOB2DATE ,
            MAX(CASE WHEN UDF_FIELD = 'FOB2AMT' THEN UDF_Numeric_Data
                END) AS FOB2AMT ,
            MAX(CASE WHEN UDF_FIELD = 'MISC1VENDOR' THEN UDF_Char_Data
                END) AS MISC1VENDOR ,
            MAX(CASE WHEN UDF_FIELD = 'MISC1DATE' THEN UDF_Date_Data
                END) AS MISC1DATE ,
            MAX(CASE WHEN UDF_FIELD = 'MISC1AMT' THEN UDF_Numeric_Data
                END) AS MISC1AMT ,
            MAX(CASE WHEN UDF_FIELD = 'MISC2VENDOR' THEN UDF_Char_Data
                END) AS MISC2VENDOR ,
            MAX(CASE WHEN UDF_FIELD = 'MISC2DATE' THEN UDF_Date_Data
                END) AS MISC2DATE ,
            MAX(CASE WHEN UDF_FIELD = 'MISC2AMT' THEN UDF_Numeric_Data
                END) AS MISC2AMT ,
            MAX(CASE WHEN UDF_FIELD = 'OCN1VENDOR' THEN UDF_Char_Data
                END) AS OCN1VENDOR ,
            MAX(CASE WHEN UDF_FIELD = 'OCNC1DATE' THEN UDF_Date_Data
                END) AS OCN1DATE ,
            MAX(CASE WHEN UDF_FIELD = 'OCN1AMT' THEN UDF_Numeric_Data
                END) AS OCN1AMT ,
            MAX(CASE WHEN UDF_FIELD = 'OCN2VENDOR' THEN UDF_Char_Data
                END) AS OCN2VENDOR ,
            MAX(CASE WHEN UDF_FIELD = 'OCN2DATE' THEN UDF_Date_Data
                END) AS OCN2DATE ,
            MAX(CASE WHEN UDF_FIELD = 'OCN2AMT' THEN UDF_Numeric_Data
                END) AS OCN2AMT ,
            MAX(CASE WHEN UDF_FIELD = 'DUTY1VENDOR' THEN UDF_Char_Data
                END) AS DUTY1VENDOR ,
            MAX(CASE WHEN UDF_FIELD = 'DUTY1DATE' THEN UDF_Date_Data
                END) AS DUTY1DATE ,
            MAX(CASE WHEN UDF_FIELD = 'DUTY1AMT' THEN UDF_Numeric_Data
                END) AS DUTY1AMT ,
            MAX(CASE WHEN UDF_FIELD = 'DUTY2VENDOR' THEN UDF_Char_Data
                END) AS DUTY2VENDOR ,
            MAX(CASE WHEN UDF_FIELD = 'DUTY2DATE' THEN UDF_Date_Data
                END) AS DUTY2DATE ,
            MAX(CASE WHEN UDF_FIELD = 'DUTY2AMT' THEN UDF_Numeric_Data
                END) AS DUTY2AMT ,
            MAX(CASE WHEN UDF_FIELD = 'INLAND1VENDOR' THEN UDF_Char_Data
                END) AS INLAND1VENDOR ,
            MAX(CASE WHEN UDF_FIELD = 'INLAND1DATE' THEN UDF_Date_Data
                END) AS INLAND1DATE ,
            MAX(CASE WHEN UDF_FIELD = 'INLAND1AMT' THEN UDF_Numeric_Data
                END) AS INLAND1AMT ,
            MAX(CASE WHEN UDF_FIELD = 'INLAND2VENDOR' THEN UDF_Char_Data
                END) AS INLAND2VENDOR ,
            MAX(CASE WHEN UDF_FIELD = 'INLAND2DATE' THEN UDF_Date_Data
                END) AS INLAND2DATE ,
            MAX(CASE WHEN UDF_FIELD = 'INLAND2AMT' THEN UDF_Numeric_Data
                END) AS INLAND2AMT
    FROM    CTEraw d
    GROUP BY Division ,
            JoinFromHdrPKey ,
            shpm_ref
    ORDER BY Division ,
            JoinFromHdrPKey ,
            shpm_ref;

Open in new window

Sample-Data.xlsx
PortletPaulEE Topic Advisor
Most Valuable Expert 2014
Awarded 2013
Commented:
That's not data, it is a result.

I can't tell you why without access to the tables.

Start small, what does this return?

          SELECT   JoinFromHdrPKey
               FROM     [dataKLL].[dbo].[KLL Extender UDF Values with Data]
               WHERE    UDF_Date_Data >= @start
                        AND UDF_Date_Data < @end

Open in new window

does that look OK?  
If yes, then the next bit      
WITH    CTEid
          AS ( SELECT   JoinFromHdrPKey
               FROM     [dataKLL].[dbo].[KLL Extender UDF Values with Data]
               WHERE    UDF_Date_Data >= @start
                        AND UDF_Date_Data < @end
             ) 
SELECT   D.division ,
                        SHPMH.shpm_ref ,
                        t.JoinFromHdrPKey ,
                        t.UDF_FIELD ,
                        t.UDF_Char_Data ,
                        t.UDF_Date_Data ,
                        t.UDF_Numeric_Data
               FROM     [dataKLL].[dbo].[KLL Extender UDF Values with Data] t
                        INNER JOIN CTEid r ON t.JoinFromHdrPKey = r.JoinFromHdrPKey
                        INNER JOIN ZZMSHPMH SHPMH ON t.JoinFromHdrPKey = SHPMH.pkey
                        LEFT OUTER JOIN ZZCORDRD D ON D.SHP_SEQ = SHPMH.SHIPMENT_NUM

Open in new window

Does it look OK?

Author

Commented:
Thank you. All the information you provided was very clear.
PortletPaulEE Topic Advisor
Most Valuable Expert 2014
Awarded 2013

Commented:
Happy to help.
Appreciate the prompt closure.
Cheers, Paul

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial