我观察到了一个类似的问题T SQL Conditional String Concatenation但我仍然无法听到如何做到这一点。我什至不知道从哪里开始。
到目前为止,我的查询如下所示:
SELECT
R.ReservationsID 'Job No',
C.[Trading Name]'Customer Name',
C.[Business Address1] 'Customer Address1',
C.[Business Address2]'Customer Address2',
C.[Business Town]'Customer Town',
C.[Business Postal Code]'Customer Postcode',
C.CustomerID' Customer ID',
I.InvoiceID' InvoiceID' ,
'address' AS [Supplier Address 1], --CHANGE
'address' AS [Supplier Address 2], --CHANGE
'town' AS [town], --CHANGE
'post code' AS [Post Code], --CHANGE
'Supplier' AS [Supplier], --CHANGE
R.Quantity 'Purchase QTY',
SM.Instock 'QTY In Stock',
SM.StockCode 'Product Code',
SM.StockDescription 'Sale Unit',
R.[Delivery Date],
V.[Registration]'Vehicle Reg',
I.[NET] 'Sale Price',
I.[GROSS] 'Total Sale Price',
R.[Delivery Date] ' Del Date',
R.Quantity as qty, --CHANGE
SM.StockDescription AS [Purchase Unit], --CHANGE
0.0 AS [Purchase total price], --CHANGE
0.0 AS [PO No],
(D.Forename + ' ' + D.Surname) AS [Driver Name],
(convert(varchar(10),dateadd(MM,-7,GETDATE()),103)) AS 'Date -7 Months',
0.0 AS [Purchase Invoice],
0.0 AS [Purchase Price],
RI.StockID
FROM
dbo.tblReservation R
LEFT JOIN tblReservationItems RI
ON RI.ReservationsID = R.ReservationsID
AND (RI.Deleted != 1) AND (R.Completed !=0)
LEFT JOIN tblInvoice I ON I.InvoiceID = R.Invoice AND (I.Deleted != 1)
LEFT JOIN tblCustomer C ON C.CustomerID = R.CustomerID
LEFT JOIN tblStockMaster SM ON SM.StockID = RI.StockID
LEFT JOIN dbo.tblvehicle V ON v.VehicleID = R.VehicleID
LEFT JOIN dbo.tblDriver D ON D.DriverID = R.DriverID
WHERE (I.Deleted = 0)
AND (r.completed=1)
AND [Delivery Date] >= DATEADD(month, -7, GETDATE())
AND [Delivery Date] IS NOT NULL
该报告基于 R.ReservationsID 'Job No'
但是每个“工作编号”可以有多个“销售单位”和“采购单位”,所以我想要做的是将各种“采购单位”和“销售单位”连接成一个单独的列用逗号与“工作编号”对齐。
为了更加混淆这一点,我还想将购买单位和销售单位的数量合并到列中,这样它看起来有点像这样。
期望的输出:
New_Concat_Column
[QTY] + 'x' + [SALE UNIT] + ","[QTY] + 'x' + [SALE UNIT] + ","
任何有关如何解决此问题的指示将不胜感激。最好在纯 SQL 中执行此操作,但也欢迎使用 Crystal Reports 中的任何方法。