How to merge lines?

Asked

Viewed 404 times

1

How can I merge two or more **Linhas x Coluna** of the consultation below?

select cast(OS.CreateDate as date) as DtMovimentacao, OS.Id as NrOS, OS.Description Origem, OSI.Description as ItemSaida, OSI.Quantity as QtdSaida,
OSE.Description, OSE.Quantity, OSE.Notes
from Asset_WorkOrders OS
join Asset_WorkOrderOtherCost OSI
on OSI.WorkOrderId = OS.Id
join 
(
    select OS.WorkOrderId, OSI.Description,OSI.Quantity,OSI.Notes from Asset_WorkOrders OS
    join Asset_WorkOrderOtherCost OSI
    on OSI.WorkOrderId = OS.Id
    where TypeId = 56
)OSE
on OSE.WorkOrderId = OS.Id
where OS.Id = 147412
and OS.TypeId = 34

I needed the result to stay that way.

That in the query or through the Report Server.

  • You want a line to have N information from Itemsaida and Qtdsaida?

  • Exactly, something that will clear the field in case it is identical to the others leaving only one...

1 answer

2


EDIT: With SQL I think the only way is to check the information that is different between the lines and use the FOR XML PATH in that column, in the following example I used in the column "Description"

select cast(OS.CreateDate as date) AS DtMovimentacao, OS.Id AS NrOS,
Origem = (SELECT OS2.Description + CHAR(10)
FROM Asset_WorkOrders OS2
WHERE OS.Id = OS2.Id
  and OS.TypeId = OS2.TypeId
FOR XML PATH(''))
OS.Description as Origem,

OSI.Description as ItemSaida, OSI.Quantity as QtdSaida,
OSE.Description, OSE.Quantity, OSE.Notes
from Asset_WorkOrders OS
join Asset_WorkOrderOtherCost OSI
on OSI.WorkOrderId = OS.Id
join 
(
    select OS.WorkOrderId, OSI.Description,OSI.Quantity,OSI.Notes from Asset_WorkOrders OS
    join Asset_WorkOrderOtherCost OSI
    on OSI.WorkOrderId = OS.Id
    where TypeId = 56
)OSE
on OSE.WorkOrderId = OS.Id
where OS.Id = 147412
and OS.TypeId = 34

Since you have not included information from the tables, there may be some inconsistency in the result of the Origin column, in this case you have to include in the subquery some JOINs or validations in WHERE.

  • But then it would still bring the information in the two lines only now grouped in one column

  • Neither is it right ? you want the result of the query Merge columns in the Excel spreadsheet ?

  • Not in the spreadsheet of Excel, but in the way that the Excel makes, that in query or in report server.

  • With XML PATH you include a break line CHR(10) after each word, even then the row would be one, but in the column that use this function the data would be below each other, I think the best solution might be in Report Server, I do not know use it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.