Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 在2.5和1.5值附近。所以,从技术上来说,我猜有“重启”。然而,为了克服它,我做了一些不同的事情。但是谢谢你的建议。如果我的解决方案表现不够好,我可能会重新考虑你的解决方案。 CREATE TABLE [dbo].[TestTable] ( [P_Sql_Sql Server_Gaps And Islands - Fatal编程技术网

Sql 在2.5和1.5值附近。所以,从技术上来说,我猜有“重启”。然而,为了克服它,我做了一些不同的事情。但是谢谢你的建议。如果我的解决方案表现不够好,我可能会重新考虑你的解决方案。 CREATE TABLE [dbo].[TestTable] ( [P

Sql 在2.5和1.5值附近。所以,从技术上来说,我猜有“重启”。然而,为了克服它,我做了一些不同的事情。但是谢谢你的建议。如果我的解决方案表现不够好,我可能会重新考虑你的解决方案。 CREATE TABLE [dbo].[TestTable] ( [P,sql,sql-server,gaps-and-islands,Sql,Sql Server,Gaps And Islands,在2.5和1.5值附近。所以,从技术上来说,我猜有“重启”。然而,为了克服它,我做了一些不同的事情。但是谢谢你的建议。如果我的解决方案表现不够好,我可能会重新考虑你的解决方案。 CREATE TABLE [dbo].[TestTable] ( [ProjectID] [INT] NOT NULL, [Index] [INT] NOT NULL, [Voltage] [DECIMAL](18, 3) NOT NULL, [Current] [DECIMAL](18,

在2.5和1.5值附近。所以,从技术上来说,我猜有“重启”。然而,为了克服它,我做了一些不同的事情。但是谢谢你的建议。如果我的解决方案表现不够好,我可能会重新考虑你的解决方案。
CREATE TABLE [dbo].[TestTable]
(
    [ProjectID] [INT] NOT NULL,
    [Index] [INT] NOT NULL,
    [Voltage] [DECIMAL](18, 3) NOT NULL,
    [Current] [DECIMAL](18, 3) NOT NULL
)
ProjectID   Index   Voltage     Current
---------------------------------------
1           1       2.3         3.4 
1           2       2.5         3.3
1           3       2.7         3.0
1           4       2.8         2.9
1           5       2.5         3.1
1           6       2.0         3.4
1           7       1.2         3.5
1           8       0.5         3.0
2           1       2.0         1.0
2           2       5.0         2.0
2           3       3.0         2.0
2           4       1.0         1.0
WITH CTE AS
(
    SELECT
        StartingTable.ProjectID,
        MIN(StartingTable.[Index]) StartingIndex,
        MIN(EndingTable.[Index]) - 1 EndingIndex
    FROM
        TestTable StartingTable
        JOIN TestTable EndingTable ON StartingTable.ProjectID = EndingTable.ProjectID
            AND EndingTable.[Index] > StartingTable.[Index]
    WHERE
        StartingTable.Voltage >= 2.5
        and EndingTable.Voltage <= 1.5
    GROUP BY
        StartingTable.ProjectID
)
SELECT
    TestTable.ProjectID,
    MAX(Voltage) MaxVoltage,
    StartingIndex,
    EndingIndex
FROM
    TestTable
    JOIN CTE ON TestTable.ProjectID = CTE.ProjectID
        AND TestTable.[Index] >= StartingIndex
        AND TestTable.[Index] <= EndingIndex
GROUP BY
    TestTable.ProjectID,
    StartingIndex,
    EndingIndex
ProjectID MaxVoltage StartingIndex EndingIndex
1         2.800      2             6
2         5.000      2             3
MAX(Voltage) OVER (PARTITION BY ProjectID ORDER BY [Index] ROWS BETWEEN Voltage >= 2.5 AND Voltage >= 1.5)
WITH CTE AS
(
    SELECT
        ProjectID,
        [Index],
        MAX(Voltage) OVER (PARTITION BY ProjectId ORDER BY [Index] ROWS UNBOUNDED PRECEDING) MaxVoltage
    FROM
        TestTable
)
SELECT
    TestTable.ProjectID,
    MAX(Voltage) MaxVoltage,
    MIN(TestTable.[Index]) StartingIndex,
    MAX(TestTable.[Index]) EndingIndex
FROM
    TestTable
    JOIN CTE ON TestTable.ProjectID = CTE.ProjectID
        AND TestTable.[Index] = CTE.[Index]
WHERE
    MaxVoltage >= 2.5
    AND Voltage >= 1.5
GROUP BY
    TestTable.ProjectID
SELECT tt.ProjectID, 
       MAX(tt.Voltage) AS MaxVoltage,
       x.StartIndex,
       MAX(tt.[Index]) AS EndIndex
FROM TestTable AS tt
JOIN 
(  
    SELECT ProjectID, 
           MIN([Index]) AS StartIndex
    FROM TestTable
    WHERE Voltage >= 2.5
    GROUP BY ProjectID 
) AS x ON tt.ProjectID = x.ProjectID 
WHERE tt.Voltage >= 1.5 
  AND tt.[Index] >= x.StartIndex
GROUP BY tt.ProjectID, x.StartIndex
SELECT
   ProjectID,
   max(Voltage) as MaxVoltage,
   MIN(case when Voltage >= 2.5 then [index] end) AS StartingIndex,
   MAX(case when Voltage >= 1.5 then [index] end) AS EndingIndex
FROM TestTable
group by ProjectID
having MAX(Voltage) >= 2.5 -- to filter group which never reached 2.5
with cte as 
(
   SELECT
      ProjectID,
      [Index],
      Voltage,
      max(case when Voltage < 1.5 then [Index] end)
      over (partition by ProjectID
            order by [Index]
            rows unbounded preceding) AS grp -- same value for a range of rows >= 1.5
   FROM TestTable
 )
select
   ProjectID,
   max(Voltage) as MaxVoltage,
   MIN(case when Voltage >= 2.5 then [index] end) AS StartingIndex,
   MAX([index]) AS EndingIndex
from cte
where Voltage >=1.5
group by ProjectID, grp
having MAX(Voltage) >= 2.5 -- to filter group which never reached 2.5
order by ProjectID, grp
;
SELECT [ProjectID], MAX([Voltage]) AS MaxVoltage, 
       MIN(CASE WHEN [Voltage] >= 2.5 THEN [Index] END) AS [StartingIndex],
       MAX(CASE WHEN [Voltage] >= 1.5 THEN [Index] END) AS [EndingIndex]
FROM [dbo].[TestTable]
WHERE [Voltage] >= 1.5
GROUP BY [ProjectId]
HAVING MAX([Voltage]) >= 2.5
ProjectID | MaxVoltage | StartingIndex | EndingIndex
--------: | :--------- | ------------: | ----------:
        1 | 2.800      |             2 |           6
        2 | 5.000      |             2 |           3
SELECT [ProjectID], MAX([Voltage]) AS [MaxVoltage],
       MIN(CASE WHEN [Voltage] >= 2.5 THEN [Index] END) AS [StartingIndex],
       MAX(CASE WHEN [Voltage] >= 1.5 THEN [Index] END) AS [EndingIndex]
FROM (SELECT [ProjectId], [Index], [Voltage], 
             [Index] - ROW_NUMBER() OVER(PARTITION BY [ProjectID] ORDER BY [Index]) AS [VoltageRun]
      FROM [dbo].[TestTable]       
      WHERE [Voltage] >= 1.5) [TestTable]
GROUP BY [ProjectID], [VoltageRun]
HAVING MAX([Voltage]) >= 2.5
ORDER BY [ProjectID], [VoltageRun]
ProjectId | Index | Voltage | VoltageRun
--------: | ----: | :------ | :---------
        1 |     1 | 2.300   | 0         
        1 |     2 | 2.500   | 0         
        1 |     3 | 2.700   | 0         
        1 |     4 | 2.800   | 0         
        1 |     5 | 2.500   | 0         
        1 |     6 | 2.000   | 0         
        1 |     9 | 2.300   | 2         
        1 |    10 | 2.500   | 2         
        1 |    11 | 2.700   | 2         
        1 |    12 | 2.800   | 2         
        1 |    13 | 2.500   | 2         
        1 |    14 | 2.000   | 2         
        2 |     1 | 2.000   | 0         
        2 |     2 | 5.000   | 0         
        2 |     3 | 3.000   | 0