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 |
|
vishalj
Starting Member
32 Posts |
Posted - 2006-05-11 : 16:45:34
|
| I have a query which join 5 tables say my result isWarehouse ProductName Features Price Manf I just need to make some analysis and need to select any 5 features from Features column for each productname. There can be 10 - 50 features mentioned in the table with features.Say my result it like only for productname and features columnwhich come from diff tables. Here I am getting more than 5 featuresI need to reduce that to 5 for each. So how do I do this.Productname FeaturesAAAAA 12AAAAA 145AAAAA 23AAAAA 34234AAAAA 234234AAAAA 32234AAAAA 134234AAAAA 21AAAAA 14AAAAA 556BBBBB 19BBBBB 23BBBBB 6BBBBB 1BBBBB 2BBBBB 11BBBBB 15BBBBB 12BBBBB 6BBBBB 4Also very important this need to be done is how do i avoid repititionsand do the cross- tab join and make only one row for one product nameand get the result like thisProductname FeaturesAAAAA 12, 45, 234, 256, 25677, 24223........BBBBB 12,121, 144, 58885, 8888, 99999.....CCCCC 111, 144, 2333, 22333, 22221....... |
|
|
Srinika
Master Smack Fu Yak Hacker
1378 Posts |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
|
|
vishalj
Starting Member
32 Posts |
Posted - 2006-05-12 : 15:32:36
|
| THis does not solve my FIRST part. I think I have to write a cursor to do that. but would that be the best way or if there is another wa i am not sure. |
 |
|
|
cmdr_skywalker
Posting Yak Master
159 Posts |
Posted - 2006-05-12 : 21:38:40
|
| I am not sure what you mean by 5 features (to limit the output to just 5 or have two records with 'AAA' because you have 10 features (10/5=2).)For the first denormalization, here's my suggestion:SET NOCOUNT ONDROP TABLE WAREHOUSEGODROP TABLE TEMPDBGOCREATE TABLE WAREHOUSE( ProductName VARCHAR(10), Features VARCHAR(20))goinsert into warehouse values('AAAAA','12')insert into warehouse values('AAAAA','145')insert into warehouse values('AAAAA','23')insert into warehouse values('AAAAA','34234')insert into warehouse values('AAAAA','234234')insert into warehouse values('AAAAA','32234')insert into warehouse values('AAAAA','134234')insert into warehouse values('AAAAA','21')insert into warehouse values('AAAAA','14')insert into warehouse values('AAAAA','556')insert into warehouse values('BBBBB','19')insert into warehouse values('BBBBB','23')insert into warehouse values('BBBBB','6')insert into warehouse values('BBBBB','1')insert into warehouse values('BBBBB','2')insert into warehouse values('BBBBB','11')insert into warehouse values('BBBBB','15')insert into warehouse values('BBBBB','12')insert into warehouse values('BBBBB','6')insert into warehouse values('BBBBB','4')go--GET THE DISTINCT LIST OF NAMES and CREATE THE DENORMALIZE TABLE--MODIFY THIS QUERY IF YOU NEED TWO AAA, use the principle belowSELECT DISTINCT PRODUCTNAME, CAST('' AS VARCHAR(200)) FeaturesINTO TEMPDBFROM WAREHOUSEgo--use the hint and top so it will be much fasterWHILE EXISTS( SELECT TOP 1 1 FROM TEMPDB T WITH (FASTFIRSTROW) JOIN WAREHOUSE W ON (T.ProductName = W.ProductName) WHERE CHARINDEX(' ' + W.Features +',', T.Features) = 0)BEGIN UPDATE t SET Features = T.Features + ' ' + W.Features + ',' FROM TEMPDB T JOIN WAREHOUSE W ON (T.ProductName = W.ProductName) WHERE CHARINDEX(' ' + W.Features +',', T.Features) = 0ENDGO--show the resultselect *from TEMPDBORDER BY PRODUCTNAMEdrop table WAREHOUSEgoDROP TABLE TEMPDBGO--result:--productname features--AAAAA 556, 14, 21, 134234, 32234, 234234, 34234, 23, 145, 12,--BBBBB 4, 6, 12, 15, 11, 2, 1, 23, 19,May the Almighty God bless us all! |
 |
|
|
cmdr_skywalker
Posting Yak Master
159 Posts |
Posted - 2006-05-12 : 21:41:08
|
| The worst runtime will be of Q(n) where the n is the highest count of features for a product.May the Almighty God bless us all! |
 |
|
|
|
|
|
|
|