473,773 Members | 2,306 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

question about a select


OK, I have the following table:

create table citations_by_le vel
(
aid smallint,
wid smallint,
v_level varchar(50),
w_level varchar(50),
x_level varchar(50),
y_level varchar(50),
z_level varchar(50),
byteloc integer
);

(If it helps, aid/wid identifies a text or work, the levels are
citation levels for that work (all but z_level potentially optional);
eg for some work y_level might indicate chapters [z_levels indicate
lines], and byteloc is the file position of that particular citation
in the work).

What I would *like* to be able to do is construct a query that groups
by a level, but sorts by byteloc. I don't seem to be able to do this.

Here are some examples. Note that y_level (any level) may have
duplicates (which I want to eliminate), and that it's ordering is
strictly on byteloc, not on its own value. postgres doesn't seem to
have envisioned this scenario and/or I'm not being creative enough in
constructing the query...

Text=# select distinct on (y_level) y_level from citations_by_le vel where aid=543 and wid=1;
y_level
---------
1
10
10a
11
12
13
14
15
16
17
18
19
2
20
21
22
23
24
25
26
27
28
29
2a
3
30
31
32
33
34
35
36
37
4
5
5a
6
7
7,8
8
9
t
(42 rows)

but as you can see, the "ordering" winds up being alphabetic on
y_level which simply does not do. [In this case it is only
coincidental that y_level appears numeric, it is a string and could be
anything; and the 7,8 is such an example]. The *byteloc* associated
with a given y_level (the location of that particular citation) does,
but I can't seem to use it:

Text=# select distinct on (y_level) y_level, byteloc from citations_by_le vel where aid=543 and wid=1 order by byteloc;
ERROR: SELECT DISTINCT ON expressions must match initial ORDER BY expressions

Text=# select distinct on (y_level) y_level, byteloc from citations_by_le vel where aid=543 and wid=1 order by byteloc, y_level;
ERROR: SELECT DISTINCT ON expressions must match initial ORDER BY expressions

???

--Cindy
--
ct*****@uci.edu

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postg resql.org so that your
message can get through to the mailing list cleanly

Nov 12 '05 #1
3 2914
On Thu, 9 Oct 2003, Cindy wrote:

Text=# select distinct on (y_level) y_level, byteloc from
citations_by_le vel where aid=543 and wid=1 order by byteloc;
ERROR: SELECT DISTINCT ON expressions must match initial ORDER BY
expressions


Maybe something like:

select y_level from (select distinct on (y_level) y_level,
byteloc from citations_by_le vel where aid=543 and wid=1) tab order by
byteloc;

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 12 '05 #2

Thanks for the speedy response!

Stephan Szabo writes:
On Thu, 9 Oct 2003, Cindy wrote:

Text=# select distinct on (y_level) y_level, byteloc from
citations_by_le vel where aid=543 and wid=1 order by byteloc;
ERROR: SELECT DISTINCT ON expressions must match initial ORDER BY
expressions


Maybe something like:

select y_level from (select distinct on (y_level) y_level,
byteloc from citations_by_le vel where aid=543 and wid=1) tab order by
byteloc;


Hm...this seems to pick out the y_level with the largest byteloc
associated with it...I need the y_level with the smallest byteloc and
then sorted by that...lemme play this, but any other suggestions also
welcomed...

(EG, I got:
Text=# select y_level, byteloc from (select distinct on (y_level)
y_level, byteloc from citations_by_le vel where aid=543::smalli nt and
wid=1::smallint ) tab order by byteloc;
y_level | byteloc
---------+---------
13 | 24017
11 | 36231
8 | 47240
10 | 96777
2 | 285678
5 | 400212
7,8 | 420879
....

but if you look at the raw data (or some of it):
---------+---------
1 | 29
1 | 84
2 | 423
3 | 827
4 | 1005
5 | 1169
6 | 1530
1 | 1698
2 | 1988
3 | 2284
4 | 2460
5 | 2638
6 | 2924
7 | 3155
8 | 3396
1 | 3673
2 | 4095
3 | 4387
4 | 4699
5 | 4944
6 | 5055
7 | 5406
8 | 5704
9 | 5996
10 | 6349
1 | 6578
2 | 7110

I want

1 | 29
2 | 423
3 | 827
4 | 1005
5 | 1169
6 | 1530
7 | 3155
8 | 3396
9 | 5996
10 | 6349

etc...
--
Cindy
ct*****@uci.edu

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 12 '05 #3

On Thu, 9 Oct 2003, Cindy wrote:
Stephan Szabo writes:
>On Thu, 9 Oct 2003, Cindy wrote:
>> Text=# select distinct on (y_level) y_level, byteloc from
>> citations_by_le vel where aid=543 and wid=1 order by byteloc;
>> ERROR: SELECT DISTINCT ON expressions must match initial ORDER BY
>> expressions

>
>Maybe something like:
>
>select y_level from (select distinct on (y_level) y_level,
>byteloc from citations_by_le vel where aid=543 and wid=1) tab order by
>byteloc;


Hm...this seems to pick out the y_level with the largest byteloc
associated with it...I need the y_level with the smallest byteloc and
then sorted by that...lemme play this, but any other suggestions also
welcomed...


I think an order by y_level, byteloc in the subquery might give you the
ordering you want for the distinct on step, and then the outer order by
will order the y_levels by their respective bytelocs.
---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 12 '05 #4

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

Similar topics

11
12722
by: Markus Breuer | last post by:
I have a question about oracle commit and transactions. Following scenario: Process A performs a single sql-INSERT into a table and commits the transaction. Then he informs process B (ipc) to read the new date. So process B starts "select ..." but does not get the previously inserted row. The timespan between commit and select is very short. (NOTE: two different sessions are used) Questions: 1.) Does commit when returning from call...
3
1848
by: ricksql | last post by:
#temptable got order,fname and age. trying to find two high maxes per each order. query returns (1) but (2) is correct answer. supposedly, max(age2) < max(age1). **** select d.order,case d.t when 1 then s.fname else null end as fname, case d.t when 1 then s.age else null end as age1, case d.t when 2 then s.fname else null end as fname, case d.t when 2 then s.age else null end as age2 from #temptable s
1
1789
by: Scott | last post by:
The following is the XML I have to work with. Below is the question <Table0> <CaseID>102114</CaseID> <CaseNumber>1</CaseNumber> <DateOpened>2005-06-14T07:26:00.0000000-05:00</DateOpened> <OnCallPerson /> <CallType>Exposure</CallType> <ExposureReason>General</ExposureReason> <OtherExposureReason>Unintentional</OtherExposureReason> <ClientName>Test Client</ClientName>
5
4455
by: Sue | last post by:
I wrote a script that uses the sp_refreshviews. The script will be part of a larger one that is automatically run in multiple databases where different views exist. Question: My understanding of views is that by simply stating 'select * from myviewname where 0=1' that the view is recompiled. If so, what advantages are there in using sp_refreshviews? I couldn't handle the errors that sp_refreshview produced (I am sure due to my lack...
8
3271
by: Együd Csaba | last post by:
Hi All, how can I improve the query performance in the following situation: I have a big (4.5+ million rows) table. One query takes approx. 9 sec to finish resulting ~10000 rows. But if I run simultaneously 4 similar queries it takes nearly 5 minutes instead of 4 times 9 seconds or something near of that. here is a sample query: select mertido, fomeazon, ertektipus, mertertek from t_me30 where fomeazon in (select distinct fomeazon...
3
6472
by: Tcs | last post by:
My backend is DB2 on our AS/400. While I do HAVE DB2 PE for my PC, I haven't loaded it yet. I'm still using MS Access. And no, I don't believe this is an Access question. (But who knows? I COULD be wrong... :) I've tried the access group...twice...and all I get is "Access doesn't like ".", which I know, or that my query names are too long, as there's a limit to the length of the SQL statement(s). But this works when I don't try to...
2
3249
by: Eitan | last post by:
Hello, I want a solutions for a compicateds sql select statments. The selects can use anything : views, stored procedures, analytic functions, etc... (not use materialized view, unless it is neccessary). question 1) The selects can use anything : views, stored procedures, analytic functions, etc...
10
2111
by: Robert | last post by:
I am an attorney in a non-profit organization and a self-taught programmer. I'm trying to create a client db that will allow me to search for potential conflicts of interest based either on Social Security # or on Last Name. I've created two different tables with the following fields in each table: ClientInfo Client# (primary key) First Name Middle Name Last Name
5
3846
by: wugon.net | last post by:
question: db2 LUW V8 UNION ALL with table function month() have bad query performance Env: db2 LUW V8 + FP14 Problem : We have history data from 2005/01/01 ~ 2007/05/xx in single big table, we try separate this big table into twelve tables and create a view
25
5403
by: bonneylake | last post by:
Hey Everyone, Well i am not sure if my question needs to be here or in coldfusion. If i have my question is in the wrong section i am sorry in advance an will move it to the correct section. Basically i have a multiple select box. An it works except for this one part i want to add to it.The first box i have is called project members which shows the users you can choose to send an email to and the other box is called assignees which is the...
0
9621
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
10264
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...
0
10106
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10039
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
9914
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...
0
8937
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7463
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
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2852
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.