SQL Server:根据其他两列的条件更新布尔列
当我的SQL Server:根据其他两列的条件更新布尔列,sql,sql-server,tsql,Sql,Sql Server,Tsql,当我的InvoiceFileId只有一个InvoiceId时,我需要将列PrimaryInvoiceFile中的所有行设置为1(true) 但是,如果InvoiceId有多个InvoiceFileId,我需要将所有PrimaryInvoiceFile行设置为0(false),除了根据添加日期添加的最新InvoiceFileId之外 例如,它应该如下所示: |CreatedDate|InvoiceId|InvoiceFileId|PrimaryInvoiceFile| +-----------+-
InvoiceFileId
只有一个InvoiceId时,我需要将列PrimaryInvoiceFile
中的所有行设置为1(true)
但是,如果InvoiceId
有多个InvoiceFileId
,我需要将所有PrimaryInvoiceFile
行设置为0(false),除了根据添加日期添加的最新InvoiceFileId
之外
例如,它应该如下所示:
|CreatedDate|InvoiceId|InvoiceFileId|PrimaryInvoiceFile|
+-----------+---------+-------------+------------------+
|2019-01-16 | 1 | 1 | 1 |
|2019-01-17 | 2 | 2 | 1 |
|2019-01-18 | 3 | 3 | 0 |
|2019-01-19 | 3 | 4 | 0 |
|2019-01-20 | 3 | 5 | 1 |
|2019-01-21 | 4 | 6 | 1 |
我刚刚添加了PrimaryInvoiceFile
列迁移,并将默认值设置为0
在此方面的任何帮助都将不胜感激!我一直在绞尽脑汁试图让我的更新语句执行此更新。请尝试以下操作:
;WITH Data AS (
SELECT t.CreatedDate,t.InvoiceId,t.InvoiceFieldId,t.PrimaryInvoiceFile
,COUNT(*)OVER(PARTITION BY t.InvoiceId) AS [cnt]
FROM [YourTableName] t
)
UPDATE d SET d.PrimaryInvoiceFile = CASE WHEN d.cnt = 1 THEN 1 ELSE 0 END
FROM Data d
;
要玩的查询:
DROP TABLE IF EXISTS #YourTableName;
CREATE TABLE #YourTableName(CreatedDate DATETIME2,InvoiceId INT, InvoiceFieldId INT,PrimaryInvoiceFile BIT);
INSERT INTO #YourTableName(CreatedDate,InvoiceId,InvoiceFieldId)VALUES
('2019-01-16',1,1),('2019-01-17',2,2),('2019-01-18',3,3),('2019-01-19',3,4),('2019-01-20',3,5),('2019-01-21',4,6)
;WITH Data AS (
SELECT t.CreatedDate,t.InvoiceId,t.InvoiceFieldId,t.PrimaryInvoiceFile,COUNT(*)OVER(PARTITION BY t.InvoiceId) AS [cnt]
FROM #YourTableName t
)
UPDATE d SET d.PrimaryInvoiceFile = CASE WHEN d.cnt = 1 THEN 1 ELSE 0 END
FROM Data d
;
SELECT t.CreatedDate,t.InvoiceId,t.InvoiceFieldId,t.PrimaryInvoiceFile
FROM #YourTableName t
;
DROP TABLE IF EXISTS #YourTableName;
您可以在执行更新时使用rownumber来实现所需的结果。此外,按降序排序,以便获得最近的日期
让我们创建一个表,将PrimaryInvoiceFile保持为null,然后稍后更新
select '2019-01-16' as CreatedDate, 1 as invoiceID, 1 as Invoicefield, null
as PrimaryInvoiceFile
into #temp
union all
select '2019-01-17' as CreatedDate, 2 as invoiceID, 2 as Invoicefield, null
as Primaryinvoicefile union all
select '2019-01-18' as CreatedDate, 3 as invoiceID, 3 as Invoicefield, null
as Primaryinvoicefile union all
select '2019-01-19' as CreatedDate, 3 as invoiceID, 4 as Invoicefield, null
as Primaryinvoicefile union all
select '2019-01-20' as CreatedDate, 3 as invoiceID, 5 as Invoicefield, null
as Primaryinvoicefile union all
select '2019-01-21' as CreatedDate, 4 as invoiceID, 6 as Invoicefield, null
as Primaryinvoicefile
update t
set Primaryinvoicefile = tst.Rownum
from #temp t
join
(Select invoiceID, Invoicefield,CreatedDate,
case when ROW_NUMBER() over (partition by invoiceID order by createddate desc) = 1
then 1 else 0 end as Rownum from #temp) tst
on tst.CreatedDate = t.CreatedDate
and tst.invoiceID = t.invoiceID
and tst.Invoicefield = t.Invoicefield
Case statement would make sure that you are value as 1 for only the rows where you have 1 row for invoice ID or for the most recent data.
select * from #temp
输出:
CreatedDate invoiceID Invoicefield PrimaryInvoiceFile
2019-01-16 1 1 1
2019-01-17 2 2 1
2019-01-18 3 3 0
2019-01-19 3 4 0
2019-01-20 3 5 1
2019-01-21 4 6 1
如果默认PrimaryInvoiceFile设置为0,则需要更新按InvoiceId分组的最大CreatedDate的PrimaryInvoiceFile
UPDATE inv1
SET inv1.PrimaryInvoiceFile = 1
FROM invoices inv1 JOIN
(SELECT max(CreatedDate) as maxDate, InvoiceId
FROM invoices
GROUP BY InvoiceId ) as inv2
WHERE inv1.CreatedDate=inv2.maxDate and inv1.InvoiceId= inv2.InvoiceId