Select single row of data for each ID in Select Statement

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
mburk1968Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PortletPaulfreelancerCommented:
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
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mburk1968Author 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

0
PortletPaulfreelancerCommented:
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
0
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

mburk1968Author 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
0
PortletPaulfreelancerCommented:
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?
0
mburk1968Author Commented:
Thank you. All the information you provided was very clear.
0
PortletPaulfreelancerCommented:
Happy to help.
Appreciate the prompt closure.
Cheers, Paul
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SSRS

From novice to tech pro — start learning today.