If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > PostgreSQL > Group by clause- hide column name

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-06-11, 08:46
SanCJ SanCJ is offline
Registered User
 
Join Date: Jan 2011
Posts: 1
Question Group by clause- hide column name

Hi
I have a table named t_food.

In that, my fields are " food_id ,food_name , food_qty , food_nature, food_price "

and i want to list all food items with sum of its quantites . I'm using group by clause like this .


select food_id , food_name , sum(food_qty) as qty, food_nature,food_price from t_food group by

food_id,food_name,food_nature , food_price ;

but what i want my query as

select food_id , food_name , sum(food_qty) as qty, food_nature,food_price from t_food group by food_id ;

Is there anyway to group by database items by omitting/hiding column names ..?

Thanks in advance...!

Last edited by SanCJ; 01-06-11 at 08:57.
Reply With Quote
  #2 (permalink)  
Old 01-06-11, 08:50
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,408
I don't really understand you question.

Please:

post some sample data (preferrably as INSERT INTO statements)
post the desired outcome based on that sample data
and make sure to use [code] tags to make your SQL readable
Reply With Quote
  #3 (permalink)  
Old 01-10-11, 14:37
loquin loquin is offline
Super Moderator
 
Join Date: Jun 2004
Location: Arizona, USA
Posts: 1,797
(and, offhand, it appears that the database should be normalized...)

It looks like there's food information, and inventory information, and pricing information, all stored in one table???
__________________
Lou
使大吃一惊
"Lisa, in this house, we obey the laws of thermodynamics!" - Homer Simpson
"I have my standards. They may be low, but I have them!" - Bette Middler
"It's a book about a Spanish guy named Manual. You should read it." - Dilbert

Reply With Quote
  #4 (permalink)  
Old 01-10-11, 14:57
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,538
Quote:
Originally Posted by SanCJ View Post
and i want to list all food items with sum of its quantites
there's your problem right there

you can't show detailed and aggregate information at the same time

you can either list all food items, or you can show the sum, but not both -- at least, not without a lot more explanation on your part about ~which~ sum you want to show for ~each~ item
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 01-10-11, 16:22
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,408
Quote:
Originally Posted by r937 View Post
you can't show detailed and aggregate information at the same time
Not entirely true

Code:
SELECT food_id, 
       food_name, 
       sum(food_qty) over (partition by food_nature) as nature_qty, 
       food_nature,
       food_price 
FROM t_food;

Last edited by shammat; 01-10-11 at 16:56. Reason: Removed the senseless grouping by PK as pointed out by r937
Reply With Quote
  #6 (permalink)  
Old 01-10-11, 16:37
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,538
Quote:
Originally Posted by shammat View Post
Not entirely true
we can get into a nice discussion on several tangents here...

but suffice to say that food_id is presumably the primary key of that table, so partitioning over it will give only one row, hence the sum isn't any different from the food_qty
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 01-10-11, 16:53
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,408
Quote:
Originally Posted by r937 View Post
but suffice to say that food_id is presumably the primary key of that table, so partitioning over it will give only one row, hence the sum isn't any different from the food_qty
Good point

Consider it an example
Reply With Quote
Reply

Tags
column names, group by, query, select, sum

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On