从日期间隔获取行,即使它不存在于一个表中

时间:2012-09-04 09:39:25

标签: sql

我有这个查询

SELECT Date,  IFNULL(Price, '------') AS Price
       FROM productHistory
WHERE Date between '2012-08-15' and '2012-08-19' AND Company='AAA' AND Product='PPP'
GROUP BY Date

结果是:

Date.......Price
-------------------
2012-08-15...100,00 
2012-08-19...110,00

它应该是这样的:

Date.........Price
-------------------------------------
2012-08-15......100,00   
2012-08-16......--------
2012-08-17......--------
2012-08-18......--------
2012-08-19......110,00

我只使用一张桌子,我在这个论坛中检查了类似的静止但我找不到解决方案。

当我得到这个时,我想要做的是在查询中添加更多公司作为列来获得这样的结果。好吧,我必须改变查询..

Date.........PriceCompany1.....PriceCompany2.....PriceCompany3 
----------------------------------------------------------  
2012-08-15......100,00................................100,00..................................100,0
2012-08-16......---------...............................100,00...................................---------
2012-08-17......---------..............................----------.................................110,00
2012-08-19......110,00..............................100,00..................................----------

我需要做什么?

2 个答案:

答案 0 :(得分:0)

为此

创建StoredProcedure
CREATE PROCEDURE [dbo].[GetMasterData]
   @startDate DATETIME,
   @endDate DATETIME
AS    
BEGIN
SET NOCOUNT ON;

WITH dates(Date) AS 
(
    SELECT @startdate as Date
    UNION ALL
    SELECT DATEADD(d,1,[Date])
    FROM dates 
    WHERE DATE < @enddate
)
SELECT Date as Date1  into #tmp1  FROM dates
OPTION (MAXRECURSION 0) 


Select Date1,IsNULL(Price, '------') AS Price  from #tmp1 left outer join
productHistory on  #tmp1.Date1 = productHistory.Date
and Company='AAA' AND Product='PPP'

Drop table #tmp1

END

并使用下面的行执行sp

exec GetMasterData '08 / 15/2012','08/19/2012'

答案 1 :(得分:0)

不需要程序,只需简单的SQL即可:

-- generate "pseudo calendar table" from the data:
WITH dt AS (
        SELECT DISTINCT zdate FROM producthistory
        WHERE zdate between '2012-08-10' and '2012-08-20'
        )
-- a poor man's PIVOT:
SELECT dt.zdate
        , pa.price AS price_a
        , pb.price AS price_b
        , pc.price AS price_c
FROM dt
LEFT JOIN producthistory pa ON pa.zdate = dt.zdate AND pa.company='AAA' AND pa.product='ppp'
LEFT JOIN producthistory pb ON pb.zdate = dt.zdate AND pb.company='BBB' AND pb.product='ppp'
LEFT JOIN producthistory pc ON pc.zdate = dt.zdate AND pc.company='CCC' AND pc.product='ppp'

ORDER BY zdate
;

由于OP没有显示任何表格定义或数据,我必须发明自己的:

CREATE TABLE producthistory
        ( company varchar
        , product varchar
        , zdate Date NOT NULL
        , price INTEGER
        );

INSERT INTO producthistory ( company, product, zdate, price ) VALUES
  ( 'AAA', 'ppp', '2012-08-15', 100)
, ( 'AAA', 'ppp', '2012-08-15', 110)
, ( 'AAA', 'ppp', '2012-08-15', 120)
, ( 'BBB', 'ppp', '2012-08-16', 200)
, ( 'BBB', 'qqq', '2012-08-16', 210)
, ( 'BBB', 'ppp', '2012-08-16', 220)
, ( 'CCC', 'ppp', '2012-08-15', 300)
;

结果:

CREATE TABLE
INSERT 0 7
   zdate    | price_a | price_b | price_c 
------------+---------+---------+---------
 2012-08-15 |     100 |         |     300
 2012-08-15 |     110 |         |     300
 2012-08-15 |     120 |         |     300
 2012-08-16 |         |     200 |        
 2012-08-16 |         |     220 |        
(5 rows)

添加IFNULL / COALESCE功能(或其他格式化琐事)留给读者练习