| Author |
Topic |
|
dnf999
Constraint Violating Yak Guru
253 Posts |
Posted - 2007-01-03 : 09:52:11
|
| Hi I need to remove same value positive and negative dupes (where they equate to 0)I.eSupplier_no, Invoice_Date, Invoice_Amount123, 10/10/05, -10123, 10/10/05, 10123, 10/10/05, 200123, 10/10/05, 200So, the match on Supplier_no, Invoice_date and Invoice_Amount should be removed if the Invoice_Amount cancels each other out. Therefore the table should only display the below:Supplier_no, Invoice_Date, Invoice_Amount123, 10/10/05, 200123, 10/10/05, 200As these valus are both positive they can stay. If the values are negative and positive and result in 0 then they need to be removed.Thanks in advance!! |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2007-01-03 : 11:36:30
|
| Are you saying you want to physically delete those rows from the table, or that when you are returning results in a SELECT you want to suppress certain rows?Either way, your table needs a primary key to do this, otherwise there is no way to tell what data is valid and what should be deleted and what shouldn't. Does it have a PK? Or, do you just want to return total amounts per day in a SELECT:select supplier_no, Invoice_date, sum(Invoice_Amount) as TotalAmountfrom YourTablegroup by supplier_no, Invoice_Date???- Jeff |
 |
|
|
dnf999
Constraint Violating Yak Guru
253 Posts |
Posted - 2007-01-03 : 11:44:09
|
| Physically delete these rows from a table. |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2007-01-03 : 12:02:11
|
| OK, then, what is the tables primary key?and if your data is like this:123, 10/10/05, -10123, 10/10/05, 10123, 10/10/05, -10Which ones get deleted?Or if it is like this:123, 10/10/05, -10123, 10/10/05, 200123, 10/10/05, 10Do the -10 and 10 still get deleted?And finally, if it is like this:123, 10/10/05, -10123, 10/10/05, 5123, 10/10/05, 5Do we do nothing, or do those 3 rows get deleted (since they add up to zero) ?- Jeff |
 |
|
|
dnf999
Constraint Violating Yak Guru
253 Posts |
Posted - 2007-01-03 : 12:33:09
|
| OK, then, what is the tables primary key?--Currently the table has no primary key (although I could create a numeric identity Primary Keyand if your data is like this:123, 10/10/05, -10123, 10/10/05, 10123, 10/10/05, -10Which ones get deleted?-- one -10 and one 10, leaving one -10Or if it is like this:123, 10/10/05, -10123, 10/10/05, 200123, 10/10/05, 10Do the -10 and 10 still get deleted? --YesAnd finally, if it is like this:123, 10/10/05, -10123, 10/10/05, 5123, 10/10/05, 5Do we do nothing, or do those 3 rows get deleted (since they add up to zero) ?--We can highlight these, but not delete them.Thanks! |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-01-03 : 13:47:41
|
Take a look at this-- prepare sample datadeclare @t table (rowid int identity(1, 1), supplier int, invoice datetime, amount int)insert @tselect 123, '10/10/05', -10 union allselect 123, '10/10/05', 10 union allselect 123, '10/10/05', -10 union allselect 123, '10/10/05', -10 union allselect 123, '10/10/05', 200 union allselect 123, '10/10/05', 0 union allselect 123, '10/10/05', 10 union allselect 123, '10/10/05', -10 union allselect 123, '10/10/05', 5 union allselect 123, '10/10/05', 5select * from @t-- remove pairsdeletefrom @twhere amount = 0declare @currentrowid int, @supplier int, @amount int, @invoice datetime, @rowid intselect @currentrowid = 1while @currentrowid <= (select max(rowid) from @t) begin select @supplier = supplier, @invoice = invoice, @amount = amount from @t where rowid = @currentrowid select @rowid = min(rowid) from @t where supplier = @supplier and invoice = @invoice and amount = -@amount if @rowid is not null delete from @t where rowid in (@rowid, @currentrowid) select @currentrowid = min(rowid), @rowid = null from @t where rowid > @currentrowid end-- show the resultselect supplier, invoice, amountfrom @torder by supplier, invoice, amount Peter LarssonHelsingborg, Sweden |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2007-01-03 : 14:12:10
|
| Peso -- without a PK, this doesn't help much since there is no way to delete the rows from the table short of using ADO or something and manually stepping through the rows.- Jeff |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-01-03 : 14:44:06
|
I am sorry to disappoint you, Jeff.There is a way to do this without Primary Key.-- prepare sample datadeclare @t table (supplier int, invoice datetime, amount int)insert @tselect 123, '10/10/05', -10 union allselect 123, '10/10/05', 10 union allselect 123, '10/10/05', -10 union allselect 123, '10/10/05', -10 union allselect 123, '10/10/05', 200 union allselect 123, '10/10/05', 0 union allselect 123, '10/10/05', 10 union allselect 123, '10/10/05', -10 union allselect 123, '10/10/05', 5 union allselect 123, '10/10/05', 5select supplier, invoice, amountfrom @torder by supplier, invoice, amount-- remove pairsdeletefrom @twhere amount = 0declare @supplier int, @amount int, @invoice datetime, @nextsupplier int, @nextamount int, @nextinvoice datetimeset rowcount 1select @supplier = supplier, @invoice = invoice, @amount = amountfrom @torder by supplier, invoice, amountwhile @supplier is not null begin delete from @t where supplier = @supplier and invoice = @invoice and amount = -@amount if @@rowcount > 0 begin delete from @t where supplier = @supplier and invoice = @invoice and amount = @amount select @supplier = supplier, @invoice = invoice, @amount = amount from @t order by supplier, invoice, amount end else begin select @nextsupplier = supplier, @nextinvoice = invoice, @nextamount = amount from @t where supplier = @supplier and invoice = @invoice and amount > @amount order by supplier, invoice, amount if @nextsupplier is null select @nextsupplier = supplier, @nextinvoice = invoice, @nextamount = amount from @t where supplier = @supplier and invoice > @invoice order by supplier, invoice, amount if @nextsupplier is null select @nextsupplier = supplier, @nextinvoice = invoice, @nextamount = amount from @t where supplier > @supplier order by supplier, invoice, amount if @@rowcount > 0 select @supplier = @nextsupplier, @invoice = @nextinvoice, @amount = @nextamount else select @supplier = null end endset rowcount 0-- show the resultselect supplier, invoice, amountfrom @torder by supplier, invoice, amount Peter LarssonHelsingborg, Sweden |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2007-01-03 : 14:55:38
|
| Hi Peso -- I'm not really disapointed, there's always ways to do it (i.e., as I mentioned with a cursor) but the idea is that if you have a table w/o a primary key, then as someone once said (Celko?) you don't have a table; there's no way to objectively analyze and work with the data.Anyway, any solution proposed is worthless until the questions I posed earlier are answered. I'm sure there's other scenarios that I didn't think of that will pop up as well.- Jeff |
 |
|
|
|
|
|