简体   繁体   English

SQL查询总和,乘以并组合5个表然后分组

[英]SQL Query Sum, Multiply and combine 5 tables then group

I've created my table.我已经创建了我的表。 I'm trying to craate a query that multiply and add sold_quantity from sold table and sale_price from on_sale table and called it R1 for now, and retail_price from product table and sold_quantity from sold table called it R2 for now.我正在尝试创建一个查询,该查询从已售表中乘以并添加已售出数量和来自 on_sale 表中的销售价格,并暂时将其称为 R1,将产品表中的零售价格和已售表中的已售数量称为 R2。
In my query, I want to calculate my revenue.在我的查询中,我想计算我的收入。 The catch is there's two different date but one sale quantity.问题是有两个不同的日期但一个销售数量。 That means it's kinda hard for me to distinguish twO types of sales( discounted sale, retail sale).这意味着我很难区分两种类型的销售(打折销售、零售销售)。
For example, on Feb.1st, I have a sale going on, I sold 10 quantity, and price sold is as sale_price and date is saved as sale_date, refer to On_sale table.例如,2月1日,我有一个促销活动,我卖出了10件,售价为sale_price,日期保存为sale_date,参考On_sale表。 On Feb.2, I sold 8 quantity , but price sold is saved as retail_price and saved as sold_date. 2 月 2 日,我卖出了 8 个数量,但已售出的价格保存为零售价格并保存为已售出日期。

CREATE TABLE Sold (
  store_number int(16)  NOT NULL AUTO_INCREMENT,
  pid int(16) NOT NULL,
  sold_date date NOT NULL,
  sold_quantity int(16) NOT NULL,
  PRIMARY KEY (pid,store_number,sold_date)
);


 CREATE TABLE Store (
  store_number int(16)  NOT NULL AUTO_INCREMENT,
  phone_number varchar(16)  NOT NULL DEFAULT '0',
  street_address varchar(250) NOT NULL,
  city_name varchar(250) NOT NULL,
  state varchar(250) NOT NULL,
  PRIMARY KEY (store_number)
);
CREATE TABLE On_sale (
  pid int(16) NOT NULL,
  sale_date date NOT NULL,
  sale_price float(16) NOT NULL,
  PRIMARY KEY (pid,sale_date)
);

CREATE TABLE Product (
  pid int(16) NOT NULL,
  product_name varchar(250) NOT NULL,
  retail_price float(16) NOT NULL,
  manufacture_name varchar(250) NOT NULL,
  PRIMARY KEY (pid)
);
CREATE TABLE City (
  city_name varchar(250) NOT NULL,
  state varchar(250) NOT NULL,
  population int(16) NOT NULL DEFAULT '0',
  PRIMARY KEY (city_name,state)
);


This is what I want SAMPLE DATA这就是我想要的样本数据

STORE TABLES
store_number  phone_number  street_address city_name state 
     1           #             ###          New York    NY
     2           #             ###          HOUSTON     TX
     3           #             ###          L.A         CA


Sold Tables
store_number  PID  SOLD DATE  SOLD_QUANTITY  
     1         1      2/2/2017    3
     2         2      2/3/2018    3
     3         3      2/5/2019    4

On_sale Tables
PID  SALE_DATE    SALE PRICE  
1      2/4/2018    2

Product Tables
PID  PRODUCT NAME  RETAIL_PRICE manufacture_name
1       XX           5              XXX          
2      XX          4             XXX       
3       XX           3              XXX       


CITY TABLE

CITY_NAME  STATE    POPULATION  
New York   NY    100
HOUSTON    TX    200
L.A        CA    201

RESULT 

YEAR  REVENUE POPULATION
2017   15       (NEW YORK)SMALL 
2018   14        (HOUSTON)MEDIUM
2019   12       (L.A) LARGE

Explanation of my data我的数据说明
: This is very confusing. : 这很混乱。 First I need to display year based on sold date and sale date, then calculate revenue.首先我需要根据销售日期和销售日期显示年份,然后计算收入。 For example,in year 2018, the revneue is (2 from on_sale table's sale_price) + (12 (3 * 4, 3 is the sold_quantity from sold_table, and 4 is retail_price) = 14.例如,在 2018 年,revneue 是 (2 from_sale table's sale_price) + (12 (3 * 4, 3 is the sell_quantity from sell_table, and 4 is retail_price) = 14。
The city size is separated by ranges, where 0>#<100 is small 100>=x<200 is medium and anything above 200 is large.城市规模由范围分隔,其中 0>#<100 是小 100>=x<200 是中等,任何高于 200 的都是大。 the city name in the parenthesis is just to help track.括号中的城市名称只是为了帮助跟踪。 The city is based on the city name and state in store table, and that is doen by comparing store_number on both sold table and store table城市基于商店表中的城市名称和州,这是通过比较已售表和商店表上的 store_number 来完成的
This requires me to join city table after querying to get R1(normal price) and R2(on sale price).这需要我在查询后加入 city 表以获取 R1(正常价格)和 R2(销售价格)。 Here's what I got.这是我得到的。 I'm very lost我很失落

SELECT Sold.sold_quantity,
       Product.retail_price AS S1
    SUM(Product.retail_price*Sold.sold_quantity) AS R1

FROM  Sold LEFT JOIN Product
ON     Sold.pid=Product.pid

SELECT Sold.sold_quantity,
       On_sale.sale_price AS S2
    SUM(On_sale.sale_price) AS R2

FROM  Sold LEFT JOIN On_sale
ON     Sold.pid=On_sale.pid

You can try below -你可以试试下面——

SELECT year(sold_date) as yr,population
,SUM(Product.retail_price*Sold.sold_quantity) -SUM(On_sale.sale_price * Sold.sold_quantity) AS revenue
FROM  Sold 
LEFT JOIN Product ON  Sold.pid=Product.pid
LEFT JOIN On_sale ON Sold.pid=On_sale.pid
left join city on city.city_name=product.manufacture_name 
group by year(sold_date),population

I don't understand the use of LEFT JOIN in your queries.我不明白在您的查询中使用LEFT JOIN You seem to have a well-defined data model and the ids should line up.您似乎有一个明确定义的数据模型,并且 id 应该对齐。

You specifically say that you want to add R1 and R2 , so the query you describe looks like this:您特别说要添加R1R2 ,因此您描述的查询如下所示:

SELECT year(s.sold_date) as yr, c.population,
       (SUM(p.retail_price * s.sold_quantity) +
        SUM(os.sale_price * s.sold_quantity)
       ) AS revenue
FROM  Sold s JOIN
      Product p
      ON s.pid = p.pid JOIN
      city c
      ON c.city_name = p.manufacture_name LEFT JOIN
      On_sale os
      ON s.pid = os.pid AND
         os.sale_date = s.sold_date
GROUP BY year(s.sold_date), c.population
ORDER BY year(s.sold_date), c.population;

Note that this adds an extra condition saying that the dates match for the on_sale table.请注意,这会添加一个额外的条件,说明日期与on_sale表匹配。 That makes sense to me.这对我来说很有意义。

Adding these two revenue numbers seems very suspicious to me.将这两个收入数字相加对我来说似乎很可疑。 I would expect that you would want the sales price, if it exists, and otherwise the retail price.我希望您需要销售价格(如果存在),否则需要零售价格。 If so then the calculation would be:如果是这样,那么计算将是:

       SUM(COALESCE(os.sale_price, p.retail_price) * s.sold_quantity) AS revenue

If you want the sum of the discount amounts, then the difference makes sense.如果您想要折扣金额的总和,那么差异是有道理的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM