在“图书”类别中提供的所有产品中,过去3个月内订购的百分比是多少?
ORDERS
order_id
customer_id
order_date
product_id
PRODUCTS
product_id
product_name
product_category
product_price
答案 0 :(得分: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
答案 1 :(得分: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]
答案 2 :(得分:0)
首先你必须告诉我们你正在使用的数据库..
它会相应变化.. 如果它是Mysql和C#fornt结束那么代码wiil
....通过sql查询接受返回的行到daatareader然后应用计算。我希望它能清除你的怀疑