473,385 Members | 1,409 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,385 software developers and data experts.

question about a select


OK, I have the following table:

create table citations_by_level
(
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_level 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_level 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_level 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*******@postgresql.org so that your
message can get through to the mailing list cleanly

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

Text=# select distinct on (y_level) y_level, byteloc from
citations_by_level 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_level 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_level 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_level 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_level where aid=543::smallint 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_level 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_level 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
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...
3
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...
1
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>...
5
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...
8
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...
3
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...
2
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...
10
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...
5
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...
25
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. ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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...

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.