T-Sql problem, please help I am stuck.

  • Hi

    Following is the scenario I am facing:

    Table#A

    AttributeID | AttributeName

    1 Alpha

    2 Brave

    3 Charlie

    Table#B

    ColumnID | ColumnValue

    1 Reading1

    2 Reading2

    3 Reading3

    and Finally Mapper Table

    MappingID | ColumnID | AttributeID

    1 1 1

    2 1 2

    3 1 3

    What I want is a structure like this :

    -------------------------------------------------------------------------------

    | Reading1 | Reading 2 | Reading 3

    -------------------------------------------------------------------------------

    Alpha | x | |

    -------------------------------------------------------------------------------

    Bravo | y | |

    -------------------------------------------------------------------------------

    Charlie | z | |

    --------------------------------------------------------------------------------

    This is basically a report format and i cant develop a cross tab cuz it has some grouping and calculation issue so is there a possible way out in T-Sql. I am using sql server 2005 and pivot command is not helping me beacause i dont know the number of columns to pivot data, please help me out here ppl.

  • You would need to build up the pivot statement and then execute this. An example is on:

    http://www.sqlservercentral.com/forums/shwmessage.aspx?forumid=338&messageid=367396#bm367610

    Regards,

    Andras


    Andras Belokosztolszki, MCPD, PhD
    GoldenGate Software

  • You'd probably get more help if your output example matched the example inputs in your post... for example... where the heck did the "x", "y", and "z" come from???

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

  • quotewhere the heck did the "x", "y", and "z" come from

    Probably from the aforementioned 'some grouping and calculation issue' in the original post. 

    Far away is close at hand in the images of elsewhere.
    Anon.

  • well we will put values against these values, see above 2 tables are just the tables of where rows and cols are defined there is another table where we define values against each of them like:

    ID | RowID | ColID | Value

    1 ... ... Some Value

    2 ... ... Some Value

  • You're missing the point, Uzwa... From the data examples you have in the first post, what do you want the output to look like?

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

  • I want a cross tab basically, i can do it by Crystal Cross tab reports but problem is I have to perform not only addition but subtraction and discounts on data so i want sql to generate data for my in an order so that i can fit it in a tabular fashion and perform calculations.

  • You're still missing the point... you provided certain base data in your original post and an output sample that cannot be...

    I'm just asking for you to provide the correct output sample so we can help you with the code... not going to waste time guessing what you want...

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

Viewing 8 posts - 1 through 7 (of 7 total)

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