在1个返回行

时间:2018-01-31 10:15:19

标签: sql sql-server-2014

我有一个查询,每个DetectorID应返回1行。我们目前的情况如下:

DECLARE @StartDate datetime SET @StartDate  = '2017-12-01 00:00'
DECLARE @EndDate datetime select @EndDate ='2018-01-01 00:00'

DECLARE @Limit1 real SET @Limit1 = 200; DECLARE @AssenLimit1 real SET @AssenLimit1 = 0;
DECLARE @Limit2 real SET @Limit2 = 300; DECLARE @AssenLimit2 real SET @AssenLimit2 = 0;
DECLARE @Limit3 real SET @Limit3 = 350; DECLARE @AssenLimit3 real SET @AssenLimit3 = 0;
DECLARE @Limit4 real SET @Limit4 = 400; DECLARE @AssenLimit4 real SET @AssenLimit4 = 0;
DECLARE @Limit5 real set @Limit5 = 500; DECLARE @AssenLimit5 real SET @AssenLimit5 = 0; 

SELECT DetectorID,  AVG(ValidWIM) AS 'PI_mean_WIM',
COUNT(ValidWIM) AS 'PI_mean_WIM2',
COUNT(CASE
        WHEN ValidWIM > 0.5 THEN ValidWIM ELSE NULL END)
        AS 'PI_mean_WIM3'

,AVG((ValidWDD_Left+ValidWDD_Right)/2) AS 'PI_mean_WDD' 
,COUNT(ValidWDD_Left) AS 'PI_mean_WDD2'
,COUNT (CASE
        WHEN (ValidWDD_Left>0.5 AND ValidWDD_Right>0.5) THEN ValidWDD_Left ELSE NULL END)
        AS PI_mean_WDD3
,COUNT(CASE 
        WHEN AxleLoad IS NOT NULL Then TrainPassageInformationID ELSE NULL END)
         AS 'nWheels' 
, COUNT(CASE
        WHEN (PeakForceLeft > @Limit1 OR PeakForceRight>@Limit1) THEN TrainPassageInformationID ELSE NULL END)
        AS 'n>200'
, COUNT(CASE
        WHEN (PeakForceLeft > @Limit2 OR PeakForceRight>@Limit2) THEN TrainPassageInformationID ELSE NULL END)
        AS 'n>300'
, COUNT(CASE
        WHEN (PeakForceLeft > @Limit3 OR PeakForceRight>@Limit3) THEN TrainPassageInformationID ELSE NULL END)
        AS 'n>350'
, COUNT(CASE
        WHEN (PeakForceLeft > @Limit4 OR PeakForceRight>@Limit4) THEN TrainPassageInformationID ELSE NULL END)
        AS 'n>400'
, COUNT(CASE
        WHEN (PeakForceLeft > @Limit5 OR PeakForceRight>@Limit5) THEN TrainPassageInformationID ELSE NULL END)
        AS 'n>500'
From WheelDamage 
where (TimeOfAxle > @StartDate AND TimeOfAxle < @EndDate) and DetectorID in (11,12)
GROUP BY DetectorID 
order by DetectorID asc
SELECT DetectorID,
AVG(PeakForceLeft) AS 'NoiseLeft' FROM WheelDamage as t
where PeakForceLeft in (
    select top 10 percent PeakForceLeft 
    from wheeldamage as tt
    where tt.WheelDamageID = t.WheelDamageID AND tt.PeakForceLeft <> 0  AND tt.PeakForceLeft  IS NOT NULL AND (t.TimeOfAxle > @StartDate AND t.TimeOfAxle < @EndDate)
    order by tt.PeakForceLeft asc)
Group By DetectorID 
order by DetectorID asc


SELECT DetectorID,
AVG(PeakForceRight) AS 'NoiseRight' FROM WheelDamage as t
where PeakForceLeft in (
    select top 10 percent PeakForceRight
    from wheeldamage as tt
    where tt.WheelDamageID = t.WheelDamageID AND tt.PeakForceRight <> 0  AND tt.PeakForceLeft  IS NOT NULL AND (t.TimeOfAxle > @StartDate AND t.TimeOfAxle < @EndDate)
    order by tt.PeakForceLeft asc)
    Group By DetectorID 
order by DetectorID asc

SELECT DetectorId, COUNT (DateTime) AS 'Tags1' 
FROM [TagPassage] 
WHERE  Valid = 1 AND (DateTime > @StartDate AND DateTime < @EndDate)
GROUP by DetectorID 
Order by DetectorID asc

SELECT DetectorID, COUNT(CASE 
    WHEN (TotalWeight=0 OR TotalWeight IS NULL) AND (HasTrainStandStill<>1 OR HasTrainStandStill IS NULL) THEN  (TrainPassageInformationID) ELSE NULL END) AS 'notAnalyzed'
 , SUM (TotalWeight) AS 'TotalWeight'
FROM TrainPassageInformation 
WHERE   (Datetime > @StartDate AND Datetime < @EndDate) 
Group By DetectorID 
order by DetectorID asc

这是我理解的5个新行。这是因为我有5个选择语句。但是,我需要知道如何将最底层的4个select语句放在最上面的语句中,这样它才能返回1行。

我尝试了Select(Select X xxx)。但是这给出了它返回超过1行的错误。

Query Result

上图显示当前结果。我希望底部的4个表是第一个表中的列。

1 个答案:

答案 0 :(得分:0)

你可以通过DetectorID加入/左连接所有脚本作为表和链接,然后你可以从右下角的4个脚本中选择列。