What mistakes with this query? - postgresql

$query = "select (select count(a.serial_no) from tra2 a where a.model_no = ".$id." and a.flag = cast(3 as character varying) + (select count(a.serial_no) from stk a where a.model_no = ".$id." and a.trans_id is NULL)as qty)";
$result = $this->db->query($query);
return $result->result();
When i run the query
ERROR: syntax error at or near "as"
LINE 1: ...ck a where a.model_no = K258 and a.trans_id is NULL)as qty)

Add '' (quotes) on your WHERE clauses. I guess the data type is VARCHAR so you have to use quotes in your query
Like this
.... WHERE a.model_no = '" . $id . "' AND ...

The end of the quest must be:
...a.trans_id is NULL)) as qty"
Currently You have some like this
select (select 1 + 2 as qty)
but You want
select (select 1 + 2) as qty

Related

T-SQL how to assign a variable inside another variable assignment

I've inherited a stored proc and I need to add and assign a new variable. The piece of the code that's relevant is:
DECLARE #tableRows VARCHAR(MAX) = '';
SET #tableRows
= N'<tr>
<bgcolor="#6081A0" style="FONT-FAMILY:arial,san-serif;FONT-WEIGHT:normal;color:#6081A0"> :: Resource Scheduler</tr>'
+ N'<tr>
Date: ' + CAST(CONVERT(NVARCHAR, DATENAME(WEEKDAY, #rpt_start_date)) AS VARCHAR(100)) + ' '
+ CAST(CONVERT(NVARCHAR, CAST(#rpt_start_date AS DATE), 100) AS VARCHAR(100)) + '</tr>'
+ '<table border="1" width="100%">'
+ '<tr bgcolor="#DC5E3F" style="FONT-FAMILY:arial,san-serif;FONT-WEIGHT:bold;color:white">'
+ '<td style="text-align:center;vertical-align:middle">START TIME</td>'
+ '<td style="text-align:center;vertical-align:middle">END TIME</td>'
+ '<td style="text-align:center;vertical-align:middle">ROOM</td>'
+ '<td>MEETING TITLE</td>'
+ '<td style="text-align:center;vertical-align:middle">ATTENDEES</td>'
+ '<td>INVITEE' + CHAR(39) + 'S NAME</td><td>HOSTS NAMES</td>'
+ '<td>FOOD SERVICES REQUESTS</td>'
+ '<td>TECHNOLOGY REQUESTS</td>'
+ '<td>OFFICE SERVICES REQUESTS</td></tr>';
SELECT #tableRows
= #tableRows + '<tr ' + 'bgcolor=' +
+ IIF(ROW_NUMBER() OVER (ORDER BY s.[sched_id] DESC) % 2 = 0, '"lightgrey', '"white') + '">'
+ '<td style="text-align:center;vertical-align:middle">' + CAST(CONVERT(NVARCHAR, CAST(srd.[mtg_start_date_local] AS TIME), 100) AS VARCHAR(100)) + '</td>'
+ '<td style="text-align:center;vertical-align:middle">' + CAST(CONVERT(NVARCHAR, CAST(srd.[mtg_end_date_local] AS TIME), 100) AS VARCHAR(100)) + '</td>'
+ '<td style="text-align:center;vertical-align:middle">' + CAST(r.[res_hdr] AS VARCHAR(100)) + '</td>'
+ '<td>' + CAST(s.[sched_desc] AS VARCHAR(100)) + '</td>'
+ '<td style="text-align:center;vertical-align:middle">' + CAST(s.[num_attendees] AS VARCHAR(100)) + '</td>'
+ '<td>' + CAST(ru.[user_name] AS VARCHAR(100)) + '</td>'
+ '<td>' + CAST(hu.[user_name] AS VARCHAR(100)) + '</td>'
+ '<td>' + CAST(dbo.ufn_rsConcatCustomTabServices(#rs_customtab_food,s.[sched_id]) AS VARCHAR(4000)) + '</td>'
+ '<td>' + CAST(dbo.ufn_rsConcatCustomTabServices(#rs_customtab_tech,s.[sched_id]) AS VARCHAR(4000)) + '</td>'
+ '<td>' + CAST(dbo.ufn_rsConcatCustomTabServices(#rs_customtab_os,s.[sched_id]) AS VARCHAR(4000)) + '</td></tr>'
FROM
tbl_sched s WITH (NOLOCK)
INNER JOIN
tbl_sched_res_date srd WITH (NOLOCK)
ON s.[sched_id] = srd.[sched_id]
INNER JOIN
tbl_sched_request sr WITH (NOLOCK)
ON s.[sched_id] = sr.[sched_id]
INNER JOIN
tbl_user ru WITH (NOLOCK)
ON sr.[req_for_user_id] = ru.[user_id]
INNER JOIN
tbl_user hu WITH (NOLOCK)
ON s.create_by = hu.[user_id]
INNER JOIN
tbl_res r WITH (NOLOCK)
ON srd.[res_id] = r.[res_id]
INNER JOIN
tbl_grp g WITH (NOLOCK)
ON r.[grp_id] = g.[grp_id]
INNER JOIN
tbl_loc l WITH (NOLOCK)
ON g.[loc_id] = l.[loc_id]
INNER JOIN
tbl_region rg WITH (NOLOCK)
ON l.[region_id] = rg.[region_id]
LEFT OUTER JOIN -- changed from inner join
tbl_sched_udf_val suv_f WITH (NOLOCK)
ON suv_f.[sched_id] = s.[sched_id]
AND suv_f.[udf_id] =
(
SELECT
u.[udf_id]
FROM
tbl_udf u WITH (NOLOCK)
WHERE
u.[udf_desc] LIKE #rs_customtab_food
)
AND suv_f.[string_value] IS NOT NULL
AND suv_f.[string_value] = 'Yes'
LEFT OUTER JOIN -- changed from inner join
tbl_sched_udf_val suv_t WITH (NOLOCK)
ON suv_t.[sched_id] = s.[sched_id]
AND suv_t.[udf_id] =
(
SELECT
u.[udf_id]
FROM
tbl_udf u WITH (NOLOCK)
WHERE
u.[udf_desc] LIKE #rs_customtab_tech
)
AND suv_t.[string_value] IS NOT NULL
AND suv_t.[string_value] = 'Yes'
LEFT OUTER JOIN -- changed from inner join
tbl_sched_udf_val suv_o WITH (NOLOCK)
ON suv_o.[sched_id] = s.[sched_id]
AND suv_o.[udf_id] =
(
SELECT
u.[udf_id]
FROM
tbl_udf u WITH (NOLOCK)
WHERE
u.[udf_desc] LIKE #rs_customtab_os
)
AND suv_o.[string_value] IS NOT NULL
AND suv_o.[string_value] = 'Yes'
LEFT OUTER JOIN
tbl_sched_res_setup srs WITH (NOLOCK)
ON (
s.[sched_id] = srs.[sched_id]
AND srd.[res_id] = srs.[res_id]
)
LEFT OUTER JOIN
tbl_setup su WITH (NOLOCK)
ON (srs.[setup_id] = su.[setup_id])
WHERE
l.[loc_id] = 13-- 1177 Sixth Ave ( ONLY )
AND s.[deleted_flag] = 0
AND r.[obsolete_flag] = 0
AND g.[obsolete_flag] = 0
AND l.[obsolete_flag] = 0
AND rg.[obsolete_flag] = 0
AND srd.[busy_start_date_local] >= CONVERT(NVARCHAR(20), #rpt_start_date, 112)
AND srd.[busy_start_date_local] < CONVERT(NVARCHAR(20), #rpt_end_date, 112)
ORDER BY
srd.[mtg_start_date_local],
r.[res_hdr];
SELECT #tableRows = #tableRows + '</table>';
As you can see, it's a complicated query. #tableRows is used later on to create the body of an email. Now, I need to get s.sched_desc (see line 7 of SELECT statement) and assign it to a second variable, so that I can use it in the Subject line of the same email. I've tried adding
+ (SELECT #sched_desc = SELECT [sched_desc])
to the bottom of the SELECT statement but it's no good (incorrect syntax near parenthesis). I've also tried
+ '<td>' + (SELECT #sched_desc = CAST(s.[sched_desc] AS VARCHAR(100))) + '</td>'
but again it's expecting another parenthesis. I know I can do this by turning this whole thing into a string and then executing it with sp_executesql (see this example) but I'd prefer to avoid dynamic sql if possible. On the other hand, I really don't want to execute this query twice. Is there another way to get around this?
Your stated task would be more easily accomplished and supported by employing a templating language and some basic string interpolation.
It is not possible to set a variable value within the process of setting another variable's value, but you can do so within the same SELECT.
In your query, add a comma after the closing quote and then set your #sched_desc variable:
SELECT #tableRows = #tableRows + '<tr ' + 'bgcolor=' ... </td></tr>',
#sched_desc = [sched_desc]
FROM tbl_sched s WITH (NOLOCK)
INNER JOIN tbl_sched_res_date srd WITH (NOLOCK)
ON s.[sched_id] = srd.[sched_id]
...
The second variable assignment has access to the same data as the original query, but it will need to be included in whatever selecting process is used to retrieve the value of #tableRows.
As an additional note, I will strongly advise you to identify alternatives to using NOLOCK - here are some links to get you started on that path:
https://www.brentozar.com/archive/2021/11/nolock-is-bad-and-you-probably-shouldnt-use-it/
https://www.brentozar.com/archive/2018/10/using-nolock-heres-how-youll-get-the-wrong-query-results/
https://www.brentozar.com/archive/2016/12/nolock-ever-right-choice/
https://www.brentozar.com/archive/2021/01/but-surely-nolock-is-okay-if-no-ones-changing-data-right/
Paneerakbari is correct that you can assign (and build up) more than one variable in the select. Here is a simplified example that may make things clearer.
DECLARE #TableRows VARCHAR(MAX) = '<table>'
DECLARE #Subject VARCHAR(MAX) = '' -- Only the last value is retained here
SELECT
#TableRows = #TableRows + '<tr><td>' + A.Info + '</td></tr>',
#Subject = A.Title
FROM (
VALUES
(1, 'This', 'This stuff'),
(2, 'That', 'That stuff'),
(3, 'More', 'More stuff')
) A(ID, Title, Info)
ORDER BY A.ID
SET #TableRows = #TableRows + '</table>'
SELECT #Subject, #TableRows
Result:
#Subject = 'More'
#TableRows = '<table><tr><td>This stuff</td></tr><tr><td>That stuff</td></tr><tr><td>More stuff</td></tr></table>'
For readability and maintainability, I often find it useful to move complex intermediate calculations into a CROSS APPLY block, the results of which can then be referenced in the final select.
DECLARE #TableRows VARCHAR(MAX) = '<table>'
DECLARE #Subject VARCHAR(MAX) = '' -- Only the last value is retained here
SELECT
#TableRows = #TableRows + R.ComplexRowConstruction,
#Subject = A.Title
FROM (
VALUES
(1, 'This', 'This stuff'),
(2, 'That', 'That stuff'),
(3, 'More', 'More stuff')
) A(ID, Title, Info)
CROSS APPLY (
SELECT ComplexRowConstruction =
'<tr>'
+ '<td>' + A.Info + '</td>'
+ '</tr>'
) R
ORDER BY A.ID
SET #TableRows = #TableRows + '</table>'
SELECT #Subject, #TableRows

How to fix Incorrect syntax near '=' error in stored procedure that uses dynamic SQL

I attempted to add some additional columns to an existing stored procedure. This entailed not only adding the columns but also creating a LEFT JOIN subquery to gather the additional columns that is commented on " Addition of Sales and Warranties totals". Now, when I execute this code within my stored procedure, I'm getting an error in the SQL command.
I've tried opening a new query window and trying to recreate the query in the proper format to execute it, but I get the following errors: Msg 102, Level 15, State 1, Line 60
Incorrect syntax near ' + case when isnull(#TaxHeaders,') = ' then '.
Msg 102, Level 15, State 1, Line 117
Incorrect syntax near ' + case when isnull(#TaxHeaders,') = ' then '.
I've also tried changing the location of my LEFT JOIN query (SWT,GT) above the T and T1 LEFT JOIN that includes the pivot.
CREATE Procedure [GP].[spInvoiceReprintsSummary]
#CompanyKey varchar(2)=5,
#ParentCustomerNum varchar(255)='BCAA01', --'AAANYCITY', --'AAANPENN01', --'CAAQUEBE01', --'BCAA01', --'CAAQUEBE01',
#StartDateKey varchar(8)=20130306,
#EndDateKey varchar(8)=20130315,
#TaxHeaders varchar(255)='HST,GST,PST,QST' --'GST, QST'
As
Begin
Declare #SQL as varchar(max)
Set #SQL = '
Select S.CompanyKey, S.TaxScheduleID, S.CustomerKey, S.ParentCustomerNum, S.StationID, S.CustomerName, S.DocumentTypeKey, S.DocumentNum, Case When Left(S.DocumentNum, 2)=''ST'' Then 0 ELSE 1 End As EligibleDiscount,S.ExtendedPrice,S.PurchaseOrderNum, DocumentDate, S.TaxAmount, SWT.SalesTotal, SWT.WarrantiesTotal, S.TerritoryId ' + case when isnull(#TaxHeaders,'') = '' then '' else ',' + #TaxHeaders end + ' From
(
--Get distinct documents
Select --Top 100
SD.CompanyKey, SD.TaxScheduleID, CS.CustomerKey, CS.ParentCustomerNum, CS.StationID, CS.CustomerName, SD.DocumentTypeKey, SD.DocumentNum,SD.PurchaseOrderNum, convert(datetime,convert(varchar, DateKey, 112)) As DocumentDate,sum(SD.TaxAmount) as TaxAmount, sum(SD.ExtendedPrice) As ExtendedPrice, CS.TerritoryId From GP.SalesDetail SD
Inner Join GP.Customers CS
on SD.CustomerKey = CS.CustomerKey
Where CS.ParentCustomerNum= ''' + #ParentCustomerNum + ''' And SD.DateKey Between ' + #StartDateKey + ' and ' + #EndDateKey + ' And SD.CompanyKey = ' + #CompanyKey + ' And SD.PostStatus = 1 And SD.VoidStatus = 0
Group by SD.CompanyKey, SD.TaxScheduleID, CS.CustomerKey, CS.ParentCustomerNum, CS.StationID, CS.CustomerName, SD.DocumentTypeKey, SD.DocumentNum,SD.PurchaseOrderNum, convert(datetime,convert(varchar, DateKey, 112)), CS.TerritoryId
--Order By SD.PurchaseOrderNum, SD.DocumentTypeKey
) S
---Addition of sales and warranties totals
LEFT JOIN(
SELECT CASE WHEN GroupName = ''SALES'' then sum(QuantitySold) else 0 end as SalesTotal,CASE WHEN GroupName = ''WARRANTIES'' then sum(QuantitySold) else 0 end as WarrantiesTotal, DocumentNum, CompanyKey FROM
(SELECT
SD.QuantitySold , DocumentNum , SD.CompanyKey
,CASE WHEN I.userDefItemClass1 = ''BATTERY/RETURN'' AND SD.QuantitySold >= 0 THEN ''SALES''
WHEN I.userDefItemClass1 = ''BATTERY/RETURN'' AND SD.QuantitySold < 0 THEN ''RETURNS''
WHEN I.ItemNum = ''RESTOCKING FEE'' AND sd.ExtendedPrice >= 0 THEN ''RETURNS''
WHEN I.ItemNum = ''RESTOCKING FEE'' AND sd.ExtendedPrice < 0 THEN ''SALES''
WHEN I.userDefItemClass1 = ''WARRANTY'' THEN ''WARRANTIES''
WHEN I.userDefItemClass1 = ''NRF'' THEN ''RECYCLING FEES''
WHEN I.userDefItemClass1 = ''CORES'' THEN ''SPENT BATTERIES''
WHEN I.ItemNum IN (''PU2'',''PU4.3'') AND sd.ExtendedPrice > 0 THEN ''RETURNS''
WHEN I.ItemNum IN (''PU2'',''PU4.3'') AND sd.ExtendedPrice < 0 THEN ''SALES''
ELSE ''OTHER'' END AS GroupName
FROM GP.SalesDetail AS SD
INNER JOIN GP.Items AS I ON SD.ItemKey = I.ItemKey
Inner Join GP.Customers CS on SD.CustomerKey = CS.CustomerKey
WHERE
CS.ParentCustomerNum= '' + #ParentCustomerNum + '' And SD.DateKey Between ' + #StartDateKey + ' and ' + #EndDateKey + ' And SD.CompanyKey = ' + #CompanyKey + ' And SD.PostStatus = 1 And SD.VoidStatus = 0
) GN
GROUP BY DocumentNum, GroupName, CompanyKey
) SWT
ON S.Companykey = SWT.CompanyKey And S.DocumentNum = SWT.DocumentNum
Left join (
--Pivot the Tax Types
Select Companykey, DocumentTypeKey, DocumentNum ' + case when isnull(#TaxHeaders,'') = '' then '' else ',' + #TaxHeaders end + ' From (
SELECT SD.CompanyKey,TD.DocumentTypeKey, TD.DocumentNum, TS.TaxDetailLabel, SUM(TD.TaxAmount) AS TaxAmount
FROM GP.TaxSchedules AS TS INNER JOIN
(SELECT SD.CompanyKey, SD.TaxScheduleId, SD.DocumentTypeKey, SD.DocumentNum
FROM GP.SalesDetail SD
Inner Join GP.Customers CS
on SD.CustomerKey = CS.CustomerKey
Where CS.ParentCustomerNum= ''' + #ParentCustomerNum + ''' And SD.DateKey Between ' + #StartDateKey + ' and ' + #EndDateKey + ' And SD.CompanyKey = ' + #CompanyKey + ' And SD.PostStatus = 1 And SD.VoidStatus = 0
GROUP BY SD.CompanyKey, SD.TaxScheduleId, SD.DocumentTypeKey, SD.DocumentNum) AS SD
ON TS.CompanyKey = SD.CompanyKey AND TS.TaxScheduleId = SD.TaxScheduleId
INNER JOIN GP.TaxDetails AS TD ON TD.DocumentNum = SD.DocumentNum AND TD.DocumentTypeKey = SD.DocumentTypeKey AND
TD.LineItemSequenceNum = 0 AND TD.TaxDetailID = TS.TaxDetailId AND TD.CompanyKey = TS.CompanyKey
GROUP BY SD.CompanyKey,TD.DocumentTypeKey,TD.DocumentNum, TS.TaxDetailLabel
) T
Pivot (sum(TaxAmount) For TaxDetailLabel In (' + case when isnull(#TaxHeaders,'') = '' then 'Tax' else #TaxHeaders end + ')) as PVT
) T1
on S.Companykey = T1.CompanyKey And S.DocumentTypeKey = T1.DocumentTypeKey And S.DocumentNum = T1.DocumentNum'
--print #SQL
Exec (#SQL)
End
I expect an output of the original result set including the new columns for SalesTotal and WarrantiesTotal
Replace
CASE
WHEN isnull(#taxheaders,'') = ''
then ''
ELSE ',' + #taxheaders
END
with
CASE
WHEN #taxheaders IS NULL
THEN ''
ELSE ',' + #taxheaders
This should fix the error. Thanks..

get OBJECT_ID in linked server

Second select (from linked server) does not return any values.. Object_ID doesnt work. Is any workaround?
select '', name
FROM sys.databases
WHERE 1 = 1
AND NAME <> db_name() -- exclude current database
AND CASE
WHEN STATE = 0
THEN CASE
WHEN OBJECT_ID(NAME + '.dbo.tPA_SysParam', 'U') IS NOT NULL
THEN 1
END
END = 1
union
select '[LINKED]', name
FROM [LINKED].master.sys.databases
WHERE 1 = 1
AND CASE
WHEN STATE = 0
THEN CASE
WHEN OBJECT_ID('[LINKED].'+NAME + '.dbo.tPA_SysParam', 'U') IS NOT NULL
THEN 1
END
END = 1
You can also mimic OBJECT_ID with a little help from the PARSENAME function:
Declare #FullTableName nvarchar(max) = '[dbo].[MyTable]';
Select t.object_id
From [LINKED].MyDatabase.sys.tables As t
Inner Join [LINKED].MyDatabase.sys.schemas As s On t.schema_id = s.schema_id
Where t.[name] = PARSENAME(#FullTableName, 1)
And s.[name] = PARSENAME(#FullTableName, 2)

T_SQL The multi part identifier could not be bound error

I have a query which works fine, but I am trying to create a dynamic pivot out of it to get a better end result table.
I found this on SO but I cant relate it to my issue.
The multi-part identifier could not be bound
My working code is this:
DECLARE #RangeDate as date
set #RangeDate = (select distinct cd.weDate from CM_DATA cd where cd.year = 2015 and cd.week = 45)
set #RangeDate = DATEADD(WW, -7, #RangeDate)
DECLARE #SQL as VARCHAR(MAX)
DECLARE #Columns AS VARCHAR(MAX)
SELECT #Columns =
COALESCE(#Columns + ', ','') + QUOTENAME(YearWeek)
FROM
(
SELECT DISTINCT YearWeek
FROM CM_DATA
where weDate >= #RangeDate
) AS B
SET #SQL = '
WITH PivotData AS
(
select cd.Country
, cd.Chain
, cd.YearWeek
, left(sm.Planogram, 2) as planogram
, cd.StoreNo
, cd.UID
, cd.ShortCode
, lp.Family
, lp.ColourShort
, pr.type
, cd.Volume
, ul.WOSOR
from vw_V2_UsrVarLst ul
left join CM_DATA cd on cd.Country = ul.CountryCode and cd.Chain = ul.Chain
left join V2_StoreMaster sm on sm.CountryCode = ul.CountryCode and sm.Chain = ul.Chain and sm.StoreNo = cd.StoreNo and sm.StoreNm = cd.StoreNm and cd.YearWeek between sm.YYYYWW and sm.YYYYWWEND
left join tblProducts pr ON pr.[COUNTRY CODE] = ul.CountryCode and pr.SKU = cd.UID
left join V2_LanguagePack LP ON LP.ShortCode = cd.ShortCode AND lp.Lang = ul.UsrLang
where cd.Country = ul.CountryCode and cd.Chain = ul.Chain and planogram is not null and left(cd.UID, 10) in (select lv.UID from V2_live lv where lv.CountryCode = ul.CountryCode and lv.Chain = ul.Chain and cd.YearWeek between lv.YYYYWW and lv.YYYYWWEND) and cd.weDate >= ' + #RangeDate + ' and sm.Planogram != ''Z''
)
select cd.Country
, cd.Chain
, left(sm.Planogram, 2) as planogram
, cd.StoreNo
, cd.UID
, cd.ShortCode
, lp.Family
, lp.ColourShort
, pr.type
, cd.Volume
, ' + #Columns + '
, ul.WOSOR
FROM PivotData
PIVOT
(
SUM(Volume)
FOR YearWeek
IN(' + #Columns + ')
) AS PivotResult'
EXEC (#SQL)
Can anyone spot what up here
KR
Martin
Try SELECT #SQL instead of your EXEC.
You probably must set the output to Text and use the query options (right click into the query window) to set the max length of text output to a higher value (maximum is 8192).
Than you can paste the result of your dynamic SQL into a new query window and execute this there. You should get a speaking error message and you should even jump to the right place with a double click...
Good luck!

Get index of row within a group?

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 ''
*/
If you're using SQL 2005 or later and I've interpreted your question correctly, you should be able to adapt the following into your function.
WITH [UnitExt] AS
(
SELECT
[Unit].[UnitId],
[Unit].[Title],
COUNT(*) AS [Count]
FROM [Unit]
INNER JOIN [UnitOption] ON [UnitOption].[UnitId] = [Unit].[UnitId]
GROUP BY
[Unit].[UnitId],
[Unit].[Title]
)
SELECT
[Quote].[Title] + '-' + [UnitExt].[Title] + '-' + [UnitExt].[UnitId] +
CASE
WHEN [UnitExt].[Count] > 1 THEN '-' +
CAST([UnitOption].[UnitOptionId] AS varchar(max))
ELSE ''
END
FROM [Quote]
INNER JOIN [UnitOption] ON [UnitOption].[UnitOptionId] =
[Quote].[UnitOptionId]
INNER JOIN [UnitExt] ON [UnitExt].[UnitId] = [UnitOption].[UnitId]
WHERE [Quote].[QuoteId] = #QuoteId
Something like this should do it.
SELECT DISTINCT Quote.Title +
' - ' + Unit.Title +
' - ' + Unit.UnitId +
CASE
WHEN COUNT(*) OVER(PARTITION BY Quote.Id) > 0
THEN
' - ' + CAST(ROW_NUMBER() OVER (PARTITION BY Quote.Id ORDER BY Quote.UnitOptionId) AS varchar)
ELSE
''
END
FROM Quote
JOIN UnitOption ON UnitOption.Id = Quote.UnitOptionId
JOIN Unit ON Unit.Id = UnitOption.UnitId
WHERE Quote.Id = #QuoteId
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