Author |
Topic |
Maharisi
Starting Member
19 Posts |
Posted - 2007-07-02 : 08:43:17
|
Hello020 20051101 20051130 111130X0 20051201 20051231 11113020 20060101 20060131 11113020 20060203 20060228 11113020 20060301 20091231 11113020 20100101 20700101 11113HOW CAN I GROUP THIS RECORDS TO020 20051101 20051130 111130X0 20051201 20051231 11113020 20060101 20700101 11113THANKS |
|
Dallr
Yak Posting Veteran
87 Posts |
Posted - 2007-07-02 : 08:52:12
|
From the results you posted i believe you want something like the following. I have used your data values as field names so just replace them with the relevant field name. Select 020 , min(20060101) as MinDate, max(20070101) as MaxDate, 1113From YourTableNameGroup by 020,1113Dallr |
|
|
Maharisi
Starting Member
19 Posts |
Posted - 2007-07-02 : 09:11:47
|
Sorry I need Group by all groups according to atributes TYPETYPE A B C020 20040101 20051031 11113 020 20051101 20051130 111130X0 20051201 20051231 11113020 20060101 20060131 11113020 20060203 20060228 11113020 20060301 20091231 11113020 20100101 20700101 11113Group these records to TYPE A B C020 20040101 20051130 11113 0X0 20051201 20051231 11113020 20060101 20700101 11113Thanks a lot |
|
|
Maharisi
Starting Member
19 Posts |
Posted - 2007-07-02 : 09:12:32
|
A,B,C are the next atributtes |
|
|
Dallr
Yak Posting Veteran
87 Posts |
Posted - 2007-07-02 : 10:56:42
|
Sorry but i don't understand what you are asking. Can you please explain a bit more. Dane |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2007-07-02 : 11:59:12
|
Maharisi -- as Dallr indicated, you have not provided enough information for us to assist you. You must explain to us the logic that is applied to generate those results, otherwise we are just guessing. If we guess, it takes longer and you will not get the right answer. If you provide specific details, there is no guessing involved and we can give you exactly what you want. Makes sense, right?- Jeffhttp://weblogs.sqlteam.com/JeffS |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-07-02 : 12:12:56
|
Or with SQL Server 2005, SELECT Type, A, B, C FROM (SELECT Type, A, B, C, ROW_NUMBER() OVER (PARTITION BY Type ORDER BY A) AS RecID) AS dWHERE RecID = 1Peter LarssonHelsingborg, Sweden |
|
|
Maharisi
Starting Member
19 Posts |
Posted - 2007-07-03 : 07:27:14
|
Thanks you Peso But I dont have SQL SERVER 2005. I have 2000Function ROW_NUMBER is not provided in.Have you another idea? |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-07-03 : 09:38:42
|
[code]-- prepare sample datacreate table #sample ( a varchar(3), t1 smalldatetime, t2 smalldatetime, b smallint )insert #sampleselect '020', '20060301', '20091231', 11113 union allselect '020', '20051101', '20051130', 11113 union allselect '020', '20060101', '20060131', 11113 union allselect '0X0', '20051201', '20051231', 11113 union allselect '020', '20060203', '20060228', 11113 union allselect '020', '20100101', ' 20700101', 11113-- set up sample date in ordercreate unique clustered index ix_sample on #sample (t1, a)alter table #sample add grp intgo-- update grp#declare @grp int, @last varchar(3)update #sampleset @grp = grp = case when isnull(@last, '') <> a then isnull(@grp, 0) + 1 else @grp end, @last = a-- show the expected outputselect a, min(t1) as t1, max(t2) as t2from #samplegroup by a, grporder by 2alter table #sample drop column grpgoselect * from #sampledrop table #sample[/code]Peter LarssonHelsingborg, Sweden |
|
|
facestheband
Starting Member
6 Posts |
Posted - 2007-07-05 : 00:22:44
|
You may not need a group by here at all if you don't need a count.write the followingselect distinct field1, field2, field3 from tablenameif you need a count at the end of each row then writeselect field1, field2, field3, count(*) from tablename group by field1, field2, field3 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-07-05 : 02:34:49
|
quote: Originally posted by facestheband You may not need a group by here at all if you don't need a count.write the followingselect distinct field1, field2, field3 from tablenameif you need a count at the end of each row then writeselect field1, field2, field3, count(*) from tablename group by field1, field2, field3
Is this related to this thread?MadhivananFailing to plan is Planning to fail |
|
|
Maharisi
Starting Member
19 Posts |
Posted - 2007-07-12 : 05:10:13
|
Thanks guys,Really great ideas, especially Peter Larsson |
|
|
|