Home » SQL & PL/SQL » SQL & PL/SQL » SQL conditional show group by number (9.i)
SQL conditional show group by number [message #301166] Tue, 19 February 2008 06:55 Go to next message
kroeseg
Messages: 1
Registered: February 2008
Junior Member
My records look like this:

A B
1 1000
1 1000
1 1000
2 100
2 110
3 100
4 50
4 50
4 100
5 75
6 900
6 1000

I have to construct a query that results the following:

A B
1 1000
1
1
2 100
2 10
3 100
4 50
4
4 50
5 75
6 900
6 100

If A has more than one occurrence the query-result should only contain the first value of B when B is the same in every result (i.e. A=1).

If A has more than one occurrence the result should contain the sum of B when B is differnt in a result (i.e. A=2).

Is there anyone who has a solution for this?



Re: SQL conditional show group by number [message #301168 is a reply to message #301166] Tue, 19 February 2008 07:11 Go to previous messageGo to next message
S.Rajaram
Messages: 1027
Registered: October 2006
Location: United Kingdom
Senior Member
search for analytic functions in sql reference manual. In particular search for "LAG" analytic functions. Last but not least read the forum guidlines before you make your next post.

Regards

Raj
Re: SQL conditional show group by number [message #301180 is a reply to message #301166] Tue, 19 February 2008 07:31 Go to previous message
Michel Cadot
Messages: 64111
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
LAG function
BREAK SQL*Plus command

Regards
Michel
Previous Topic: Needed SQL Help
Next Topic: merged: extracting query from a procedure
Goto Forum:
  


Current Time: Mon Dec 05 19:16:31 CST 2016

Total time taken to generate the page: 0.11047 seconds