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 > MySQL > nid help please

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-17-07, 13:03
itanacious itanacious is offline
Registered User
 
Join Date: Dec 2007
Posts: 5
Question nid help please

hey guys can you help me with my query promblems, this is the table that i have created

----Accounts----
Date | amount | customerID


Date -> the current date and time of the date entry
Amount -> is the credit or debit / they differ when amount < 0 it is credit and when amount > 0 it's debit

Date is my primary key

sample data
Date | amount | customerID
12/13/07 | 100 | cus001
12/20/07 | 200 | cus001
12/23/07 | -50 | cus001
12/24/07 | 500 | cus005


I need to query like this: where customerID = cus001

Date | debit | credit | balance | customerID
12/13/07 | 100 | 0 | 100 | cus001
12/20/07 | 200 | 0 | 300 | cus001
12/23/07 | 0 | 50 | 250 | cus001

will it be possible?
thanks in advance...
Reply With Quote
  #2 (permalink)  
Old 12-17-07, 13:09
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Date is the primary key???

so, in other words, only one customer can make only one transaction on any date?

something seems very fishy about this

when is this assignment due?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 12-17-07, 14:18
Littlefoot Littlefoot is offline
Lost Boy
 
Join Date: Jan 2004
Location: Croatia, Europe
Posts: 3,629
If we forget about design flaw, the answer is YES (it is possible) - it would require the DECODE (or CASE) and the SUM analytical function:
Code:
SQL> select * From accounts;

DATUM        AMOUNT CUSTOMERID
-------- ---------- ----------
13.12.07        100 cus001
20.12.07        200 cus001
23.12.07        -50 cus001
24.12.07        500 cus005

SQL> SELECT datum,
  2    CASE
  3      WHEN amount >= 0 THEN amount
  4      ELSE 0
  5     END debit,
  6    CASE
  7      WHEN amount > 0 THEN 0
  8      ELSE ABS(amount)
  9    END credit,
 10    SUM(amount) over (ORDER BY datum) balance,
 11    customerid
 12  FROM ACCOUNTS
 13  WHERE customerid = 'cus001';

DATUM         DEBIT     CREDIT    BALANCE CUSTOMERID
-------- ---------- ---------- ---------- ----------
13.12.07        100          0        100 cus001
20.12.07        200          0        300 cus001
23.12.07          0         50        250 cus001

SQL>
Next time, please, do some initial effort by yourself and come here if you have a question about the specific problem. This was one-time spoonfeeding; don't expect more of it.
Reply With Quote
  #4 (permalink)  
Old 12-17-07, 18:29
itanacious itanacious is offline
Registered User
 
Join Date: Dec 2007
Posts: 5
wow thx r937 ur right only one transaction per/day and about the smelly fishy ur right too. my teacher smell kinda fishy..
Reply With Quote
  #5 (permalink)  
Old 12-17-07, 20:00
itanacious itanacious is offline
Registered User
 
Join Date: Dec 2007
Posts: 5
Lightbulb translate this SQL queries PLEASE.....

Code:
SQL> select * From accounts;

DATUM        AMOUNT CUSTOMERID
-------- ---------- ----------
13.12.07        100 cus001
20.12.07        200 cus001
23.12.07        -50 cus001
24.12.07        500 cus005

SQL> SELECT datum,
  2    CASE
  3      WHEN amount >= 0 THEN amount
  4      ELSE 0
  5     END debit,
  6    CASE
  7      WHEN amount > 0 THEN 0
  8      ELSE ABS(amount)
  9    END credit,
 10    SUM(amount) over (ORDER BY datum) balance,
 11    customerid
 12  FROM ACCOUNTS
 13  WHERE customerid = 'cus001';

DATUM         DEBIT     CREDIT    BALANCE CUSTOMERID
-------- ---------- ---------- ---------- ----------
13.12.07        100          0        100 cus001
20.12.07        200          0        300 cus001
23.12.07          0         50        250 cus001

SQL>
thx in advance
Reply With Quote
  #6 (permalink)  
Old 12-17-07, 20:24
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
translate? into what? english?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 12-17-07, 20:58
itanacious itanacious is offline
Registered User
 
Join Date: Dec 2007
Posts: 5
Smile Nid help Please

can someone translate this Sql queries to Mysql
it use over() function which I have not idea what is the counterpart in MySql

Code:
SQL> select * From accounts;

DATUM        AMOUNT CUSTOMERID
-------- ---------- ----------
13.12.07        100 cus001
20.12.07        200 cus001
23.12.07        -50 cus001
24.12.07        500 cus005

SQL> SELECT datum,
  2    CASE
  3      WHEN amount >= 0 THEN amount
  4      ELSE 0
  5     END debit,
  6    CASE
  7      WHEN amount > 0 THEN 0
  8      ELSE ABS(amount)
  9    END credit,
 10    SUM(amount) over (ORDER BY datum) balance,
 11    customerid
 12  FROM ACCOUNTS
 13  WHERE customerid = 'cus001';

DATUM         DEBIT     CREDIT    BALANCE CUSTOMERID
-------- ---------- ---------- ---------- ----------
13.12.07        100          0        100 cus001
20.12.07        200          0        300 cus001
23.12.07          0         50        250 cus001

SQL>
thx in advance
Reply With Quote
  #8 (permalink)  
Old 12-17-07, 21:07
itanacious itanacious is offline
Registered User
 
Join Date: Dec 2007
Posts: 5
sorry I just want to know what is counterpart of over()function in MySql
I'm just confuse how the Over() function works

I'm so Embarrass, sorry.. I'm Just Learning ORA, although I have some background in MySql.
Reply With Quote
  #9 (permalink)  
Old 12-17-07, 21:21
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
ah, into mysql

well, i am curious why you posted in the oracle forum

okay, moving your thread to the mysql forum

p.s. please stop creating new threads, it is just more work for us to merge them back into one

thanks
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

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