【发布时间】: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