generating two commencing queries and getting the results in the same order?

  • Thread starter Thread starter Morris
  • Start date Start date
M

Morris

Hi.

I've got a table with about 10 columns. Im running the first query:

1) SELECT Id, Supplier FROM tableA WHERE condition

and the second one:

2) SELECT field2, field3, field4 ... field10 FROM tableA WHERE
conditions

Now - is it possible to have both outputs generated in the same order?
I cannot get all the data in one go, cause I need to distribute in
different parts of Excel spreadsheets.

Thanks for your help.

morris
 
If you want the output in an order, then you must specify that order. SO it
is possible to get the outputs into the same order.

You can use fields in the order by clause that are not in the Select clause.

Select Id, Supplier
FROM TableA
WHERE ....
ORDER BY field2, field3, field4

SELECT Field2, Field3, Field4 ...
FROM TableA
WHERE ....
ORDER BY field2, field3, field4

Those two set of data would be ordered by field2, field3, and field4.
HOWEVER, if those three fields aren't sufficient to specify a unique order
you would need to add more fields to the Order by clause. You could ensure
a unique order by using the primary key as the last field in the Order By
clause or simply Ordering by the primary key and not bothering with any
other fields (since the primary key by definition would ensure a unique sort
order)
 
Those two set of data would be ordered by field2, field3, and field4.
HOWEVER, if those three fields aren't sufficient to specify a unique order
you would need to add more fields to the Order by clause. You could ensure
a unique order by using the primary key as the last field in the Order By
clause or simply Ordering by the primary key and not bothering with any
other fields (since the primary key by definition would ensure a unique sort
order)

Thanks for a very comprehensive reply, I haven't been touching SQL for
a looong time, and I totally forgot about the uniqueness of the primary
key. I looked at the table they asked me to work on and of course there
is one, so I'm using ORDER BY that key

Thank you once again, John!

Morris
 
Back
Top