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 |
|
MikeB
Constraint Violating Yak Guru
387 Posts |
Posted - 2005-12-02 : 11:25:23
|
I have a table:tbComponentsID | Mark------------------1 | 401tbComponentStrandsID | Qty | Material-------------------1 | 4 | 1/2"1 | 2 | 5-7/16"I need to create a view where it would result in:vComponentStrandsID | Pattern------------------------1 | (4)1/2" + (2)5-7/16" Any ideas on how I can do this?Mike B |
|
|
jhermiz
3564 Posts |
Posted - 2005-12-02 : 12:14:45
|
SELECT ID, MIN(Qty) + ' ' + MIN(MATERIAL) AS B, MAX(Qty) + ' ' + MAX(Material) AS C FROM TABLE2GROUP BY ID Keeping the web experience alive -- [url]http://www.web-impulse.com[/url] |
 |
|
|
jhermiz
3564 Posts |
Posted - 2005-12-02 : 13:08:20
|
Actually that probably wont work, it depends on your data, is it always a max of 2 per ID ? Keeping the web experience alive -- [url]http://www.web-impulse.com[/url] |
 |
|
|
MikeB
Constraint Violating Yak Guru
387 Posts |
Posted - 2005-12-02 : 13:38:43
|
quote: Originally posted by jhermiz Actually that probably wont work, it depends on your data, is it always a max of 2 per ID ? Keeping the web experience alive -- [url]http://www.web-impulse.com[/url]
Well, I tried a couple of things and the only thing I can get to work is by using a cursor within a UDF. The only problem is that I cannot use the ORDER BY when declaring the cursor. Is this normal. I figured I can use any SELECT statement with a cursor.DECLARE Pattern CURSOR FOR (SELECT TOP 100 PERCENT t1.Quantity, t2.Descr, t1.Location FROM dbo.tbHollowcoreComponentStrands t1 INNER JOIN dbo.tbMaterials t2 ON t1.fkMaterialID = t2.MaterialID WHERE (t1.fkProjectNumber = @cProject) AND (t1.fkComponentID = @cComponent) ORDER BY t1.Location ASC) Any thoughts?Mike B |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-12-03 : 07:49:40
|
use the trick in this function of avoid the cursor: --run this whole block to see it workset nocount oncreate table tbComponents (ID int, Mark int)create table tbComponentStrands (ID int,Qty int, Material varchar(10))goinsert tbComponents (id,mark) select 1 , 401 union allselect 2, 402insert tbComponentStrandsselect 1, 4, '1/2"' union allselect 1, 2, '5-7/16"' union allselect 2, 100, '2-1/4"' union allselect 2, 10, '1/4"'gocreate function dbo.udf_componentPattern(@componentid int)returns varchar(200)asbegin declare @pattern varchar(200) select @pattern = coalesce(@pattern + ' + ' + '(' + convert(varchar,qty) + ')' + Material, '(' + convert(varchar,qty) + ')' + Material) from tbComponentStrands where id = @componentid return @patternendgoselect id, dbo.udf_componentPattern(id) as patternfrom tbcomponentsgodrop function dbo.udf_componentPatterndrop table tbComponentStrandsdrop table tbComponentsbtw, if you remove the enclosing paranthesis from the cursor definition, I think your error goes away. I think removing the "top 100 percent" will also do the trick.Be One with the OptimizerTG |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-12-03 : 08:01:08
|
Also, for you and anyone else, as long as you're going to the trouble of typing the structure and contents of your tables like this:quote: tbComponentsID | Mark------------------1 | 401tbComponentStrandsID | Qty | Material-------------------1 | 4 | 1/2"1 | 2 | 5-7/16"
make it easy on yourself and us by scripting the tables and paste that in, then just add a few insert statements as sample data. You'll get more responses and we won't have to guess at datatypes and stuff. Thanks!create table tbComponents (ID int, Mark int)create table tbComponentStrands (ID int,Qty int, Material varchar(10))goinsert tbComponents (id,mark) select 1 , 401 union allselect 2, 402insert tbComponentStrandsselect 1, 4, '1/2"' union allselect 1, 2, '5-7/16"' union allselect 2, 100, '2-1/4"' union allselect 2, 10, '1/4"'go Be One with the OptimizerTG |
 |
|
|
jhermiz
3564 Posts |
Posted - 2005-12-03 : 21:03:13
|
TG i love your posts, slick solution.Good to have you on board.Jon Keeping the web experience alive -- [url]http://www.web-impulse.com[/url] |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-12-04 : 15:10:14
|
Be One with the OptimizerTG |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-12-04 : 23:51:29
|
TG is one of the Tsql Gurus MadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|
|