Hi I have a table Called tbdSales
Brand Cust_ID Prd_ID
Aftron 44301 T3485
Aftron 44301 T0628
Aftron 44301 T2952
Aftron 44301 T1958
Aftron 44302 T1940
Aftron 44302 T1939
Aftron 44303 T2419
Aftron 44303 T2045
In this table I want the Product_ID in comma separated with group by the Brand & Cust_ID
I have produced the query as follows:
SELECT DISTINCT
Brand
, Cust_ID
, (
SELECT DISTINCT second_id + ', '
FROM tbdSales t2
WHERE t2.Brand = t1.Brand AND t2.Cust_ID = t1.Cust_ID
FOR XML PATH('')
) AS prd_ID into SalReport
FROM tbdSales t1
GROUP BY Brand,Cust_ID
The above query is giving results. But, if the records are more (10,000) then it's taking much time like 5 mins.
Please let me know any other way for reducing query completion time.
distinctin your sub-query? It will be faster if you remove it. Thedistinctin your main query is unnecessary since you are already doing a group by but the query optimizer is smart enough to realize that and has probably already optimized that away. So, remove bothdistinct, the one in the sub-query will affect performance.