如何在没有汇总的情况下进行数据透视?

时间:2018-12-17 02:52:15

标签: sql sql-server

我有下面列出的一些示例数据,希望有人可以帮助我。

Date                    WellName   Reading
----------------------------------------
11/30/2018 12:00:00 AM    Well One   10
11/30/2018 12:00:00 AM    Well Two   20
11/30/2018 12:00:00 AM    Well Three 10
12/1/2018 12:00:00 AM     Well One   14
12/1/2018 12:00:00 AM     Well Two   25
12/1/2018 12:00:00 AM     Well Three 11
12/2/2018 12:00:00 AM     Well One   17
12/2/2018 12:00:00 AM     Well Two   43
12/2/2018 12:00:00 AM     Well Three 27
12/3/2018 12:00:00 AM     Well One   25
12/3/2018 12:00:00 AM     Well Two   56
12/3/2018 12:00:00 AM     Well Three 67
12/16/2018 12:00:00 AM    Well One   10
12/16/2018 12:00:00 AM    Well Three 10
12/16/2018 12:00:00 AM    Well Two   11

我需要的是顶部列出的日期,并认为PIVOT可以解决问题,但我似乎无法使其正常工作。我不需要对值或任何东西求和,只需要翻转数据即可。

没有日期时,我还需要有一个值,所以我假设我需要创建一个日期表并将其联接。

有什么想法或建议吗?

2 个答案:

答案 0 :(得分:0)

日期可以翻转,但是您要根据井号或读数日期填写什么值。

我在这里使用了井名。您可以尝试以下代码。

create table stk_test2(dt datetime,wellname varchar(15),reading int)
select * from stk_test2

declare @col nvarchar(max)
select @col = 
(select replace(replace(STUFF((SELECT distinct ',[' +
                       convert(varchar(20),dt,21) as dt
                      FROM stk_test2
                      FOR XML PATH(''), TYPE
                     ).value('.', 'NVARCHAR(MAX)') 
                        , 1, 1, ''),'.',''),',','],'))+']'

select @col

declare @sql nvarchar(max)
select
 @sql = 'select * from 
( 
select dt,wellname from stk_test2
)ds
pivot(
Min(wellname) 
for dt in ('+@col+')
)fropp'

EXEC Sp_executesql @sql 

答案 1 :(得分:0)

感谢大家的建议-都非常有用。我最终使用以下查询并将结果扔到我正在使用的软件的Cross-Tab组件中...

declare @DateTable table (date DATETIME,  PRIMARY KEY (date))

DECLARE
  @basedate DATETIME,
  @offset   INT
SELECT
  @basedate = '{varFrom}',
  @offset = 1
  INSERT INTO @DateTable SELECT @basedate

WHILE ( DATEADD(DAY, @offset, @basedate) <= '{varTo}')
BEGIN
  INSERT INTO @DateTable SELECT DATEADD(DAY, @offset, date) FROM @DateTable where DATEADD(DAY, @offset, date) <= '{varTo}'
  SELECT @offset = @offset + @offset
END

BEGIN
WITH cte AS
(
select wod.[Well One] as Reading,'Well One' as Well, Convert(date,wo.BeginDate) as BeginDate from [reports].[WorkOrders] wo
INNER JOIN reports.WorkOrderForm_WellReadingForm wod on wod.ID = wo.ID
UNION ALL
select wod.[Well Two] as Reading,'Well Two' as Well, Convert(date,wo.BeginDate) as BeginDate from [reports].[WorkOrders] wo
INNER JOIN reports.WorkOrderForm_WellReadingForm wod on wod.ID = wo.ID
UNION ALL
select wod.[Well Three] as Reading,'Well Three' as Well, Convert(date,wo.BeginDate) as BeginDate from [reports].[WorkOrders] wo
INNER JOIN reports.WorkOrderForm_WellReadingForm wod on wod.ID = wo.ID
),
CTE_Main (Well,BeginDate,ReadingChange) AS
(
SELECT rc.Well, rc.BeginDate, COALESCE(CAST(rc.Reading AS VARCHAR(20)) + ' (' + CASE WHEN rc.Reading < rp.Reading THEN '-' ELSE '+' END + CAST(rc.Reading - rp.Reading AS VARCHAR(20)) + ')',CAST(rc.Reading as VARCHAR(20))) as ReadingChange
FROM cte rc --reading current
LEFT JOIN cte rp --reading prior
    ON rc.Well = rp.Well
    AND rc.BeginDate = DATEADD(DAY,1,rp.BeginDate)
)
select well.Well,well.ReadingChange, CONVERT(date,cal.date) as date from @DateTable cal
LEFT JOIN CTE_Main well on well.BeginDate = cal.date
ORDER BY cal.date
END