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 / DB2 Topics / November 2005

Tip: Looking for answers? Try searching our database.

Using "*" in subquery

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bernd Hohmann - 15 Nov 2005 20:49 GMT
I have a question about using "*" in (sub)queries.

This example query counts the number of records for a certain customer
starting with record "80172" and stops after 10 rows.

SELECT PK_ORDER,PK_CUSTOMER FROM
   (SELECT PK_ORDER,PK_CUSTOMER, ROW_NUMBER() OVER() AS rn
     FROM CUSTOMER_ORDERS_HD WHERE PK_CUSTOMER='10649'
     ORDER BY PK_ORDER DESC) AS T2
   WHERE T2.rn <= 80172 FETCH FIRST 10 ROWS ONLY

Quite easy.

Unfortunately I have to rename all the column labels for display routine
(PK_ORDER AS "Order-Nb" and so on).

1st idea
--------
SELECT * FROM
   (SELECT PK_ORDER AS "ORDER-NB",PK_CUSTOMER, ROW_NUMBER() OVER()...

Bad idea: the result of "row_number()" gets its own column in the output
(Output is "ORDER-NB, PK_CUSTOMER, T2")

2nd idea
--------
SELECT PK_ORDER AS "ORDER-NB",PK_CUSTOMER FROM
   (SELECT *, ROW_NUMBER() OVER() AS rn

Failed because "SELECT *, ROW_NUMBER()" doesn't work.

Any other hints than declaring all rows in both selects?

Bernd

Signature

"Ja, alles meine Herren" sprach Fürst Lichnowsky. "Ooch det roochen?"
"Ja, auch das Rauchen." "Ooch im Tiergarten?" "Ja, auch im Tiergarten
darf geraucht werden, meine Herren." Und so endeten die Barrikadenkämpfe
des 18. März in Berlin

Knut Stolze - 16 Nov 2005 10:58 GMT
> I have a question about using "*" in (sub)queries.
>
[quoted text clipped - 28 lines]
>
> Any other hints than declaring all rows in both selects?

Use a correlation name for the table in the inner FROM clause:

SELECT name1, name2
FROM   ( SELECT t.*, row_number() over() AS rn
        FROM   tab1 AS t )
WHERE  ...

Signature

Knut Stolze
DB2 Information Integration Development
IBM Germany

 
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.