我需要从两张桌子中占用两张桌子
表1 TableMaster
_______________________________
|TableNo | TableType | Shared |
-------------------------------
|1 | WT | FULL |
|2 | WT | FULL |
|3 | WT | SHARED |
|4 | WT | SHARED |
-------------------------------
表2 TableSharedDetails
______________________________________________
|TableNo | ReservedDate | TableType | Shared |
----------------------------------------------
|4 | 29-12-2016 | WT | FULL |
|4 | 30-12-2016 | WT | FULL |
----------------------------------------------
我的桌面管理员有完整和共享表格。全表占用率为1,共享表占用率为2,我们将共享表转换为完整表,因为我正在使用表2 TableSharedDetails。
当我们在该日期将表格转换为完整时,我需要增加全表+ 1并减少共享表格 - 1
现在我想要特定时期的表类型明智可用状态,例如(2016年12月28日至2016年12月31日)
输出
______________________________________________
| ReservedDate | TableType | Shared |Totalava|
----------------------------------------------
| 28-12-2016 | WT | FULL | 2 |
| 28-12-2016 | WT | Shared | 2 |
| 29-12-2016 | WT | FULL | 3 |
| 29-12-2016 | WT | Shared | 1 |
| 30-12-2016 | WT | FULL | 3 |
| 30-12-2016 | WT | Shared | 1 |
| 31-12-2016 | WT | FULL | 2 |
| 31-12-2016 | WT | Shared | 2 |
----------------------------------------------
这里Totalava是tablemaster的桌子数
我尝试了一个程序,但这不正确
Create Procedure TableAvaStatus (@StartDate as DateTime, @EndDate as DateTime)
as
declare @AvaStatus table (ReserveDate DateTime, TableType varchar(10), Shared varchar(10), TotalAva int)
While @StartDate < @EndDate
Begin
INSERT INTO @AvaStatus (ReserveDate, TableType, Shared, TotalAva)
SELECT
@StartDate,
TableType,
Shared,
(CASE
WHEN Shared = 'FULL' THEN COUNT(*)
ELSE COUNT(*) END)
+ (SELECT COUNT(*) FROM TableSharedDetails
WHERE Shared = 'FULL'
AND TableType = A.TableType
GROUP BY TableType) AS TotalAva
FROM TableMaster A
End
Select * From @AvaStatus
答案 0 :(得分:2)
假设ReservedDate的数据类型为 foreach (Match match in Regex.Matches(File.ReadAllText(file), @"<Root>[.\s]*<\/Root>"))
{}
。
尝试:
Date
SQL小提琴:http://sqlfiddle.com/#!6/84458/33
说明:
1- declare @startDate date
declare @endDate date
select @startDate = '2016-12-28'
select @endDate = '2016-12-31'
-- Recursive CTE to generate dates between start and end dates
;with dateRange as
(
select @startDate as dt
union all
select dateadd(dd, 1, dt)
from dateRange
where dateadd(dd, 1, dt) < +dateadd(dd, 1, @endDate)
)
select
tbl_master.dt as ReservedDate,
tbl_master.TableType,
tbl_master.Shared,
case tbl_master.shared
when 'FULL' then
tbl_master.avail+coalesce(tbl_shared.shared_count,0)
when 'SHARED' then
tbl_master.avail-coalesce(tbl_shared.shared_count,0)
else
tbl_master.avail
end as Totalavail
from (
-- Join date range data with `TableMaster` to get table availablity
select dateRange.dt,TableMaster.TableType,TableMaster.Shared,count(*) as avail
from dateRange left join TableMaster on 1=1
group by dateRange.dt,TableMaster.TableType,TableMaster.Shared
) as tbl_master
-- Join `TableSharedDetails` for adding and subtracting counts from table availability
left join (
select ReservedDate,TableType,count(*) as shared_count
from TableSharedDetails
group by ReservedDate,TableType
) tbl_shared on
tbl_master.dt=tbl_shared.ReservedDate
and tbl_master.TableType=tbl_shared.TableType;
的使用:这是用于生成提供给存储过程的开始日期和结束日期之间的日期的递归CTE。
2-生成日期范围后,使用with dateRange
加入范围数据以获取表的可用性。由于没有特定的条件要加入,所以我将1 = 1(在所有条件下均为True)将所有日期范围数据与TableMaster
组合。
3-获得表可用性后,加入TableMaster
以添加和减去FULL和共享表数据。