条件级联T-SQL

时间:2013-06-11 10:53:12

标签: sql tsql crystal-reports

我观察到一种类似的问题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' 但是,每个“作业号”可以有多个“销售单位”和“购买单位”,所以我要做的是将各种“购买单位”和“销售单位”连接成1个单独的列,用逗号分隔以与“工作号码”。

为了更加混淆这一点,我还希望了解购买单位的数量&销售单位进入专栏,所以看起来有点像这样。

期望的输出:

New_Concat_Column

[QTY] + 'x' + [SALE UNIT] + ","[QTY] + 'x' + [SALE UNIT] + ","

任何关于如何解决这个问题的提示都将非常感激。在纯SQL中执行此操作会很好,但Crystal Reports中的任何方法也是受欢迎的。

1 个答案:

答案 0 :(得分: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是某种字符串。