473,721 Members | 1,816 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ORDER BY a SubSelect?

Is it possible to ORDER BY a SubSelect? I don't see why it wouldn't
be, but I'm having some trouble. Hopefully it's just a simple error in
syntax and somebody can tell me. Here's what I'm trying to do...
I've got two tables:

Table1:
TestData Filename
Bird FileB
Blue FileA
Circle FileC
Fish FileB
Green FileA
Nofile1 (NULL)
Nofile2 (NULL)
Nofile3 (NULL)
Square FileC
Trapezoid FileC
Triangle FileC
Yellow FileA

Table2:
DateImported Filename
9/20/2005 FileA
9/21/2005 FileB
9/22/2005 FileC
I'd like to get the full listing of elements from Table1, sorted by the
DateImported field that corresponds to them by the two tables
"Filename" field.

I know that one way of doing it would be like so:
SELECT DateImported, *
FROM TABLE1, TABLE2
WHERE Table2.Filename =Table1.Filenam e
ORDER BY DateImported;

However, that query does not return the records from Table1 that have a
(NULL) value for their Filename.

My next guess was to do this:

SELECT (SELECT DateImported FROM Table2 WHERE
Table2.Filename =Table1.Filenam e) AS ImportDate, *
FROM TABLE1
ORDER BY ImportDate;

If you take out the "ORDER BY ImportDate" from this query, it returns
ALL the elements from Table1, just like I want and once I have them I
can sort them by their ImportDate by right-clicking on the column name.
However, I need the query to do the sorting and when I leave the
"ORDER BY ImportDate" on the end of that query, Access for some reason
doesn't realize that it's the name of my subselect column.

Any ideas?

Nov 13 '05 #1
4 7558
You can order by a subquery result value, but you need to duplicate the entire
subquery in the order by clause. You can't refer to the select column name.
In your case, though, an outer join would make much more sense.

SELECT TABLE2.DateImpo rted, TABLE1.*
FROM TABLE1 LEFT JOIN TABLE2
ON TABLE1.Filename =TABLE2.Filenam e
ORDER BY TABLE2.DateImpo rted;

I recommend always qualifying all the names because there could be duplicated
names between your tables (either now or later), and also because without name
qualifiers, when you go back to maintain the query, you have to remember what
field comes from where or dig through all the table definitions to find out.

On 22 Sep 2005 08:10:52 -0700, dt******@gmail. com wrote:
Is it possible to ORDER BY a SubSelect? I don't see why it wouldn't
be, but I'm having some trouble. Hopefully it's just a simple error in
syntax and somebody can tell me. Here's what I'm trying to do...
I've got two tables:

Table1:
TestData Filename
Bird FileB
Blue FileA
Circle FileC
Fish FileB
Green FileA
Nofile1 (NULL)
Nofile2 (NULL)
Nofile3 (NULL)
Square FileC
Trapezoid FileC
Triangle FileC
Yellow FileA

Table2:
DateImported Filename
9/20/2005 FileA
9/21/2005 FileB
9/22/2005 FileC
I'd like to get the full listing of elements from Table1, sorted by the
DateImported field that corresponds to them by the two tables
"Filename" field.

I know that one way of doing it would be like so:
SELECT DateImported, *
FROM TABLE1, TABLE2
WHERE Table2.Filename =Table1.Filenam e
ORDER BY DateImported;

However, that query does not return the records from Table1 that have a
(NULL) value for their Filename.

My next guess was to do this:

SELECT (SELECT DateImported FROM Table2 WHERE
Table2.Filenam e=Table1.Filena me) AS ImportDate, *
FROM TABLE1
ORDER BY ImportDate;

If you take out the "ORDER BY ImportDate" from this query, it returns
ALL the elements from Table1, just like I want and once I have them I
can sort them by their ImportDate by right-clicking on the column name.
However, I need the query to do the sorting and when I leave the
"ORDER BY ImportDate" on the end of that query, Access for some reason
doesn't realize that it's the name of my subselect column.

Any ideas?


Nov 13 '05 #2
is this what u mean?

SELECT Table2.dateImpo rt, Table2.filename , Table1.testData
FROM Table2 RIGHT JOIN Table1 ON Table2.filename = Table1.filename
ORDER BY Table2.dateImpo rt DESC;

Nov 13 '05 #3
I tried duplicating the entire subquery in the order by clauase and
Access gave me an error. The statement you put together with the outer
join works and gives me exactly what I was looking for. Thank you.

Also, you're right about always qualifying the names, that's much more
clear. Thanks again.

Nov 13 '05 #4
On 22 Sep 2005 09:10:40 -0700, dt******@gmail. com wrote:
I tried duplicating the entire subquery in the order by clauase and
Access gave me an error. The statement you put together with the outer
You probably pasted the " As <fieldname>" part. That part is not valid in the
ORDER BY clause.
join works and gives me exactly what I was looking for. Thank you.

Also, you're right about always qualifying the names, that's much more
clear. Thanks again.


Nov 13 '05 #5

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

Similar topics

0
1747
by: limbert | last post by:
------=_NextPart_000_0001_01C34D7B.DFBF53C0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Hi, =20 I was reading the manual and it said that the subselect is only
6
16322
by: Greg Stark | last post by:
So I have a query in which some of the select values are subqueries. The subqueries are aggregates so I don't want to turn this into a join, it would become too complex and postgres would have trouble optimizing things. So my question is, is there some way to have a subselect return multiple columns and break those out in the outer query? Something like: SELECT x,y,z,
7
10656
by: Jean-Francois.Doyon | last post by:
Hello, I'm trying to retrieve a limited number of random rows, and order them by a column, and am not having any luck with that last part: SELECT * FROM tablename ORDER BY random(), id LIMIT 10 Returns everything more or less as expected, except for the fact that the results aren't sorted by "id" ...
3
7199
by: Neil Zanella | last post by:
Hello, I would like to ask the about the following... PostgreSQL allows tables resulting from subselects to be renamed with an optional AS keyword whereas Oracle 9 will report an error whenever a table is renamed with the AS keyword. Furthermore, in PostgreSQL when the result of a subselect is referenced in an outer select it is required that the subselect result be named, whereas this is not true in Oracle. I wonder what standard SQL...
4
2749
by: James | last post by:
I have a performance problem with the following query and variations on the subselect. The EXISTS version of the first example will complete in ~10 minutes. The NOT logic in both the examples makes them both keep running long enough that a communications error is the only result returned so far. This is a federated view computer ~160 k rows, computer_sys_id is PK . matched_sware ~ 18 million no PK , no index. Any suggestions on...
5
2244
by: Bob Stearns | last post by:
Is there an easy way (without duplication of the complex expression) to use the same complex expression in all three places? Will something like this work? WITH (SELECT t.*, <complex expr> AS x FROM <base_table> AS t) AS wt SELECT * FROM wt WHERE ABS(x)<10 ORDER BY ABS(x)
1
1724
by: Marco Lazzeri | last post by:
I'd like to reference values returned by a subselect in the same SELECT query. Example: SELECT id, ( SELECT COUNT(*) FROM second ) AS value_to_reference, ( value_to_reference + 1 ) AS value_that_uses_referenced_one FROM first;
2
1777
by: Jerry LeVan | last post by:
I have an srf sql function "annual_report(<year>)" that as 14 columns, a category, 12 month numeric columns, and a total numeric column. The function finds monthly totals for each category (which is the target of "order by") and the grand total for the specified year. I have another sql function annual_profit_loss(<year>) that summarizes the total
2
7354
by: Morten K. Poulsen | last post by:
(re-post) Dear list, Please let me know if this is not the list to ask this kind of question. I am trying to optimize a query that joins two relatively large (750000 rows in each) tables. If I do it using a subselect, I can "force" the planner to choose the fastest path. Now, my question is:
0
8834
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9365
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9127
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9056
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6664
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5973
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4483
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3186
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2569
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.