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

时间:2019-03-03 03:26:10

标签: mysql sql

我已经创建了表格。我正在尝试创建一个查询,该查询将已售表中的sold_quantity乘以on_sale表中的sale_price并相加,现在称为R1,产品表中的Retail_price和已售表中的sold_quantity称为R2。
在查询中,我要计算我的收入。收获是有两个不同的日期,但有一个销售数量。这意味着我很难区分两种销售类型(折扣销售,零售销售)。
例如,在2月1日,我正在进行一次销售,我卖出10个数量,出售的价格为sale_price,日期保存为sale_date,请参阅On_sale表。在2月2日,我卖出了8个数量,但是卖出的价格保存为retail_price并保存为sold_date。

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)
);


这就是我想要的 样本数据

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

我的数据说明
: 这非常令人困惑。首先,我需要根据销售日期和销售日期显示年份,然后计算收入。例如,在2018年,收入是(on_sale表的sale_price中的2)+(12(3 * 4,3是sold_table中的sold_quantity,4是retail_price)=14。
城市大小由范围分隔,其中0>#<100小100> = x <200是中等,大于200的任何东西都大。括号中的城市名称仅用于帮助跟踪。城市基于商店表中的城市名称和州,通过比较已售表和商店表上的store_number来确定城市
 这要求我在查询后加入城市表以获取R1(正常价格)和R2(销售价格)。这就是我得到的。我很迷路

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

2 个答案:

答案 0 :(得分:1)

您可以在下面尝试-

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

答案 1 :(得分:0)

我不理解您的查询中使用@implementation UIWebView (KeyboardAppearanceAndAccessoryHiding) - (void) setModifiedWebviewView { // find the UIWebBrowserView for (UIView *browserView in self.scrollView.subviews) { if ([NSStringFromClass([browserView class]) hasPrefix:@"UIWebBrowserView"]) { // Allocate a UIWebBrowserView subclass Class newClass = objc_allocateClassPair([browserView class], "UIWebBrowserModified", 0); // Add a nil method to hide the accessory view IMP nilImp = [self methodForSelector:@selector(methodReturningNil)]; class_addMethod(newClass, @selector(inputAccessoryView), nilImp, "@@:"); // Add a method to set dark or light keyboard Method m = class_getInstanceMethod([self class], @selector(keyboardAppearance)); IMP keyboardAppearanceImp = method_getImplementation(m); const char* typeEncoding = method_getTypeEncoding(m); class_addMethod(newClass, @selector(keyboardAppearance), keyboardAppearanceImp, typeEncoding); // Replace the class of the UIWebBrowserView with the new subclass objc_registerClassPair(newClass); object_setClass(browserView, newClass); break; } } } - (id)methodReturningNil { return nil; } - (UIKeyboardAppearance)keyboardAppearance { return _s_isDark ? UIKeyboardAppearanceDark : UIKeyboardAppearanceLight; } @end // We also need to extend the text input traits @interface UITextInputTraits @end @interface UITextInputTraits (ForWebViewFields) @end @implementation UITextInputTraits (ForWebViewFields) - (UIKeyboardAppearance)keyboardAppearance { return _s_isDark ? UIKeyboardAppearanceDark : UIKeyboardAppearanceLight; } @end 。您似乎有一个定义明确的数据模型,并且ID应该对齐。

您专门说您想添加 LEFT JOINR1,因此您描述的查询如下:

R2

请注意,这会添加一个附加条件,表明日期与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; 表匹配。这对我来说很有意义。

将这两个收入数字相加对我来说非常可疑。我希望您会希望获得销售价格(如果存在)以及零售价格。如果是这样,那么计算将是:

on_sale

如果要折价金额的总和,则差额是有意义的。