-2

我创建了 3 个表

表 1 保存用户销售日期的“Sale”
表 2 将 factorid 和 productid 保存在其中 factoreid 表中的“ProductFactor”
3 “Product” 保存了 productid 和产品名称

我想在 3 表中选择这个结果:

显示用户 factorid +price+ saledate + 在这个 factorid 中可用的任何产品名称

但是什么时候这样做,例如当在一个具有 3 个 productid 的 factorid 中时,显示如下:

    
    日期factoreid名称价格
    2013-09-25 1 1 一 2000
    2013-09-25 1 1 b 3000
    2013-09-25 2 2 c 4000
    2013-09-25 3 3 d 3500
    2013-09-25 1 1 e 8000

我想展示这个:

    
    日期 factoreid 名称 价格 sumprice
    2013-09-25 1 1 a,b,ea价格,b价格,c价格这3个产品名称的总和
    2013-09-25 2 2 c 4000
    2013-09-25 3 3 d 3500 我的代码是:
CREATE PROCEDURE [dbo].[GetUserSaleReport] 
    @CurrentUserId uniqueidentifier 
AS BEGIN 

SELECT      dbo.Sale.SaleDate,
            dbo.ProductFactor.FactoreId,
            dbo.Product.ProductName,
            dbo.Product.Price
FROM        Sale 

INNER JOIN  ProductFactor 
    ON      dbo.Sale.FactoreId=dbo.ProductFactor.FactoreId  

INNER JOIN dbo.Product
    ON     dbo.ProductFactor.ProductId=dbo.Product.ProductId  

WHERE      dbo.Sale.UserId = @CurrentUserId 
    AND    dbo.Sale.FactoreId=dbo.ProductFactor.FactoreId
    AND    dbo.ProductFactor.ProductId=dbo.Product.ProductId  

ORDER BY   dbo.Sale.SaleDate 

END
4

1 回答 1

0

我没有所有三个表,但假设所有这些数据都来自一个表,你可以做这样的事情......你可以在加入所有表并在一个地方获得所有必需的数据后应用相同的登录

SELECT DISTINCT  [DATE], Factoreid, STUFF(T2.NameList, 1, 2, '') AS name, STUFF(T3.PriceList, 1, 2, '') AS price, SUM(Price) PriceSUM
    FROM TableName t 
                OUTER APPLY (
                            SELECT ', ' + name [text()]
                            FROM TableName 
                            WHERE Factoreid = t.Factoreid
                            FOR XML PATH('')
                            ) T2(NameList)
                OUTER APPLY (
                            SELECT  ', ' + CAST(price AS VARCHAR) [text()]
                            FROM TableName 
                            WHERE Factoreid = t.Factoreid
                            FOR XML PATH('')
                            ) T3(PriceList)
    GROUP BY [DATE], Factoreid, STUFF(T2.NameList, 1, 2, ''), STUFF(T3.PriceList, 1, 2, '')

我已经尝试调整了我认为它会起作用的代码试试这个

CREATE PROCEDURE [dbo].[GetUserSaleReport] 
@CurrentUserId uniqueidentifier 
AS BEGIN 
    SET NOCOUNT ON;

IF OBJECT_ID('tempdb..#Temptable') IS NOT NULL
DROP TABLE #Temptable    

SELECT      dbo.Sale.SaleDate           AS [DATE],
            dbo.ProductFactor.FactoreId AS Factoreid,
            dbo.Product.ProductName     AS Name,
            dbo.Product.Price           AS Price
        INTO #Temptable
FROM        Sale 

INNER JOIN  ProductFactor 
    ON      dbo.Sale.FactoreId=dbo.ProductFactor.FactoreId  

INNER JOIN dbo.Product
    ON     dbo.ProductFactor.ProductId=dbo.Product.ProductId  

WHERE      dbo.Sale.UserId = @CurrentUserId 
    AND    dbo.Sale.FactoreId=dbo.ProductFactor.FactoreId
    AND    dbo.ProductFactor.ProductId=dbo.Product.ProductId  

ORDER BY   dbo.Sale.SaleDate 

SELECT DISTINCT  [DATE], Factoreid, STUFF(T2.NameList, 1, 2, '') AS name, STUFF(T3.PriceList, 1, 2, '') AS price, SUM(Price) PriceSUM
FROM #Temptable t 
            OUTER APPLY (
                        SELECT ', ' + Name [text()]
                        FROM #Temptable 
                        WHERE Factoreid = t.Factoreid
                        FOR XML PATH('')
                        ) T2(NameList)
                OUTER APPLY (
                            SELECT  ', ' + CAST(price AS VARCHAR) [text()]
                            FROM #Temptable 
                            WHERE Factoreid = t.Factoreid
                            FOR XML PATH('')
                            ) T3(PriceList)
GROUP BY [DATE], Factoreid, STUFF(T2.NameList, 1, 2, ''), STUFF(T3.PriceList, 1, 2, '')

IF OBJECT_ID('tempdb..#Temptable') IS NOT NULL
DROP TABLE #Temptable   

  SET NOCOUNT OFF;

END
于 2013-10-10T20:58:03.443 回答