Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
Database Servers
DB2InformixIngresMS SQLOraclePervasive.SQLPostgreSQLProgressSybase
Desktop Databases
FileMakerFoxProMS AccessParadox
General
General DB TopicsDatabase Theory
Related Topics
Java Development.NET DevelopmentVB DevelopmentMore Topics ...

Database Forum / General DB Topics / General DB Topics / May 2004

Tip: Looking for answers? Try searching our database.

Simple SQL

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Clive Moore - 18 May 2004 14:13 GMT
Can anyone tell me how to change the following sql statement:

SELECT  document_id
FROM document
WHERE document_id IN
       (SELECT doc_inv_id FROM courier_info)

So that the information returned is orderd by a field in the
courier_info table.

Thanks In advance.  
Alan - 18 May 2004 19:28 GMT
Try this:

SELECT COUNT(*) FROM
(
SELECT custid, edate
FROM  dbw_promoc
MINUS
SELECT custid, edate
FROM promo@report
)
;

> Can anyone tell me how to change the following sql statement:
>
[quoted text clipped - 7 lines]
>
> Thanks In advance.
Thomas Kellerer - 18 May 2004 22:07 GMT
Clive Moore schrieb:

> Can anyone tell me how to change the following sql statement:
>
[quoted text clipped - 7 lines]
>
> Thanks In advance.  

SELECT d.document_id
FROM document d, courier_info ci
WHERE d.document_id = ci.doc_inv_id
ORDER BY ci.order_field
Clive Moore - 19 May 2004 09:50 GMT
> Clive Moore schrieb:
>
[quoted text clipped - 14 lines]
> WHERE d.document_id = ci.doc_inv_id
> ORDER BY ci.order_field

Unfortunatly I can't use this as i have a where clause in my select
on the courier_info table.  see below.  

my SQL does

SELECT  document_id
FROM document
WHERE document_id IN
        (SELECT doc_inv_id FROM courier_info
         where fielda = '10' and fieldb = 'a')
Thomas Kellerer - 19 May 2004 09:59 GMT
>>SELECT d.document_id
>>FROM document d, courier_info ci
[quoted text clipped - 11 lines]
>          (SELECT doc_inv_id FROM courier_info
>           where fielda = '10' and fieldb = 'a')

SELECT d.document_id
FROM document d, courier_info ci
WHERE d.document_id = ci.doc_inv_id
AND ci.fielda = '10' and ci.filedb = 'a'
ORDER BY ci.order_field
Clive Moore - 19 May 2004 10:18 GMT
Thanks for your reply Thomas but unfortunalty i get 228 records back
instead of my normal 78 records.  I think this has something to do with
the unique keyword.  See below. Do you know what is happening?

SELECT  document_id
FROM document
WHERE document_id IN
         (SELECT UNIQUE doc_inv_id FROM courier_info
          where fielda = '10' and fieldb = 'a')
Thomas Kellerer - 19 May 2004 10:21 GMT
> Thanks for your reply Thomas but unfortunalty i get 228 records back
> instead of my normal 78 records.  I think this has something to do with
[quoted text clipped - 5 lines]
>           (SELECT UNIQUE doc_inv_id FROM courier_info
>            where fielda = '10' and fieldb = 'a')

I guess UNIQUE does the same as DISTINCT does (never seen UNIQUE. Which DBMS is
that?)

You didn't include that in the original SQL statement, so I assumed the join
would be OK.

But you can combine the two things:

SELECT d.document_id
FROM document d, courier_info ci
WHERE d.document_id = ci.doc_inv_id
AND d.document_id IN (SELECT UNIQUE doc_inv_id FROM courier_info
                      WHERE fielda = '10' and fieldb = 'a')
ORDER BY ci.order_field

Thomas
Clive Moore - 19 May 2004 10:46 GMT
> SELECT d.document_id
> FROM document d, courier_info ci
[quoted text clipped - 4 lines]
>
> Thomas

The DB is Informix.  I tried the statement you suggest here before
posting trying a disscussion board but got back more than 200 records.

Thanks anyway
Clive
Robert Klemme - 19 May 2004 14:11 GMT
> > Thanks for your reply Thomas but unfortunalty i get 228 records back
> > instead of my normal 78 records.  I think this has something to do with
[quoted text clipped - 20 lines]
>                        WHERE fielda = '10' and fieldb = 'a')
> ORDER BY ci.order_field

That cannot work since the join is made on the non unique table.

Another and IMHO more elegant solution: inline view:

SELECT d.document_id
FROM document d, (SELECT UNIQUE doc_inv_id FROM courier_info
                        WHERE fielda = '10' and fieldb = 'a') ci
WHERE d.document_id = ci.doc_inv_id
ORDER BY ci.order_field

Regards

   robert
Thomas Kellerer - 19 May 2004 14:29 GMT
>>SELECT d.document_id
>>FROM document d, courier_info ci
>>WHERE d.document_id = ci.doc_inv_id
>>AND d.document_id IN (SELECT UNIQUE doc_inv_id FROM courier_info
>>                       WHERE fielda = '10' and fieldb = 'a')
>>ORDER BY ci.order_field

> That cannot work since the join is made on the non unique table.

Of course! You are right.
Didn't think long enough before posting :)

> Another and IMHO more elegant solution: inline view:
>
[quoted text clipped - 3 lines]
> WHERE d.document_id = ci.doc_inv_id
> ORDER BY ci.order_field

I was thinking of the inline view as well, but I'm not sure if Informix supports
that...

Regards
Thomas
Robert Klemme - 19 May 2004 15:29 GMT
> >>SELECT d.document_id
> >>FROM document d, courier_info ci
[quoted text clipped - 7 lines]
> Of course! You are right.
> Didn't think long enough before posting :)

Such things happen.  No need to worry.

> > Another and IMHO more elegant solution: inline view:
> >
[quoted text clipped - 6 lines]
> I was thinking of the inline view as well, but I'm not sure if Informix supports
> that...

If not, Clive can create a real view instead.

Kind regards

   robert
--CELKO-- - 19 May 2004 16:24 GMT
> SELECT  document_id
> FROM document
> WHERE document_id IN
>         (SELECT doc_inv_id FROM courier_info)

>> So that the information returned is orderd by a field [sic] in the
courier_info table. <<

Rows are not records; fields are not columns; tables are not files.  A
result set has be turned into a CURSOR (a sequential file structure)
before it can be sorted.  Since the cursor is created after the result
set, it can only use column names in the SELECT list.  YOur sort
column has to be in your SELECT list.

I think that you meant to use plural or collective nouns for table
names; I am assuming that you have more than one document.  The name
"courier_info" is weird; how would a table about couriers not contain
information about couriers?  This is like putting "tbl-" prefixes on a
table name -- what else would be?

Since you did not bother to post DDL or even tell us the name of the
sort column, let me make wild guessses:

DECLARE Foobar CURSOR FOR
SELECT D1.document_id, C1.sort_col
  FROM Documents AS D1, Couriers AS C1
 WHERE D1.document_id = C1.doc_inv_id
 ORDER BY sort_col;

Of course, if there is a 1:m relationship between Documents and
Couriers, you will get multiple rows.  Since the multiple sort_col
values can vary within each document_id, there is no way to get just
the document_id from what you posted.  We have to pick one value and
assume that document_id is unique:

DECLARE Foobar CURSOR FOR
SELECT D1.document_id, MIN(C1.sort_col) AS min_sort_col
  FROM Documents AS D1, Couriers AS C1
 WHERE D1.document_id = C1.doc_inv_id
 GROUP BY D1.document_id
 ORDER BY min_sort_col;
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.