Home All Groups Group Topic Archive Search About

What am I missing with this simple query?

Author
13 Sep 2006 6:35 PM
MarkT
I am trying to add the results of this query to a new table using the
procedure below:

INSERT into db.TempCheckHistGN (EmpID,GrossPay,NetPay)
SELECT EmployeeID, SUM(GrossPay), as Expr1, Sum(NetPay) as Expr2 FROM
dbo.tblPACheckHist
WHERE (CheckDate Between '11/1/2005' AND '12/31/2010') AND (EmployeeID >=''
AND EmployeeID <='zzzzzz')
GROUP BY EmployeeID

I get a duplicate key error (EmpID is the key) but I shouldn't since the
records are being summed by EmployeeID

Thanks

Author
13 Sep 2006 6:41 PM
Aaron Bertrand [SQL Server MVP]
Is the key on EmpID only?  Are you sure there aren't already rows in the
"new" table?

A


Show quote
"MarkT" <Ma***@discussions.microsoft.com> wrote in message
news:C2393D51-91CD-4709-9543-8B7DC12380BC@microsoft.com...
>I am trying to add the results of this query to a new table using the
> procedure below:
>
> INSERT into db.TempCheckHistGN (EmpID,GrossPay,NetPay)
> SELECT EmployeeID, SUM(GrossPay), as Expr1, Sum(NetPay) as Expr2 FROM
> dbo.tblPACheckHist
> WHERE (CheckDate Between '11/1/2005' AND '12/31/2010') AND (EmployeeID
>  >=''
> AND EmployeeID <='zzzzzz')
> GROUP BY EmployeeID
>
> I get a duplicate key error (EmpID is the key) but I shouldn't since the
> records are being summed by EmployeeID
>
> Thanks
Author
13 Sep 2006 6:42 PM
SQL Menace
Maybe there is already data in the TempCheckHistGN  table??

Denis the SQL Menace
http://sqlservercode.blogspot.com/


MarkT wrote:
Show quote
> I am trying to add the results of this query to a new table using the
> procedure below:
>
> INSERT into db.TempCheckHistGN (EmpID,GrossPay,NetPay)
> SELECT EmployeeID, SUM(GrossPay), as Expr1, Sum(NetPay) as Expr2 FROM
> dbo.tblPACheckHist
> WHERE (CheckDate Between '11/1/2005' AND '12/31/2010') AND (EmployeeID >=''
> AND EmployeeID <='zzzzzz')
> GROUP BY EmployeeID
>
> I get a duplicate key error (EmpID is the key) but I shouldn't since the
> records are being summed by EmployeeID
>
> Thanks
Author
13 Sep 2006 6:44 PM
Tracy McKibben
MarkT wrote:
Show quote
> I am trying to add the results of this query to a new table using the
> procedure below:
>
> INSERT into db.TempCheckHistGN (EmpID,GrossPay,NetPay)
> SELECT EmployeeID, SUM(GrossPay), as Expr1, Sum(NetPay) as Expr2 FROM
> dbo.tblPACheckHist
> WHERE (CheckDate Between '11/1/2005' AND '12/31/2010') AND (EmployeeID >=''
> AND EmployeeID <='zzzzzz')
> GROUP BY EmployeeID
>
> I get a duplicate key error (EmpID is the key) but I shouldn't since the
> records are being summed by EmployeeID
>
> Thanks

Is the table empty?  Have you confirmed that there are no unique indexes
or constraints on the table?


--
Tracy McKibben
MCDBA
http://www.realsqlguy.com
Author
13 Sep 2006 6:56 PM
Warren Brunk
First off, is this even correct syntax?

> INSERT into db.TempCheckHistGN (EmpID,GrossPay,NetPay)
> SELECT EmployeeID, SUM(GrossPay), as Expr1, Sum(NetPay) as Expr2 FROM
> dbo.tblPACheckHist
> WHERE (CheckDate Between '11/1/2005' AND '12/31/2010') AND (EmployeeID
>  >=''
> AND EmployeeID <='zzzzzz')
> GROUP BY EmployeeID

I think you should remove the comma from SUM(GrossPay), as Expr1 to result
in this...

> INSERT into db.TempCheckHistGN (EmpID,GrossPay,NetPay)
> SELECT EmployeeID, SUM(GrossPay) as Expr1, Sum(NetPay) as Expr2 FROM
> dbo.tblPACheckHist
> WHERE (CheckDate Between '11/1/2005' AND '12/31/2010') AND (EmployeeID
>  >=''
> AND EmployeeID <='zzzzzz')
> GROUP BY EmployeeID

If there is a primary key on the EmployeeID on the TempCheckHistGN table you
will need to make sure that there are not duplicate employeeID's in your
select statement.



--
/*
Warren Brunk - MCITP - SQL 2005, MCDBA
www.techintsolutions.com
*/


Show quote
"MarkT" <Ma***@discussions.microsoft.com> wrote in message
news:C2393D51-91CD-4709-9543-8B7DC12380BC@microsoft.com...
>I am trying to add the results of this query to a new table using the
> procedure below:
>
> INSERT into db.TempCheckHistGN (EmpID,GrossPay,NetPay)
> SELECT EmployeeID, SUM(GrossPay), as Expr1, Sum(NetPay) as Expr2 FROM
> dbo.tblPACheckHist
> WHERE (CheckDate Between '11/1/2005' AND '12/31/2010') AND (EmployeeID
>  >=''
> AND EmployeeID <='zzzzzz')
> GROUP BY EmployeeID
>
> I get a duplicate key error (EmpID is the key) but I shouldn't since the
> records are being summed by EmployeeID
>
> Thanks
Author
13 Sep 2006 6:57 PM
Arnie Rowland
In addition to the above comments, I noticed that you have one too many
commas in the SELECT statement.
"SUM(GrossPay), as Expr1," should not have the comma between the ending
parenthesis and 'as' -actually, it doesn't need the entire "as Expr1," part
of the statement.

--
Arnie Rowland, Ph.D.
Westwood Consulting, Inc

Most good judgment comes from experience.
Most experience comes from bad judgment.
- Anonymous


Show quote
"MarkT" <Ma***@discussions.microsoft.com> wrote in message
news:C2393D51-91CD-4709-9543-8B7DC12380BC@microsoft.com...
>I am trying to add the results of this query to a new table using the
> procedure below:
>
> INSERT into db.TempCheckHistGN (EmpID,GrossPay,NetPay)
> SELECT EmployeeID, SUM(GrossPay), as Expr1, Sum(NetPay) as Expr2 FROM
> dbo.tblPACheckHist
> WHERE (CheckDate Between '11/1/2005' AND '12/31/2010') AND (EmployeeID
>  >=''
> AND EmployeeID <='zzzzzz')
> GROUP BY EmployeeID
>
> I get a duplicate key error (EmpID is the key) but I shouldn't since the
> records are being summed by EmployeeID
>
> Thanks
Author
13 Sep 2006 7:17 PM
Tracy McKibben
Arnie Rowland wrote:
> In addition to the above comments, I noticed that you have one too many
> commas in the SELECT statement.
> "SUM(GrossPay), as Expr1," should not have the comma between the ending
> parenthesis and 'as' -actually, it doesn't need the entire "as Expr1," part
> of the statement.
>

Good eyes...


--
Tracy McKibben
MCDBA
http://www.realsqlguy.com
Author
13 Sep 2006 7:30 PM
MarkT
Sorry about the syntax I didn't cut and paste - I retyped it.

Show quote
"Arnie Rowland" wrote:

> In addition to the above comments, I noticed that you have one too many
> commas in the SELECT statement.
> "SUM(GrossPay), as Expr1," should not have the comma between the ending
> parenthesis and 'as' -actually, it doesn't need the entire "as Expr1," part
> of the statement.
>
> --
> Arnie Rowland, Ph.D.
> Westwood Consulting, Inc
>
> Most good judgment comes from experience.
> Most experience comes from bad judgment.
> - Anonymous
>
>
> "MarkT" <Ma***@discussions.microsoft.com> wrote in message
> news:C2393D51-91CD-4709-9543-8B7DC12380BC@microsoft.com...
> >I am trying to add the results of this query to a new table using the
> > procedure below:
> >
> > INSERT into db.TempCheckHistGN (EmpID,GrossPay,NetPay)
> > SELECT EmployeeID, SUM(GrossPay), as Expr1, Sum(NetPay) as Expr2 FROM
> > dbo.tblPACheckHist
> > WHERE (CheckDate Between '11/1/2005' AND '12/31/2010') AND (EmployeeID
> >  >=''
> > AND EmployeeID <='zzzzzz')
> > GROUP BY EmployeeID
> >
> > I get a duplicate key error (EmpID is the key) but I shouldn't since the
> > records are being summed by EmployeeID
> >
> > Thanks
>
>
>
Author
13 Sep 2006 7:47 PM
Aaron Bertrand [SQL Server MVP]
> Sorry about the syntax I didn't cut and paste - I retyped it.

WHY?  Don't ever do that.
Author
13 Sep 2006 7:08 PM
Carl
If this is the EXACT syntax of your SQL statement, the following snip is incorrect:
SUM(GrossPay), as Expr1
remove the comma after SUM(GrossPay)

MarkT wrote:
Show quote
> I am trying to add the results of this query to a new table using the
> procedure below:
>
> INSERT into db.TempCheckHistGN (EmpID,GrossPay,NetPay)
> SELECT EmployeeID, SUM(GrossPay), as Expr1, Sum(NetPay) as Expr2 FROM
> dbo.tblPACheckHist
> WHERE (CheckDate Between '11/1/2005' AND '12/31/2010') AND (EmployeeID >=''
> AND EmployeeID <='zzzzzz')
> GROUP BY EmployeeID
>
> I get a duplicate key error (EmpID is the key) but I shouldn't since the
> records are being summed by EmployeeID
>
> Thanks
Author
13 Sep 2006 7:08 PM
--CELKO--
Are the two data elements emp_id and employee_id actually the same data
element?  If so, then use the same name for them.  If not, check that
they converted properly on insertion. Let's clean up the code a bit
- ISO-8601 dates, consistent use of BETWEEN and removal of silly
prefixes, etc:

INSERT INTO db.TempCheckHistgn (emp_id, gross_pay, net_pay)
SELECT emp_id, SUM(gross_pay), SUM(net_pay)
  FROM dbo.PayCheckHistory
WHERE check_date BETWEEN '2005-11-01' AND '2010-31-2010'
   AND emp_id BETWEEN '' AND 'ZZZZZZ' -- is this redundant?
GROUP BY emp_id;

>> I get a duplicate key error (emp_id is the key) but I shouldn't since the records [sic] are being summed by emp_id.  <<

Making a guess at the DDL that you did not post, I would assume that
the INSERT INTO has been run twice.  I would also guess that emp_id
BETWEEN '' AND 'ZZZZZZ' covers all the personnel identifiers, but
there might ones with numerics and perhaps the all-blank one is
special. It looks weird, tho.   That predicate looks more like a
CHECK() than a search condition.

You talked about "records"; you materialize and store computed
data; this is a symptom of still having a file system mindset.  This
problem used to happen in file systems when the same transaction tape
was hung by the next shift.

In the relational world, we avoid temp tables of all kinds by using
virtual tables, such as a VIEW, which is guaranteed to be up-to-date
when it is invoked:

CREATE VIEW PaycheckHistorySummary (emp_id, gross_pay_tot, net_pay_tot)

AS
SELECT emp_id, SUM(gross_pay), SUM(net_pay)
  FROM dbo.PaycheckHistory
WHERE check_date BETWEEN '2005-11-01' AND '2010-31-2010'
   AND emp_id BETWEEN '' AND 'ZZZZZZ' -- is this redundant?
GROUP BY emp_id;
Author
13 Sep 2006 7:17 PM
MarkT
Thanks everyone - there was a record already in the table.  I knew it had to
be something obvious.
Author
14 Sep 2006 3:43 PM
Jim Underwood
They usually are.  Sometimes it just takes a second set of eyes to see what
is in front of our faces.

Show quote
"MarkT" <Ma***@discussions.microsoft.com> wrote in message
news:23F6447A-2ED1-4ECD-BC49-34818F55DEA7@microsoft.com...
> Thanks everyone - there was a record already in the table.  I knew it had
to
> be something obvious.

AddThis Social Bookmark Button