Home » SQL & PL/SQL » SQL & PL/SQL » SQL query
SQL query [message #561125] Thu, 19 July 2012 01:20 Go to next message
satheesh_ss
Messages: 61
Registered: July 2012
Member
Source is Sales Table:

Sno Prod Sales Sales_Amount
1 A 10 2000
2 A 20 1000
3 C 10 3000
4 D 30 4000
5 A 20 1000

Target :

Sales_Count T_Sales_Amt Sales_Count(A) T_Sales_Amt(A)
------------------------------------------------------
90 11000 50 4000

I tried to get the output for this query but i m getting error...My query and error is

select sum(sales) sales_count,sum(sales_amount) t_sales_amount
,(select sum(sales_count) sales_count(A),sum(sales_amount)
t_sales_amt(A) from sales_table group by prod) from sales_table;

ERROR at line 1:
ORA-00923: FROM keyword not found where expected

Can anyone correct my query to get the proper output....Thanks.
Re: SQL query [message #561128 is a reply to message #561125] Thu, 19 July 2012 01:22 Go to previous messageGo to next message
Littlefoot
Messages: 19650
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
What are these "(A)" supposed to do?
Re: SQL query [message #561130 is a reply to message #561128] Thu, 19 July 2012 01:26 Go to previous messageGo to next message
satheesh_ss
Messages: 61
Registered: July 2012
Member
@Littlefoot : I used sales_count(A) etc as a alias for the column sum(sales_count)....But i m getting error....Help me.
Re: SQL query [message #561133 is a reply to message #561130] Thu, 19 July 2012 01:29 Go to previous messageGo to next message
Littlefoot
Messages: 19650
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
It is an invalid alias. Either remove (A), or - if you insist on it - enclose the whole alias into the double quotes.
sum(sales_count) sales_count
or
sum(sales_count) "sales_count(A)"
Re: SQL query [message #561134 is a reply to message #561125] Thu, 19 July 2012 01:30 Go to previous messageGo to next message
Michel Cadot
Messages: 59290
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Michel Cadot wrote on Tue, 17 July 2012 15:04
Michel Cadot wrote on Tue, 17 July 2012 11:43
Michel Cadot wrote on Tue, 17 July 2012 11:36
From your previous topic:

Michel Cadot wrote on Tue, 17 July 2012 11:35
From your previous topic:

Michel Cadot wrote on Sat, 14 July 2012 16:11
None.

From your previous topics:

Michel Cadot wrote on Thu, 12 July 2012 19:52
...
You not only have to read the Concepts manual but also the Database SQL Reference.

Regards
Michel


Michel Cadot wrote on Thu, 12 July 2012 15:11
From your previous topic:

Michel Cadot wrote on Thu, 12 July 2012 14:08
Please feedback to your previous topics and thanks people that (try to) help you.
Also from your previous topics:

Michel Cadot wrote on Thu, 12 July 2012 08:39
All your questions are answered in Database Concepts.
Please read it.

Regards
Michel


Michel Cadot wrote on Thu, 12 July 2012 08:47
...
Database Concepts

Regards
Michel






Regards
Michel


In addition, this is a FAQ already posted a hundred time, so SEARCH before posting (accordingly to the guide).

Regards
Michel



What is your problem in writing this?
With any SQL question, Post a working Test case: create table and insert statements along with the result you want with these data then we will be able work with your table and data. Explain with words and sentences the rules that lead to this result.

Use SQL*Plus and copy and paste what you already tried.

Regards
Michel


...



Please read OraFAQ Forum Guide and How to use [code] tags and make your code easier to read.
Make sure that lines of code do not exceed 80 characters when you format.
Indent the code, use code tags and align the columns in result.
Use the "Preview Message" button to verify.
Also always post your Oracle version, with 4 decimals.
Re: SQL query [message #561149 is a reply to message #561134] Thu, 19 July 2012 01:47 Go to previous messageGo to next message
satheesh_ss
Messages: 61
Registered: July 2012
Member
Onece again i m getting error....


SQL> ed
Wrote file afiedt.buf

1 select sum(sales) "sales_count",sum(sales_amount) "t_sales_amount"
2 ,(select sum(sales_count) "sales_count(A)",sum(sales_amount)
3* "t_sales_amt(A)" from sales_table group by prod) from sales_table
SQL> /
,(select sum(sales_count) "sales_count(A)",sum(sales_amount)
*
ERROR at line 2:
ORA-00913: too many values

Send me the correct query....
Re: SQL query [message #561151 is a reply to message #561149] Thu, 19 July 2012 02:04 Go to previous messageGo to next message
Littlefoot
Messages: 19650
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
Uh, oh ... you should really pay attention to links Michel keeps posting. If you formatted code properly, it would be easier to spot the culprit. Please, click here - you'll get the idea in no more than 10 seconds.

DECODE is what you need, I think.
select 
  sum(sales) c1,
  sum(sales_amount) c2,
  sum(decode(prod, 'A', sales)) c3,
  sum(decode(prod, 'A', sales_amount)) c4
from sales_table

[Updated on: Thu, 19 July 2012 02:05]

Report message to a moderator

Re: SQL query [message #561154 is a reply to message #561151] Thu, 19 July 2012 02:44 Go to previous message
satheesh_ss
Messages: 61
Registered: July 2012
Member
@Littlefoot : Thanks dude...Its working
Previous Topic: Howto execute several sql statements in one command of the DataAdapter Class
Next Topic: 2nd Decimal Place not appearing
Goto Forum:
  


Current Time: Wed Oct 01 08:56:16 CDT 2014

Total time taken to generate the page: 0.08508 seconds