在查询中使用函数结果以及在Case语句中使用结果

时间:2019-05-06 23:02:57

标签: sql-server

我正在计算行号作为查询中的字段。

我想使用该行号,然后根据查询中的行号创建另一个字段

尝试了几种可能性的迭代,并研究了建议的可能解决方案。

  SELECT a.Client_ID,
  CAST(ROW_NUMBER() OVER(PARTITION BY a.Client_ID ORDER BY 
  a.Billing_Order ASC) AS VARCHAR) AS Row_No,
  CASE a.Client_ID
   WHEN 1 THEN 'First_'
   WHEN 2 THEN 'Second_'
   WHEN 3 THEN 'Third_'
   WHEN 4 THEN 'Fourth_'
   ELSE 'Fifth_'
   END As Row_No_Name,
  a.Billing_Order,
  a.Payer_ID,
  b.Payer_Name,
  b.payertype_name As Payer_Type,
  a.Insurance_ID,
  a.Group_No,
  a.Copay_Fee,
  b.external_id As Fund_Code,
  a.Start_Date,
  CASE WHEN a.date_created > a.date_updated THEN a.date_created ELSE a.date_updated END As Last_Date_Updated
  FROM Credible_Client_Insurance_Raw_Data a
  LEFT OUTER JOIN Credible_Payers b
  ON a.Payer_ID = b.Payer_ID
  --
  WHERE a.End_Date IS NULL AND a.Deleted = 'false' and a.inactive = 'false'

预期结果:

     Client_ID, Row_No, Row_No_Name, Billing_Order etc
     1234       1       First_       3              etc
     1234       2       Second_      4              etc

0 个答案:

没有答案