pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

用于從歷史更改表中選擇實體的 SQL 查詢

SQL Query for selecting entities from a history change table(用于從歷史更改表中選擇實體的 SQL 查詢)
本文介紹了用于從歷史更改表中選擇實體的 SQL 查詢的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我必須創建一個我不確定如何處理的查詢,因為我無法涵蓋所有??情況.

I have to create a query that I am not really sure how to handle since I can't get all the cases covered.

我有一張名為 company 的表,其中包含以下(相對)列

I have one table called company with the following (relative) columns

餐桌公司

列:

Id | Name | Status | Status_Effective_Date

假設 Status 可以從 1 到 12 取值.邏輯是公司處于狀態 2 例如自 01/01/2018 (status_effective_date)

Let's say that Status can take values from 1 to 12. The logic is that the company is in status 2 for example since 01/01/2018 (status_effective_date)

表公司狀態歷史

Id | Company_Id | Status | Status_Effective_Date

此表保存狀態發生的更改的歷史記錄.例如,如果我有兩個 Id = 10 的公司條目,例如

This table holds the history of the changes that have happened to the status. If for example I have two entries for the company with Id = 10 like

Row_1 : 1 | 10 | 1 | 02/03/2011
Row_2 : 2 | 10 | 5 | 06/08/2013

那么 Id = 10 的公司從 02/03/201106/08/2013 的狀態是 1.之后就是狀態5.

Then the company with Id = 10 was is status 1 from 02/03/2011 until 06/08/2013. After that it was is status 5.

我想要做的是創建一個報告,向我顯示在選定日期范圍內處于選定狀態的所有公司.

現在,假設我想使用 status = 101/01/2017 - 31/12/2017 之間的日期范圍進行查詢

Now for example let's say I want to query with status = 1 and date range between 01/01/2017 - 31/12/2017

我必須涵蓋的我能理解的案例是:(是我想要的案例,沒有我不想要的案例)

The cases that I have to cover that I can understand are: (YES the cases that I want and NO the cases that I don't want)

  1. 一家公司始終處于狀態 1 且從未改變(是)

  1. One company was always in status 1 and never changed (YES)

1.1 company_table 條目的狀態 = 1 且生效日期早于開始日期

1.1 The company_table entry has status = 1 and effective date before the start date

1.2 company_status_history 表沒有任何行,因為沒有應用狀態更改

1.2 The company_status_history table doesn't have any rows since no change in the status has been applied

一家公司處于狀態 1 并在日期范圍 (NO) 之前更改為不同狀態

One company was in status 1 and changed to different status before the date range (NO)

2.1 company_table 條目的狀態 <> 1 和生效日期早于開始日期

2.1 The company_table entry has status <> 1 and effective date before the start date

2.2 company_status_history_table 有一個狀態為 1 的公司條目,生效日期為初始生效日期(初始狀態),以及一個新狀態 (<>1) 的公司條目,生效日期為初始生效日期更改(在日期范圍之前)

2.2 The company_status_history_table has one entry of the company in the status 1 with effective date the initial effective date (initial state) and one entry of the company in the new status (<>1) with effective date the date of the change (before the date range)

一家公司處于狀態 1 并在日期范圍內更改為不同的狀態 (YES)

One company was in status 1 and changed to different status inside the date range (YES)

3.1 company_table 條目的狀態為 <> 1 并且生效日期在開始日期之后和結束日期之前

3.1 The company_table entry has status <> 1 and effective date after the start date and before the end date

3.2 company_status_history_table 有一個狀態為 1 的公司條目,其生效日期為初始生效日期(初始狀態),以及一個處于新狀態 (<>1) 的公司條目,生效日期為初始生效日期更改(在日期范圍內)

3.2 The company_status_history_table has one entry of the company in the status 1 with effective date the initial effective date (initial state) and one entry of the company in the new status (<>1) with effective date the date of the change (inside the date range)

一家公司處于狀態 1 并在日期范圍之后更改為不同的狀態 (YES)

One company was in status 1 and changed to different status after the date range (YES)

4.1 company_table 條目的狀態為 <> 1 并且生效日期在結束日期之后

4.1 The company_table entry has status <> 1 and effective date after the after the end date

4.2 company_status_history_table 有一個狀態為 1 的公司條目,生效日期為初始生效日期(初始狀態),以及一個處于新狀態 (<>1) 的公司條目,生效日期為初始生效日期更改(在日期范圍之后)

4.2 The company_status_history_table has one entry of the company in the status 1 with effective date the initial effective date (initial state) and one entry of the company in the new status (<>1) with effective date the date of the change (after the date range)

一家公司在日期范圍之前處于狀態 <>1 并更改為狀態 = 1 (是)

One company was in status <>1 and changed to status = 1 before the date range (YES)

5.1 company_table 條目的狀態為 1 或 <> 1(因為它可能再次更改),如果它仍處于 status = 1,則生效日期可能早于日期范圍;如果再次更改,則生效日期可能在日期范圍之前.

5.1 The company_table entry has status 1 or <> 1 (since it might have changed again) and the effective date could be before the date range if it is still in status = 1 or some later date if it has changed again.

5.2 company_status_history_table 有一項前狀態公司的條目,其生效日期為初始生效日期(初始狀態),以及至少一項新狀態(=1)的公司條目,生效日期為生效日期更改(在日期范圍之前)

5.2 The company_status_history_table has one entry of the company in the previous status with effective date the initial effective date (initial state) and at least one entry of the company in the new status (=1) with effective date the date of the change (before the date range)

一家公司處于狀態 <>1 并在日期范圍內更改為狀態 1 (YES)

One company was in status <>1 and changed to status 1 inside the date range (YES)

6.1 company_table 條目的狀態為 1 或 <>1(如果再次更改),并且生效日期為 date_range 內的日期,如果再次更改,則為某個較晚的日期

6.1 The company_table entry has status 1 or <>1 (if it has changed again) and effective date the date inside the date_range or some later one if it has changed again

6.2 company_status_history_table 有一項處于初始狀態的公司條目,生效日期為初始生效日期(初始狀態),并且至少有一項處于狀態 1 的公司條目,生效日期為變更日期(內部日期范圍)

6.2 The company_status_history_table has one entry of the company in the initial status with effective date the initial effective date (initial state) and at least one entry of the company in the status 1 with effective date the date of the change (inside the date range)

一家公司的狀態為 <>1,但在日期范圍 (NO) 之后更改為狀態 1

One company was in status <>1 and changed to status 1 after the date range (NO)

7.1 company_table 條目的狀態為 1 或 <>1(如果有其他更改)和結束日期之后的生效日期

7.1 The company_table entry has status 1 or <>1 (if there has been another change) and effective date after the after the end date

7.2 company_status_history_table 有一項初始狀態的公司 <>1 的生效日期為初始生效日期(initial state)和至少一項新狀態的公司(1)的生效日期為更改日期(在日期范圍之后)

7.2 The company_status_history_table has one entry of the company in the initial status <>1 with effective date the initial effective date (initial state) and at least one entry of the company in the new status (1) with effective date the date of the change (after the date range)

到目前為止我所嘗試的是以下內容:

What I have tried so far is the below:

-- Case 6
select *
from company com, company_status_history csh 
where csh.company_status_id = 1
    and com.company_id = csh.company_id 
    and csh.company_status_eff_date > '20170101'
    and csh.company_status_eff_date < '20171231'
union
-- Case 1
select *
from company com
where com.company_status_id = 1
    and com.company_status_eff_date < '20181231'
    and com.company_id NOT IN (select company_id 
                        from company_status_history csh)

我猜使用聯合可能有更有效的方法.

I am guessing there might be a more effective way from using the union.

我缺少的是案例 3、4、5,這是我應該從以下查詢的前一個 company_status_history 條目(生效日期)中了解的部分,如果更改應將公司包括在我的最終列表中.

What I am missing is cases 3,4,5 and it is the part that I should understand from the previous company_status_history entry (effective date) of the following query if the change should include the company into my final list.

select * from company com, company_status_history csh 
where com.company_id = csh.company_id 

我們將不勝感激.

推薦答案

感謝 這個答案.我發布它以防其他人需要它.

I finally got the working query thanks to this answer to my simplified problem. I am posting it in case someone else needs it.

-- View that gets the entity_history_status with start date and end date
-- At the moment if it is only one entry in the table (it went to current) the end_date is the same as the start date
;WITH changed_companies_with_ranges AS
(
    -- Get the final table for the selected status
    SELECT company_id,
           MIN(company_status_eff_date) AS start_date, 
           CASE 
              WHEN COUNT(*) > 1 THEN DATEADD(DAY, -1, MAX(company_status_eff_date))
              ELSE min(company_status_eff_date) -- Check why it changes the result when I set the today as end_date
           END AS end_date,
           grp

    FROM (
              -- Something with counting again in order to get the start and end date according to this grp
              SELECT company_status_hist_id, company_id, company_status_id, company_status_eff_date,
                     ROW_NUMBER() OVER (PARTITION BY company_id ORDER BY company_status_eff_date) - 
                     cnt AS grp
              FROM (
                        -- Get the entries from the history table that had at any point the selected status and add a new column saying how many entries do they have
                        SELECT esh2.company_status_hist_id, esh2.company_id, esh2.ecompany_status_id, esh2.company_status_eff_date, x.cnt
                        FROM company_status_history AS esh2
                        OUTER APPLY 
                        (
                           SELECT COUNT(*) AS cnt
                           FROM ecompany_status_history AS c
                           WHERE c.company_status_id = 1 -- The selected status
                                 AND c.company_id  = esh2.company_id 
                                 AND c.company_status_eff_date < esh2.company_status_eff_date
                        ) AS x
                   ) as CTE
            ) as CTE2 
    GROUP BY company_id, grp
    HAVING COUNT(CASE WHEN company_status_id = 1 THEN 1 END) > 0 -- The selected status
)
SELECT * FROM (
    SELECT 
     en.company_id
    ,en.company_name
    ,en.company_reg_num
    FROM company en
    where en.company_id in(
                            select company_id 
                            from changed_entitities_with_ranges 
                            where start_date = end_date
                        )
                        and en.company_status_eff_date > '2017-01-01 00:00:00.000' -- Start Date
    union
    select 
     en.company_id
    ,en.company_name
    ,en.company_reg_num
    FROM company en
    where en.company_id in(
                            select company_id 
                            from changed_entitities_with_ranges 
                            where (start_date between '2017-01-01 00:00:00.000' and '2017-12-31 00:00:00.000') -- Range
                               or (start_date < '2017-01-01 00:00:00.000' and end_date > '2017-01-01 00:00:00.000') -- Start date -- End date
                         )
    union
    -- 1. Without any history changes + 3. changed to 1 before start date + 5 Changes to the normal status from other statuses before the end date + 7 Changes to the normal status from other statuses in between the period
    -- Gets the entities that haven't changed at all and have been in status 1 before the end date
    SELECT 
     en.company_id
    ,en.company_name
    ,en.company_reg_num
    FROM company en
    WHERE en.company_status_id = 1
                       AND en.company_status_eff_date < '2017-12-31 00:00:00.000'
    UNION
    -- 2. Changes to the other statuses from the status of normal after the start date + 4. Changes to the other statuses from the status of normal before the end date  + 6. Changes to the other statuses from the status of normal in between the period
    -- Gets the entities that have been changed to any status but were created or altered ato some point inside the range
    SELECT 
     en.company_id
    ,en.company_name
    ,en.company_reg_num
    from company en
    where en.company_id IN (select company_id from company_status_history es 
                       where es.company_status_eff_date BETWEEN '2017-01-01 00:00:00.000' AND '2017-12-31 00:00:00.000' AND es.company_status_id = 1)
) as result ORDER BY company_id

這篇關于用于從歷史更改表中選擇實體的 SQL 查詢的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

What SQL Server Datatype Should I Use To Store A Byte[](我應該使用什么 SQL Server 數據類型來存儲字節 [])
Interpreting type codes in sys.objects in SQL Server(解釋 SQL Server 中 sys.objects 中的類型代碼)
Typeorm Does not return all data(Typeorm 不返回所有數據)
Typeorm .loadRelationCountAndMap returns zeros(Typeorm .loadRelationCountAndMap 返回零)
How to convert #39;2016-07-01 01:12:22 PM#39; to #39;2016-07-01 13:12:22#39; hour format?(如何將“2016-07-01 01:12:22 PM轉換為“2016-07-01 13:12:22小時格式?)
MS SQL: Should ISDATE() Return quot;1quot; when Cannot Cast as Date?(MS SQL:ISDATE() 是否應該返回“1?什么時候不能投射為日期?)
主站蜘蛛池模板: 螺旋丝杆升降机-SWL蜗轮-滚珠丝杆升降机厂家-山东明泰传动机械有限公司 | 大通天成企业资质代办_承装修试电力设施许可证_增值电信业务经营许可证_无人机运营合格证_广播电视节目制作许可证 | 高压包-点火器-高压发生器-点火变压器-江苏天网 | 高压微雾加湿器_工业加湿器_温室喷雾-昌润空气净化设备 | 3d打印服务,3d打印汽车,三维扫描,硅胶复模,手板,快速模具,深圳市精速三维打印科技有限公司 | 盘扣式脚手架-附着式升降脚手架-移动脚手架,专ye承包服务商 - 苏州安踏脚手架工程有限公司 | 玻璃钢型材_拉挤模具_玻璃钢拉挤设备——滑县康百思 | 高效节能电机_伺服主轴电机_铜转子电机_交流感应伺服电机_图片_型号_江苏智马科技有限公司 | 耐高温风管_耐高温软管_食品级软管_吸尘管_钢丝软管_卫生级软管_塑料波纹管-东莞市鑫翔宇软管有限公司 | Copeland/谷轮压缩机,谷轮半封闭压缩机,谷轮涡旋压缩机,型号规格,技术参数,尺寸图片,价格经销商 CTP磁天平|小电容测量仪|阴阳极极化_双液系沸点测定仪|dsj电渗实验装置-南京桑力电子设备厂 | 智能终端_RTU_dcm_北斗星空自动化科技| 衬塑设备,衬四氟设备,衬氟设备-淄博鲲鹏防腐设备有限公司 | 一体化污水处理设备-一体化净水设备-「山东梦之洁水处理」 | 气体热式流量计-定量控制流量计(空气流量计厂家)-湖北南控仪表科技有限公司 | 活性炭-蜂窝-椰壳-柱状-粉状活性炭-河南唐达净水材料有限公司 | 制氮设备-变压吸附制氮设备-制氧设备-杭州聚贤气体设备制造有限公司 | 粉末冶金注射成型厂家|MIM厂家|粉末冶金齿轮|MIM零件-深圳市新泰兴精密科技 | 光栅尺厂家_数显表维修-苏州泽升精密机械| 湖北省煤炭供应链综合服务平台 | 祝融环境-地源热泵多恒系统高新技术企业,舒适生活环境缔造者! | 小型单室真空包装机,食品单室真空包装机-百科 | 食品无尘净化车间,食品罐装净化车间,净化车间配套风淋室-青岛旭恒洁净技术有限公司 | 沈阳缠绕膜价格_沈阳拉伸膜厂家_沈阳缠绕膜厂家直销 | 蜗轮丝杆升降机-螺旋升降机-丝杠升降机厂家-润驰传动 | 水环真空泵厂家,2bv真空泵,2be真空泵-淄博真空设备厂 | 杭州高温泵_热水泵_高温油泵|昆山奥兰克泵业制造有限公司 | 高空重型升降平台_高空液压举升平台_高空作业平台_移动式升降机-河南华鹰机械设备有限公司 | 语料库-提供经典范文,文案句子,常用文书,您的写作得力助手 | 退火炉,燃气退火炉,燃气热处理炉生产厂家-丹阳市丰泰工业炉有限公司 | 工业插头-工业插头插座【厂家】-温州罗曼电气 | Duoguan 夺冠集团| 杭州门窗厂家_阳光房_包阳台安装电话-杭州窗猫铝合金门窗 | 膏方加工_丸剂贴牌_膏滋代加工_湖北康瑞生物科技有限公司 | 斗式提升机_链式斗提机_带式斗提机厂家无锡市鸿诚输送机械有限公司 | 网络推广公司_网络营销方案策划_企业网络推广外包平台-上海澜推网络 | 溶氧传感器-pH传感器|哈美顿(hamilton)| 运动木地板厂家_体育木地板安装_篮球木地板选购_实木运动地板价格 | 塑木弯曲试验机_铜带拉伸强度试验机_拉压力测试台-倾技百科 | 传爱自考网_传爱自学考试网| 小程序开发公司_APP开发多少钱_软件开发定制_微信小程序制作_客户销售管理软件-济南小溪畅流网络科技有限公司 | 立刷【微电签pos机】-嘉联支付立刷运营中心 |