2015-09-18 29 views
0

我已經在YTD(迄今),QTD(季度到日期)和MTD(月迄今)的基礎上顯示總產品銷售。事情是我必須從那些只顯示一個。在選擇的基礎上只能看到一個輸出,就像我們有單選按鈕可以從衆多選項中選擇一個。這裏也給出一個輸入來選擇,並根據該輸入生成輸出。輸入可以是任何YTD,QTD或MTD。輸出是基於輸入生成的。我不知道如何計算一個輸出可以變化的列輸出。 我有一個產品表-1一列計算多個輸出

Product_ID  Product_name   Price 
1     Mobile    200 
2     T.V.    400 
3     Mixer    300 

我有一個Sales表像這個 -

Product_ID   Sales_Date   Quantity 
1     01-01-2015   30 
2     03-01-2015   40 
3     06-02-2015   10 
1     22-03-2015   30 
2     09-04-2015   10 
3     21-05-2015   40 
1     04-06-2015   40 
2     29-07-2015   30 
1     31-08-2015   30 
3     14-09-2015   30 

而且我的輸出中列包含3列是爲─ Product_id, Product_Name and Total_Amount。 列TOTAL_AMOUNT(數量*價格)要計算出售用戶即給定輸入的基礎上,

IF it is YTD then it should calculate the total sale from Starting Date of Year (01-01-2015) to the current_date(sysdate), 

IF it is QTD then in which quarter the current date is falling i.e if current month is september then from 1 July to current_date(sysdate), 

IF it is MTD then in which month the current date is falling to the current_date(sysdate). 

誰能幫助。謝謝!!!

回答

0
-- step 1 
create or replace view my_admin 
as 
select 'YTD' element, product_id, sum(quantity) sum_quantity 
from sales 
where Sales_date between trunc(sysdate,'Y') and sysdate 
group by product_id 
union 
select 'QTD', product_id, sum(quantity) sum_quantity 
from sales 
where Sales_date between trunc(sysdate,'Q') and sysdate 
group by product_id 
union 
select 'MTD', product_id, sum(quantity) sum_quantity 
from sales 
where Sales_date between trunc(sysdate,'MM') and sysdate 
group by product_id 



-- step 2 
select element, p.product_name, (sum_quantity * p.PRICE) agregate 
from my_admin a 
inner join products p on a.product_id = p.product_id 
where element = (:input) 
+0

該查詢不詢問應該計算哪個product_id(它只是計算 –

+0

對不起,我忘了加入產品;腳本已被修改,再加上價格已乘以 – ridi

+0

是的,它工作:)謝謝! ! –

0

我的推測是你有3個單選按鈕(在我的例子中是變量:YTD,:QTD,:MTD),其中只有一個值可以被用戶選中,其餘的將是空的。

您可以使用這樣的事情,讓你想要的東西:

select SUM(a.QTY*B.PRICE) from PRODUCTS a 
inner join SALES B on a.PRODUCT_ID=B.PRODUCT_ID 
where 
(:YTD is null or B.SALES_DATE between '01-JAN-15' and sysdate) 
and 
(:QTD is null or TO_CHAR(B.SALES_DATE, 'YYYY-Q')=TO_CHAR(sysdate, 'YYYY-Q')) 
and 
(:MTD is null or TO_CHAR(B.SALES_DATE, 'MM')=TO_CHAR(sysdate, 'MM')); 

你可以在這裏進行測試sqlfiddle

+0

查詢被給予同樣的結果對於所有3 :(並且我的數量是包含在銷售表中並且價格包含在產品表 –

+0

請在[sqlfiddle](http://sqlfiddle.com/)上創建表格和數據,以便我可以更正腳本。 –

+0

對不起,我是...試着但我不能創建它總是拋出錯誤我不知道爲什麼 –