Tuesday, March 27, 2012
A duplicate value cannot be inserted into a unique index
weeks (but at least it is my own creation). Having said that, I'm not sure
if this is a replication problem or exactly what I have on my hands here.
When I submit an Insert to a particular table, I can submit as many Inserts
as I would like with no exceptions -- until I replicate my changes back to
the server, then download that table again. It would appear that I have
identical data in both the SQLCE table and the SQL2k table, but for a reason
that I have yet to figure out, the downloaded table will no longer accept
Inserts. I get the message: "A duplicate value cannot be inserted into a
unique index. [,,,,,].
I do not have indexes on any of the columns. I do have a primary key. Any
advice would be appreciated.
It means you are trying to insert identical values into your PK.
"Earl" <brikshoe@.newsgroups.nospam> wrote in message
news:uXndNKDNFHA.2020@.TK2MSFTNGP10.phx.gbl...
> I'll first give the caveat that I've been away from this project for many
> weeks (but at least it is my own creation). Having said that, I'm not sure
> if this is a replication problem or exactly what I have on my hands here.
> When I submit an Insert to a particular table, I can submit as many
> Inserts as I would like with no exceptions -- until I replicate my changes
> back to the server, then download that table again. It would appear that I
> have identical data in both the SQLCE table and the SQL2k table, but for a
> reason that I have yet to figure out, the downloaded table will no longer
> accept Inserts. I get the message: "A duplicate value cannot be inserted
> into a unique index. [,,,,,].
> I do not have indexes on any of the columns. I do have a primary key. Any
> advice would be appreciated.
>
|||Nope. Identical values into unique indexed columns. I just dropped the
indexes for now.
"ChrisR" <noemail@.bla.com> wrote in message
news:uFug$fHNFHA.576@.TK2MSFTNGP15.phx.gbl...
> It means you are trying to insert identical values into your PK.
> "Earl" <brikshoe@.newsgroups.nospam> wrote in message
> news:uXndNKDNFHA.2020@.TK2MSFTNGP10.phx.gbl...
>
Monday, March 19, 2012
A basic question: Removing duplicate results from Max function
Say I have a table Job with columns name, date, salary . I want to get
the name ,date and salary for the date when that person earned maximum
salary. I am using something like
SELECT X.name,X.date,X.salary
FROM job X
WHERE X.salary IN
(SELECT MAX(Y.salary) FROM job Y where Y.name= X.name);
The problem is ; if a person earns maximum salary on two dates, both of
the dates are printed. I just want to get any one of those two rows.
I tried
SELECT X.name,Min(X.date),X.salary
FROM job X
WHERE X.salary IN
(SELECT MAX(Y.salary) FROM job Y where Y.name= X.name);
but it gives error.
Can anybody please suggest a solution?
Regards,
Aamir(aamircheema@.gmail.com) writes:
> Say I have a table Job with columns name, date, salary . I want to get
> the name ,date and salary for the date when that person earned maximum
> salary. I am using something like
>
> SELECT X.name,X.date,X.salary
> FROM job X
> WHERE X.salary IN
> (SELECT MAX(Y.salary) FROM job Y where Y.name= X.name);
>
> The problem is ; if a person earns maximum salary on two dates, both of
> the dates are printed. I just want to get any one of those two rows.
> I tried
> SELECT X.name,Min(X.date),X.salary
> FROM job X
> WHERE X.salary IN
> (SELECT MAX(Y.salary) FROM job Y where Y.name= X.name);
> but it gives error.
SELECT a.name, a.date, a.salary
FROM tbl a
JOIN (SELECT b.name, date = MAX(b.date)
FROM tbl b
JOIN (SELECT name, salary = MAX(salary)
FROM tbl
GROUP BY name) c ON c.name = b.name
AND c.salary = b.salary
GROUP BY a1.name) b ON a.name = b.name
AND a.date = b.date
This presumes that (name, date) is unique, and a person does not have
two salaries the same day.
The inner selects are derived tables - sort of virtual temp tables within
the query. A very powerful tool to write complex queries. A derived table
is independent of the outer query, and this why the alias b can be reused.
Note that they are not necessarily computed in whole - the optimizer often
recast computation order for a very very effceient query plan.
--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||aamircheema@.gmail.com wrote:
> Hi,
> Say I have a table Job with columns name, date, salary . I want to get
> the name ,date and salary for the date when that person earned maximum
> salary. I am using something like
>
> SELECT X.name,X.date,X.salary
> FROM job X
> WHERE X.salary IN
> (SELECT MAX(Y.salary) FROM job Y where Y.name= X.name);
>
> The problem is ; if a person earns maximum salary on two dates, both of
> the dates are printed. I just want to get any one of those two rows.
> I tried
> SELECT X.name,Min(X.date),X.salary
> FROM job X
> WHERE X.salary IN
> (SELECT MAX(Y.salary) FROM job Y where Y.name= X.name);
> but it gives error.
> Can anybody please suggest a solution?
> Regards,
> Aamir
In SQL Server 2005:
WITH j AS
(SELECT name, date, salary,
RANK() OVER (PARTITION BY name ORDER BY salary DESC, date DESC) rnk
FROM job)
SELECT name, date, salary
FROM j
WHERE rnk = 1 ;
That assumes the combination of (name,salary,date) is unique. If it
isn't then just add other columns to the ORDER BY specification to make
a key.
--
David Portas, SQL Server MVP
Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.
SQL Server Books Online:
http://msdn2.microsoft.com/library/...US,SQL.90).aspx
--|||On 10 Jun 2006 07:48:38 -0700, aamircheema@.gmail.com wrote:
(snip)
>I tried
>SELECT X.name,Min(X.date),X.salary
>FROM job X
>WHERE X.salary IN
> (SELECT MAX(Y.salary) FROM job Y where Y.name= X.name);
>but it gives error.
>Can anybody please suggest a solution?
Hi Aamir,
Here's a third suggestion:
SELECT X.name,Min(X.date),X.salary
FROM job X
WHERE X.salary IN
(SELECT MAX(Y.salary) FROM job Y where Y.name= X.name)
GROUP BY X.name, X.salary;
(Untested - see www.aspfaq.com/5006 if you prefer a tested reply)
--
Hugo Kornelis, SQL Server MVP|||
Thanks Everybody. That helped a lot