简体   繁体   English

条件级联T-SQL

[英]Conditional Concatenation T-SQL

I have observed a kind of similar question T SQL Conditional String Concatenation but stil can't get my hear around how to do this., I can't even figure out where to start. 我已经观察到一种类似的问题,即T SQL条件字符串连接,但是stil无法让我听到如何做的事情。我什至不知道从哪里开始。

My query so far looks like this: 到目前为止,我的查询如下所示:

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

The report is based around R.ReservationsID 'Job No' However there can be multiple "Sale Units" and "Purchase Units" to each "Job No" so what I am trying to do is concatenate the various "purchase units" and "Sale units" into 1 single column separated by commas to align with the "Job Number". 该报告基于R.ReservationsID 'Job No'但是每个“ Job No”可以有多个“ Sale Units”和“ Purchase Units”,因此我要尝试的是将各种“ purchase unit”和“ Sale”串联起来单位”分成1个单列,以逗号分隔,以与“职位编号”对齐。

To confuse this even more I would also like to concat the QTY of Purchase Units & Sale Unit's into the column so it would look a little like this. 让我更加困惑的是,我还想将“购买单位和销售单位”的“数量”合并到该列中,这样看起来就有点像这样。

Desired Output: 所需输出:

New_Concat_Column

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

Any pointers about how to go about this would be greatly appreciated. 任何有关如何做到这一点的指针将不胜感激。 It would be good to do this in pure SQL but any methods in Crystal Reports are also welcome. 在纯SQL中执行此操作会很好,但也欢迎Crystal Reports中的任何方法。

You need to use CAST or CONVERT to change the numeric data to strings: 您需要使用CASTCONVERT来将数字数据更改为字符串:

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

Aside: It is often helpful to tag database questions with the database software and version, eg SQL Server 2008R2, and to include applicable table schemas. 另外:用数据库软件和版本(例如SQL Server 2008R2)标记数据库问题并包括适用的表架构通常很有帮助。 I have assumed that Sale Unit is a string of some sort. 我以为“ Sale Unit是某种字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM