【问题标题】:SQL select column values based on 2 datesSQL 根据 2 个日期选择列值
【发布时间】:2018-03-30 14:02:57
【问题描述】:

我有一个很大的 select 语句,它是由许多其他表和连接构建的。

我需要另一个包含相同数据但基于 2 个不同日期的 select 语句。

当前选择结果如下

Date       |   ID   |Account|Prod|Location|Cost|Owner  |
10-10-2017 | #73818 | B17H12|M104|EastNY  |400 |Jay Bob|
10-11-2017 | #77618 | B16H14|M104|EastNY  |200 |Jay Bob|
10-11-2017 | #73818 | B17H12|M106|WestNY  |300 |Jay Bob|
10-10-2017 | #75543 | B13H17|M106|WestNY  |900 |Jay Bob|

我需要比较的新语句与上面相同,但返回我选择的 2 个不同日期的状态,比如第 10 天和第 11 天,所以表格就是这样

Owner    |   ID    | Account |    Date 1  | Prod |Location | Cost | Date 2     | Prod | Location |Cost|
Jay Bob  |  #73818 | B17H12  | 10-10-2017 | M104 | EastNY  | 400  | 10-11-2017 | M106 | EastNY    |300 |
Jay Bob  |  #77618 | B16H14  | 10-10-2017 | NULL |   NULL  | NULL | 10-11-2017 | M104 | EastNY    |200 |
Jay Bob  |  #75543 | B13H17  | 10-10-2017 | M106 | WestNY  | 900  | 10-11-2017 | NULL | NULL      |NULL|

这是原始的擦洗语句 unit.sample_date 有日期, unit.entity_id 是 ID 在这种情况下,我不需要很多这样的行,但有些我需要选择我需要的是来自 2 个不同日期的这些比较列的子集。

    with temp as (select platform as pltfrm, total_01, total_02, entity_identification.display_name as Platform from hardware
    LEFT join host_info on hardware.id = host_info.hardware_id
    left join entity_identification on host_info.entity_id = entity_identification.entity_id 
     where platform= 'Plat01')
    select unit.sample_date,
      entity_identification.display_name as hostname,
      entity_identification.object_id,
      pim.[Product_Code] as 'PrdCode',
      pim.ProductName,
      ha4.attr_value as Region,
      ha5.attr_value as Plat01_Account,
      ha1.attr_value as 'Environment',
      ha2.attr_value as 'Inventory_Code',
      ha3.attr_value as 'moving_Group',
      enid01.object_id as 'moving_Group_ID',
      unit.exist_space_type as 'Current_space_type',
      unit.recomm_space_type as 'Recommended_space_type',
      CASE
        when ha.attr_value IS NULL then 'N/A' else ha.attr_value end as 'Price_Model',
      unit.exist_cost as 'Current_space_Cost',
      unit.recomm_cost as 'Recommended_space_Cost',
      unit.exist_cost - unit.recomm_cost as 'Savings_Month',
      (unit.exist_cost - unit.recomm_cost) / 30 as 'Savings_Daily',
      CASE 
        when unit.exist_cost > unit.recomm_cost AND ((unit.recomm_space_type not like 'idle%')  AND (substring(unit.recomm_space_type, 0, 2) != substring(unit.exist_space_type, 0, 2))) then 'too_much'
        when unit.exist_cost < unit.recomm_cost  AND ((unit.recomm_space_type not like 'idle%') AND (substring(unit.recomm_space_type, 0, 2) != substring(unit.exist_space_type, 0, 2))) then 'too_little' 
        when unit.exist_cost = unit.recomm_cost then 'Just_Right' 
        when substring(unit.recomm_space_type, 0, 2) = substring(unit.exist_space_type, 0, 2) then 'update'
        when unit.recomm_space_type like 'idle%' then 'remove' end as 'Status_On_Date',

      v_system_info_hist.total_01 as 'Cur_LT01_Total_T01Low',
      ((unit.exist_cost) / (v_system_info_hist.total_01)*1000) as 'Cur_Cost_Per_T01',
      case
        when temp.total_01 IS NULL then 0 else temp.total_01 end as 'Rec_Total_LT01_T01Low',
      case
        when temp.total_01 IS NULL then 0 else ((unit.recomm_cost) / (temp.total_01)*1000) end as 'Rec_Cost_Per_T01',
      case
        when temp.total_01 IS NULL then ((unit.exist_cost) / (v_system_info_hist.total_01)*1000) else ( ((unit.exist_cost) / (v_system_info_hist.total_01)*1000)-((unit.recomm_cost) / (temp.total_01)*1000) ) end as 'Savings_per_T01',
        case
        when temp.total_02 IS NULL then ((unit.exist_cost) / (v_system_info_hist.total_02)) else  ((unit.exist_cost/v_system_info_hist.total_01)*temp.total_01) - (unit.recomm_cost) end as 'LT01_Projected_Total_Potential_Cost_Saving',
      v_system_info_hist.total_02 as 'Cur_T02_Count',
      unit.exist_cost / v_system_info_hist.total_02 as 'Cur_Cost_Per_T02',
      case
        when temp.total_02 IS NULL then 0 else temp.total_02 end as 'Rec_T02_Count',
      case
        when temp.total_02 IS NULL then 0 else ((unit.recomm_cost) / (temp.total_02)) end as 'Rec_Cost_Per_T02',
      case
        when temp.total_02 IS NULL then ((unit.exist_cost) / (v_system_info_hist.total_02)) else ( ((unit.exist_cost) / (v_system_info_hist.total_02))-((unit.recomm_cost) / (temp.total_02)) ) end as 'Savings_per_T02',
      case
        when temp.total_02 IS NULL then ((unit.exist_cost) / (v_system_info_hist.total_02)) else ( ((unit.exist_cost/v_system_info_hist.total_02)*temp.total_02) - (unit.recomm_cost)  ) end as 'T02_Projected_Total_Potential_Cost_Saving',
      case
        when ha6.attr_value IS NULL AND unit.exist_cost > unit.recomm_cost AND ((unit.recomm_space_type not like 'idle%')  AND (substring(unit.recomm_space_type, 0, 2) != substring(unit.exist_space_type, 0, 2))) then 85
        when ha6.attr_value IS NULL AND unit.exist_cost < unit.recomm_cost  AND ((unit.recomm_space_type not like 'idle%') AND (substring(unit.recomm_space_type, 0, 2) != substring(unit.exist_space_type, 0, 2))) then 80
        when ha6.attr_value IS NULL AND unit.exist_cost = unit.recomm_cost then 5
        when ha6.attr_value IS NULL AND substring(unit.recomm_space_type, 0, 2) = substring(unit.exist_space_type, 0, 2) then 88 
        when ha6.attr_value IS NULL AND unit.recomm_space_type like 'idle%' then 90 else ha6.attr_value end as 'Confidence_Score',
      case
        when ha7.attr_value IS NULL AND unit.exist_cost > unit.recomm_cost AND ((unit.recomm_space_type not like 'idle%')  AND (substring(unit.recomm_space_type, 0, 2) != substring(unit.exist_space_type, 0, 2))) then 3
        when ha7.attr_value IS NULL AND unit.exist_cost < unit.recomm_cost  AND ((unit.recomm_space_type not like 'idle%') AND (substring(unit.recomm_space_type, 0, 2) != substring(unit.exist_space_type, 0, 2))) then 5
        when ha7.attr_value IS NULL AND unit.exist_cost = unit.recomm_cost then 5
        when ha7.attr_value IS NULL AND substring(unit.recomm_space_type, 0, 2) = substring(unit.exist_space_type, 0, 2) then 2
        when ha7.attr_value IS NULL AND unit.recomm_space_type like 'idle%' then 2 else ha7.attr_value end as 'PSFT_Ticket_Sev',
      pim.SeniorTechOwner,
      pim.TechOwner,
      pim.SupportEmail,
      pim.SeniorProductOwner,
      pim.PSFT,
      pim.SlackChannel,
      v_ig.ig_name as Availability_Zone,
      pim.[Asset_Id] as 'Asset_Id'
     FROM [dbo].[unit_u_transfer_hist] unit 
     LEFT JOIN host_attributes on host_attributes.host_name = unit.entity_id and host_attributes.attr_key = 'attr_5'
     LEFT JOIN host_attributes ha on ha.host_name = unit.entity_id and ha.attr_key = 'attr_10'
     LEFT JOIN host_attributes ha1 on ha1.host_name = unit.entity_id and ha1.attr_key = 'attr_3'
     LEFT JOIN host_attributes ha2 on ha2.host_name = unit.entity_id and ha2.attr_key = 'attr_4' 
     LEFT JOIN host_attributes ha3 on ha3.host_name = unit.entity_id and ha3.attr_key = 'attr_9'
     LEFT JOIN host_attributes ha4 on ha4.host_name = unit.entity_id and ha4.attr_key = 'VE_LOCATION_DATA'
     LEFT JOIN host_attributes ha5 on ha5.host_name = unit.entity_id and ha5.attr_key = 'VE_LOCATION_DOM'
     LEFT JOIN host_attributes ha6 on ha6.host_name = unit.entity_id and ha6.attr_key = 'attr_14'
     LEFT JOIN host_attributes ha7 on ha7.host_name = unit.entity_id and ha7.attr_key = 'attr_15'
     LEFT JOIN v_system_info_hist on v_system_info_hist.entity_id = unit.entity_id and dbo.v_system_info_hist.sample_date = unit.sample_date
     LEFT JOIN entity_identification enid01  on v_system_info_hist.parent_entity_id = enid01.entity_id and dbo.v_system_info_hist.sample_date =  unit.sample_date
     LEFT JOIN temp on unit.recomm_space_type like temp.platform+'%'

     LEFT JOIN v_ig on v_ig.ig_id = unit.ig_id
     LEFT JOIN [dbo].[ud_tkm_pimm] pim on pim.[Product_Code] = Replace(host_attributes.attr_value, 'PRD00000','PRD') 
     OR pim.[Product_Code] = Replace(host_attributes.attr_value, 'PRD0000','PRD') 
     LEFT JOIN entity_identification on entity_identification.entity_id = unit.entity_id
 where unit.sample_date >= DATEADD(day, -13, GETDATE()) ORDER BY PrdCode desc

【问题讨论】:

  • 什么 rdbms 以及查询是否太大以至于无法发布?
  • 不是关于它有多大(90 行)更多的是关于机密性。我会看看我是否可以擦洗它并发布它。
  • rdbms 没关系?
  • 哪个 dbms?对于不同的 dbms,您将得到不同的答案。如果你得到错误的 dbms 的答案,那就没用了。我不知道这篇文章是如何被点赞的。缺少标签,没有尝试。
  • 数据库是MS SQL

标签: sql select join


【解决方案1】:

从您的结果集中,您的 2 个日期似乎基于 2 个具有相同 ID 的不同记录。如果不查看要查询的表的详细信息,很难告诉您如何执行此操作,但基本想法是在 ID 和使它们不同的条件上再次加入表

即

    SELECT d.ID, d.Date AS Date1, d2.Date AS Date2
    FROM IDTABLE d
    LEFT JOIN IDTABLE d2
    ON d.ID=d2.ID
    AND d2.Date<d1.Date

【讨论】:

    【解决方案2】:

    您没有指定数据库,所以我使用 SQL Server 语法。您需要对这两个日期进行子选择,然后进行完全外部联接。

    DECLARE @first datetime, @second datetime
    SELECT @first='10/10/2017', @second='10/11/2017'
    
    SELECT isnull(first.Owner, second.Owner) Owner,
        isnull(first.ID, second.ID) ID,
        isnull(first.Account, second.Account) Account,
        @first [Date 1],
        first.Prod,
        first.Location,
        first.Cost,
        @second [Date 2],
        second.Prod,
        second.Location,
        second.Cost 
    FROM
    (SELECT * FROM Table WHERE Date=@first) first
    FULL OUTER JOIN
    (SELECT * FROM Table WHERE Date=@second) second
        ON first.Owner=second.Owner 
            and first.ID=second.ID
            and first.Account=second.Account
    

    【讨论】:

    • 抱歉是 MS SQL - 我在上面的原始帖子中添加了我目前拥有的完整查询。
    • 好的,完美。那么上面的代码应该适合你
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-11
    • 1970-01-01
    • 2022-06-23
    • 2019-03-29
    • 1970-01-01
    相关资源
    最近更新 更多