Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 在列不一致的情况下,在不使用聚合函数的情况下动态透视表数据?_Sql_Sql Server_Tsql_Pivot Without Aggregate - Fatal编程技术网

Sql 在列不一致的情况下,在不使用聚合函数的情况下动态透视表数据?

Sql 在列不一致的情况下,在不使用聚合函数的情况下动态透视表数据?,sql,sql-server,tsql,pivot-without-aggregate,Sql,Sql Server,Tsql,Pivot Without Aggregate,我读了很多文章,但仍然不能得到要求的结果 我想显示重量、脉搏、血压和测量的所有数据,包括基于用户在前端选择的日期FromeDate和ToDate的冗余行。因此,FromDate和ToDate之间的不同日期的列将以轴为中心,但每次都不一致 我应该做什么改变?请快来帮忙。提前谢谢 以下是我的SQL存储过程: declare @Id int declare @FromDatesp datetime declare @ToDatesp datetime declare @ProtocolGroupId

我读了很多文章,但仍然不能得到要求的结果

我想显示重量、脉搏、血压和测量的所有数据,包括基于用户在前端选择的日期FromeDate和ToDate的冗余行。因此,FromDate和ToDate之间的不同日期的列将以轴为中心,但每次都不一致

我应该做什么改变?请快来帮忙。提前谢谢

以下是我的SQL存储过程:

declare @Id int
declare @FromDatesp datetime
declare @ToDatesp datetime
declare @ProtocolGroupId int

set @Id=2
set @FromDatesp='2011/11/07'
set @ToDatesp='2012/2/29'
set @ProtocolGroupId=0

Declare @FromDate datetime
Declare @ToDate datetime

set @FromDate= convert(datetime,@FromDatesp,121)
set @ToDate=convert(datetime,@ToDatesp,121)

DECLARE @TableOfDates TABLE(DateValue DATETIME)

DECLARE @CurrentDate DATETIME

SET @CurrentDate = @FromDate

WHILE @CurrentDate <= @ToDate
BEGIN
    INSERT INTO @TableOfDates(DateValue) VALUES (@CurrentDate)

    SET @CurrentDate = DATEADD(DAY, 1, @CurrentDate)
END

--SELECT convert(varchar(10),DateValue,111) FROM @TableOfDates

DECLARE @belowcols NVARCHAR(2000)
SELECT  @belowcols= STUFF(( SELECT DISTINCT
                                '],[' + convert(varchar(10), DateValue,111)
                        FROM    @TableOfDates AS t2  
                        FOR XML PATH('')
                      ), 1, 2, '') + ']'

                      print @belowcols

--DECLARE @abovecols NVARCHAR(2000)
--select @abovecols= replace ( @belowcols,'[','ISNULL([')
--select @abovecols = REPLACE(@abovecols,']','],0)')

Declare @Weightval varchar(MAX)
Declare @Pulseval varchar(MAX)
Declare @Oxval varchar(MAX)
Declare @BPSysval varchar(MAX)
Declare @BPDiasval varchar(MAX)
Declare @Alertval varchar(MAX)
Declare @Final varchar(MAX)
Declare @Question varchar(MAX)
Declare @Survey varchar(Max)

set @Weightval =''
set @BPSysval=''
set @BPDiasval=''
set @Pulseval =''
set @Oxval =''
set @Survey=''
declare @x int
DECLARE @PId varchar(500)
DECLARE @Config_XML varchar(1000)
DECLARE cur_Level CURSOR FOR 
select Id as PId,convert(varchar(1000),Config_XML)  from TblPatientCareplan where patientId=@Id and ProtocolGroupId=@ProtocolGroupId and Type='Reminder' and Parent_Id=(
select Id from TblPatientCareplan where patientId=@Id and ProtocolGroupId=@ProtocolGroupId and Type='Group' and IsDeleted=0) and IsDeleted=0
OPEN cur_Level
    FETCH NEXT FROM cur_Level INTO @PId, @Config_XML
    WHILE @@FETCH_STATUS = 0
    BEGIN
        print @PId 
        print @Config_XML 

            select @x=SUBSTRING(@Config_XML,15,2)
            print @x
            if(@x='18')
            begin 
            --set @Weightval ='SELECT ''weight;''+ convert(varchar(100),t1.Weight)  +'';''+convert(varchar(100),t1.Id) +'';''+ ISNULL((select top(1) ISNULL((convert(varchar(100),Alert_Type)),0) from TblAlertMessage where Record_Id=t1.Id and DeviceTypeId=1 order by alert_type asc),'''') as ''Value'',''1'' as ''DisplayOrder'', convert(varchar(10),t1.CreatedDt,111) as ''CreatedDt'',''Weight'' as ''ItemType'',PatientId FROM TblWeightScaleData t1 JOIN (SELECT MAX(CreatedDt) Max_EnteredOn_By_Day FROM TblWeightScaleData where PatientId='''+convert(varchar(1000),@Id)+''' GROUP BY convert(varchar, CreatedDt, 112)) t2 ON t1.CreatedDt = t2.Max_EnteredOn_By_Day where PatientId='''+convert(varchar(1000),@Id)+''' and CreatedDt  between '''+ CONVERT(varchar(1000), @FromDate)+''' and '''+CONVERT(varchar(1000),@ToDate)+''' and IsDeleted=0  '
            set @Weightval ='SELECT ''weight;''+ convert(varchar(100),t1.Weight)  +'';''+convert(varchar(100),t1.Id) +'';''+ ISNULL((select top(1) ISNULL((convert(varchar(100),Alert_Type)),0) from TblAlertMessage where Record_Id=t1.Id and DeviceTypeId=1 order by alert_type asc),'''') as ''Value'',''1'' as ''DisplayOrder'', convert(varchar(10),t1.CreatedDt,111) as ''CreatedDt'',''Weight'' as ''ItemType'',PatientId FROM TblWeightScaleData t1 where PatientId='''+convert(varchar(1000),@Id)+''' and CreatedDt  between '''+ CONVERT(varchar(1000), @FromDate)+''' and '''+CONVERT(varchar(1000),@ToDate)+''' and IsDeleted=0  '          
            end
            if(@x='19')
            begin 
            --set @BPSysval='SELECT ''sys;''+ convert(varchar(100),t1.Systolic)  +'';''+convert(varchar(100),t1.Id) +'';''+ ISNULL((select top(1) ISNULL((convert(varchar(100),Alert_Type)),0) from TblAlertMessage where Record_Id=t1.Id and DeviceTypeId=2 order by alert_type asc),'''') as ''Value'',''4'' as ''DisplayOrder'', convert(varchar(10),t1.CreatedDt,111) as ''CreatedDt'',''Systolic'' as ''ItemType'',PatientId FROM TblBloodPressureData t1 JOIN (SELECT MAX(CreatedDt) Max_EnteredOn_By_Day FROM TblBloodPressureData where PatientId='''+convert(varchar(1000),@Id)+''' GROUP BY convert(varchar, CreatedDt, 112)) t2 ON t1.CreatedDt = t2.Max_EnteredOn_By_Day where PatientId='''+convert(varchar(1000),@Id)+''' and CreatedDt  between '''+ CONVERT(varchar(1000), @FromDate)+''' and '''+CONVERT(varchar(1000),@ToDate)+''' and IsDeleted=0  '
            --set @BPDiasval='SELECT ''dias;''+ convert(varchar(100),t1.Diastolic)  +'';''+convert(varchar(100),t1.Id) +'';''+ ISNULL((select top(1) ISNULL((convert(varchar(100),Alert_Type)),0) from TblAlertMessage where Record_Id=t1.Id and DeviceTypeId=2 order by alert_type asc),'''') as ''Value'',''5'' as ''DisplayOrder'', convert(varchar(10),t1.CreatedDt,111) as ''CreatedDt'',''Diastolic'' as ''ItemType'',PatientId FROM TblBloodPressureData t1 JOIN (SELECT MAX(CreatedDt) Max_EnteredOn_By_Day FROM TblBloodPressureData where PatientId='''+convert(varchar(1000),@Id)+''' GROUP BY convert(varchar, CreatedDt, 112)) t2 ON t1.CreatedDt = t2.Max_EnteredOn_By_Day where PatientId='''+convert(varchar(1000),@Id)+''' and CreatedDt  between '''+ CONVERT(varchar(1000), @FromDate)+''' and '''+CONVERT(varchar(1000),@ToDate)+''' and IsDeleted=0  '
            set @BPSysval='SELECT ''sys;''+ convert(varchar(100),t1.Systolic)  +'';''+convert(varchar(100),t1.Id) +'';''+ ISNULL((select top(1) ISNULL((convert(varchar(100),Alert_Type)),0) from TblAlertMessage where Record_Id=t1.Id and DeviceTypeId=2 order by alert_type asc),'''') as ''Value'',''4'' as ''DisplayOrder'', convert(varchar(10),t1.CreatedDt,111) as ''CreatedDt'',''Systolic'' as ''ItemType'',PatientId FROM TblBloodPressureData t1  where PatientId='''+convert(varchar(1000),@Id)+''' and CreatedDt  between '''+ CONVERT(varchar(1000), @FromDate)+''' and '''+CONVERT(varchar(1000),@ToDate)+''' and IsDeleted=0  '
            set @BPDiasval='SELECT ''dias;''+ convert(varchar(100),t1.Diastolic)  +'';''+convert(varchar(100),t1.Id) +'';''+ ISNULL((select top(1) ISNULL((convert(varchar(100),Alert_Type)),0) from TblAlertMessage where Record_Id=t1.Id and DeviceTypeId=2 order by alert_type asc),'''') as ''Value'',''5'' as ''DisplayOrder'', convert(varchar(10),t1.CreatedDt,111) as ''CreatedDt'',''Diastolic'' as ''ItemType'',PatientId FROM TblBloodPressureData t1 where PatientId='''+convert(varchar(1000),@Id)+''' and CreatedDt  between '''+ CONVERT(varchar(1000), @FromDate)+''' and '''+CONVERT(varchar(1000),@ToDate)+''' and IsDeleted=0  '
            end
            if(@x='20')
            begin 
            --set @Pulseval ='SELECT ''pulse;''+ convert(varchar(100),t1.Pulse) +'';''+convert(varchar(100),t1.Id) +'';''+ ISNULL((select top(1) ISNULL((convert(varchar(100),Alert_Type)),0) from TblAlertMessage where Record_Id=t1.Id and DeviceTypeId=3 order by alert_type asc),'''') as ''Value'',''2'' as ''DisplayOrder'', convert(varchar(10),t1.CreatedDt,111) as ''CreatedDt'',''Pulse'' as ''ItemType'',PatientId FROM TblPulseOxData t1 JOIN (SELECT MAX(CreatedDt) Max_EnteredOn_By_Day FROM TblPulseOxData where PatientId='''+convert(varchar(1000),@Id)+''' GROUP BY convert(varchar, CreatedDt, 112)) t2 ON t1.CreatedDt = t2.Max_EnteredOn_By_Day where PatientId='''+convert(varchar(1000),@Id)+''' and CreatedDt  between '''+ CONVERT(varchar(1000), @FromDate)+''' and '''+CONVERT(varchar(1000),@ToDate)+''' and IsDeleted=0  '
            --set @Oxval ='SELECT ''oxygen;''+ convert(varchar(100),t1.SpO2)  +'';''+convert(varchar(100),t1.Id) +'';''+ ISNULL((select top(1) ISNULL((convert(varchar(100),Alert_Type)),0) from TblAlertMessage where Record_Id=t1.Id and DeviceTypeId=3 order by alert_type asc),'''') as ''Value'',''3'' as ''DisplayOrder'', convert(varchar(10),t1.CreatedDt,111) as ''CreatedDt'',''Oxygen'' as ''ItemType'',PatientId FROM TblPulseOxData t1 JOIN (SELECT MAX(CreatedDt) Max_EnteredOn_By_Day FROM TblPulseOxData where PatientId='''+convert(varchar(1000),@Id)+''' GROUP BY convert(varchar, CreatedDt, 112)) t2 ON t1.CreatedDt = t2.Max_EnteredOn_By_Day where PatientId='''+convert(varchar(1000),@Id)+''' and CreatedDt  between '''+ CONVERT(varchar(1000), @FromDate)+''' and '''+CONVERT(varchar(1000),@ToDate)+''' and IsDeleted=0  '
            set @Pulseval ='SELECT ''pulse;''+ convert(varchar(100),t1.Pulse) +'';''+convert(varchar(100),t1.Id) +'';''+ ISNULL((select top(1) ISNULL((convert(varchar(100),Alert_Type)),0) from TblAlertMessage where Record_Id=t1.Id and DeviceTypeId=3 order by alert_type asc),'''') as ''Value'',''2'' as ''DisplayOrder'', convert(varchar(10),t1.CreatedDt,111) as ''CreatedDt'',''Pulse'' as ''ItemType'',PatientId FROM TblPulseOxData t1 where PatientId='''+convert(varchar(1000),@Id)+''' and CreatedDt  between '''+ CONVERT(varchar(1000), @FromDate)+''' and '''+CONVERT(varchar(1000),@ToDate)+''' and IsDeleted=0  '
            set @Oxval ='SELECT ''oxygen;''+ convert(varchar(100),t1.SpO2)  +'';''+convert(varchar(100),t1.Id) +'';''+ ISNULL((select top(1) ISNULL((convert(varchar(100),Alert_Type)),0) from TblAlertMessage where Record_Id=t1.Id and DeviceTypeId=3 order by alert_type asc),'''') as ''Value'',''3'' as ''DisplayOrder'', convert(varchar(10),t1.CreatedDt,111) as ''CreatedDt'',''Oxygen'' as ''ItemType'',PatientId FROM TblPulseOxData t1 where PatientId='''+convert(varchar(1000),@Id)+''' and CreatedDt  between '''+ CONVERT(varchar(1000), @FromDate)+''' and '''+CONVERT(varchar(1000),@ToDate)+''' and IsDeleted=0  '
            end         

        FETCH NEXT FROM cur_Level INTO @PId, @Config_XML
    END
CLOSE cur_Level
DEALLOCATE cur_Level

set @Survey='select ''-'' as ''Value'', ''5'' as ''DisplayOrder'' , CONVERT(varchar(10), getdate(), 111) AS ''CreatedDt'',''Survey'' AS ''ItemType'','''+convert(varchar(1000),@Id)+''' as ''PatientId''  from TblAlertMessage'

set @Question ='SELECT ''survey;''+ CONVERT(varchar(100), t1.Answer) + '';'' + CONVERT(varchar(100), t1.Id) +'';''+ ISNULL((select top(1) ISNULL((convert(varchar(100),Alert_Type)),0) from TblAlertMessage where Record_Id=t1.Id and DeviceTypeId=4 order by alert_type asc),'''') as ''Value'', ''6'' AS ''DisplayOrder'', CONVERT(varchar(10), t1.CreatedDate, 111) AS ''CreatedDt'',t1.Question AS ''ItemType'', t1.PatientId FROM dbo.TblProtocolGroupData AS t1 INNER JOIN (SELECT MAX(CreatedDate) AS Max_EnteredOn_By_Day FROM dbo.TblProtocolGroupData WHERE (PatientId='''+convert(varchar(1000),@Id)+''') AND (ProtocolGroupId = '''+convert(varchar(1000),@ProtocolGroupId)+''') GROUP BY Question, CONVERT(varchar, CreatedDate, 112)) AS t2 ON t1.CreatedDate = t2.Max_EnteredOn_By_Day WHERE PatientId='''+convert(varchar(1000),@Id)+''' AND (ProtocolGroupId = '''+convert(varchar(1000),@ProtocolGroupId)+''') and CreatedDate  between '''+ CONVERT(varchar(1000), @FromDate)+''' and '''+CONVERT(varchar(1000),@ToDate)+''' and IsDeleted=0  '


if(DATALENGTH(@Weightval)>3)
set @Weightval= ' union ' +@Weightval

if(DATALENGTH(@BPSysval)>3)
begin
set @BPSysval= ' union ' +@BPSysval
set @BPDiasval= ' union ' +@BPDiasval
end
if(DATALENGTH(@Pulseval)>3)
begin
set @Pulseval= ' union ' +@Pulseval
set @Oxval= ' union ' +@Oxval
end


print(@Weightval)
print(@BPSysval)
print(@BPDiasval)
print(@Pulseval)
print(@Oxval)



Set @Final=@Survey + ' union ' + @Question + @Weightval + @BPDiasval + @BPSysval + @Pulseval + @Oxval


DECLARE @query NVARCHAR(max)
SET @query = 'SELECT  ItemType, '+@belowcols +'


FROM    ( 

'+@Final+'

        )p PIVOT ( Max(Value)
                    FOR [CreatedDt] 
                      IN ('+
@belowcols +')
                  ) AS pvt order by DisplayOrder ;'

                  print(@query)

                  exec(@query)                                             

只需编写几个SQL语句,并使用IF在运行时确定要使用哪个语句

Create Proc DoReport(@rptType int)
As
Begin


    if @rptType = 1 -- Group by col1
    Begin
        -- Sql for group by col1 here.

    end else if @rpttype = 2 -- group by col2
    begin
        -- Sql for group by col2 here.


    end


End

在日期范围之间提取日期、体重、脉搏、氧气、血压和测量变得简单得多。有什么原因不能在选择后交换行和列吗?如果运行此操作,则可以得到fromdate和todate之间的结果,但每个项目类型只有一行我实际上不想要的行。我想要所有项目类型的重复行(如果有)。若你们能看到我对原始源代码查询进行了注释,并将其更改为通用形式,这样我就可以避免任何max或GROUPBY。我的所有数据通过pivot中的最大聚合函数获取。。。希望你能理解我的需要,现在有没有办法避免在pivot中出现max??