| Author |
Topic |
|
SamC
White Water Yakist
3467 Posts |
Posted - 2003-07-16 : 14:59:03
|
Help Mr. Crossjoin !I've got a complex SELECT that generates a rowset that fills out a datagrid.I know, sequencing the rows is presentation side but..If I have a sequence table, can I use it to add a new first column to the rowset without changing the order of the rowset?I'd like to show an abbreviated example of the problem. It's not easy.SELECT a, b, c FROM MyTable WHERE ..... ORDER BY ....--- Can this be modified to join with table SequenceSelect Seq, A.*FROM Sequence SAbracadabra (SELECT TOP 100 percent a, b, c FROM MyTable WHERE .... ORDER BY ...) A Sam |
|
|
setbasedisthetruepath
Used SQL Salesman
992 Posts |
Posted - 2003-07-16 : 15:26:18
|
| Does the following state the problem correctly?You have a sequence table:12358...and a data table:abcdeand you'd like to see:1 a2 b3 c5 d8 ewith the join occurring on the same ordinal row position?Jonathan{0} |
 |
|
|
SamC
White Water Yakist
3467 Posts |
Posted - 2003-07-16 : 15:52:20
|
| Close, but importantly (I think) the data table is a subquery, complete with WHERE and ORDER BY clauses.The trick is adding a sequence ID to the resulting subquery, preserving the order.I've got a primitive ASP datagrid that should do this job, but it doesn't. I'd like the result set to have the column prefix so viewers of the datagrid result can identify rows by row number.Sam |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2003-07-16 : 16:28:05
|
Sam,What about managing it in the backend...something likeDECLARE @data Table (col1 int IDENTITY(1,1), col2 varchar(20))INSERT INTO @Data(col2)SELECT 'Apples' UNION ALLSELECT 'Bananas' UNION ALLSELECT 'Cherries' UNION ALLSELECT 'Dill' SELECT *, 1 AS RequestId INTO RequestTable From @DataSELECT * From RequestTable Brett8-) |
 |
|
|
SamC
White Water Yakist
3467 Posts |
Posted - 2003-07-16 : 16:57:32
|
| My choice is to insert it into a temporary table with a sequence column. Update the sequence column sequentially after the insert. Then select the whole enchelada.I'd rather find an abracadabra join to do the job however. Nuts.Sam |
 |
|
|
setbasedisthetruepath
Used SQL Salesman
992 Posts |
Posted - 2003-07-16 : 17:02:55
|
| Well, you could include a rank column in the subquery and an identity in the sequence table and join on that, but you would have to work out the ties.Jonathan{0} |
 |
|
|
SamC
White Water Yakist
3467 Posts |
Posted - 2003-07-16 : 17:31:04
|
| Here's our finalist. Not too bad actually, nice curves, no tally table.SELECT IDENTITY(1,1) as Row, A.*INTO #MyTableFROM (SELECT TOP 100 percent a, b, c FROM MyTable WHERE .... ORDER BY ) A SELECT * FROM #MyTable -- Creates the returned ROWSETORDER BY Row -- Is an ORDER BY necessary ?DROP TABLE #MyTable -- Clean upRETURN |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2003-07-16 : 17:46:15
|
| Sam -- in this case, I think what you've posted with the temp table will be the most efficient .... too bad IDENTITY() can't be used in a SELECT statement, though I suppose I can understand why they don't allow it ....A sequence/tally/numbers table really provides no help for this type of problem. (nor does a CROSS JOIN!)i initially read your problem the same as Jonathan did, but it seems much simplier, so go with the temp table.- Jeff |
 |
|
|
Vivaldi
Constraint Violating Yak Guru
298 Posts |
Posted - 2003-07-16 : 17:47:11
|
| Umm,I am not a sql x pertbut I created a user defined function to do the lifting.I doubt this helps at all, but I thought I would share since I pluckedthe info from here. Its probably inefficient, but seems to work pretty well.CREATE FUNCTION [dbo].[chargeSeqNum] ( @id int )RETURNS int AS BEGIN Declare @seq int Set @seq = ( Select count(*) from ( select o.rid from offenses o Cross JOIN offenses r WHERE o.rid = r.rid and o.dateadded >= r.dateadded and o.offenseid = @offenseid group by o.rid,r.dateadded ) as x ) Return @seqEND________________________________________________(Beer + Beer + Beer + Beer + Beer + Martini w/French Vodka + Beer + Beer + Beer) = Sick |
 |
|
|
SamC
White Water Yakist
3467 Posts |
Posted - 2003-07-16 : 18:08:54
|
| Hi Vivaldi,That UDF probably works fine, but I'm thinking it'll be slower because it has to do a select / join to calculate a row number for every row.What do you think?Sam |
 |
|
|
Vivaldi
Constraint Violating Yak Guru
298 Posts |
Posted - 2003-07-17 : 10:16:26
|
| Probably..I don't have enough experience to really understand performanceissues at this level, outside of using logic. (I am sure you are right about it being slow)I wasn't suggesting you use it, just showing what I did for a large view that I wroteto hide some of the complexity. I just don't care if the third party needs to wait for an extra second or two for the data set.I probably should, but I don't.________________________________________________(Beer + Beer + Beer + Beer + Beer + Martini w/French Vodka + Beer + Beer + Beer) = Sick |
 |
|
|
|