3

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.

6
  • 3
    Do you have index on Brand and cust_id columns? Commented Aug 31, 2012 at 6:49
  • 3
    Do you really need the distinct in your sub-query? It will be faster if you remove it. The distinct in 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 both distinct, the one in the sub-query will affect performance. Commented Aug 31, 2012 at 7:00
  • I have checked by removing distinct but no diffrence in the time Commented Aug 31, 2012 at 7:10
  • @KerrekSB what sort of aggregate is that? Commented Aug 31, 2012 at 7:45
  • 2
    @KerrekSB I'm afraid the answer is no: msdn.microsoft.com/en-us/library/ms173454 Commented Aug 31, 2012 at 7:55

2 Answers 2

1

Try this SQLFiddle example. It uses recursive query with CTE. For faster results you need indexes on Brand, Cust_ID, Prd_ID:

with t2 as 
( select t0.*,
  row_number() over (partition by Brand,Cust_id order by Prd_id asc) G_id
  from
 (
  select distinct Brand,Cust_id,Prd_id from tbdSales
 ) t0 

 ),
  t1 as (
    select t.*,
           cast(Prd_id as varchar(max)) as m2
     from t2 t where g_id=1

 union all
 select b.*,
        cast(c.m2+','+b.Prd_id as varchar(max)) as m2
     from t2 b
         inner join t1 c
             on (c.g_id+1 = b.G_id) 
                and (b.Brand=c.Brand)
                and (b.Cust_id=c.Cust_Id)


)
  select brand,cust_id,M2 as Prd_id from 
  (
  select t1.*, 
         row_number() over (partition by Brand,Cust_id order by g_id desc) rn 
             from t1
  ) t3 where rn=1
 order by Brand,Cust_id
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Thanks for replay. By Using above query i am geting result but still timing is same issue
You should create indexes to get results faster. For example: CREATE INDEX Idx_tbdSales_Brand ON tbdSales(Brand); CREATE INDEX Idx_tbdSales_Cust_ID ON tbdSales(Cust_ID); CREATE INDEX Idx_tbdSales_Prd_ID ON tbdSales(Prd_ID);
0
SELECT distinct brand, Cust_ID, Replace( Replace( Replace(
                            (   select t2.Prd_ID
                                from @Sales as t2 
                                where t2.brand = t1.brand and t2.Cust_ID = t1.Cust_ID
                                For XML Raw)
                                 , '"/><row Prd_ID="', ', ')
                                 , '<row Prd_ID="', '')
                                 , '"/>', '') FROM  @Sales t1

Hi, try this.

1 Comment

Hi Thanks for replay. By Using above query i am geting result but still timing is same issue

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.