0

我目前拥有的是:

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

我想有逗号分隔的值,也想对金额列使用分组子句。

目前我要做的是:

    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
4

2 回答 2

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]

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

于 2012-12-10T13:09:46.450 回答
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 值之间也没有空格。您可以通过更改 select ',' 来调整它,以便在其中有一个空格和 STUFF 命令的参数(将第二个 1 更改为 2)。

基本上 FOR XML PATH('') 位采用其给定的迷你结果集并返回一些没有 XML 文字的文本(由于 '')。这是使用 STUFF 整理的,以删除 XML PATH 结果开头的前导 , 。

希望这可以帮助!:)

于 2012-12-10T13:06:00.263 回答