Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8172851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:54:50+00:00 2026-06-06T21:54:50+00:00

I have a TVF that returns two columns: ‘measure’ (a name) and ‘score’, a

  • 0

I have a TVF that returns two columns: ‘measure’ (a name) and ‘score’, a numeric score for that measure:

dbo.ScoringFunction(param1, param2, ..., paramN)
Measure   Score
-------   -----
measure1  10
measure2  5
...       ...
measureN  15

I’m running this function against a large number of rows that contain its parameters:

Name   Param1   Param2   ...   ParamN
----   ------   ------         ------
Daniel 12       5              6
etc.

I am trying to find a way to display the measures and their scores next to the parameters that determine those scores:

Name   Param1   Param2   ...   ParamN   measure1   measure2   ...   measureN
----   ------   ------         ------   --------   --------         --------
Daniel 12       5              6        10         5                15
etc.

So far I have tried using a pivot table, but it’s tricky since the data being pivoted is contained in a TVF rather than a static table. I’ve also tried using CROSS APPLY, but once I have the data (measures & scores), I’m still unable to pivot it into a nicely formatted row.

If anybody has any ideas, they would be much appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T21:54:53+00:00Added an answer on June 6, 2026 at 9:54 pm

    Without changing too much (hopefully), I would do the pivoting in the function, then use the function in CROSS APPLY and pull the columns. So if your function is something like this:

    CREATE FUNCTION dbo.ScoringFunction (parameters)
    RETURNS TABLE
    RETURN (
    
    SELECT Measure, Score
    FROM …
    
    )
    

    then I would rewrite it like this:

    CREATE FUNCTION dbo.ScoringFunction (parameters)
    RETURNS TABLE
    RETURN (
    
    WITH originalSelect AS (
      SELECT Measure, Score
      FROM …
    )
    SELECT
      measure1,
      measure2,
      …
    FROM originalSelect
    PIVOT (
      MAX(Score) FOR Measure IN (measure1, measure2, …)
    ) p
    
    )
    

    and use it in the final query like this:

    SELECT
      t.Name,
      t.param1,
      t.param2,
      …
      x.measure1,
      x.measure2,
      …
    FROM atable t
    CROSS APPLY dbo.ScoringFunction (t.param1, t.param2, …) x
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an inline TVF that accept a primary key of a table and
I have the following TVF for fulltext search: FUNCTION [dbo].[Fishes_FullTextSearch] (@searchtext nvarchar(4000), @limitcount int)
I need to create a view or a table valued function that returns one
I have a simple class that imports another class from another jar. Everything compiles
Have a network location that shows paths in the 8.3 short format. I need
Have data that has this kind of structure. Will be in ascending order by
Have data that has this kind of structure: $input = [ { animal: 'cat',
Have some dates in my local Oracle 11g database that are in this format:
I've cretaed a TVF which returns a table with parent-records from a recursive CTE
I have a stored procedure (sql2005) that needs to call a table valued function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.