Re: Help with an Update Statment
- From: --CELKO-- <jcelko212@xxxxxxxxxxxxx>
- Date: Tue, 27 May 2008 14:37:03 -0700 (PDT)
Your table has no key; can I guess you meant to post this?
CREATE TABLE Foobar
(cust_id INTEGER NOT NULL,
officers_id VARCHAR(10) NOT NULL,
PRIMARY KEY (cust_id, officers_id)
officer_rank INTEGER NOT NULL);
The next question is why do you want to build a table that has to be
constantly updated when you can use a VIEW that is always right?
CREATE VIEW TopTwoOfficers (cust_id, officer_1, officer_2
AS
SELECT cust_id,
MAX (CASE WHEN ranking = 1 THEN officers_id ELSE NULL END) AS
officer_1,
MAX (CASE WHEN ranking = 2 THEN officers_id ELSE NULL END) AS
officer_2
FROM (SELECT cust_id, officers_id,
ROW_NUMBER() OVER(PARTITION BY cust_id
ORDER BY officer_rank,
officers_id)
FROM Foobar) AS Top(cust_id, officers_id, ranking)
WHERE ranking IN (1, 2)
GROUP BY cust_id;
There is no need to use the proprietary PIVOT extension or multiple
CTEs, either.
.
- Follow-Ups:
- Re: Help with an Update Statment
- From: Tony Rogerson
- Re: Help with an Update Statment
- Prev by Date: Re: Strange connection problem
- Next by Date: Re: Strange connection problem
- Previous by thread: Re: Help with an Update Statment
- Next by thread: Re: Help with an Update Statment
- Index(es):
Relevant Pages
|