第一种:如果住院日期小于8月27日,并且出院日期大于8月31日,那住院天数就是8月31日-8月27日
第二种:如果住院日期小于8月27日,并且出院日期小于8月31日,那住院天数就是出院日期-8月27日
第三种:如果住院日期大于等于8月27日,并且出院日期大于8月31日,那住院天数就是8月31-入院日期
第四种:如果住院日期大于等于8月27日,并且出院日期小于8月31日,那住院天数就是出院日期-住院日期
然后最后要把天数累加起来
sql 语句要怎么写。
4 个解决方案
#1
楼主描述的31号没有等于的情况。用case when 写一遍条件就行了:
--测试数据
if not object_id(N'Tempdb..#A') is null
drop table #A
Go
Create table #A([IHEHEnterDate] Date,[IHEHLeaveDate] Date)
Insert #A
select '2017-08-26','2017-09-01' union all
select '2017-08-26','2017-08-30' union all
select '2017-08-28','2017-09-01' union all
select '2017-08-28','2017-08-30'
Go
--测试数据结束
SELECT CASE WHEN IHEHEnterDate < '2017-08-27'
AND IHEHLeaveDate > '2017-08-31'
THEN DATEDIFF(DAY, '2017-08-27', '2017-08-31')
WHEN IHEHEnterDate < '2017-08-27'
AND IHEHLeaveDate < '2017-08-31'
THEN DATEDIFF(DAY, '2017-08-27', IHEHLeaveDate)
WHEN IHEHEnterDate >= '2017-08-27'
AND IHEHLeaveDate > '2017-08-31'
THEN DATEDIFF(DAY, IHEHEnterDate, '2017-08-31')
WHEN IHEHEnterDate >= '2017-08-27'
AND IHEHLeaveDate < '2017-08-31'
THEN DATEDIFF(DAY, IHEHEnterDate, IHEHLeaveDate)
ELSE 0
END AS 天数
FROM #A
SELECT SUM(CASE WHEN IHEHEnterDate < '2017-08-27'
AND IHEHLeaveDate > '2017-08-31'
THEN DATEDIFF(DAY, '2017-08-27', '2017-08-31')
WHEN IHEHEnterDate < '2017-08-27'
AND IHEHLeaveDate < '2017-08-31'
THEN DATEDIFF(DAY, '2017-08-27', IHEHLeaveDate)
WHEN IHEHEnterDate >= '2017-08-27'
AND IHEHLeaveDate > '2017-08-31'
THEN DATEDIFF(DAY, IHEHEnterDate, '2017-08-31')
WHEN IHEHEnterDate >= '2017-08-27'
AND IHEHLeaveDate < '2017-08-31'
THEN DATEDIFF(DAY, IHEHEnterDate, IHEHLeaveDate)
ELSE 0
END) AS 天数
FROM #A
#2
WITH tab AS(
SELECT '2017-08-17'AS IHEHEnterDate,'2017-09-12'AS IHEHLeaveDate
UNION ALL
SELECT '2017-08-17','2017-08-27'
UNION ALL
SELECT '2017-08-27','2017-09-05'
UNION ALL
SELECT '2017-08-28','2017-08-30'
UNION ALL
SELECT '2017-08-27','2017-08-30'
)
SELECT SUM(ISNULL(A,0)) AS Asum,SUM(ISNULL(B,0)) AS Bsum,SUM(ISNULL(C,0)) AS Csum,SUM(ISNULL(D,0)) AS Dsum
FROM (--2
SELECT IHEHEnterDate,IHEHLeaveDate,--1
CASE WHEN IHEHEnterDate<'2017-08-27' AND IHEHLeaveDate>'2017-08-31' THEN DATEDIFF(DAY,'2017-08-27','2017-08-31')END AS A,
CASE WHEN IHEHEnterDate<'2017-08-27' AND IHEHLeaveDate<'2017-08-31' THEN DATEDIFF(DAY,'2017-08-27',IHEHLeaveDate)END AS B,
CASE WHEN IHEHEnterDate>='2017-08-27' AND IHEHLeaveDate>'2017-08-31' THEN DATEDIFF(DAY,IHEHEnterDate,'2017-08-31')END AS C,
CASE WHEN IHEHEnterDate>='2017-08-27' AND IHEHLeaveDate<'2017-08-31' THEN DATEDIFF(DAY,IHEHEnterDate,IHEHLeaveDate)END AS D
FROM tab --1
) t --2
不知道是不是要这样啊,分四类统计天数
SELECT '2017-08-17'AS IHEHEnterDate,'2017-09-12'AS IHEHLeaveDate
UNION ALL
SELECT '2017-08-17','2017-08-27'
UNION ALL
SELECT '2017-08-27','2017-09-05'
UNION ALL
SELECT '2017-08-28','2017-08-30'
UNION ALL
SELECT '2017-08-27','2017-08-30'
)
SELECT SUM(ISNULL(A,0)) AS Asum,SUM(ISNULL(B,0)) AS Bsum,SUM(ISNULL(C,0)) AS Csum,SUM(ISNULL(D,0)) AS Dsum
FROM (--2
SELECT IHEHEnterDate,IHEHLeaveDate,--1
CASE WHEN IHEHEnterDate<'2017-08-27' AND IHEHLeaveDate>'2017-08-31' THEN DATEDIFF(DAY,'2017-08-27','2017-08-31')END AS A,
CASE WHEN IHEHEnterDate<'2017-08-27' AND IHEHLeaveDate<'2017-08-31' THEN DATEDIFF(DAY,'2017-08-27',IHEHLeaveDate)END AS B,
CASE WHEN IHEHEnterDate>='2017-08-27' AND IHEHLeaveDate>'2017-08-31' THEN DATEDIFF(DAY,IHEHEnterDate,'2017-08-31')END AS C,
CASE WHEN IHEHEnterDate>='2017-08-27' AND IHEHLeaveDate<'2017-08-31' THEN DATEDIFF(DAY,IHEHEnterDate,IHEHLeaveDate)END AS D
FROM tab --1
) t --2
不知道是不是要这样啊,分四类统计天数
#3
#4
SELECT COUNT(1) FROM
(SELECT DATEADD(DD,number,[IHEHEnterDate]) AS DATE_NEW
FROM TABLE A
JOIN master.dbo.spt_values B ON DATEDIFF(DD,[IHEHEnterDate],[IHEHLeaveDate])>=number
WHERE TYPE='P') A
WHERE DATE_NEW BETWEEN '2017-08-27' AND '2017-08-31'
(SELECT DATEADD(DD,number,[IHEHEnterDate]) AS DATE_NEW
FROM TABLE A
JOIN master.dbo.spt_values B ON DATEDIFF(DD,[IHEHEnterDate],[IHEHLeaveDate])>=number
WHERE TYPE='P') A
WHERE DATE_NEW BETWEEN '2017-08-27' AND '2017-08-31'
#1
楼主描述的31号没有等于的情况。用case when 写一遍条件就行了:
--测试数据
if not object_id(N'Tempdb..#A') is null
drop table #A
Go
Create table #A([IHEHEnterDate] Date,[IHEHLeaveDate] Date)
Insert #A
select '2017-08-26','2017-09-01' union all
select '2017-08-26','2017-08-30' union all
select '2017-08-28','2017-09-01' union all
select '2017-08-28','2017-08-30'
Go
--测试数据结束
SELECT CASE WHEN IHEHEnterDate < '2017-08-27'
AND IHEHLeaveDate > '2017-08-31'
THEN DATEDIFF(DAY, '2017-08-27', '2017-08-31')
WHEN IHEHEnterDate < '2017-08-27'
AND IHEHLeaveDate < '2017-08-31'
THEN DATEDIFF(DAY, '2017-08-27', IHEHLeaveDate)
WHEN IHEHEnterDate >= '2017-08-27'
AND IHEHLeaveDate > '2017-08-31'
THEN DATEDIFF(DAY, IHEHEnterDate, '2017-08-31')
WHEN IHEHEnterDate >= '2017-08-27'
AND IHEHLeaveDate < '2017-08-31'
THEN DATEDIFF(DAY, IHEHEnterDate, IHEHLeaveDate)
ELSE 0
END AS 天数
FROM #A
SELECT SUM(CASE WHEN IHEHEnterDate < '2017-08-27'
AND IHEHLeaveDate > '2017-08-31'
THEN DATEDIFF(DAY, '2017-08-27', '2017-08-31')
WHEN IHEHEnterDate < '2017-08-27'
AND IHEHLeaveDate < '2017-08-31'
THEN DATEDIFF(DAY, '2017-08-27', IHEHLeaveDate)
WHEN IHEHEnterDate >= '2017-08-27'
AND IHEHLeaveDate > '2017-08-31'
THEN DATEDIFF(DAY, IHEHEnterDate, '2017-08-31')
WHEN IHEHEnterDate >= '2017-08-27'
AND IHEHLeaveDate < '2017-08-31'
THEN DATEDIFF(DAY, IHEHEnterDate, IHEHLeaveDate)
ELSE 0
END) AS 天数
FROM #A
#2
WITH tab AS(
SELECT '2017-08-17'AS IHEHEnterDate,'2017-09-12'AS IHEHLeaveDate
UNION ALL
SELECT '2017-08-17','2017-08-27'
UNION ALL
SELECT '2017-08-27','2017-09-05'
UNION ALL
SELECT '2017-08-28','2017-08-30'
UNION ALL
SELECT '2017-08-27','2017-08-30'
)
SELECT SUM(ISNULL(A,0)) AS Asum,SUM(ISNULL(B,0)) AS Bsum,SUM(ISNULL(C,0)) AS Csum,SUM(ISNULL(D,0)) AS Dsum
FROM (--2
SELECT IHEHEnterDate,IHEHLeaveDate,--1
CASE WHEN IHEHEnterDate<'2017-08-27' AND IHEHLeaveDate>'2017-08-31' THEN DATEDIFF(DAY,'2017-08-27','2017-08-31')END AS A,
CASE WHEN IHEHEnterDate<'2017-08-27' AND IHEHLeaveDate<'2017-08-31' THEN DATEDIFF(DAY,'2017-08-27',IHEHLeaveDate)END AS B,
CASE WHEN IHEHEnterDate>='2017-08-27' AND IHEHLeaveDate>'2017-08-31' THEN DATEDIFF(DAY,IHEHEnterDate,'2017-08-31')END AS C,
CASE WHEN IHEHEnterDate>='2017-08-27' AND IHEHLeaveDate<'2017-08-31' THEN DATEDIFF(DAY,IHEHEnterDate,IHEHLeaveDate)END AS D
FROM tab --1
) t --2
不知道是不是要这样啊,分四类统计天数
SELECT '2017-08-17'AS IHEHEnterDate,'2017-09-12'AS IHEHLeaveDate
UNION ALL
SELECT '2017-08-17','2017-08-27'
UNION ALL
SELECT '2017-08-27','2017-09-05'
UNION ALL
SELECT '2017-08-28','2017-08-30'
UNION ALL
SELECT '2017-08-27','2017-08-30'
)
SELECT SUM(ISNULL(A,0)) AS Asum,SUM(ISNULL(B,0)) AS Bsum,SUM(ISNULL(C,0)) AS Csum,SUM(ISNULL(D,0)) AS Dsum
FROM (--2
SELECT IHEHEnterDate,IHEHLeaveDate,--1
CASE WHEN IHEHEnterDate<'2017-08-27' AND IHEHLeaveDate>'2017-08-31' THEN DATEDIFF(DAY,'2017-08-27','2017-08-31')END AS A,
CASE WHEN IHEHEnterDate<'2017-08-27' AND IHEHLeaveDate<'2017-08-31' THEN DATEDIFF(DAY,'2017-08-27',IHEHLeaveDate)END AS B,
CASE WHEN IHEHEnterDate>='2017-08-27' AND IHEHLeaveDate>'2017-08-31' THEN DATEDIFF(DAY,IHEHEnterDate,'2017-08-31')END AS C,
CASE WHEN IHEHEnterDate>='2017-08-27' AND IHEHLeaveDate<'2017-08-31' THEN DATEDIFF(DAY,IHEHEnterDate,IHEHLeaveDate)END AS D
FROM tab --1
) t --2
不知道是不是要这样啊,分四类统计天数
#3
#4
SELECT COUNT(1) FROM
(SELECT DATEADD(DD,number,[IHEHEnterDate]) AS DATE_NEW
FROM TABLE A
JOIN master.dbo.spt_values B ON DATEDIFF(DD,[IHEHEnterDate],[IHEHLeaveDate])>=number
WHERE TYPE='P') A
WHERE DATE_NEW BETWEEN '2017-08-27' AND '2017-08-31'
(SELECT DATEADD(DD,number,[IHEHEnterDate]) AS DATE_NEW
FROM TABLE A
JOIN master.dbo.spt_values B ON DATEDIFF(DD,[IHEHEnterDate],[IHEHLeaveDate])>=number
WHERE TYPE='P') A
WHERE DATE_NEW BETWEEN '2017-08-27' AND '2017-08-31'