Domanda

So I'm in need of some help on a query that will mark duplicates in a specific column.

essentially I have 2 columns like this:

cust_id    Order_id
  001        001
  001        002
  001        003
  002        001
  003        001
  003        002

I want to make a new column in my Q that marks a '1' for unique and '0' for duplicates of only the cust_id column. My thought is to use the min or < function on the order_id column in order to determine which cust_id was entered first to mark as '1'

So far this is what I have come up with which is obviously not right:

SELECT
A.cust_id,
B.order_id,
CASE
WHEN COUNT(A.cust_id) > 1 THEN 0
WHEN COUNT(A.cust_id) = 1 THEN 1
END AS 'TESTING'

FROM CUST A INNER JOIN ORDER B
ON B.cust_id = A.cust_id

GROUP BY B.order_id, A.cust_id

This runs but only marks everything with a '1'. I know that it's because it's doing exactly what I asked it to do, and that it is actually looking at the value of the cust_id and why ALL are > 1 and returning all '1's.

How do I re-write this to mark the first instance of cust_id with a '1' and the rest (or duplicate cust_id's as '0'

the purpose is so I can summarize the '1's in excel and do calculations from there for a pivot chart.

Thanks in advance for the help!!!!! this site has saved my life more than once!

EDIT:

Using MS SQL 2008 R2

È stato utile?

Soluzione

cust_id    Order_id     Unique
  001        001          1
  001        002          0 
  001        003          0
  002        001          1
  003        001          1
  003        002          0

Is the above result set what you are looking for? Below is an example based on the AdventureWorks database. The reason you are getting all 1's above is due to the grouping over cust and order. You want to group over just Cust.

use AdventureWorks2008R2

;with CustSales as

(

select 

    C.CustomerID,

    H.SalesOrderID, 

    ROW_NUMBER() over (partition by C.CustomerID order by SalesOrderID)  as s 

from Sales.Customer C 

join Sales.SalesOrderheader H on 

C.CustomerID = H.CustomerID 

)

select 

    CustomerID,

    SalesOrderID,

    case when s = 1 then 1 else 0 end as [Unique]

from CustSales 

Altri suggerimenti

SELECT t1.*, CASE s.num WHEN 1 THEN 1 ELSE 0 END uniq
  FROM Table1 t1
    JOIN (SELECT cust_id, COUNT(*) num FROM Table1 GROUP BY cust_id) s
      ON s.cust_id = t1.cust_id

SQLFiddle

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top