IWETHEY v. 0.3.0 | TODO
1,095 registered users | 0 active users | 0 LpH | Statistics
Login | Create New User
IWETHEY Banner

Welcome to IWETHEY!

New Re: 'fraid not
I am going to assume that in the real table there is some data that would preclude you simply doing this.

select a,b, max(date), max(c), max(d) from tbl group by a,b

I can see two possible solutions other then what Morgan and Ben suggested.

The ugliest is to use a loop and scan through the records one by one. Create an index that contains all the fields you want to include in the selection critera and then use that to pick through the records one by one, inserting the records you want into a second table.

I do not think you need to go that far here, but I have ended up using them when the critera for selecting records would not condense into a single query or when the critera depended on looking at more then one record at a time.

The second goes like this. Run a query to group by (a,b) and stick them in the second table, with data filled in with nulls. Then do a series of queries like this to fill in the data.
\nUpdate tbl2 set (all the data fields) from\n  (select B.* from \n    (select a,b, max(date) as xDate, count(1) as RowCount, c,d\n      from tbl group by a,b,c,d) as A,\n  tbl as B where B.date = a.xDate and RowCount = 1) as GD,\ntbl2 where GD.a = tbl2.a and GD.b = tbl2.b and tbl2.date is null\n

The idea in this case is that query picks out all the rows where there is only one date and moves them over. Then you can write another query that looks at the next field and only moves them over and so on.

I've used this a couple of times when 90% or more of the records where clean and there where only a handful of records that needed scrubbing, often by hand.

Given what you have said, in SLQ Sever I would probably end up using the same solution you came up with, but would put a primary key on the table and create an index that covers the fields that are part of the critera.

Jay
New Is "SQL Sever" intentional? (Regardless, I like it. :-)
To deny the indirect purchaser, who in this case is the ultimate purchaser, the right to seek relief from unlawful conduct, would essentially remove the word consumer from the Consumer Protection Act
- [link|http://www.techworld.com/opsys/news/index.cfm?NewsID=1246&Page=1&pagePos=20|Nebraska Supreme Court]
     Slow query needs optimizing - (tangaroa) - (19)
         Re: Slow query needs optimizing - (pwhysall) - (3)
             Just imagine a Beowolf cluster of those. -NT - (ChrisR) - (2)
                 Great, now we're turning into /. - (inthane-chan)
                 Running DOSiX! -NT - (Another Scott)
         Can you post - (jbrabeck) - (6)
             'fraid not - (tangaroa) - (5)
                 pump to file, cat file |sort -k3 >outputfile - (boxley)
                 If you're allowed correlated subquery, then: - (morganek) - (1)
                     Re: If you're allowed correlated subquery, then: - (tangaroa)
                 Re: 'fraid not - (JayMehaffey) - (1)
                     Is "SQL Sever" intentional? (Regardless, I like it. :-) -NT - (ben_tilly)
         I don't recognize the "within" clause - (ben_tilly)
         Not quite sure what you are doing - (JayMehaffey) - (6)
             Whew, glad it's not just me that didn't understand. -NT - (jbrabeck)
             Pet peeve on SQLServer - (ChrisR) - (4)
                 Rarely use real temporary tables - (JayMehaffey) - (3)
                     I just finished writing about 15 stored procedures. - (mmoffitt) - (2)
                         Table variables are great - (ChrisR) - (1)
                             Thanks. - (mmoffitt)

You just don't see sideburns like that anymore!
87 ms