Prathap
Prathap

Reputation: 313

Concatenate a single Column value depending on Condition

Below are the 3 tables

QuotationMaster

QuoteID QuoteNo  CustomerName
-----------------------------
1       Q1      Name1 
2       Q2     Name2   
3       Q3      Name3   
4       Q4      Name4   
5       Q5      Name5   

QuoteItemDetails : one quote can have many items

QuoteItemID  QuoteID    ItemCode    ItemID   
---------------------------------------------
   1          1         100          1 
   1          1         200          2       
   2          2         200          2       

QuoteBatchDetails : one QuoteItem can have many batches of QuoteID and ItemID are the common columns. BatchNo is varchar

QuotebatchID  QuoteID   BatchNo    ItemID   
---------------------------------------------
   1          1          A          1       
   2          1          B          1  
   3          1          C          2  
   4          2          E          2       
   5          2          F          2  

I want the result as

  QuoteID    QuoteNo     CustName  ItemCode  BatchNo     
    -------------------------------------------------
       1          Q1         Name1     100       A,B 
       1          Q1         Name1     200        C     
       2          Q2         Name2     200       E,F   

I want to create a procedure which takes QuoteID as parameter of INT type and get the result as above.

The only problem I am facing is to concatenate the BatchNo which depends on ItemID and further on QuoteID.

Using the below query I am able to concatenate the BatchNo for a particular ID but I am not sure how to add this to the main procedure, when I do that errors pops up like subquery returns more than one value.I understand because for every quote there can be more than 1 item.

select 
    ID.QuoteID,ID.ItemID,
    stuff((select ', ' + BatchNo 
           from SD_QuoteBatchDetails BD where ID.ItemID=BD.ItemID and ID.QuoteID=BD.QuoteID       
           for xml path('')),
          1,2,'') [Values]
from SD_QuoteItemDetails QID,SD_QuoteBatchDetails ID where ID.QuoteID=QID.QuoteID 
group by ID.ItemID,ID.QuoteID

Can anyone provide a query for the same.

Upvotes: 1

Views: 309

Answers (1)

John Woo
John Woo

Reputation: 263893

SELECT  b.QuoteItemID, 
        a.QuoteNo,
        a.CustomerName,
        b.ItemCode,
        c.BatchList
FROM    QuotationMaster a
        INNER JOIN QuoteItemDetails b
            ON a.QuoteID = b.QuoteID
        INNER JOIN
        (
          SELECT
               QuoteID, 
               ItemID,
               STUFF(
                   (SELECT ', ' + BatchNo
                    FROM   QuoteBatchDetails
                    WHERE  QuoteID = a.QuoteID AND
                           ItemID = a.ItemID
                    FOR XML PATH (''))
                    , 1, 1, '')  AS BatchList
          FROM  QuoteBatchDetails AS a
          GROUP BY QuoteID, ItemID
        ) c ON  b.QuoteID = c.QuoteID  AND
                b.ItemID = c.ItemID;

Upvotes: 1

Related Questions