473,700 Members | 2,743 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to group by a joined query?

suppose I've got two table:

laser_uni=# \d t1
Table "public.t1"
Column | Type | Modifiers
--------+------+-----------
name | text |
addr | text |

laser_uni=# \d t2
Table "public.t2"
Column | Type | Modifiers
--------+---------+-----------
name | text |
len | integer |
of | integer |

and I want to use join to select out data and then group by one column,
like this:

laser_uni=# select t1.name, t1.addr, t2.name, t2.len, t2.of from t1
right join t2 on t1.name=t2.name group by t2.name;
ERROR: Attribute t1.name must be GROUPed or used in an aggregate function

seems the I must gorup all those fields:

laser_uni=# select t1.name as t1name, t1.addr as t1addr, t2.name as
t2name, t2.len, t2.of from t1 right join t2 on t1.name=t2.name group by
t1.name, t1.addr, t2.name, t2.len, t2.of;
t1name | t1addr | t2name | len | of
--------+--------+--------+-----+----
| | henry | 2 | 4
| | laser | 4 | 4
(2 rows)

is it specification compliant or postgresql specific?

Thanks

Laser
---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 11 '05 #1
2 1601
Weiping He wrote:
suppose I've got two table:

laser_uni=# \d t1
Table "public.t1"
Column | Type | Modifiers
--------+------+-----------
name | text |
addr | text |

laser_uni=# \d t2
Table "public.t2"
Column | Type | Modifiers
--------+---------+-----------
name | text |
len | integer |
of | integer |

and I want to use join to select out data and then group by one
column, like this:

laser_uni=# select t1.name, t1.addr, t2.name, t2.len, t2.of from t1
right join t2 on t1.name=t2.name group by t2.name;
ERROR: Attribute t1.name must be GROUPed or used in an aggregate
function

seems the I must gorup all those fields:

laser_uni=# select t1.name as t1name, t1.addr as t1addr, t2.name as
t2name, t2.len, t2.of from t1 right join t2 on t1.name=t2.name group
by t1.name, t1.addr, t2.name, t2.len, t2.of;
t1name | t1addr | t2name | len | of
--------+--------+--------+-----+----
| | henry | 2 | 4
| | laser | 4 | 4
(2 rows)

is it specification compliant or postgresql specific?

Thanks

reread the docs, seems use DISTINCE ON clause solved my problem:

select distinct on( t2.len) t1.name as t1name, t1.addr as t1addr,
t2.name as t2name, t2.len, t2.of from t1 right join t2 on t1.name=t2.name ;
Thanks

Laser
---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 11 '05 #2
Weiping He <la***@zhengmai .com.cn> writes:
laser_uni=# select t1.name, t1.addr, t2.name, t2.len, t2.of from t1
right join t2 on t1.name=t2.name group by t2.name;
ERROR: Attribute t1.name must be GROUPed or used in an aggregate function seems the I must gorup all those fields: is it specification compliant or postgresql specific?


It's spec-compliant according to SQL92.

There is some amazingly complex verbiage in SQL99 that seems intended to
allow the system to deduce that some GROUP BY specs are redundant and
can be omitted. We don't implement that (yet) and I don't think it
would apply to your example anyway, since neither table's name column
is marked as primary key.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 11 '05 #3

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

Similar topics

1
3082
by: Shawn | last post by:
First I have to say I do not know SQL or VB very well...so if I'm able to get this to work without learning a new language then I'll be ecstatic. OK...I have a tbl of approximately 1000 lines of data and about 20-30 columns each. The actual query itself is irrelevant, but I'm meshing this up against multiple other tbls that I'm linked too. What I need is this 1000 lines of data to remain in tact (at least what's left of them) when the...
1
2090
by: Michael | last post by:
I have a query that uses two joined tables. The query contains data. I would like to pull in a data drom another table but when I add that table to the query design and make the join,the existing data is not shown in the query in datasheet view. I have realised that this is because the joined fields in the existing data were not populated to link to the newly added table. I can understand why data was not pulled in from the new table...
2
3142
by: cefrancke | last post by:
I can't seem to find a straight answer for my specific issue. Any help would be appreciated. I would like to count the various items in a table where the fields have a 'group' relationship. I would like to count the items per group. (Please forgive the inappropriate use of database terms.) An example follows:
1
3180
by: Ersin Gençtürk | last post by:
hi , I have 2 tables , tUser and tUserRole and I have a query like : select * from tUser inner join tUserRole on tUserRole.UserId=tUser.UserId where tUser.UserId=1 this will return a user with associated roles. Also I have a typed dataset with two tables inside : tUser and tUserRole
2
2025
by: mike | last post by:
Hi I am trying to work out if this is possible in a select query I have a group by query which could result in several rows, what I want to do is do a text equivalent of a sum() eg: SELECT sum(inv_id),date,cust from invoice group by date,cust
5
2512
by: zMisc | last post by:
Are there any tricks in updaitng a JOINed recordset? I joned to tables and when I try to change a field on the recordset and update it, I get this error: "Unknown column 'CCDE' in 'where clause'. CCDE is a field from one of the table and is not a field I am updating. I uses MyODBC with VB6 and MySQL 5.
1
1383
by: nhiras | last post by:
I have 4 fields in my table Field1, Field2, Field3 and Amount. A unique record in this table is defined by the combination of Field1, Field2 and Field3. I have a query that rolls up by Field1. The other 2 fields - Field2 and Field3 are not used in the aggregate query - hence are rolled up. The Amount field is Summed up to display the total. Question - I want to know what makes up the aggregate record. How do I find out what records...
4
2035
by: Frank List | last post by:
Hi, I've run into this problem many times and have not found a good solution yet. Here's what I have: table ParentTable - the "1" table table ChildTable- the "many" table ParentTable inner joined to ChildTable on ParentTable.ParentID =
2
1908
by: Jonathan Woods | last post by:
Hi there, I have no idea why the following TSQL does not work.Any Idea? I am using SQL 2000 Server CREATE PROCEDURE RptDailySummary2 @FromTxDate DATETIME, @ToTxDate DATETIME AS BEGIN
10
9996
by: Rudolf Bargholz | last post by:
Perhaps some kind soul could help me out with an SQL I have been trying all day to get to work, where one colum is just not summing up the way I want it to. I have the following data GRP_SEQ ITEM_SEQ NR_ITEMS PERSONS_SEQ 1 A 2 aa 1 A 2 bb
0
8642
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9206
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
9066
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...
0
8916
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
6558
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
4400
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...
0
4652
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2383
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2022
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.