Lets say i have a table with the following data
Customer table:
Name amount date_created invoice_number
--------------------------------------------------
John 50 11April2012 12
Bob 150 15April2012 32
David 506 10May2012 52
Paul 80 12Aug2012 12
Mark 10 11Jan2012 52
Summary table:
Name amount
---------------------
Sally 250
Darren-32 150
I would like to select all the rows where date_created is between the start_date and end_date of the current_quarter. If date_created is within the current quarter i would like to append the invoice_number to the name before doing the insert statement (See example in the summary table above).
INSERT summary(name, amount)
SELECT name|| '-' || invoice_number, date_created, invoice_number
From Customer;
-
How can i modify the above to use either “Decode” function or a “Case” function (or any other “IF statement” type function) to check the value of date_created and append invoice_number if date_created is within the current quarter.
-
Obviously i will need to know the start and end dates of the current quarter and will need to store them somewhere before doing the comparison. Is this possible at all with pure SQL? PL/SQL is not an option.
Assuming we are in Q1 (Apr – Jun) the end result should be:
Name amount
---------------------
Sally 250
Darren-32 150
John-12 50
Bob-32 150
David-52 506
Paul 80
Mark 10
I am reading the Customer table from an Oracle 10G database and populating the summary into a “Summary” table which resides in a SQL server database. The fact that i am inserting into an SQL Server database should not really matter. I am reading the data from an Oracle database so the syntax should be Oracle compatible.
The Oracle syntax to do what you describe in your two bullet-points would be