2017-07-03 13 views
0

"書籍"カテゴリで利用可能なすべての製品のうち、過去3か月間に注文された割合はどれですか?注文した製品の割合を確認

ORDERS 
order_id 
customer_id 
order_date 
product_id 

PRODUCTS 
product_id 
product_name 
product_category 
product_price 

答えて

0

あなたがカウンタの割合を超えていると仮定します。値を比較する場合は、選択を少し下に変更する必要があります。 以下は私がそれをやる方法です。私はあなたがいくつかの構文エラーを持つかもしれないように私はそれを実行していないことに注意してくださいが、それは簡単に修正する必要があります。

WITH books AS (
    SELECT order_date 
    FROM orders 
    JOIN products 
     ON orders.product_id = products.product_id 
    WHERE products.product_category = 'books' 
), 
last_three_months AS (
    SELECT 0 as counter_1, count(*) AS counter_2 
    FROM books 
    WHERE order_date > DATEADD(MONTH, -3, GETDATE()); 
), 
total_orders AS (
    select count(*) AS counter_1, 0 as counter_2 
    FROM books 
), 
all_together AS (
SELECT counter_1, counter_2 
    FROM last_three_months 
UNION ALL 
SELECT counter_1, counter_2 
    FROM total_orders 
), 
sale_sums AS (
    SELECT SUM(counter_2) AS counter_2, SUM(counter_1) AS counter_1 
    FROM all_together 
) 
SELECT counter_2/counter_1 * 100 
FROM sale_sums 
0

この簡単なクエリを試してみてください。これは単純なPSQLです。シンプルな構文を学ぶと、自分でそれを行うことができます。

あなたは私たちにあなたが使用しているデータ・ベースを指示する必要が最初
WITH ordersLastThree as 
(
Select o.* FROM PRODUCTS p,ORDERS o where p.product_id=o.product_id and 
p.product_category = [Books product_category] 
and o.order_date >= DATEADD(MONTH, -3, GETDATE()) 
) 
Select (ISNULL((Select count(*) from ordersLastThree),1)*100)/count(o.*) as Result 
FROM PRODUCTS p,ORDERS o where p.product_id=o.product_id and 
p.product_category = [Books product_category] 
0

..

それMySQLとC#forntエンド

wiil、コードならば、それは...それに応じ をvarryます。.. SQLクエリによって返された行をdaatareaderに受け入れ、計算を適用します。あなたの疑問をクリアすることを願っています。

関連する問題