query logic question...

  • 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?

  • 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.

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

  • 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...

  • 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

  • Not sure how to use UNPIVOT but i will do some checking to see if it will do the job.

    Thanks...

  • 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

  • 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;

  • 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.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • 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...

  • 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...

  • roy.tollison (11/16/2013)


    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...

    Heh... yeah. Let us know how the cursor thing works you for you. 😉

    Since folks still have questions about what you're actually trying to do, I recommend that you try to clarify the problem using the suggestions from the 1st link under "Helpful Links" in my signature line below. Obviously, you don't have to post the whole table or a million rows of data but there's nothing like cold hard data in a table to clarify a problem.

    --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.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • CREATE TABLE [dbo].[nTable](

    [p_key] [int] NOT NULL,

    [mf_1] [char](5) NULL,

    [mf_2] [char](5) NULL,

    [mf_3] [char](5) NULL,

    [mf_4] [char](5) NULL,

    [mf_5] [char](5) NULL,

    [cf_1] [int] NULL,

    [cf_2] [int] NULL,

    [cf_3] [int] NULL,

    [cf_4] [int] NULL,

    [cf_5] [int] NULL,

    CONSTRAINT [PK_nTable] PRIMARY KEY CLUSTERED

    (

    [p_key] ASC

    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

    ) ON [PRIMARY]

    Sample Data...

    p_keymf_1mf_2mf_3mf_4mf_5cf_1cf_2cf_3cf_4cf_5

    1AAA BBB CCC DDD EEE 55555

    2BBB DDD BBB BBB BBB 55555

    3EEE DDD CCC BBB AAA 55555

    4FFF HHH III AAA NULL4444NULL

    5ZZZ NULLNULLNULLNULL1NULLNULLNULLNULL

    6GGG BBB EEE AAA NULL4444NULL

    7MMM EEE AAA NULLNULL333NULLNULL

    8NULLNULLNULLNULLNULL00000

    9RRR TTT DDD BBB CCC 55555

    10BBB CCC AAA NULLNULL333NULLNULL

    11CCC AAA NULLBBB NULL33NULL3NULL

    12WWW UUU AAA CCC EEE 55555

    13SSS AAA DDD EEE NULL4444NULL

    14EEE FFF BBB AAA NULL4444NULL

    15TTT NULLNULLNULLNULL1NULLNULLNULLNULL

    16YYY AAA NULLNULLNULL22NULLNULLNULL

    17GGG XXX BBB NULLNULL333NULLNULL

    18BBB AAA CCC EEE DDD 55555

    19PPP BBB TTT JJJ NULL4444NULL

    20KKK LLL NULLNULLNULL22NULLNULLNULL

    drop table pvtTable

    select p_key, mf_value, mf_field, REPLACE(mf_field,'mf_','cf_') as cf_field into pvtTable from nTable

    unpivot

    (mf_value for mf_field in

    (mf_1, mf_2, mf_3, mf_4, mf_5)) as pvtTable where mf_value in ('AAA','BBB')

    declare getCSField cursor for select p_key, cf_field from pvtTable

    open getCSField

    declare @cmd varchar(max),

    @P_Keyint,

    @CSField as varchar(100);

    fetch next from getCSField into @P_Key, @CSField

    while @@FETCH_STATUS = 0

    begin

    begin try

    execute ('select ' + @P_Key + ' as p_key, ' + @CSField + ' from nTable where p_key = ' + @P_Key)

    end try

    begin catch

    select @P_Key, @CSField

    end catch

    fetch next from getCSField into @P_Key, @CSField;

    end

    CLOSE getCSField

    DEALLOCATE getCSField

Viewing 12 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic. Login to reply