0

我有一个包含字段的销售表:产品、日期、数量。我需要像这样以每日格式提取产品明智销售

Product 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 .............. 31 Total Sale
Rice    X X X X X X X X X XX XX XX XX XX XX XX............... XX    
Tea     X X X X X X X X X XX XX XX XX XX XX XX............... XX

有没有人知道如何做到这一点。

4

5 回答 5

2

您将不得不使用PIVOT,我在这里最多只放了 6 天,最多包括 31

WITH base AS (
SELECT product, day(date) AS DAY, sum(qty) AS tot
FROM product GROUP BY product, date)
SELECT product, [1], [2], [3], [4], [5], [6]
FROM base pivot(sum(tot) FOR DAY IN ([1],[2],[3],[4],[5],[6])) as tot;
于 2013-07-08T08:13:10.947 回答
0

这是你想要的吗?

SELECT PRODUCT, T_DATE,SUM(QTY)
FROM (SELECT PRODUCT, TRUNC(DATE) T_DATE,QTY
       FROM TABLE_NAME) 
GROUP BY PRODUCT, T_DATE
ORDER BY PRODUCT
于 2013-07-08T08:01:06.330 回答
0

尝试 -:

DECLARE @cols1 NVARCHAR(MAX);
DECLARE @cols2 NVARCHAR(MAX);

DECLARE @sql NVARCHAR(MAX);
DECLARE @myDate DATE;
DECLARE @dNum AS INT;

SET @myDate = '2013-01-01'
SET @dNum = DATEDIFF(DAY, DATEADD(DAY, 1-DAY(@myDate), @myDate), DATEADD(MONTH, 1, DATEADD(DAY, 1-DAY(@myDate), @myDate)))

DROP TABLE ##tttt;

CREATE TABLE ##tttt (
    [product] NVARCHAR(20)
    , [date] DATETIME
    , [quantity] INT
)

INSERT INTO ##tttt ([product], [date], [quantity]) VALUES ('Rice', '2013-01-01', 1)
, ('Rice', '2013-01-02', 5)
, ('Rice', '2013-01-03', 2)
, ('Rice', '2013-01-04', 5)
, ('Rice', '2013-01-05', 3)
, ('Rice', '2013-01-30', 4)
, ('Rice', '2013-01-30', 15)
, ('Rice', '2013-01-30', 1)
, ('Rice', '2013-01-30', 3)
, ('Rice', '2013-01-30', 4)
, ('Tea', '2013-01-01', 1)
, ('Tea', '2013-01-01', 3)
, ('Tea', '2013-01-01', 4)
, ('Tea', '2013-01-01', 1)
, ('Tea', '2013-01-01', 6)
, ('Tea', '2013-01-01', 7)
, ('Tea', '2013-01-03', 2)
, ('Tea', '2013-01-05', 3)
, ('Tea', '2013-01-07', 4)
, ('Tea', '2013-01-08', 7)
, ('Tea', '2013-01-09', 8)
, ('Tea', '2013-01-10', 9)
, ('Tea', '2013-01-21', 1)
, ('Tea', '2013-01-23', 1)
, ('Tea', '2013-01-24', 4)
, ('Tea', '2013-01-31', 10)
, ('Tea', '2014-01-31', 200)

SELECT *
FROM ##tttt
WHERE [date] >= CAST(@myDate AS NVARCHAR(20)) AND [date] < CAST(DATEADD(MONTH, 1, @myDate) AS NVARCHAR(20))

DECLARE @i INT;
SET @i = 1

WHILE @i <= @dNum
BEGIN
    SET @cols1 = ISNULL(@cols1, '') + CASE WHEN @i > 1 THEN ',' ELSE '' END + '[' + CASE WHEN LEN(@i) = 1 THEN '0' ELSE '' END + CAST(@i AS NVARCHAR(2)) + ']'
    SET @cols2 = ISNULL(@cols2, '') + CASE WHEN @i > 1 THEN ',' ELSE '' END + 'ISNULL([' + CASE WHEN LEN(@i) = 1 THEN '0' ELSE '' END + CAST(@i AS NVARCHAR(2)) + '], 0) AS [' + CASE WHEN LEN(@i) = 1 THEN '0' ELSE '' END + CAST(@i AS NVARCHAR(2)) +']'
    SELECT @i = @i + 1;
END;

SET @sql = 'SELECT [product], ' + @cols2 + '
FROM (
    SELECT [product], CASE WHEN LEN(DAY([date])) = 1 THEN ''0'' ELSE '''' END + CAST(DAY([date]) AS NVARCHAR(2)) AS day1, [quantity]
    FROM ##tttt
    WHERE [date] >= ''' + CAST(@myDate AS NVARCHAR(20)) + ''' AND [date] < ''' + CAST(DATEADD(MONTH, 1, @myDate) AS NVARCHAR(20)) + '''
) AS T
PIVOT (
    SUM([quantity])
    FOR day1 IN (' + @cols1 + ')
) PIVOTTABLE
;'

PRINT @sql
EXEC(@sql)
于 2013-07-08T08:54:20.793 回答
0

希望对你有帮助

    SELECT * 
FROM
    (
        SELECT productName, 
               [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13], [14], [15], [16], [17], [18], [19], [20], [21], [22], [23], [24], [25], [26], [27], [28], [29],[30], [31]
        FROM  
             (
               SELECT productName, day(pDate) AS dayOfDate, sum(quantity) AS Qty
               FROM [yourTableName]  --Enter here your table name]
               WHERE -- Use this to display data for July 2013.
                     Month(pDate) = 7 and YEAR(pDate) = 2013
               GROUP BY productName, day(pDate)
             ) AS tempTable
        PIVOT 
            (sum(Qty) FOR dayOfDate IN ([1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13], [14], [15], [16], [17], [18], [19], [20], [21], [22], [23], [24], [25], [26], [27], [28], [29],[30], [31])) as Qty
    ) AS detailTable,
    (
        SELECT productName, sum(quantity) as TOTAL
        FROM [yourTableName]  --Enter here your table name]
        WHERE -- Use this to display data for July 2013.
              Month(pDate) = 7 and YEAR(pDate) = 2013
        GROUP BY productName
    ) AS totalTable
WHERE totalTable.productName = detailTable.productName

我已经在 ADVENTUREWORKS 数据库上对其进行了测试。我使用了以下查询:

SELECT * 
FROM
    (
        SELECT productid, 
               [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13], [14], [15], [16], [17], [18], [19], [20], [21], [22], [23], [24], [25], [26], [27], [28], [29],[30], [31]
        FROM  
             (
               SELECT productid, day(Modifieddate) AS dayOfDate, sum(Orderqty) AS Qty
               FROM Sales.SalesOrderDetail  --Enter here your table name
               WHERE -- Use this to display data for February 2004.
                     Month(ModifiedDate) = 2 and YEAR(ModifiedDate) = 2004
               GROUP BY productid, day(Modifieddate)
             ) AS tempTable
        PIVOT 
            (sum(Qty) FOR dayOfDate IN ([1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13], [14], [15], [16], [17], [18], [19], [20], [21], [22], [23], [24], [25], [26], [27], [28], [29],[30], [31])) as Qty
    ) AS detailTable,
    (
        SELECT productid, sum(Orderqty) as TOTAL
        FROM Sales.SalesOrderDetail 
        WHERE -- Use this to display data for February 2004.
              Month(ModifiedDate) = 2 and YEAR(ModifiedDate) = 2004
        GROUP BY productid
    ) AS totalTable
WHERE totalTable.ProductID = detailTable.ProductID
于 2013-07-08T08:55:48.047 回答
-1

您可以在 oracle 中使用以下语句

SELECT 
  product
, sum( CASE  WHEN TO_CHAR(date, 'dd') = '01' THEN qty ELSE 0 END ) as 1
, sum( CASE  WHEN TO_CHAR(date, 'dd') = '02' THEN qty ELSE 0 END ) as 2
, sum( CASE  WHEN TO_CHAR(date, 'dd') = '03' THEN qty ELSE 0 END ) as 3
, sum( CASE  WHEN TO_CHAR(date, 'dd') = '04' THEN qty ELSE 0 END ) as 4
, sum( CASE  WHEN TO_CHAR(date, 'dd') = '05' THEN qty ELSE 0 END ) as 5
, sum( CASE  WHEN TO_CHAR(date, 'dd') = '06' THEN qty ELSE 0 END ) as 6
, sum( CASE  WHEN TO_CHAR(date, 'dd') = '07' THEN qty ELSE 0 END ) as 7
, sum( CASE  WHEN TO_CHAR(date, 'dd') = '08' THEN qty ELSE 0 END ) as 8
, sum( CASE  WHEN TO_CHAR(date, 'dd') = '09' THEN qty ELSE 0 END ) as 9
, sum( CASE  WHEN TO_CHAR(date, 'dd') = '10' THEN qty ELSE 0 END ) as 10
  sum( CASE  WHEN TO_CHAR(date, 'dd') = '11' THEN qty ELSE 0 END ) as 11
, sum( CASE  WHEN TO_CHAR(date, 'dd') = '12' THEN qty ELSE 0 END ) as 12
, sum( CASE  WHEN TO_CHAR(date, 'dd') = '13' THEN qty ELSE 0 END ) as 13
, sum( CASE  WHEN TO_CHAR(date, 'dd') = '14' THEN qty ELSE 0 END ) as 14
, sum( CASE  WHEN TO_CHAR(date, 'dd') = '15' THEN qty ELSE 0 END ) as 15
, sum( CASE  WHEN TO_CHAR(date, 'dd') = '16' THEN qty ELSE 0 END ) as 16
, sum( CASE  WHEN TO_CHAR(date, 'dd') = '17' THEN qty ELSE 0 END ) as 17
, sum( CASE  WHEN TO_CHAR(date, 'dd') = '18' THEN qty ELSE 0 END ) as 18
, sum( CASE  WHEN TO_CHAR(date, 'dd') = '19' THEN qty ELSE 0 END ) as 19
, sum( CASE  WHEN TO_CHAR(date, 'dd') = '20' THEN qty ELSE 0 END ) as 20
  sum( CASE  WHEN TO_CHAR(date, 'dd') = '21' THEN qty ELSE 0 END ) as 21
, sum( CASE  WHEN TO_CHAR(date, 'dd') = '22' THEN qty ELSE 0 END ) as 22
, sum( CASE  WHEN TO_CHAR(date, 'dd') = '23' THEN qty ELSE 0 END ) as 23
, sum( CASE  WHEN TO_CHAR(date, 'dd') = '24' THEN qty ELSE 0 END ) as 24
, sum( CASE  WHEN TO_CHAR(date, 'dd') = '25' THEN qty ELSE 0 END ) as 25
, sum( CASE  WHEN TO_CHAR(date, 'dd') = '26' THEN qty ELSE 0 END ) as 26
, sum( CASE  WHEN TO_CHAR(date, 'dd') = '27' THEN qty ELSE 0 END ) as 27
, sum( CASE  WHEN TO_CHAR(date, 'dd') = '28' THEN qty ELSE 0 END ) as 28
, sum( CASE  WHEN TO_CHAR(date, 'dd') = '29' THEN qty ELSE 0 END ) as 29
, sum( CASE  WHEN TO_CHAR(date, 'dd') = '30' THEN qty ELSE 0 END ) as 30
, sum( CASE  WHEN TO_CHAR(date, 'dd') = '31' THEN qty ELSE 0 END ) as 31
, sum(qty)                                                         as total
FROM 
table
group by product
;
于 2013-07-08T09:06:28.247 回答