Jump to content

Display One Result From Several Tables Issue


chrisdo

Recommended Posts

I'm trying to write an SQL questions which selects specific values from diffrent tables, all of diffrent sort, but I can't get it quite to work.The main problem is one of my tables (param) which looks like this:username: xxxctag: 1234tag: 1value: 10username: xxxctag: 1234tag:2value: 20This is the only table where one column (value) needs to be printed several times. But I dont want all the rest of my queries to be printed several times because of this.If I use: select param.value from param where ctag=12970 AND (tag=2000 OR tag=2001). Both are shown after one an other which I like.BUT, when I try to create ONE query to include several of my other tables, the result is that it prints everything twice because "value" is requested twice.Here is my full question:selectcon.description,underlyings.beta,con.ctagparam.value,fromcon,underlyings,paramwherecon.ctag = 1234 ANDunderlyings.underlying=con.symbol AND(param.ctag=TAG AND (param.tag=1 OR param.tag=2))Pretty much I need all these questions below, to show ONE result and the param table is confusing me.select con.description from con where ctag=TAGselect con.tag from con where ctag=TAGselect underlyings.beta from underlyings where underlyings.underlying=con.symbol #I NEED THIS QUESTIONS TO USE RESULT FROM con TABLE so I only need to change TAG in my questions.select param.value from param where ctag=TAG AND tag=2000select param.value from param where ctag=TAG AND tag=2001At the moment I can get an output qhich looks like this:description testbeta: 1ctag: 1234value 10description testbeta: 1ctag: 1234value 20I would like it to show as:description testtag: 1234value: 10value: 20beta: 1contracttag 13367Im guessing I need to do some kind of joins but Im not sure how. UNION fails aswell beacuse it says the results needs to be of the same sort. If anyone could just show me how to do the param.value togehter with on otherquestions too show as one result it would be so helpful.Thanks very much in advance.

Link to comment
Share on other sites

You would need to use joins where you join on the same table more than once, one join for each value in the same table you're trying to get. You need to give the columns aliases also, you can't have multiple columns with the same name. e.g.: SELECTcon.description,underlyings.beta,con.ctag,p1.value AS value1,p2.value AS value2FROM conINNER JOIN underlyings ON underlyings.underlying=con.symbolINNER JOIN param AS p1 ON p1.tag=1INNER JOIN param AS p2 ON p2.tag=2WHEREcon.ctag = 1234 AND p1.tag=TAG AND p2.tag=TAG

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...