Consulta T-SQL, combina columnas de varias filas en una sola columna

I have seeen some examples of what I am trying to do using COALESCE and FOR XML (seems like the better solution). I just can't quite get the syntax right.

Here is what I have (I will shorten the fields to only the key ones):

Table                    Fields
------                   -------------------------------
Requisition              ID, Number
IssuedPO                 ID, Number
Job                      ID, Number
Job_Activity             ID, JobID (fkey)
RequisitionItems         ID, RequisitionID(fkey), IssuedPOID(fkey), Job_ActivityID (fkey)

I need a query that will list ONE Requisition per line with its associated Jobs and IssuedPOs. (The requisition number start with "R-" and the Job Number start with "J-").


R-123 | "PO1; PO2; PO3" | "J-12345; J-6780"

Sure thing Adam!

Here is a query that returns multiple rows. I have to use outer joins, since not all Requisitions have RequisitionItems that are assigned to Jobs and/or IssuedPOs (in that case their fkey IDs would just be null of course).

SELECT DISTINCT Requisition.Number,  IssuedPO.Number, Job.Number
        FROM Requisition
        INNER JOIN RequisitionItem on RequisitionItem.RequisitionID = Requisition.ID
        LEFT OUTER JOIN Job_Activity on RequisitionItem.JobActivityID = Job_Activity.ID
        LEFT OUTER JOIN Job on Job_Activity.JobID = Job.ID
        LEFT OUTER JOIN IssuedPO on RequisitionItem.IssuedPOID = IssuedPO.ID

preguntado el 08 de enero de 11 a las 23:01

A query is not the best place to do such things: what you want is against the relational nature of SQL. You can do the grouping either in your app code or in Transact-SQL stored procedure code. -

@9000 While I agree that it isn't something ideally done in SQL, I can't see how doing it in a stored procedure would be any "better" (and it would be identical query syntax, unless you moved to doing something in temp or memory tables). -

@adam-robinson: In TSQL you have cursor loops and mutable variables that help assemble several rows into one string. You can create a function that does an entire detail query and packs it into a string, and use it in a master query. -

@9000: What I'm saying is that there's nothing particularly mejor about using a stored procedure here than doing it in a single query. In any case, for string concatenation like he's looking for, using FOR XML is generally better (and has the benefit of not requiring any actual procedural code), even if it's a bit difficult to digest. -

Your relationship between requisitions and jobs is not obvious to me. Perhaps you should post an example query that gives you the data you want in individual rows, then someone can provide you with a FOR XML-based query to concatenate the values and reduce the results to one row per requisition. -

1 Respuestas

Here's one way to do it using subqueries:

select  'R-' + cast(r.number as varchar(32)) as RequisitionNumber
,       (
        select  'PO' + CAST(ip.number as varchar(32)) + ';'
        from    IssuedPO ip
        join    RequisitionItems ri
        on = ri.IssuedPOID
        where   ri.RequisitionID =
        for xml path('')
        ) as POList
,       (
        select  'J-' + CAST(j.number as varchar(32)) + ';'
        from    Job j
        join    Job_Activity ja
        on = ja.JobID
        join    RequisitionItems ri
        on      ri.Job_ActivityID =
        where   ri.RequisitionID =
        for xml path('')
        ) as JobList
from    Requisition r

Respondido el 09 de enero de 11 a las 04:01

I will try that out, thanks. You didnt have to prepend the "R-" and "PO-" that was just for clarity. :) - Shayne

Remember to use a cast if you're using it report or else it'd return an invalid data type - LeSteelBox

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.