简体   繁体   English

如何获得给定表的先前ID值?

[英]How to get the previous Id value for a given table?

I have two input tables: 我有两个输入表:

1) Site: 1)网站:

 site_id||   site_name||   site_location

 1000   ||    abc     ||    XYZ_123
 1001   ||    tyu     ||    ERD_123
 1002   ||    iok     ||    FTR_678
 1003   ||    okn     ||    YHU_987
 1004   ||    ybg     ||    OLP_008
 1005   ||    qwe     ||    PLM_126

2) 2)

product: 产品:

 Product_id|| product_name||start_date||end_date

   212     || sme1        ||2014-12-25||2017-03-13
   250     || try1        ||2013-12-15|| 2017-03-13
   267     || inu1        || 2015-03-27|| 2017-03-17

I need to check how many times the id is repeated and order it like the output table. 我需要检查ID重复多少次,并像输出表一样对其进行排序。

The previousId is to be caluclated if the preoduct_id is repeated and its less than the end date. 如果preoduct_id重复且小于结束日期,则会计算previousId。

This is what I have done 'till now. 这就是我现在所做的。

select top 1 d2.PRODUCT_id, d2.last_date, d1.*
from output_table d1,
     output_table  d2 
where d1.SITE_id = d2.Site_id
  and d1.START_DATE >= d2.end_DATE
  and d1.Site_id=1001
  and d1.PRODUCT_id = 250
order by d2.End_date desc 

any help is appreciated. 任何帮助表示赞赏。

i need to output as my table. 我需要输出为我的桌子。

this is my output table structure : 这是我的输出表结构:

Id|| site_id|| product_id|| previous_id|| start_date|| end_date  ||Previous_site_id || repeated_times || Previous_id

1 || 1000   ||250        || null       || 2015-01-01||2017-03-13 || 1001            ||3              || 2
2 || 1001   ||250        || 1          || 2014-12-25||2015-01-01 || 1002            ||3               || 3
3 || 1002   ||250        || 2          || 2013-12-15||2014-12-25 || Null            ||3               || Null
4 || 1003   ||267        || null       || 2015-03-27|| 2017-03-17|| Null            ||0               || Null
5 ||1004    ||212        || null       || 2016-01-01||2017-03-13 ||1004             ||2               || 6
6 || 1005   ||212        || 1          || 2014-12-25||2015-12-30 ||Null             ||2               || Null

Since you provided a pretty poor description of your schema, I've made a lot by guess. 由于您对架构的描述很差,因此我做了很多猜测。 Considering you use MSSQL 2012, there should be LAG windowed function that could be used in some way similar to the following: 考虑到您使用的是MSSQL 2012,应该有LAG窗口函数,该函数可以类似于以下内容的方式使用:

WITH CountedResult AS
(
    SELECT
        ROW_NUMBER() OVER(ORDER BY product_id, site_id) AS Id, -- use some id instead, if you have
        site_id,
        product_id,
        [start_date],
        [end_date],
        COUNT(*) OVER(PARTITION BY product_id) AS repeated_times
    FROM output_table
    WHERE
        site_id=1001
        AND product_id = 250
)
SELECT 
    Id, site_id, product_id, [start_date], [end_date],
    LAG(site_id, 1, 0) OVER (PARTITION BY product_id ORDER BY [start_date] DESC) AS previous_site_id,
    repeated_times,
    LAG(Id, 1, 0) OVER (PARTITION BY product_id ORDER BY [start_date] DESC) AS previous_id,
FROM CountedResult
ORDER BY product_id, site_id

Read more about LAG on MSDN: https://msdn.microsoft.com/en-us/library/hh231256.aspx 在MSDN上阅读有关LAG更多信息: https : //msdn.microsoft.com/en-us/library/hh231256.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM