Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.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 server SQL Server语句时的大小写_Sql Server_Select_Stored Procedures_Case - Fatal编程技术网

Sql server SQL Server语句时的大小写

Sql server SQL Server语句时的大小写,sql-server,select,stored-procedures,case,Sql Server,Select,Stored Procedures,Case,我有一个存储过程在工作中,但是,我在运行它时丢失了一些数据。我知道原因,但我很难弄明白。。。这是我的代码: INSERT INTO tempIntake (Pop, PlanID, PopFull, ApptDate, [1stAppt], Followup, Rn, UserID) SELECT Pop, PlanID, PopFull, InterviewDate, 1 stAppt, Followup, rn, @UserID FROM ( SELECT *, ROW_NUMBER

我有一个存储过程在工作中,但是,我在运行它时丢失了一些数据。我知道原因,但我很难弄明白。。。这是我的代码:

INSERT INTO tempIntake (Pop, PlanID, PopFull, ApptDate, [1stAppt], Followup, Rn, UserID)
SELECT Pop, PlanID, PopFull, InterviewDate, 1 stAppt, Followup, rn, @UserID
FROM (
    SELECT *, ROW_NUMBER() OVER (PARTITION BY PlanID ORDER BY AddedDate ASC) AS rn
    FROM VInfo
    WHERE AddedDate IS NOT NULL
) t
WHERE rn = 1 AND interviewdate >= @fromDate AND interviewDate <= @toDate
所以我想做的是……对于一些PLANID,我没有额外的日期。因为我按addedate进行过滤,如果为null,则不会显示该数据,即使我需要显示该数据。在这种情况下,我想让它成为一个虚拟日期,如“2016年1月1日”,这样,当实际添加的日期在表中可用时,将使用它代替虚拟日期

如果AddedDate不能大于GETDATE,则可以使用ISNULLAddedDate、GETDATE并删除where AddedDate不为null的条件:

Insert into tempIntake(Pop, PlanID, PopFull, ApptDate, 1stAppt, Followup,   Rn, UserID)
    select 
        Pop, PlanID, PopFull, InterviewDate, 1stAppt, Followup, rn, @UserID 
    from 
        (Select 
             *,
             row_number() over (partition by PlanID order BY ISNULL(AddedDate,GETDATE())) as rn 
         from 
             VInfo) t
    where 
        rn = 1 
        and interviewdate >= @fromDate 
        and interviewDate <= @toDate

这听起来可能很奇怪,但你能不能…删除addedate不为null的地方?我现在做的更奇怪。对于某些PlanId的addeddate=null和某些情况下的实际日期,我有多个值。当我按PlanID orderBy AddedDate ASC进行分区时,如果没有where AddedDate不为null,我似乎会调出所有的null值,而实际上我需要调出最小日期。我不知道为什么会这样。我所需要做的就是基本上指定日期为空,然后将其设置为2016年1月1日-这一切都很好。千千万万条记录中只有一条记录的数据没有出现,然后使用ISNULLAddedDate、GETDATE按PlanId顺序的超额分配,并删除where AddedDate is not null post作为答案,我将接受。thanksquick问题-如果我按IsnuLLaddedDate,GetDate desc as rn按PlanID顺序进行分区,为什么我得不到正确的结果-我得到所有这些空值-除非我包括addeddate不为空的地方-发生了一些事情。您显示它对ASC有效的方式,但是当我按DESC分区时,我会感到奇怪data@FatBoySlim7我的意思是,这是很明显的,因为你用GETDATE替换addedate,如果你把它们排序为DESC,你会先得到那些空值