473,750 Members | 2,478 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 1604
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
3085
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
2095
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
3145
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
3183
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
2029
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
2513
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
1386
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
2037
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
1911
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
10002
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
9001
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
8839
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
9584
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
9344
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
9257
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
6810
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
6081
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
4893
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3327
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

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.