检索多个但不是所有字段但是同一列

时间:2015-06-17 13:13:46

标签: sql sql-server tsql stored-procedures

我需要从excel文档中为驱动程序填充Rota表,其中存储了行程ID号。

上传到应用程序时我需要它通过SP从每天的运行表中获取该运行的开始时间

这一部分不是问题,一切正常,当某些日子是“0”时,问题就开始了。 (休息日)所以除非司机每周都有,否则数据不会返回。

我知道这不是一个非常有吸引力的SQL,但它只是一个快速的模型来解释这个问题。

这是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

1 个答案:

答案 0 :(得分:0)

好吧工会支点的想法我无法工作,但我确实找到了另一种方法,可以帮助其他枢轴不适合他们的人。

只需声明每天的变量并对每个变量运行单数查询,将它们转储到临时表中并选择*表。

它是否比组合查询更有效,可能不是。 它是最干净/最简洁的方法,绝对不是。 它是否做到了这一点。

如果有人希望发布更好的版本,请这样做。

(   
@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     将@ effectivedate0声明为datetime     将@ effectivedate1声明为datetime     将@ effectivedate2声明为datetime     将@ effectivedate3声明为datetime     将@ effectivedate4声明为datetime     将@ effectivedate5声明为datetime     将@ effectivedate6声明为datetime     将@ 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