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

  • Home
  • SEARCH
  • 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 3361218
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T03:07:34+00:00 2026-05-18T03:07:34+00:00

I have two tables: Unit: UnitId int PK Title varchar UnitOption: UnitOptionId int PK

  • 0

I have two tables:

Unit:
UnitId int PK
Title varchar

UnitOption:
UnitOptionId int PK
UnitId int FK
Title varchar

Quote:
QuoteId int PK
UnitOptionId int FK
Title varchar

I want to create a scalar UDF that takes a QuoteId param and returns a varchar that contains the following description (pseudu):

Quote.Title + '-' + Unit.Title + '-' + Unit.UnitId + 
/* Here is where my question is:
If there are more than 1 UnitOption under this Unit, then
  return '-' + the UnitOption number under this Unit
  (i.e.) if under this Unit, there are 3 UnitOption with IDs 13, 17, 55
  under the unit, and the current Quote.UnitOptionId is the 17 one,
  it should return 2.
  Which means I want to retrieve an ID of this row in the group.
Else
  return ''
*/
  • 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-05-18T03:07:34+00:00Added an answer on May 18, 2026 at 3:07 am
    CREATE FUNCTION ufnGetDescription 
      (@QuoteID INT) 
    RETURNS VARCHAR(MAX) 
    AS 
      BEGIN 
      DECLARE @RetVal VARCHAR(MAX); 
    
      WITH CurRow 
      AS (SELECT quote.title + '- ' + unit.title AS start, 
        u.unitid, 
        quoteid, 
        uo.unitoptionid 
      FROM quote 
        INNER JOIN unitoption uo 
        ON quote.unitoptionid = uo.unitoptionid 
        INNER JOIN unit 
        ON uo.unitid = unit.unitid 
      WHERE quote.quoteid = @QuoteID), 
      AllUnits 
      AS (SELECT u.unitid, 
        uo.unitoptionid, 
        Row_number() 
        OVER(PARTITION BY u.unitid ORDER BY uo.unitoptionid) AS NUMBER, 
        Count(* ) 
        OVER(PARTITION BY u.unitid )  AS cntUnits 
      FROM unit 
        INNER JOIN unionoption uo 
        ON unit.unitid = uo.unitid 
      WHERE u.unitid IN (SELECT unitid 
          FROM CurRow)) 
      SELECT @RetVal = CASE 
        WHEN a.cntUnits = 1 THEN '' 
        ELSE r.start + '-' + Cast(NUMBER AS VARCHAR(max)) 
        END 
      FROM AllUnits a 
      INNER JOIN CurRow r 
      ON a.unitoptionid = r.unitoptionid 
    
      RETURN @RetVal 
    END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables that I want to use for viewing my reports which
Have two tables named Employee and Order. The relations between the tables Employee (1)
I have two tables Order , OrderStatus . Order has OrderId (pk), OrderStatusId (fk)
I have two tables with a foreign key constraint how can I delete rows
I have two tables with identical schemas and composite keys, table a and table
I have a MySQL database of 3 tables: I. Person (id, name, purchases) II.
i have two sql queries. select EmployeeID,FName from [Mydatabase].[dbo].EMPLOYEE_TABLE where EmployeeID not in (select
I have three table here.... One is menu, and second is drink, and food.
I am trying to parse the stack overflow data dump, one of the tables
Scenario: Data is received and written to database with timestamps. I need to process

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.