生成唯一数字公司明智+年度智慧

时间:2017-08-31 05:52:36

标签: sql sql-server

我有一个包含列的表:PurchaseOrderIDCompanyIDCreatedDate。我希望此表中的一个名为PONo的列将自动具有上一列中的值,如:PO / 2016(来自CreatedDate的年份)/ 001(相同年份的值将增加且公司ID相同)。

我的预期表格如下:

PurchaseOrderID    CompanyID    PONo            CreatedDate
1                      1        PO/2016/001     2016-01-31
2                      1        PO/2016/002     2016-02-22
3                      1        PO/2016/003     2016-05-25
4                      2        PO/2016/001     2016-09-1
5                      2        PO/2016/002     2016-10-11
6                      2        PO/2016/003     2016-12-31
7                      1        PO/2017/001     2017-01-31
8                      1        PO/2017/002     2017-02-2
9                      1        PO/2017/003     2017-05-20
10                     2        PO/2017/001     2017-06-25
11                     2        PO/2017/002     2017-07-15
12                     2        PO/2017/003     2017-08-22

从这张表中我想像上面的表一样生成和更新PONo。 我想查询生成PONo。

当我在表格中插入新记录时,PONo会自动从此记录中生成。

注意:如果需要添加任何列,请告诉我。

4 个答案:

答案 0 :(得分:2)

你可能正在寻找这个

;WITH CTE_CUST AS
(
SELECT   cust.cid
        ,exe.eid
        ,exe.score
        ,SUM(ISNULL(cust_exe.cnt,0)) AS total_cnt
FROM    customer cust
        CROSS JOIN
        exercise exe
        LEFT JOIN 
        customer_exercise cust_exe
            ON cust_exe.cid = cust.cid 
                AND cust_exe.eid = exe.eid 
GROUP BY cust.cid, exe.eid,exe.score
)
, CTE_RANK AS
(
SELECT  cid, DENSE_RANK() OVER (order by (SUM(ISNULL((total_cnt * cte.score),0))) DESC) rank_score
FROM    CTE_CUST cte
GROUP BY cid
)
SELECT  cust.cid, cust.eid, cust.total_cnt as 'count', rnk.rank_score 
FROM    CTE_RANK AS rnk
        JOIN
        CTE_CUST AS cust
            ON rnk.cid = cust.cid
WHERE   cust.cid IN (100, 200, 300)
            AND cust.eid IN (1, 2)  
ORDER BY rank_score

答案 1 :(得分:0)

试一试......

IF OBJECT_ID('tempdb..#TestData', 'U') IS NOT NULL 
DROP TABLE #TestData;

CREATE TABLE #TestData (
    PurchaseOrderID int NOT NULL PRIMARY KEY,
    CompanyID int NOT NULL,
    PONo CHAR(11) NULL,
    CreatedDate DATE NOT NULL 
    );
INSERT #TestData(PurchaseOrderID, CompanyID, CreatedDate) VALUES
    (1 , 1, '2016-01-31'),
    (2 , 1, '2016-02-22'),
    (3 , 1, '2016-05-25'),
    (4 , 2, '2016-09-01'),
    (5 , 2, '2016-10-11'),
    (6 , 2, '2016-12-31'),
    (7 , 1, '2017-01-31'),
    (8 , 1, '2017-02-02'),
    (9 , 1, '2017-05-20'),
    (10, 2, '2017-06-25'),
    (11, 2, '2017-07-15'),
    (12, 2, '2017-08-22');

-- SELECT * FROM #TestData td;

--=========================================

SELECT 
    *,
    PONo = CONCAT('PO/', YEAR(td.CreatedDate), '/', RIGHT(CONCAT('000', 
        ROW_NUMBER() OVER (PARTITION BY td.CompanyID, YEAR(td.CreatedDate) ORDER BY td.PurchaseOrderID)), 3))
FROM
    #TestData td;

结果...

PurchaseOrderID CompanyID   PONo        CreatedDate PONo
--------------- ----------- ----------- ----------- -------------------
1               1           NULL        2016-01-31  PO/2016/001
2               1           NULL        2016-02-22  PO/2016/002
3               1           NULL        2016-05-25  PO/2016/003
7               1           NULL        2017-01-31  PO/2017/001
8               1           NULL        2017-02-02  PO/2017/002
9               1           NULL        2017-05-20  PO/2017/003
4               2           NULL        2016-09-01  PO/2016/001
5               2           NULL        2016-10-11  PO/2016/002
6               2           NULL        2016-12-31  PO/2016/003
10              2           NULL        2017-06-25  PO/2017/001
11              2           NULL        2017-07-15  PO/2017/002
12              2           NULL        2017-08-22  PO/2017/003

答案 2 :(得分:0)

如果要生成新的序列逻辑。

CREATE TABLE PO_wise_seq
(
    PO      BIGINT,
    year_   INT,
    seq     BIGINT IDENTITY(1,1) 
)

DECLARE @PO_ID BIGINT =332
DECLARE @Year_id BIGINT =2016

INSERT INTO PO_wise_seq VALUES (@PO_ID,@Year_id)

select PO,
cast(@PO_ID as nvarchar) +'/' +cast(@Year_id as nvarchar)+'/'+cast(count(1) as nvarchar) as seq
FROM PO_wise_seq
WHERE PO=@PO_ID
    AND year_=@Year_id
GROUP BY PO,year_

答案 3 :(得分:0)

如果要生成新的序列逻辑。

CREATE TABLE PO_wise_seq
(
    CompanyID       BIGINT,
    year_   INT,
    seq     BIGINT IDENTITY(1,1) 
)

DECLARE @CompanyID BIGINT =332
DECLARE @Year_id BIGINT =2016

INSERT INTO PO_wise_seq VALUES (@CompanyID,@Year_id)

select CompanyID
    ,cast(@CompanyID as nvarchar) +'/' +cast(@Year_id as nvarchar)+'/'+cast(count(1) as nvarchar) as seq
FROM PO_wise_seq
WHERE CompanyID=@CompanyID
    AND year_=@Year_id
GROUP BY CompanyID,year_