SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


query logic question...


query logic question...

Author
Message
roy.tollison
roy.tollison
Old Hand
Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)

Group: General Forum Members
Points: 302 Visits: 211
currently i have a table that has the following layout, exact details of table not needed.

table1
pkey,
mf_1 char(5),
mf_2 char(5),
mf_3 char(5),
mf_4 char(5),
mf_5 char(5),
mf_6 char(5),
...
mf_50 char(5);

the field mf is repeated from _1 thru _50, i know it is an efficient way to input/mnt that data but that is the way they did it.
now my problem is that the information stored in mf_# can and does vary by row.
rows
1,'ABC','XYZ','MNO',....
2,'XYZ','JKL','UVW','ABC',....
3,'RST',DEF','GHI',....

Currently i am testing everyone of the mf_# to find any rows that contain 'ABC',
IS there a better way to do that or am i stuck with checking everyone of the mf_# columns?
RBarryYoung
RBarryYoung
SSC-Dedicated
SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)

Group: General Forum Members
Points: 33148 Visits: 9518
Other than Full-Text Indexes (maybe, not sure), there is no way to do this: you are stuck.

And this is only one of the reasons not to make non-relational unnormalized tables like this.

-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
roy.tollison
roy.tollison
Old Hand
Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)

Group: General Forum Members
Points: 302 Visits: 211
i agree with that my suggestion was to change the table into 2 tables master/detail but had no say in the final outcome. so i am stuck with using the long query...
autoexcrement
autoexcrement
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1064 Visits: 888
Would UNPIVOT possibly be helpful in this type of scenario?


"If I had been drinking out of that toilet, I might have been killed." -Ace Ventura
roy.tollison
roy.tollison
Old Hand
Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)

Group: General Forum Members
Points: 302 Visits: 211
Not sure how to use UNPIVOT but i will do some checking to see if it will do the job.
Thanks...
autoexcrement
autoexcrement
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1064 Visits: 888
Yeah, honestly I'm not clear on the specifics of it either. But conceptually I understand what it does, and this seems to fit the bill AFAIK... I hope you'll report back!


"If I had been drinking out of that toilet, I might have been killed." -Ace Ventura
roy.tollison
roy.tollison
Old Hand
Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)

Group: General Forum Members
Points: 302 Visits: 211
OK I can use the UNPIVOT to get me the information of the mf_# that contains a matching value.
select pkey, mf_value, mf_field from table1
unpivot
(mf_value for mf_field in (mf_1, mf_2, mf_3,..., mf_50)) as pvtTable where mf_value = 'ABC'
Now my returned rows are...
1, 'ABC',mf_1
2,'ABC',mf_4
and this is good, how would i go about using mf_field value mf_1 and mf_4 to reference a corresponding field in another table.
i would like get the values for cf_1 and cf_4 to go along with the results above...
Table2
pkey,
cf_1 int,
cf_2 int,
cf_3 int,
cf_4 int,
...
cf_50;
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (203K reputation)SSC Guru (203K reputation)SSC Guru (203K reputation)SSC Guru (203K reputation)SSC Guru (203K reputation)SSC Guru (203K reputation)SSC Guru (203K reputation)SSC Guru (203K reputation)

Group: General Forum Members
Points: 203670 Visits: 41949
roy.tollison (11/15/2013)
OK I can use the UNPIVOT to get me the information of the mf_# that contains a matching value.
select pkey, mf_value, mf_field from table1
unpivot
(mf_value for mf_field in (mf_1, mf_2, mf_3,..., mf_50)) as pvtTable where mf_value = 'ABC'
Now my returned rows are...
1, 'ABC',mf_1
2,'ABC',mf_4
and this is good, how would i go about using mf_field value mf_1 and mf_4 to reference a corresponding field in another table.
i would like get the values for cf_1 and cf_4 to go along with the results above...
Table2
pkey,
cf_1 int,
cf_2 int,
cf_3 int,
cf_4 int,
...
cf_50;


If it's always cf_1 and cf_4, a normal inner join is all that's required.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
roy.tollison
roy.tollison
Old Hand
Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)

Group: General Forum Members
Points: 302 Visits: 211
actually i am looking more for something like this
select pkey, mf_value, mf_field, REPLACE(mf_field,'mf_','cf_') as cf_field from table1, table2
unpivot
(mf_value for mf_field in (mf_1, mf_2, mf_3,..., mf_50)) as pvtTable where mf_value = 'ABC'

Yeah i know that isn't the way to do it but that is what i am looking for...
roy.tollison
roy.tollison
Old Hand
Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)

Group: General Forum Members
Points: 302 Visits: 211
i guess i could put the results into a table then use a cursor and process all the rows and use the value in the cf_field(real column name is stored in there) and then build an update command to load up the int value that is assigned to that column for that row.
will be a while before i get all the basics in place and all the bugs worked out...
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search