使用存储的PRocedure在SQL中使用逗号分隔值

时间:2012-12-10 12:50:31

标签: sql-server-2008 stored-procedures

我目前拥有的是:

COUNT DETAILS:
CNT DTLID      COUNT           TOTAL QTY        UNITPRICE            AMOUNT
1              234                 2222           1.20                 32
1              12                   123           2                     21


What i want it to be like
CNT DTLID      COUNT           TOTAL QTY         UNITPRICE            AMOUNT
1              234,12          2222 , 123        1.20,2               32 + 21 = 53

我想使用逗号分隔值,并且还希望对amount列使用group by子句。

目前我的目标是:

    ALTER PROCEDURE [dbo].[sp_Tbl_CountDetail_SelectAll]
    -- Add the parameters for the stored procedure here

AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.

    select * from Tbl_CountDetail 
    inner join tbl_Contract
    on
    tbl_CountDetail.ContractID = tbl_Contract.ContractID
    inner join tbl_Count
    on
    tbl_CountDetail.CountID = tbl_Count.CountID
    where tbl_CountDetail.isDeleted = 0
    and tbl_Contract.isdeleted = 0
END

2 个答案:

答案 0 :(得分:0)

一些友好的样本数据

create table #CountDetails
(
DTLID int,
CNT int,
Qty int,
UnitPrice money,
Amount int
)

insert into #CountDetails
SELECT
1, 234, 2222, 1.20, 32
UNION ALL SELECT
1, 12, 123, 2, 21

这是一些代码

SELECT 
    DTLIDs.DTLID,

    CNTs =
        ISNULL(
            STUFF(
                (
                    select ',' + 
                        cast(CD.cnt as varchar(50))
                    from #CountDetails CD
                    where CD.DTLID = DTLIDs.DTLID
                    order by CD.CNT
                    FOR XML PATH('')
                ),
                1, 1, ''  --removes the leading ','
            ),
            ''
        ),

    QTYs =
        ISNULL(
            STUFF(
                (
                    select ',' + 
                        cast(CD.qty as varchar(50))
                    from #CountDetails CD
                    where CD.DTLID = DTLIDs.DTLID
                    order by CD.Qty
                    FOR XML PATH('')
                ),
                1, 1, ''  --removes the leading ','
            ),
            ''
        ),

    UnitPrices =
        ISNULL(
            STUFF(
                (
                    select ',' + 
                        cast(CD.UnitPrice as varchar(50))
                    from #CountDetails CD
                    where CD.DTLID = DTLIDs.DTLID
                    order by CD.UnitPrice
                    FOR XML PATH('')
                ),
                1, 1, ''  --removes the leading ','
            ),
            ''
        ),

    AmountSum =
    (
        select SUM(Amount) from #CountDetails CD
        where CD.DTLID = DTLIDs.DTLID
    )

from (
    select distinct DTLID from #CountDetails
    ) DTLIDs

有各种方法可以调整它。例如,“AmountSum =”嵌套查询代码可以在一个组上完成 - 我就像查看其余查询结构的方式一样更加一致。

对于CSV列表,您没有指定您希望如何排序。我按值排序(例如ORDER BY CD.CNT),但您可以根据需要将其更改为订购。同样,CSV值之间没有空格。您可以通过更改选择','来调整它,以便在那里有空格和参数到STUFF命令(将第二个1更改为2)。

基本上,FOR XML PATH('')位获取给定的迷你结果集,并返回一些没有XML文字的文本(由于'')。在XML PATH结果的开头,使用STUFF来删除前导。

希望这有帮助! :)

答案 1 :(得分:0)

可以这样做......

DECLARE @VALUES NVARCHAR(1000),@UnitPrice nvarchar(100)
SELECT @VALUES = COALESCE(@VALUES + ',','') + CAST(COUNTQty AS NVARCHAR(50)),
@UnitPrice = COALESCE(@UnitPrice + ',','') + CAST(UnitPrice AS NVARCHAR(50)) FROM tableName
SELECT @VALUES as [CountQty],@Ids as [UnitPrice]

没有按价格问题检查组.. !!