I've a column with concatenated values, but the string comes with a comma at the end.
How can I remove the last comma on existing values?
SELECT
m.Mais_Id
, m.Outro
(SELECT CAST(emp.First_Name + ' ' + emp.Last_Name + ', ' AS VARCHAR(MAX))
FROM
Patos p
LEFT JOIN
Employee emp
ON
p.Pato_Id = emp.Pato_Id
WHERE
m.Pato_Id = p.Pato_Id
FOR XML PATH ('')
) AS Names
FROM
Mais m
I've this:
Mais_Id Outro Names
0 As Adn Meas, Fjds Poi, Csa Drop,
1 Be
2 Tee As Been,
This is the pretended result:
Mais_Id Outro Names
0 As Adn Meas, Fjds Poi, Csa Drop
1 Be
2 Tee As Been
Most neat way to do this is to use stuff() function:
stuff(
(
select ', ' + cast(emp.First_Name + ' ' + emp.Last_Name as varchar(max))
from Patos as p
left outer join Employee as emp on p.Pato_Id = emp.Pato_Id
where m.Pato_Id = p.Pato_Id
for xml path(''), type
).value('.', 'nvarchar(max)')
, 1, 2, '') as Names
Note it's also safer to get concatenated string with value() function, so if you have special characters like & or < it will be properly shown.
Try this:
SELECT Mais_id, m.Outro, Substring(newColumn, 0, LEN(newColumn) - 1)
FROM (
SELECT
m.Mais_Id
, m.Outro
(SELECT CAST(emp.First_Name + ' ' + emp.Last_Name + ', ' AS VARCHAR(MAX)) as newColumn
FROM
Patos p
LEFT JOIN
Employee emp
ON
p.Pato_Id = emp.Pato_Id
WHERE
m.Pato_Id = p.Pato_Id
FOR XML PATH ('')
) AS Names
FROM
Mais m
)
Related
Here is the sample data :
IdProduit Localisation Qte_EnMain
4266864286880063006 E2-R40-B-T 13.00000
4266864286880063006 E2-R45-B-T 81.00000
4266864286880063007 E2-R45-C-T 17.00000
4266864286880063008 E2-R37-B-T 8.00000
And this is what i would like to have
IdProduit AllLocalisation
4266864286880063006 E2-R40-B-T (13), E2-R45-B-T (81)
4266864286880063007 E2-R45-C-T (17)
4266864286880063008 E2-R37-B-T (8)
I watched all the examples of GROUP_CONCAT on the forum and I tried several tests.
I don't really understand STUFF().
Here is what i would like to do :
SELECT
a.IdProduit,
GROUP_CONCAT(
CONCAT(b.Localisation, ' (', CAST(ROUND(a.Qte_EnMain, 0) AS NUMERIC(36, 0)), ')')
) AS AllLocation
FROM
ogasys.INV_InventENTLoc a
LEFT JOIN ogasys.INV_LocName b ON a.IdLoc = b.IdLoc
GROUP BY a.IdProduit, b.Localisation, a.Qte_EnMain
Now because GROUP_CONCAT is nto working with MSSQL this is the query i have created with all example on this forum.
SELECT
DISTINCT
a1.IdProduit,
STUFF((SELECT DISTINCT '' + b2.Localisation
FROM
ogasys.INV_InventENTLoc a2
LEFT JOIN ogasys.INV_LocName b2 ON a2.IdLoc = b2.IdLoc
WHERE a2.IdLoc = a1.IdLoc
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 0, '') data
FROM
ogasys.INV_InventENTLoc a1
LEFT JOIN ogasys.INV_LocName b1 ON a1.IdLoc = b1.IdLoc
ORDER BY a1.IdProduit
The query only return one localisation by row i don't understand how to make this query working.
EDIT:
Here is the solution for my situation :
SELECT
a.IdProduit,
STUFF(
(SELECT ', ' + b2.Localisation + ' (' + CAST(CAST(ROUND(a2.Qte_EnMain, 0) AS NUMERIC(36, 0)) AS VARCHAR(32)) + ')'
FROM ogasys.INV_InventENTLoc a2
LEFT JOIN ogasys.INV_LocName b2 ON a2.IdLoc = b2.IdLoc
WHERE a.IdProduit = a2.IdProduit
FOR XML PATH (''))
, 1, 1, '') AS AllLocalisation
FROM
ogasys.INV_InventENTLoc a
LEFT JOIN ogasys.INV_LocName b ON a.IdLoc = b.IdLoc
GROUP BY a.IdProduit
using STUFF
declare #table table (IdProduit varchar(100), Localisation varchar(50), Qte_EnMain float)
insert into #table
values
('4266864286880063006','E2-R40-B-T', 13.00000),
('4266864286880063006','E2-R45-B-T', 81.00000),
('4266864286880063007','E2-R45-C-T', 17.00000),
('4266864286880063008','E2-R37-B-T', 8.00000)
select IdProduit,
STUFF (
(SELECT
',' + localisation + concat(' (',cast(qte_enMain as varchar(4)),') ')
FROM #table t2
where t2.IdProduit = t1.IdProduit
FOR XML PATH('')), 1, 1, ''
)
from #table t1
group by
IdProduit
TitemName and TshotName is the problem here
SELECT DISTINCT
tJobs.* ,
tCustomer.Name AS Customer_name ,
(SELECT tEmployee.First + ' ' + tEmployee.Last
FROM tEmployee
WHERE tEmployee.EmployeeID = tJobs.AccountExecutiveID) AS AccountExecutive,
(SELECT tEmployee.First + ' ' + tEmployee.Last
FROM tEmployee
WHERE tEmployee.EmployeeID = tJobs.AccountManagerID) AS AccountManager,
dbo.RetrunUserFavourite(tJobs.JobNumber, 33369, 'Employee') AS Favorites,
(SELECT COUNT(*)
FROM tShots
WHERE tShots.JobNumber = tJobs.JobNumber) AS shotcount,
SUBSTRING((SELECT ', ' + SKU + ', ' + Source + ', ' + ModelNumber
+ ', ' + Description
FROM tItems
WHERE tItems.CustomerID = tCustomer.CustomerID
FOR XML PATH('')), 3, 200000) titemName,
SUBSTRING((SELECT ', ' + ArtDirection + ', '
+ REPLACE(CONVERT(VARCHAR(5), AdDate, 110), '-',
'/')
FROM tShots
WHERE tShots.JobNumber = tJobs.JobNumber
FOR XML PATH('')), 3, 200000) TshotName
FROM
tJobs
INNER JOIN
tCustomer ON tCustomer.CustomerID = tJobs.CustomerID
WHERE
tCustomer.CustomerID = 68666
I strongly agree with M Ali's comments. Two points I would make. The first is not with regard to performance. But, instead of substring() use:
STUFF((SELECT ', ' + SKU + ', ' + Source + ', ' + ModelNumber+ ', ' + Description
FROM tItems
WHERE tItems.CustomerID = tCustomer.CustomerID
FOR XML PATH('')
), 1, 1, '') as titemName
That way, you don't need strange, meaningless numbers floating around the code.
Second, you may need indexes. Based on the highlighted performance problems, I would suggest:
tItems(CustomerID)
and:
tshots(JobNumber, ArtDirection, AdDate)
You have awful lot of string manipulation going on is your query, anyway a slightly improved version of your query would look something like...
Select DISTINCT
tJobs.*
, tCustomer.Name AS Customer_name
, AE.First + ' ' + AE.Last AS AccountExecutive
, AM.First + ' ' + AM.Last AS AccountManager
, dbo.RetrunUserFavourite(tJobs.JobNumber,33369,'Employee')AS Favorites
, TS.shotcount
, SUBSTRING(( SELECT ', ' + SKU + ', ' + Source + ', ' + ModelNumber+ ', ' + Description
FROM tItems
where tItems.CustomerID=tCustomer.CustomerID
FOR XML PATH('')), 3, 200000)titemName
, SUBSTRING(( SELECT ', ' + ArtDirection +', '+REPLACE(CONVERT(VARCHAR(5),AdDate,110), '-','/')
FROM tShots
where tShots.JobNumber=tJobs.JobNumber
FOR XML PATH('')), 3, 200000)TshotName
From tJobs
inner join tCustomer on tCustomer.CustomerID = tJobs.CustomerID
Left join tEmployee AE ON AE.EmployeeID = tJobs.AccountExecutiveID
Left join tEmployee AM ON AM.EmployeeID = tJobs.AccountManagerID
Left join (
SELECT JobNumber , Count(*) shotcount
FROM tShots
GROUP BY JobNumber
) TS ON TS.JobNumber = tJobs.JobNumber
WHERE tCustomer.CustomerID = 68666
A Couple of Pointers:
Having sub-queries in your select statement makes it very inefficient, because the sub-query is executed for each row returned by the outer query, a more sensible way of doing it would be to use joins.
You Also have a call to a User-Defined Scalar function dbo.RetrunUserFavourite() in your select , these scalar UDFs are also performance killers, again the same execution logic is applied here, they are also executed for each row returned by the outer query, a more sensible way would be to put the function logic/code inside a CTE and join your query to that CTE.
These comma delimited lists that you are creating on the fly for last two columns will be slow, maybe an Inline-Table-Valued function can give better performance here.
I tried as shown below
SELECT SUBSTRING(
(
SELECT td.Text + ', ' AS 'data()',
Tda.FirmID
FROM tblData1 tda
INNER JOIN tblData2 Td
ON Tda.ID = Td.ID
GROUP BY
Tda.Enable1,
Tda.ID,
Td.Text
HAVING ISNULL(Enable1, 0) = 1
FOR XML PATH('')
),
1,
10000
) AS Csv
Output:
Landlord Tenant, <FirmID>1</FirmID>NJ Traffic, <FirmID>1</FirmID>
Expected Output: Should be in table format
csv FirmID
Landlord Tenant, NJ Traffic 1
I got the results by using COALESCE
DECLARE #Text VARCHAR(MAX)
DECLARE #ID NUMERIC(18,0)
SELECT #Text = COALESCE(#Text + ', ', '') +
CAST(td.Text AS VARCHAR(MAX)),#ID = MAX(tda.FirmID)
FROM tblData1 tda
INNER JOIN tblData2 Td
ON Tda.ID=Td.ID
GROUP BY
Tda.Enable,
Tda.FirmID,Td.Text
HAVING ISNULL(Enable,0)=1
SELECT #Text AS Text,#ID AS ID
OutPut:
text ID
Landlord Tenant, NJ Traffic 1
Leave out the aliases in the subquery and try this:
SELECT STUFF((SELECT ', ' + td.Text
FROM tblData2 Td
WHERE ISNULL(Enable1, 0) = 1
FOR XML PATH('')
), 1, 2, '') AS Csv,
MAX(Tda.FirmID)
FROM tblData1 tda;
You cannot select multiple columns in the subquery either. After all, the purpose is to combine values into a single column. Multiple values in the select create one column, but with multiple XML tags within the string.
You also do not need the group by in the subquery.
I have this tables:
T:
D:
What I am trying to do is to get for each s_id all it's symbols (DBSymbol) in one cell (merge cells).
I have found this tutorial, and here is my code:
select T.s_id,
(select '; ' + D.symbol
from D
where T.D_b_id = D.id
FOR XML PATH('')) [DBSymbol]
from T
but here is what I am getting:
What is wrong??
Try this -
SELECT t1.s_id,
STUFF(
(SELECT '; ' + symbol AS [text()]
FROM (
SELECT t.s_id,
d.symbol
FROM T
INNER JOIN D ON T.d_b_id = D.id
WHERE t.s_id = t1.s_id
) x
FOR XML PATH('')
), 1, 1, '')
FROM T t1
GROUP BY t1.s_id
Check it: SQL Fiddle
select DISTINCT T.s_id,
Stuff((SELECT DISTINCT '; ' + D.symbol
from D
--where T.D_b_id = D.id
FOR XML PATH('')),1,1,'') [DBSymbol]
from T
Example here
I'm trying to concatenate several names into a single column but I'm having trouble getting there.
DECLARE #Names VARCHAR(8000)
SELECT
p.Pato_Id
#Names = COALESCE(#Names + ', ', '') + e.First_Name + ' ' + e.Last_Name
FROM
Patos p
LEFT JOIN
Pato_Owners po
ON
po.Pato_Id = p.Pato_Id
LEFT JOIN
Person e
ON
po.Owner_Id = e.Person_Id
How can this be done?
EDIT:
When I'm making a normal select I getting
PatoID First Last
0 John Wort
0 Dan Mass
1 Till Bos
2 Wrap Sim
2 Port Lock
And what I want is:
PatoID Names
0 John Wort, Dan Mass
1 Till Bos
2 Wrap Sim, Port Lock
You have left join which indicate that you can have null names. This would reset the values of names should that occur.
;with cte as
(
SELECT
p.Pato_Id,
coalesce(e.First_Name, '') + coalesce(' ' + e.Last_Name, '') Name
FROM
Patos p
LEFT JOIN
Pato_Owners po
ON
po.Pato_Id = p.Pato_Id
LEFT JOIN
Person e
ON
po.Owner_Id = e.Person_Id
)
select t.Pato_id
,STUFF((
select ',' + [name]
from cte t1
where t1.Pato_Id = t.Pato_Id
for xml path(''), type
).value('.', 'varchar(max)'), 1, 1, '') [Names]
from cte t
group by t.Pato_id
Try this,
DECLARE #Names VARCHAR(8000)
SELECT
p.Pato_Id,
'ServerName' = COALESCE(#Names + ', ', '') + e.First_Name + ' ' + e.Last_Name
FROM
Patos p
LEFT JOIN
Pato_Owners po
ON
po.Pato_Id = p.Pato_Id
LEFT JOIN
Person e
ON
po.Owner_Id = e.Person_Id
I think you want this,
WITH Sales_CTE (Pato_Id, First, Last)
AS
(
Select p.Pato_Id , e.First , e.Last
FROM
Patos p
LEFT JOIN
Pato_Owners po
ON
po.Pato_Id = p.Pato_Id
LEFT JOIN
Person e
ON
po.Owner_Id = e.Person_Id
)
SELECT Pato_Id,
Substring((Select ',' + ISNULL(First,'') + ISNULL(Last,'') From Sales_CTE B Where B.Pato_Id=A.Pato_Id For XML Path('')),2,8000) As names
from Sales_CTE A group by Pato_Id;