如何在 SQL 服务器的 table / 列中标记重复值?
How to tag repeating Values in a table / column in SQL Server?
我有一个 table,它是 3 个 table 的连接,显示以下信息。
|| OrderID || Order Date || Customer || Customer City || Product || Amount ||
如果客户在同一日期多次订购产品,我想获得另一列作为 || Dual Purchase ||
?
该值应为 YES
或 NO
因此,如果约翰在 6 月 16 日以 2 个单独的订单购买可乐,那么它应该 YES
针对两个订单 ID
使用window函数ROW_NUMBER()
定义OrderID
和Customer
的双记录。检查,如果行大于 1:
DECLARE @Table TABLE (OrderID int, Customer int, OrderDate date)
INSERT INTO @Table
VALUES
(1,2, Getdate()),
(1,2, DATEADD(day,1,Getdate())),
(1,2, DATEADD(day,1,Getdate())),
(2,3, Getdate()),
(2,3, DATEADD(day,1,Getdate()))
SELECT
*,
IIF(ROW_NUMBER() OVER (PARTITION BY t.OrderID, t.Customer ORDER BY t.OrderID) > 1, 'YES', 'NO') AS DualPurchase
FROM @Table AS t
你可以这样做:
SELECT OrderID
, OrderDate
, Customer
, CustomerCity
, Product
, Amount
, Case when Count(Product) Over(Partition by OrderDate, Customer, Product) > 1
then 'Yes' else 'No' end as DualPurchase
FROM TableName
我想这对你有帮助..
Select
O.OrderID,O.[Order Date],c.Customer,c.[Customer City],P.Product,O.Amount,a.[More then Once]
from Orders O
inner join Customers C
on O.Customer_Id=C.Customer_Id
inner join Product P
On O.Product_Id=P.Product_Id
Cross Apply ( select Case when count(1)>1 then 'Yes' Else 'No' [More then Once] end from Orders
where Customer_Id=O.Customer_Id and cast([Order Date] as date)=cast(O.[Order Date] as date)
group by Customer_Id,cast([Order Date] as date)
) a
我有一个 table,它是 3 个 table 的连接,显示以下信息。
|| OrderID || Order Date || Customer || Customer City || Product || Amount ||
如果客户在同一日期多次订购产品,我想获得另一列作为 || Dual Purchase ||
?
该值应为 YES
或 NO
因此,如果约翰在 6 月 16 日以 2 个单独的订单购买可乐,那么它应该 YES
针对两个订单 ID
使用window函数ROW_NUMBER()
定义OrderID
和Customer
的双记录。检查,如果行大于 1:
DECLARE @Table TABLE (OrderID int, Customer int, OrderDate date)
INSERT INTO @Table
VALUES
(1,2, Getdate()),
(1,2, DATEADD(day,1,Getdate())),
(1,2, DATEADD(day,1,Getdate())),
(2,3, Getdate()),
(2,3, DATEADD(day,1,Getdate()))
SELECT
*,
IIF(ROW_NUMBER() OVER (PARTITION BY t.OrderID, t.Customer ORDER BY t.OrderID) > 1, 'YES', 'NO') AS DualPurchase
FROM @Table AS t
你可以这样做:
SELECT OrderID
, OrderDate
, Customer
, CustomerCity
, Product
, Amount
, Case when Count(Product) Over(Partition by OrderDate, Customer, Product) > 1
then 'Yes' else 'No' end as DualPurchase
FROM TableName
我想这对你有帮助..
Select
O.OrderID,O.[Order Date],c.Customer,c.[Customer City],P.Product,O.Amount,a.[More then Once]
from Orders O
inner join Customers C
on O.Customer_Id=C.Customer_Id
inner join Product P
On O.Product_Id=P.Product_Id
Cross Apply ( select Case when count(1)>1 then 'Yes' Else 'No' [More then Once] end from Orders
where Customer_Id=O.Customer_Id and cast([Order Date] as date)=cast(O.[Order Date] as date)
group by Customer_Id,cast([Order Date] as date)
) a