0

我观察到了一个类似的问题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 中的任何方法。

4

1 回答 1

0

您需要使用CASTCONVERT将数字数据更改为字符串:

Cast( R.Quantity as VarChar(10) ) + 'x' + SM.StockDescription + ', ' +
  Cast( R.Quantity as VarChar(10) ) + 'x' + SM.StockDescription  + ',' as New_Concat_Column

旁白:用数据库软件和版本(例如 SQL Server 2008R2)标记数据库问题并包含适用的表模式通常很有帮助。我假设这Sale Unit是某种字符串。

于 2013-06-11T10:59:06.750 回答