Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
erwine
Yak Posting Veteran
69 Posts |
Posted - 2006-08-31 : 06:27:37
|
Hi SQL friends,I'm curious whether SQL can easily do grouping within a column or not.For example:Create table TESTTABLE( Item varchar(20), Qty integer)insert into TESTTABLEselect 'ar-adsds-01', 12 union allselect 'ar-adsds-02', 20 union allselect 'ad-ldssk-03', 14 union allselect 'ad-dsadd-32', 500 i want to group by column Item (by string before the first -) as :ar 32ad 514by the way, im using SQL2003, not yukon.do i need to apply regularexpression using contains??or is there any other simpler ways?thanks for your kind attention.SQL rocks..... sql is fun... |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-08-31 : 06:46:07
|
First of all, there is no such thing as SQL2003.Second, this will do the trick for you-- Prepare test dataDECLARE @Test TABLE (Item VARCHAR(20), Qty INT)INSERT @TestSELECT 'ar-adsds-01', 12 UNION ALLSELECT 'ar-adsds-02', 20 UNION ALLSELECT 'ad-ldssk-03', 14 UNION ALLSELECT 'ad-dsadd-32', 500 -- Do the work-- This will work, as long as there are 3 parts in the Item name-- Parts can be of any length greater than 0SELECT PARSENAME(REPLACE(Item, '-', '.'), 3), SUM(Qty) FROM @TestGROUP BY PARSENAME(REPLACE(Item, '-', '.'), 3)-- This will work, as long as there are at least 2 characters in the Item nameSELECT LEFT(Item, 2), SUM(Qty) FROM @TestGROUP BY LEFT(Item, 2) Peter LarssonHelsingborg, Sweden |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-08-31 : 10:06:50
|
[code]select left(Item, 2), sum(Qty)from TESTTABLEgroup by left(Item, 2)[/code] KH |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-08-31 : 10:17:22
|
Oh, sorry! Did you emphasis the change from @test table to TESTTABLE table?Peter LarssonHelsingborg, Sweden |
 |
|
erwine
Yak Posting Veteran
69 Posts |
Posted - 2006-08-31 : 11:20:59
|
Opz..did I mention 2003? lol.. It's a good solution indeed, however we need to takecare of NULL, or item doesnt have - (hyphen), or not exactly 3parts.the itemcode may be ABS0990192ABD-JAJSABD-KSKL-01929ABD-KSKL-01929-Bso basically, if I only want to group by string (regardless of the length) before -, can the code does the trick??THx..... sql is fun... |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-08-31 : 11:28:20
|
The first new sample does not conform to having "-". How to deal with that?Also, look at the other two solutions provided by me and khtan.Peter LarssonHelsingborg, Sweden |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-08-31 : 11:41:43
|
try thisselect left(Item, case when charindex('-', Item) <> 0 then charindex('-', Item) - 1 else len(Item) end), sum(Qty)from TESTTABLEgroup by left(Item, case when charindex('-', Item) <> 0 then charindex('-', Item) - 1 else len(Item) end) KH |
 |
|
erwine
Yak Posting Veteran
69 Posts |
Posted - 2006-08-31 : 12:04:49
|
well..this solution makes more sense to me..will try out tomorrow..see how it goes......ohya by the way I have another topic to solve.Can SQL does recursive loop?Thx both Peso and Khtan for your helps.I appreciate that.... sql is fun... |
 |
|
|
|
|
|
|