PostgreSQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesPostgreSQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 19th, 2013, 04:57 AM
dibblejon dibblejon is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 17 dibblejon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 48 m 31 sec
Reputation Power: 0
Grouping help

Hi

Can someone help me to group my table.

Current query is :

Code:
SELECT 
  public.slcnote.account,
  public.slcnote.keyfield,
  public.slcnote.note_date,
  public.slcnote.note_note,
  public.slcnote.note_type,
  public.slcnote.filter
FROM
  public.slcnote
WHERE
  public.slcnote.filter = 'OB' AND 
  public.slcnote.note_date >= '2010-01-01 00:00:00' AND 
  (public.slcnote.note_type = '1' OR 
  public.slcnote.note_type = '2' OR 
  public.slcnote.note_type = '3' OR 
  public.slcnote.note_type = '4' OR 
  public.slcnote.note_type = '5')
ORDER BY
  public.slcnote.account,
  public.slcnote.keyfield


This returns data like :

account1 1234 01/03/2013 note 1 OB
account1 1235 15/03/2013 note 3 OB
account1 1244 20/03/2013 note 5 OB
account2 1222 01/01/2012 note 2 OB
account2 1299 19/03/2013 note 4 OB

I want to group and see :

account1 1244 20/03/2013 note 5 OB
account2 1299 19/03/2013 note 4 OB

So basically we total each Account record by the latest keyfield and this displays the last date, note, note type.

Any help would be great, thanks!

Reply With Quote
  #2  
Old March 19th, 2013, 05:19 AM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,847 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 1 Day 10 h 36 m 10 sec
Reputation Power: 813
Hi,

is the combination of account and keyfield unique? If so, make a subselect to get the maximum keyfield for each account, and then select the other corresponding data in the main query:

Code:
SELECT
	...
FROM
	slcnote
WHERE
	(account, keyfield) IN (
		SELECT
			account
			, MAX(keyfield)
		FROM
			slcnote
		GROUP BY
			account	
	)
;


If (account, keyfield) is not unique, then you need additional criteria.

Reply With Quote
  #3  
Old March 19th, 2013, 05:26 AM
dibblejon dibblejon is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 17 dibblejon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 48 m 31 sec
Reputation Power: 0
Keyfield and account are unique

Hi Thanks

The above are unique.

Forgive my ignorance I have never used subselects before.

Tried this and it is incorrect - how should it be formatted?

Code:
SELECT 
  public.slcnote.account,
  public.slcnote.keyfield,
  public.slcnote.note_date,
  public.slcnote.note_note,
  public.slcnote.note_type,
  public.slcnote.filter
FROM
  public.slcnote
WHERE
  public.slcnote.filter = 'OB' AND 
  public.slcnote.note_date >= '2010-01-01 00:00:00' AND 
  (public.slcnote.note_type = '1' OR 
  public.slcnote.note_type = '2' OR 
  public.slcnote.note_type = '3' OR 
  public.slcnote.note_type = '4' OR 
  public.slcnote.note_type = '5')
SELECT
	
FROM
	slcnote
WHERE
	(account, keyfield) IN (
		SELECT
			account
			, MAX(keyfield)
		FROM
			slcnote
		GROUP BY
			account	
	)

Reply With Quote
  #4  
Old March 19th, 2013, 05:42 AM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,847 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 1 Day 10 h 36 m 10 sec
Reputation Power: 813
This cannot work, because you've simply written two separate queries as if it was one query.

The SELECT part from your old query goes into the SELECT part of the new query. And your WHERE part goes into the WHERE part of the subselect:

Code:
SELECT
	account
	, keyfield
	, note_date
	, note_note
	, note_type
	, filter
FROM
	slcnote
WHERE
	(account, keyfield) IN (
		SELECT
			account
			, MAX(keyfield)
		FROM
			slcnote
		WHERE
			filter = 'OB'
			AND note_date >= '2010-01-01 00:00:00'
			AND note_type IN ('1', '2', '3', '4', '5')
		GROUP BY
			account	
	)
;


Several things:
  • Don't prepend the "public" schema to every database object (I already said that last time). It's the default schema, so it's unnecessary to explicitly write it down. It just bloats your query.
  • No need to qualify every column with the table name. You only select from one table, so there's never any ambiguity.
  • If you wanna check a column against multiple values, use the IN syntax rather than a long chain of equality checks.
  • Why do you use number strings for the type? Why not actual numbers?

Reply With Quote
  #5  
Old March 19th, 2013, 06:04 AM
dibblejon dibblejon is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 17 dibblejon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 48 m 31 sec
Reputation Power: 0
Brilliant thank you.

This is a great help.

I will take on board these comments and look to write my queries differently in the future!

In regards to the note_type field - this is how it was setup by the application manufacturers. Most customers will use this field as text to record information, we use numbers

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesPostgreSQL Help > Grouping help

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap