473,396 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Using "*" in subquery

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

--
"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
Nov 15 '05 #1
1 1854
Bernd Hohmann wrote:
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?


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

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Justin | last post by:
Hi. I decided to tidy up some of my queries, but I came across something that stumpt me: mysql> SELECT -> jobs.jobId, -> jobs.active, -> jobs.title, -> jobs.listed, -> ...
1
by: sivaram | last post by:
Hi all, I have the following query in Visual Foxpro that is throwing error. UPDATE T1 set T1.C2 = (select T2.C2 from T2 where T1.c1 = T2.C1)
7
by: Willem van Rumpt | last post by:
Hi all, coming from an unmanaged programming background, I took my time to sort out the IDisposable and finalizer patterns. Just when I thought I had it all conceptually neatly arranged, the...
5
by: charliewest | last post by:
I've implemented the USING statement to ensure that my newly created sql connection closes when my method is finished using it. The USING statement is wrapped in try/catch error handling statement....
14
by: MuZZy | last post by:
Hi, Lately i've been (and still am) fixing some memory leaks problems in the project i just took over when i got this new job. Among the other issues i've noticed that for localy created objects...
0
by: Rave | last post by:
This is a long shot, but I thought I'd try it. I am currently using excel as an inventory tool. I currently have a hand-held scanner plugged into a laptop for reading barcodes. Using the "Find and...
21
by: comp.lang.tcl | last post by:
set php {<? print_r("Hello World"); ?>} puts $php; # PRINTS OUT <? print_r("Hello World"); ?> puts When I try this within TCL I get the following error:
25
by: samjnaa | last post by:
Please check for sanity and approve for posting at python-dev. In Visual Basic there is the keyword "with" which allows an object- name to be declared as governing the following statements. For...
3
by: audj | last post by:
Hello, I am trying to use a subquery to avoid duplicate entries when someone submits a form to subscribe to a mailing list. So I want to check if the email exists before adding the record. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.