Jump to content
The mkiv Supra Owners Club

sql query ...


paul_y3k

Recommended Posts

i know there's a few dba's on the site, and once again I need some help.

 

i've inherited a sql table that is structured as

 

ref name value

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

1 first fred

1 second hello

1 third dog

1 fourth cat

 

so basically a set of values for a reference.

i'm trying to pull out a couple of those values, for a given reference, but my sql isn't up to much. i.e

 

ref value

1 fred,cat

 

select name where ref=1 and value='first' and value='third'

doesn't work, and I dont think I should be using or.

 

from reading my limited library I'm not sure it's acually possible ?

Link to comment
Share on other sites

Disclaimer: This looks quite inefficient, and there's probably a better way of doing it that avoids the join, but I'm no DBA!

 

CREATE TABLE test AS

SELECT table_one.ref, table_one.first, table_two.third

FROM table table_one, table table_two

WHERE table_one.ref = table_two.ref

AND table_one.name = 'first'

AND table_two.name = 'third';

 

(or something like that?!) :D

Link to comment
Share on other sites

SELECT name, value

INTO dbo.tbl_xxx

FROM table WHERE ref='' AND (name='first' OR name='fourth');

 

But that wouldn't work as it's using an OR in the select and i want both values ?

 

 

 

CREATE TABLE test AS

SELECT table_one.ref, table_one.first, table_two.third

FROM table table_one, table table_two

WHERE table_one.ref = table_two.ref

AND table_one.name = 'first'

AND table_two.name = 'third';

 

i think my second example has confused things. so lets forget about the second table stuff for the moment. All I want is the values from the table where name = first and name = third.

 

ie. 1 FRED DOG

 

but the and keyword doesn't work.

Link to comment
Share on other sites

you mean, you want to concatenate multiple fields into one field for a given referance number? if so, then its rather difficult, depending on hopw many column values you want to use.

 

1 | Cat, dog, mouse

2 | toyota, nissan, ford

3 | dish, bowl, plate

 

For example?

 

Dont think so. i know I'm not exapling this very well at all so thanks for bearing with me.

 

lets try again ...

 

the table is defined with the following columns :

 

REF A_data b_data

 

Each row consists of the job reference (REF), a work item (a_data) and it's associated value (b_data). We can have multiple items and values per job. So we see a table with the following contents

 

REF a_data b_data

1 ITEM1 widget

1 ITEM2 doodahs

1 ITEM3 sprockett

2 ITEM1 big widget

2 ITEM2 big doodah

2 ITEM3 big sprockett

3 ITEM1 small widget

etc etc

 

I need to select the b_data for ITEM1 and ITEM3 for a given reference.

i.e SELECT b_data where REF=1 and a_data=ITEM1 and a_data=ITEM3.

 

to get a returrn of

 

1 widget sprockett

 

However that piece of sql returns no rows, and using OR doesn't seem to help either. So my problem is in the where clause ? or is it due to a poor table structure ?

Link to comment
Share on other sites

Did you try this, Paul:

 

SELECT table_one.ref, table_one.b_data, table_two.b_data

FROM table table_one, table table_two

WHERE table_one.ref = table_two.ref

AND table_one.ref = '1'

AND table_one.a_data = 'ITEM1'

AND table_two.a_data = 'ITEM3';

 

?

 

I'm going to ... but i dont understand where you are getting table two from ??

Link to comment
Share on other sites

this gives you _exactly_ what you want as described.

 

select a.ref, a.col1, b.col2 from (select ref, b_data col1 from my_table where a_data="item1") a, (select ref, b_data col2 from my_table where a_data="item3") b where a.ref = b.ref = 1;

assuming the table in my case is called "my_table", substitute as required etc)

 

took a minute, only just seen this thread.

 

bit of a weird thing to want to do though, is the db design actually any good if you need to do this sort of query.

 

there are other ways to achieve this too, which way to go kinda depends on other factors but in this simple case this does what you asked (if I understood what you asked correctly!)

Link to comment
Share on other sites

Dont think so. i know I'm not exapling this very well at all so thanks for bearing with me.

 

lets try again ...

 

the table is defined with the following columns :

 

REF A_data b_data

 

Each row consists of the job reference (REF), a work item (a_data) and it's associated value (b_data). We can have multiple items and values per job. So we see a table with the following contents

 

REF a_data b_data

1 ITEM1 widget

1 ITEM2 doodahs

1 ITEM3 sprockett

2 ITEM1 big widget

2 ITEM2 big doodah

2 ITEM3 big sprockett

3 ITEM1 small widget

etc etc

 

I need to select the b_data for ITEM1 and ITEM3 for a given reference.

i.e SELECT b_data where REF=1 and a_data=ITEM1 and a_data=ITEM3.

 

to get a returrn of

 

1 widget sprockett

 

However that piece of sql returns no rows, and using OR doesn't seem to help either. So my problem is in the where clause ? or is it due to a poor table structure ?

 

I'm seriously having an issue working out what you are trying to pull from the table..

What data do you want?

 

Not in SQL terms, but like..

 

Table has job entries, an item number etc..

 

I would like to pull all entries from the DB where the Work Item is the same as xxx...

 

 

You seem to want data from three columns for more then one row. Maybe it's the DB structure, this is where relational DB's come into their own.

 

More work to design and maintain, but easier to pull the data off them..

 

You'd have something like the following.. ID Fields are all autonumber.

 

Job Ref Table.

 

 

ID Field Job Ref

1 JB01

2 JB02

3 JB99

 

 

Item Table

 

ID Field JobID Work Item

 

1 1 ITEM 1

2 1 ITEM 2

3 2 ITEM 9

 

Then a Value table.

 

 

ID Field ITEMID Item Value

 

1 1 Widget

2 3 Small Widget.

 

So you'd get

 

JB01 Item 1 Widget...

 

 

I think I just confused myself also, but it does mean you can have many to one links..

 

IE you might have 20 items hanging off one Job ref...

 

Cheers,

 

Paul.

Link to comment
Share on other sites

it really does seem like you are trying to turn rows into columns, which in SQL is rather difficult. especially if you have a large list of ITEM NUMBERS. If you only have a couple of ITEM NUMBERS then see CHILLIs response, and just replicate fo each item. However this is not an option if you have a lot of them.

Link to comment
Share on other sites

it really does seem like you are trying to turn rows into columns, which in SQL is rather difficult. especially if you have a large list of ITEM NUMBERS. If you only have a couple of ITEM NUMBERS then see CHILLIs response, and just replicate fo each item. However this is not an option if you have a lot of them.

 

that's the problem, if you want to turn this into a generic solution then you probably need to pivot the data - otherwise it's a square peg and a round hole. For what you asked though, the solution is simple enough - if you are working with legacy data and that's all you need to do I'd use that and move on to drinking beer. If it's new, I'd redesign the db, then move onto drinking beer :D

Link to comment
Share on other sites

that's the problem, if you want to turn this into a generic solution then you probably need to pivot the data - otherwise it's a square peg and a round hole. For what you asked though, the solution is simple enough - if you are working with legacy data and that's all you need to do I'd use that and move on to drinking beer. If it's new, I'd redesign the db, then move onto drinking beer :D

 

slightly wrong... start by drinking beer, continue with drinking beer and finish with drinking beer...

 

BTW - SQL 2005 does have a pivot function that is supposed to do this, but I have not had a chance to try it.

Link to comment
Share on other sites

slightly wrong... start by drinking beer, continue with drinking beer and finish with drinking beer...

 

this is probably the best solution to this problem proposed so far :D

 

at least if you start by drinking beer the problem won't seem very important for long...

Link to comment
Share on other sites

cheers guys, i'm glad it's not just me thats getting confused with this.

yep it's a legacy system so I can't change it. All I'm trying to do it get item1 and item3 for each job id. but yep the table doesn't really support it.

 

and yep I've taken the adivce and has several beers and a curry :)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. You might also be interested in our Guidelines, Privacy Policy and Terms of Use.