检索多个但不是所有字段但同一列
Retrieving multiple but not all fields but same column
我需要从存储行程 ID 号的 excel 文档中为 driver 填充 Rota table。
在上传到应用程序时,我需要它通过 SP
从 运行s table 获取每天 运行 的开始时间
这部分不是问题,一切正常当某些日子为“0”(休息日)时问题就开始了,所以除非 driver 是一周中的每一天,否则数据不会return.
我知道这不是很吸引人 SQL 但它只是一个快速 mock-up 来解释问题。
这里是T-SQL
(
@runid0 as int = 0,
@runid1 as int = 0,
@runid2 as int = 0,
@runid3 as int = 0,
@runid4 as int = 0,
@runid5 as int = 0,
@runid6 as int = 0,
@siteid as int
)
AS
declare @effectivedate0 as datetime
declare @effectivedate1 as datetime
declare @effectivedate2 as datetime
declare @effectivedate3 as datetime
declare @effectivedate4 as datetime
declare @effectivedate5 as datetime
declare @effectivedate6 as datetime
set @effectivedate0 = (Select TOP 1 effective_from FROM db_t_run_plan WHERE effective_from <= GetDATE() AND site_id=@siteid AND run_id=@RunID0 order by effective_from DESC)
set @effectivedate1 = (Select TOP 1 effective_from FROM db_t_run_plan WHERE effective_from <= GetDATE() AND site_id=@siteid AND run_id=@RunId1 order by effective_from DESC)
set @effectivedate2 = (Select TOP 1 effective_from FROM db_t_run_plan WHERE effective_from <= GetDATE() AND site_id=@siteid AND run_id=@RunID2 order by effective_from DESC)
set @effectivedate3 = (Select TOP 1 effective_from FROM db_t_run_plan WHERE effective_from <= GetDATE() AND site_id=@siteid AND run_id=@RunID3 order by effective_from DESC)
set @effectivedate4 = (Select TOP 1 effective_from FROM db_t_run_plan WHERE effective_from <= GetDATE() AND site_id=@siteid AND run_id=@RunID4 order by effective_from DESC)
set @effectivedate5 = (Select TOP 1 effective_from FROM db_t_run_plan WHERE effective_from <= GetDATE() AND site_id=@siteid AND run_id=@RunID5 order by effective_from DESC)
set @effectivedate6 = (Select TOP 1 effective_from FROM db_t_run_plan WHERE effective_from <= GetDATE() AND site_id=@siteid AND run_id=@RunID6 order by effective_from DESC)
Select * From
(SELECT arrival as arr0 from db_t_run_orders where run_id = @runid0 and site_id=@siteid and trip_no=1 and visit_order = 1 and effective_from = @effectivedate0) a
Cross Join
(SELECT arrival as arr1 from db_t_run_orders where run_id = @runid1 and site_id=@siteid and trip_no=1 and visit_order = 1 and effective_from = @effectivedate1) b
Cross Join
(SELECT arrival as arr2 from db_t_run_orders where run_id = @runid2 and site_id=@siteid and trip_no=1 and visit_order = 1 and effective_from = @effectivedate2) c
Cross Join
(SELECT arrival as arr3 from db_t_run_orders where run_id = @runid3 and site_id=@siteid and trip_no=1 and visit_order = 1 and effective_from = @effectivedate3) d
Cross Join
(SELECT arrival as arr4 from db_t_run_orders where run_id = @runid4 and site_id=@siteid and trip_no=1 and visit_order = 1 and effective_from = @effectivedate4) e
Cross Join
(SELECT arrival as arr5 from db_t_run_orders where run_id = @runid5 and site_id=@siteid and trip_no=1 and visit_order = 1 and effective_from = @effectivedate5) f
Cross Join
(SELECT arrival as arr6 from db_t_run_orders where run_id = @runid6 and site_id=@siteid and trip_no=1 and visit_order = 1 and effective_from = @effectivedate6) g
RETURN
好吧,联合枢轴的想法我无法工作,但我确实找到了另一种方法,它可以帮助那些枢轴对他们也不起作用的其他人。
只需声明每天的变量和 运行 每个变量的单一查询,将它们转储到临时 table 和 select * table.
它会比组合查询更有效吗,可能不会。
这是最干净/最简洁的方法吗,绝对不是。
它能完成工作吗?
如果有人希望post更好的版本,显然请这样做。
(
@runid0 as int = 0,
@runid1 as int = 0,
@runid2 as int = 0,
@runid3 as int = 0,
@runid4 as int = 0,
@runid5 as int = 0,
@runid6 as int = 0,
@siteid as int
)
作为
将@effectivedate0 声明为日期时间
将@effectivedate1 声明为日期时间
将@effectivedate2 声明为日期时间
将@effectivedate3 声明为日期时间
将@effectivedate4 声明为日期时间
将@effectivedate5 声明为日期时间
将@effectivedate6 声明为日期时间
将 @arr0 声明为 varchar(5)
将 @arr1 声明为 varchar(5)
将 @arr2 声明为 varchar(5)
将 @arr3 声明为 varchar(5)
将 @arr4 声明为 varchar(5)
将 @arr5 声明为 varchar(5)
将 @arr6 声明为 varchar(5)
Declare @table Table ( day0 varchar(5), day1 varchar(5), day2 varchar(5), day3 varchar(5), day4 varchar(5), day5 varchar(5), day6 varchar(5))
BEGIN
set @effectivedate0 = (Select TOP 1 effective_from FROM dbo_t_run_plan WHERE effective_from <= GetDATE() AND site_id=@siteid AND run_id=@RunID0 order by effective_from DESC)
set @arr0 = (SELECT arrival as arr from dbo_t_run_orders where run_id = @runid0 and site_id=@siteid and trip_no=1 and visit_order = 1 and effective_from = @effectivedate0)
END
BEGIN
set @effectivedate1 = (Select TOP 1 effective_from FROM dbo_t_run_plan WHERE effective_from <= GetDATE() AND site_id=@siteid AND run_id=@RunId1 order by effective_from DESC)
set @arr1 = (SELECT arrival as arr from dbo_t_run_orders where run_id = @runid1 and site_id=@siteid and trip_no=1 and visit_order = 1 and effective_from = @effectivedate1)
END
BEGIN
set @effectivedate2 = (Select TOP 1 effective_from FROM dbo_t_run_plan WHERE effective_from <= GetDATE() AND site_id=@siteid AND run_id=@RunID2 order by effective_from DESC)
set @arr2 = (SELECT arrival as arr from dbo_t_run_orders where run_id = @runid2 and site_id=@siteid and trip_no=1 and visit_order = 1 and effective_from = @effectivedate2)
END
BEGIN
set @effectivedate3 = (Select TOP 1 effective_from FROM dbo_t_run_plan WHERE effective_from <= GetDATE() AND site_id=@siteid AND run_id=@RunID3 order by effective_from DESC)
set @arr3 = (SELECT arrival as arr from dbo_t_run_orders where run_id = @runid3 and site_id=@siteid and trip_no=1 and visit_order = 1 and effective_from = @effectivedate3)
END
BEGIN
set @effectivedate4 = (Select TOP 1 effective_from FROM dbo_t_run_plan WHERE effective_from <= GetDATE() AND site_id=@siteid AND run_id=@RunID4 order by effective_from DESC)
set @arr4 = (SELECT arrival as arr from dbo_t_run_orders where run_id = @runid4 and site_id=@siteid and trip_no=1 and visit_order = 1 and effective_from = @effectivedate4)
END
BEGIN
set @effectivedate5 = (Select TOP 1 effective_from FROM dbo_t_run_plan WHERE effective_from <= GetDATE() AND site_id=@siteid AND run_id=@RunID5 order by effective_from DESC)
set @arr5 = (SELECT arrival as arr from dbo_t_run_orders where run_id = @runid5 and site_id=@siteid and trip_no=1 and visit_order = 1 and effective_from = @effectivedate5)
END
BEGIN
set @effectivedate6 = (Select TOP 1 effective_from FROM dbo_t_run_plan WHERE effective_from <= GetDATE() AND site_id=@siteid AND run_id=@RunID6 order by effective_from DESC)
set @arr6 = (SELECT arrival as arr from dbo_t_run_orders where run_id = @runid6 and site_id=@siteid and trip_no=1 and visit_order = 1 and effective_from = @effectivedate6)
END
insert into @table values (@arr0,@arr1,@arr2,@arr3,@arr4,@arr5,@arr6)
select * from @table
RETURN
我需要从存储行程 ID 号的 excel 文档中为 driver 填充 Rota table。
在上传到应用程序时,我需要它通过 SP
从 运行s table 获取每天 运行 的开始时间这部分不是问题,一切正常当某些日子为“0”(休息日)时问题就开始了,所以除非 driver 是一周中的每一天,否则数据不会return.
我知道这不是很吸引人 SQL 但它只是一个快速 mock-up 来解释问题。
这里是T-SQL
(
@runid0 as int = 0,
@runid1 as int = 0,
@runid2 as int = 0,
@runid3 as int = 0,
@runid4 as int = 0,
@runid5 as int = 0,
@runid6 as int = 0,
@siteid as int
)
AS
declare @effectivedate0 as datetime
declare @effectivedate1 as datetime
declare @effectivedate2 as datetime
declare @effectivedate3 as datetime
declare @effectivedate4 as datetime
declare @effectivedate5 as datetime
declare @effectivedate6 as datetime
set @effectivedate0 = (Select TOP 1 effective_from FROM db_t_run_plan WHERE effective_from <= GetDATE() AND site_id=@siteid AND run_id=@RunID0 order by effective_from DESC)
set @effectivedate1 = (Select TOP 1 effective_from FROM db_t_run_plan WHERE effective_from <= GetDATE() AND site_id=@siteid AND run_id=@RunId1 order by effective_from DESC)
set @effectivedate2 = (Select TOP 1 effective_from FROM db_t_run_plan WHERE effective_from <= GetDATE() AND site_id=@siteid AND run_id=@RunID2 order by effective_from DESC)
set @effectivedate3 = (Select TOP 1 effective_from FROM db_t_run_plan WHERE effective_from <= GetDATE() AND site_id=@siteid AND run_id=@RunID3 order by effective_from DESC)
set @effectivedate4 = (Select TOP 1 effective_from FROM db_t_run_plan WHERE effective_from <= GetDATE() AND site_id=@siteid AND run_id=@RunID4 order by effective_from DESC)
set @effectivedate5 = (Select TOP 1 effective_from FROM db_t_run_plan WHERE effective_from <= GetDATE() AND site_id=@siteid AND run_id=@RunID5 order by effective_from DESC)
set @effectivedate6 = (Select TOP 1 effective_from FROM db_t_run_plan WHERE effective_from <= GetDATE() AND site_id=@siteid AND run_id=@RunID6 order by effective_from DESC)
Select * From
(SELECT arrival as arr0 from db_t_run_orders where run_id = @runid0 and site_id=@siteid and trip_no=1 and visit_order = 1 and effective_from = @effectivedate0) a
Cross Join
(SELECT arrival as arr1 from db_t_run_orders where run_id = @runid1 and site_id=@siteid and trip_no=1 and visit_order = 1 and effective_from = @effectivedate1) b
Cross Join
(SELECT arrival as arr2 from db_t_run_orders where run_id = @runid2 and site_id=@siteid and trip_no=1 and visit_order = 1 and effective_from = @effectivedate2) c
Cross Join
(SELECT arrival as arr3 from db_t_run_orders where run_id = @runid3 and site_id=@siteid and trip_no=1 and visit_order = 1 and effective_from = @effectivedate3) d
Cross Join
(SELECT arrival as arr4 from db_t_run_orders where run_id = @runid4 and site_id=@siteid and trip_no=1 and visit_order = 1 and effective_from = @effectivedate4) e
Cross Join
(SELECT arrival as arr5 from db_t_run_orders where run_id = @runid5 and site_id=@siteid and trip_no=1 and visit_order = 1 and effective_from = @effectivedate5) f
Cross Join
(SELECT arrival as arr6 from db_t_run_orders where run_id = @runid6 and site_id=@siteid and trip_no=1 and visit_order = 1 and effective_from = @effectivedate6) g
RETURN
好吧,联合枢轴的想法我无法工作,但我确实找到了另一种方法,它可以帮助那些枢轴对他们也不起作用的其他人。
只需声明每天的变量和 运行 每个变量的单一查询,将它们转储到临时 table 和 select * table.
它会比组合查询更有效吗,可能不会。 这是最干净/最简洁的方法吗,绝对不是。 它能完成工作吗?
如果有人希望post更好的版本,显然请这样做。
(
@runid0 as int = 0,
@runid1 as int = 0,
@runid2 as int = 0,
@runid3 as int = 0,
@runid4 as int = 0,
@runid5 as int = 0,
@runid6 as int = 0,
@siteid as int
)
作为 将@effectivedate0 声明为日期时间 将@effectivedate1 声明为日期时间 将@effectivedate2 声明为日期时间 将@effectivedate3 声明为日期时间 将@effectivedate4 声明为日期时间 将@effectivedate5 声明为日期时间 将@effectivedate6 声明为日期时间 将 @arr0 声明为 varchar(5) 将 @arr1 声明为 varchar(5) 将 @arr2 声明为 varchar(5) 将 @arr3 声明为 varchar(5) 将 @arr4 声明为 varchar(5) 将 @arr5 声明为 varchar(5) 将 @arr6 声明为 varchar(5)
Declare @table Table ( day0 varchar(5), day1 varchar(5), day2 varchar(5), day3 varchar(5), day4 varchar(5), day5 varchar(5), day6 varchar(5))
BEGIN
set @effectivedate0 = (Select TOP 1 effective_from FROM dbo_t_run_plan WHERE effective_from <= GetDATE() AND site_id=@siteid AND run_id=@RunID0 order by effective_from DESC)
set @arr0 = (SELECT arrival as arr from dbo_t_run_orders where run_id = @runid0 and site_id=@siteid and trip_no=1 and visit_order = 1 and effective_from = @effectivedate0)
END
BEGIN
set @effectivedate1 = (Select TOP 1 effective_from FROM dbo_t_run_plan WHERE effective_from <= GetDATE() AND site_id=@siteid AND run_id=@RunId1 order by effective_from DESC)
set @arr1 = (SELECT arrival as arr from dbo_t_run_orders where run_id = @runid1 and site_id=@siteid and trip_no=1 and visit_order = 1 and effective_from = @effectivedate1)
END
BEGIN
set @effectivedate2 = (Select TOP 1 effective_from FROM dbo_t_run_plan WHERE effective_from <= GetDATE() AND site_id=@siteid AND run_id=@RunID2 order by effective_from DESC)
set @arr2 = (SELECT arrival as arr from dbo_t_run_orders where run_id = @runid2 and site_id=@siteid and trip_no=1 and visit_order = 1 and effective_from = @effectivedate2)
END
BEGIN
set @effectivedate3 = (Select TOP 1 effective_from FROM dbo_t_run_plan WHERE effective_from <= GetDATE() AND site_id=@siteid AND run_id=@RunID3 order by effective_from DESC)
set @arr3 = (SELECT arrival as arr from dbo_t_run_orders where run_id = @runid3 and site_id=@siteid and trip_no=1 and visit_order = 1 and effective_from = @effectivedate3)
END
BEGIN
set @effectivedate4 = (Select TOP 1 effective_from FROM dbo_t_run_plan WHERE effective_from <= GetDATE() AND site_id=@siteid AND run_id=@RunID4 order by effective_from DESC)
set @arr4 = (SELECT arrival as arr from dbo_t_run_orders where run_id = @runid4 and site_id=@siteid and trip_no=1 and visit_order = 1 and effective_from = @effectivedate4)
END
BEGIN
set @effectivedate5 = (Select TOP 1 effective_from FROM dbo_t_run_plan WHERE effective_from <= GetDATE() AND site_id=@siteid AND run_id=@RunID5 order by effective_from DESC)
set @arr5 = (SELECT arrival as arr from dbo_t_run_orders where run_id = @runid5 and site_id=@siteid and trip_no=1 and visit_order = 1 and effective_from = @effectivedate5)
END
BEGIN
set @effectivedate6 = (Select TOP 1 effective_from FROM dbo_t_run_plan WHERE effective_from <= GetDATE() AND site_id=@siteid AND run_id=@RunID6 order by effective_from DESC)
set @arr6 = (SELECT arrival as arr from dbo_t_run_orders where run_id = @runid6 and site_id=@siteid and trip_no=1 and visit_order = 1 and effective_from = @effectivedate6)
END
insert into @table values (@arr0,@arr1,@arr2,@arr3,@arr4,@arr5,@arr6)
select * from @table
RETURN