Unix Technical Forum

Help with SQL Statement & Subtraction/Minus

This is a discussion on Help with SQL Statement & Subtraction/Minus within the Oracle Miscellaneous forums, part of the Oracle Database category; --> Looking for help coding this SQL statement Table1 has ID, ID_DESCRIPTION, SERVICE_DATE, SUB_CODE sample data ID, ID_DESCRIPTION, SERVICE_DATE, SUB_CODE ...


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

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-08-2008, 09:29 AM
ct witter
 
Posts: n/a
Default Help with SQL Statement & Subtraction/Minus

Looking for help coding this SQL statement

Table1

has
ID, ID_DESCRIPTION, SERVICE_DATE, SUB_CODE

sample data

ID, ID_DESCRIPTION, SERVICE_DATE, SUB_CODE
1, 1234, 4/1/2005,
2, 1234, 4/1/2005, X
3, 1234, 4/1/2005,
4, 1111, 7/1/2005,
5, 1111, 7/1/2005, X

What I want to do is get a count of Unique items per day.

However, if there is a SUB_CODE (X) then that should be subtracted.

So the final table should be

ID_DESCRIPTION, SERVICE_DATE
1234, 4/1/2005,


Thanks!

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-08-2008, 09:29 AM
Maxim Demenko
 
Posts: n/a
Default Re: Help with SQL Statement & Subtraction/Minus

ct witter schrieb:
> Looking for help coding this SQL statement
>
> Table1
>
> has
> ID, ID_DESCRIPTION, SERVICE_DATE, SUB_CODE
>
> sample data
>
> ID, ID_DESCRIPTION, SERVICE_DATE, SUB_CODE
> 1, 1234, 4/1/2005,
> 2, 1234, 4/1/2005, X
> 3, 1234, 4/1/2005,
> 4, 1111, 7/1/2005,
> 5, 1111, 7/1/2005, X
>
> What I want to do is get a count of Unique items per day.
>
> However, if there is a SUB_CODE (X) then that should be subtracted.
>
> So the final table should be
>
> ID_DESCRIPTION, SERVICE_DATE
> 1234, 4/1/2005,
>
>
> Thanks!
>


SELECT *
FROM (SELECT Service_Date,
COUNT(Id_Description) -
2 * COUNT(Decode(Sub_Code, 'X', 'X', NULL)) Cnt
FROM Table1 t
GROUP BY Service_Date)
WHERE Cnt != 0

If sub_code may contain only 'X' and NULL, you can leave out decode
expression.

Best regards

Maxim
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-08-2008, 09:29 AM
ct witter
 
Posts: n/a
Default Re: Help with SQL Statement & Subtraction/Minus

Worked Great --- Thanks!!

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 12:29 AM.


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