Author |
Topic |
tracy5436
Yak Posting Veteran
50 Posts |
Posted - 2009-05-19 : 18:18:18
|
Hi,I have the following query which extracts the contents of a table rm00300 into a temporary table lot which groups based on itemnmbr,lotnumbr, locncode, qtytyp and dtseqnum. The problem is that I want to pick up the lowest dtseqnum (date sequence number), but the query is not doing this.DECLARE @intErrorCode VARCHAR(10);SELECT itemnmbr as itemnmbr1, lotnumbr as lotnmbr1, min(daterecd) as receiptdate1,sum(qtyrecvd) as qtyrecvd1,IDENTITY(smallint, 1, 1) as dex_row_id1, locncode as locncode1,sum(qtysold) as qtysold1,qtytype as qtytype1,dtseqnum as dtseqnum1into lotFROM iv00300group by itemnmbr,lotnumbr, locncode, qtytype,dtseqnumSELECT @intErrorCode = @@ERROR IF (@intErrorCode = 0) delete iv00300SET IDENTITY_INSERT IV00300 ON insert into iv00300 (itemnmbr, lotnumbr, daterecd,qtyrecvd,dex_row_id,locncode,qtysold,qtytype,dtseqnum) select itemnmbr1, lotnmbr1, receiptdate1,qtyrecvd1,dex_row_id1,locncode1,qtysold1,qtytype1,dtseqnum1from lotSELECT @intErrorCode = @@ERROR IF (@intErrorCode = 0) drop table lotSET IDENTITY_INSERT IV00300 OFFCan anyone help ? |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-05-19 : 20:03:32
|
[code]SELECT itemnmbr as itemnmbr1, lotnumbr as lotnmbr1, min(daterecd) as receiptdate1, sum(qtyrecvd) as qtyrecvd1, IDENTITY(smallint, 1, 1) as dex_row_id1, locncode as locncode1, sum(qtysold) as qtysold1, qtytype as qtytype1, min(dtseqnum) as dtseqnum1into lotFROM iv00300group by itemnmbr,lotnumbr, locncode, qtytype ,dtseqnum[/code] KH[spoiler]Time is always against us[/spoiler] |
|
|
tracy5436
Yak Posting Veteran
50 Posts |
Posted - 2009-06-09 : 19:00:31
|
Hi,I have modified the query and used your suggestion. Howeever, when I run the query, it gives me a Violation of PRIMARY KEY constraint 'PK_IV00300'. Cannot insert duplicate key in object 'IV00300'.The statement has been terminated. error message. The primary keys on the table are itemnmbr, locncode, daterecd, dtseqnum and qtytype. The dtseqnum seems to be problem - it is not grouping by it. What am I doing wrong ?Thanks |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
tracy5436
Yak Posting Veteran
50 Posts |
Posted - 2009-06-09 : 19:32:47
|
This is my query :DECLARE @intErrorCode VARCHAR(10);drop table iv00300tempSELECT ITEMNMBR AS ITEMNMBR,LOCNCODE AS LOCNCODE,DATERECD AS DATERECD,DTSEQNUM AS DTSEQNUM,LOTNUMBR AS LOTNUMBR,QTYRECVD AS QTYRECVD,QTYSOLD AS QTYSOLD,ATYALLOC AS ATYALLOC,UNITCOST AS UNITCOST,RCTSEQNM AS RCTSEQNM,vndrnmbr as vndrnmbr,ltnumsld as ltnumsld,qtytype as qtytype,bin as bin,mfgdate as mfgdate,expndate as expndate,dex_row_id as dex_row_idinto iv00300tempfrom iv00300SELECT itemnmbr as itemnmbr1, lotnumbr as lotnmbr1, min(daterecd) as receiptdate1,sum(qtyrecvd) as qtyrecvd1,IDENTITY(smallint, 1, 1) as dex_row_id1, locncode as locncode1,sum(qtysold) as qtysold1,sum(atyalloc) as atyalloc1,qtytype as qtytype1,min(unitcost) as unitcost1,min(rctseqnm) as rctseqnm1,min(dtseqnum) as dtseqnum1into lotFROM iv00300group by itemnmbr,lotnumbr, locncode, qtytypeSELECT @intErrorCode = @@ERROR IF (@intErrorCode = 0) delete iv00300SET IDENTITY_INSERT IV00300 ON insert into iv00300 (itemnmbr, lotnumbr, daterecd,qtyrecvd,dex_row_id,locncode,qtysold,qtytype,dtseqnum,unitcost,rctseqnm,atyalloc) select itemnmbr1, lotnmbr1, receiptdate1,qtyrecvd1,dex_row_id1,locncode1,qtysold1,qtytype1,dtseqnum1,unitcost1,rctseqnm1,atyalloc1from lotSELECT @intErrorCode = @@ERROR IF (@intErrorCode = 0) drop table lotSET IDENTITY_INSERT IV00300 OFFI get the violation of primary key error when I run this query sometimes. It is making a copy of the iv00300 table into a iv00300temp table, and then grouping by itemnmbr,lotnumbr, locncode and qtytype into a table called lot. It then deletes the iv00300 table and then inserts from the lot table into the iv00300 table. |
|
|
|
|
|