SQL:如何在选择查询中基于另一个列添加ID列?

时间:2018-07-05 15:37:29

标签: sql sql-server row-number

下面是我的数据集的近似值

DataSetName Date        Sname    Level     Frequency    SetId   ScenarioId  FrequencyId
   Set A    01/31/1980  Base     64,007     Monthly       49    1           2
   Set A    02/29/1980  Base     64,014     Monthly       49    1           2
   Set A    03/31/1980  Stress   64,015     Monthly       49    2           2
   Set A    04/30/1980  Stress   64,008     Monthly       49    2           2
   Set B    05/31/1980  Storm    63,993     Monthly       54    5           2
   Set B    06/30/1980  Raptor   63,972     Monthly       54    24          2
   Set B    07/31/1980  Agile    63,788     Monthly       54    25          2
   Set B    08/31/1980  Pond     63,868     Monthly       54    27          2
   Set B    07/31/1980  Agile    63,212     Monthly       54    25          2

对于字段Sname,每次尝试运行select语句时,我都试图创建一个唯一但动态的ID。

我尝试使用row_number函数

Select a.*,ROW_NUMBER() OVER(PARTITION BY Sname Order by DataSetName) 
as S_row_id from Table1

我也尝试过使用子查询

 Select Top 100 a.*,b.S_Row_Id
 from Table1 a
 Left join
 ( Select Distinct Sname,ROW_NUMBER() OVER(PARTITION BY Sname 
 Order by Sname) as Scenario_Row_Id from Table1)b
 on a.ScenarioName=b.ScenarioName

这些都不给我我想要的输出,看起来像这样

DataSetName Date        Sname    Level     Frequency    SetId   S_row_id    FrequencyId
   Set A    01/31/1980  Base     64,007     Monthly       49    1           2
   Set A    02/29/1980  Base     64,014     Monthly       49    1           2
   Set A    03/31/1980  Stress   64,015     Monthly       49    2           2
   Set A    04/30/1980  Stress   64,008     Monthly       49    2           2
   Set B    05/31/1980  Storm    63,993     Monthly       54    3           2
   Set B    06/30/1980  Raptor   63,972     Monthly       54    4           2
   Set B    07/31/1980  Agile    63,788     Monthly       54    5           2
   Set B    08/31/1980  Pond     63,868     Monthly       54    6           2
   Set B    07/31/1980  Agile    63,212     Monthly       54    5           2
   Set B    07/31/1980  Pond     63,457     Monthly       54    6           2

我可以做些什么来实现此结果集,在该结果集中,Sname每次运行时都会以递增的顺序自动分配给它。让我知道您是否需要mroe详细信息?

3 个答案:

答案 0 :(得分:1)

类似

    SELECT T2.*, D2.TheID FROM Table1 T2 JOIN 
    (
        SELECT D1.Sname, row_number() OVER (ORDER BY D1.sname) TheID FROM 
        (SELECT DISTINCT t1.Sname From Table1 t1) D1
    ) D2
    ON T2.sname = D2.sname

答案 1 :(得分:1)

您可以使用apply

select a.*, t2.S_row_id
from Table1 t1 cross apply 
     ( select count(distinct t2.sname) as S_row_id
       from table1 t2
       where t2.sname = t1.sname and
             t2.Date <= t1.date
     ) t2;

答案 2 :(得分:1)

为什么没有DENSE_RANK函数?

class Class2 : public Class1 
{
    ...
}

with
  t as(
    select * from (values
      ('Set A', '01/31/1980', 'Base', 64.007, 'Monthly', 49, 1, 2),
      ('Set A', '02/29/1980', 'Base', 64.014, 'Monthly', 49, 1, 2),
      ('Set A', '03/31/1980', 'Stress', 64.015, 'Monthly', 49, 2, 2),
      ('Set A', '04/30/1980', 'Stress', 64.008, 'Monthly', 49, 2, 2),
      ('Set B', '05/31/1980', 'Storm', 63.993, 'Monthly', 54, 5, 2),
      ('Set B', '06/30/1980', 'Raptor', 63.972, 'Monthly', 54, 24, 2),
      ('Set B', '07/31/1980', 'Agile', 63.788, 'Monthly', 54, 25, 2),
      ('Set B', '08/31/1980', 'Pond', 63.868, 'Monthly', 54, 27, 2),
      ('Set B', '07/31/1980', 'Agile', 63.212, 'Monthly', 54, 25, 2),
      ('Set B', '07/31/1980', 'Pond', 63.457, 'Monthly', 54, 6, 2)
    )v(DataSetName, "Date", Sname, Level, Frequency, SetId, ScenarioId, FrequencyId)
  )
select *,
  dense_rank() over(order by DataSetName, Sname) S_row_id
from t;