Unix Technical Forum

SQL selective qty difference

This is a discussion on SQL selective qty difference within the Oracle Miscellaneous forums, part of the Oracle Database category; --> Hi, I have following data structure into table Name Type Qty -------- ------- -------- N1 D 3 --------| N1 ...


Go Back   Unix Technical Forum > Database Server Software > Oracle Database > Oracle Miscellaneous

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-08-2008, 11:19 AM
Anil G
 
Posts: n/a
Default SQL selective qty difference

Hi,

I have following data structure into table

Name Type Qty
-------- ------- --------
N1 D 3 --------|
N1 F 2 | ----> Difference (D - I) DIDFF
------------|
N1 I 4 --------|
|----> (S - DIDIFF) as SDIFF
N1 S 10
----------------------------------------------------------------|
N2 D 2
....
....


Any clues about performing above type operations using SQL query rather
than writing PL/SQL???


Thanks in advance,

Anil G

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-08-2008, 11:19 AM
DA Morgan
 
Posts: n/a
Default Re: SQL selective qty difference

Anil G wrote:
> Hi,
>
> I have following data structure into table
>
> Name Type Qty
> -------- ------- --------
> N1 D 3 --------|
> N1 F 2 | ----> Difference (D - I) DIDFF
> ------------|
> N1 I 4 --------|
> |----> (S - DIDIFF) as SDIFF
> N1 S 10
> ----------------------------------------------------------------|
> N2 D 2
> ...
> ...
>
>
> Any clues about performing above type operations using SQL query rather
> than writing PL/SQL???
>
>
> Thanks in advance,
>
> Anil G


Having twice looked at what you posted I have no idea what you are
asking and I sure don't know in what version you are working.
--
Daniel Morgan
University of Washington
Puget Sound Oracle Users Group
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-08-2008, 11:19 AM
Brian Peasland
 
Posts: n/a
Default Re: SQL selective qty difference

> Having twice looked at what you posted I have no idea what you are
> asking


Glad to know I wasn't the only one!

Cheers,
Brian




--
================================================== =================

Brian Peasland
dba@nospam.peasland.net
http://www.peasland.net

Remove the "nospam." from the email address to email me.


"I can give it to you cheap, quick, and good.
Now pick two out of the three" - Unknown
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-08-2008, 11:19 AM
Charles Hooper
 
Posts: n/a
Default Re: SQL selective qty difference

Anil G wrote:
> Hi,
>
> I have following data structure into table
>
> Name Type Qty
> -------- ------- --------
> N1 D 3 --------|
> N1 F 2 | ----> Difference (D - I) DIDFF
> ------------|
> N1 I 4 --------|
> |----> (S - DIDIFF) as SDIFF
> N1 S 10
> ----------------------------------------------------------------|
> N2 D 2
> ...
> ...
> Any clues about performing above type operations using SQL query rather
> than writing PL/SQL???
>
>
> Thanks in advance,
>
> Anil G


I tried to understand what you are attempting to do here. Best guess:
for the N1 Name entries, take the value of the row with "D" as the
type, subtract from that the value of the row with "I" as the type, and
assign this to a column named DIDFF. Take the value of the row with
"S" as the type, subtract from it the result of the previous
calculation, and assign this to a column named SDIFF.

If my guess is correct, a SQL statement similar to the following should
work:
SELECT
NAME,
NVL(MAX(DECODE(TYPE,'D',QTY,0)),0)-NVL(MAX(DECODE(TYPE,'I',QTY,0)),0)
DIDFF,

NVL(MAX(DECODE(TYPE,'S',QTY,0)),0)-(NVL(MAX(DECODE(TYPE,'D',QTY,0)),0)-NVL(MAX(DECODE(TYPE,'I',QTY,0)),0))
SDIFF
FROM
MY_TABLE
GROUP BY
NAME;

Basic idea of what is happening in the above: for each change in the
NAME column, find the largest QTY where the TYPE column is 'D' (assume
that there is only one matching row), then repeat this syntax for the
remaining TYPE values of interest and add/subtract the values as
needed. This will collapse the multiple rows for each change in the
NAME column to a single row, which is required for the calculations.

SQL code to generate the test environment:
CREATE TABLE MY_TABLE (
NAME VARCHAR2(10),
TYPE CHAR(1),
QTY NUMBER(10));

INSERT INTO MY_TABLE VALUES (
'N1',
'D',
3);

INSERT INTO MY_TABLE VALUES (
'N1',
'F',
2);

INSERT INTO MY_TABLE VALUES (
'N1',
'I',
4);

INSERT INTO MY_TABLE VALUES (
'N1',
'S',
10);

INSERT INTO MY_TABLE VALUES (
'N2',
'D',
2);

Charles Hooper
PC Support Specialist
K&M Machine-Fabricating, Inc.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-08-2008, 11:19 AM
Anil G
 
Posts: n/a
Default Re: SQL selective qty difference

Thank you very much.... this was absolute solution!....

Sorry about diagram that got "wrapped" and i forgot to put variables
names.

Thanks!

Anil G


Charles Hooper wrote:
> Anil G wrote:
> > Hi,
> >
> > I have following data structure into table
> >
> > Name Type Qty
> > -------- ------- --------
> > N1 D 3 --------|
> > N1 F 2 | ----> Difference (D - I) DIDFF
> > ------------|
> > N1 I 4 --------|
> > |----> (S - DIDIFF) as SDIFF
> > N1 S 10
> > ----------------------------------------------------------------|
> > N2 D 2
> > ...
> > ...
> > Any clues about performing above type operations using SQL query rather
> > than writing PL/SQL???
> >
> >
> > Thanks in advance,
> >
> > Anil G

>
> I tried to understand what you are attempting to do here. Best guess:
> for the N1 Name entries, take the value of the row with "D" as the
> type, subtract from that the value of the row with "I" as the type, and
> assign this to a column named DIDFF. Take the value of the row with
> "S" as the type, subtract from it the result of the previous
> calculation, and assign this to a column named SDIFF.
>
> If my guess is correct, a SQL statement similar to the following should
> work:
> SELECT
> NAME,
> NVL(MAX(DECODE(TYPE,'D',QTY,0)),0)-NVL(MAX(DECODE(TYPE,'I',QTY,0)),0)
> DIDFF,
>
> NVL(MAX(DECODE(TYPE,'S',QTY,0)),0)-(NVL(MAX(DECODE(TYPE,'D',QTY,0)),0)-NVL(MAX(DECODE(TYPE,'I',QTY,0)),0))
> SDIFF
> FROM
> MY_TABLE
> GROUP BY
> NAME;
>
> Basic idea of what is happening in the above: for each change in the
> NAME column, find the largest QTY where the TYPE column is 'D' (assume
> that there is only one matching row), then repeat this syntax for the
> remaining TYPE values of interest and add/subtract the values as
> needed. This will collapse the multiple rows for each change in the
> NAME column to a single row, which is required for the calculations.
>
> SQL code to generate the test environment:
> CREATE TABLE MY_TABLE (
> NAME VARCHAR2(10),
> TYPE CHAR(1),
> QTY NUMBER(10));
>
> INSERT INTO MY_TABLE VALUES (
> 'N1',
> 'D',
> 3);
>
> INSERT INTO MY_TABLE VALUES (
> 'N1',
> 'F',
> 2);
>
> INSERT INTO MY_TABLE VALUES (
> 'N1',
> 'I',
> 4);
>
> INSERT INTO MY_TABLE VALUES (
> 'N1',
> 'S',
> 10);
>
> INSERT INTO MY_TABLE VALUES (
> 'N2',
> 'D',
> 2);
>
> Charles Hooper
> PC Support Specialist
> K&M Machine-Fabricating, Inc.


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 06:52 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com