表3中的总量取决于Table1(Ship_chr)和多个table2(quant * rate)条目。
实现此目标的sqlite3查询是什么?
table 1:
order_ID date buyer ship_chr
001 01/01 abc 15
002 05/01 xyz 10
table 2:
order_ID prod quantity rate
001 pen 50 2
001 paper 25 1
001 pin 50 2
002 paper 25 1
002 pen 100 2
table 3:
order_ID total_amount
001 240
002 235
答案 0 :(得分:2)
您可以使用内部join,sum和group by
select a.order_ID, sum(a.quantity * a.rate) + b.ship_chr as total_amount
from table2 a
inner join table1 b on a.order_ID = b.order_ID
group by a.order_ID, b.ship_chr
或使用suquery求和
select t1.order_ID, t1.tot + b.ship_chr total_amount
from table1 b
inner join (
select a.order_ID, sum(a.quantity * a.rate) tot
from table2 a
group by a.order_ID
) t1 on t1.order_ID = b.order_ID
答案 1 :(得分:1)
我认为您需要在下面
SQL> insert into test (col) values (chr(52103));
1 row created.
SQL> select * From test;
COL
--------------------
í
SQL>
在两个表之间进行联接,并使用 INSERT INTO t3
SELECT t1.order_ID, sum(t2.quantity*t2.rate)+t1.ship_chr as total_amount
FROM table_1 t1 join table_2 t2 on t1.order_ID=t2.order_ID
group by t1.order_ID,t1.ship_chr
聚合函数
答案 2 :(得分:1)
加入order_id
; SUM(t2.prod * t2.quantity)
通过order_id获得收入,AVG(ship_chr)
仅将ship_chr
添加一次。
SELECT t2.order_id AS order_id,
SUM(t2.prod * t2.quantity) + AVG(t1.ship_chr) AS total_amount
FROM table2 t2 LEFT JOIN table1 t1
ON t2.order_id = t1.ordeR_id
GROUP BY t2.order_id
答案 3 :(得分:1)
每种产品的total_amount
公式似乎是rate * quantity
+ ship_chr
(注意:缩写使代码难以理解)。
因为每个订单只有一个总价,所以不需要单独的表格。而是在orders
中添加一列。
alter table orders add column total_price integer
或者您可以即时计算。使用schema from my other answer ...
select sum(op.rate * op.quantity) + o.ship_chr
from order_products op
join orders o on o.id = op.order_id
group by op.order_id
group by op.order_id
将相同顺序的所有行组合在一起。然后,我们可以使用aggregate function sum
来添加所有具有相同order_id的行。
但是您必须确保其保持最新状态。您可以使用update
使用select
对所有update orders
set total_price = (
select sum(op.rate * op.quantity) + orders.ship_chr
from order_products op
where orders.id = op.order_id
group by op.order_id
)
的订单执行此操作,以将总价格作为subquery。
insert into order_products (order_id, product_id, quantity, rate) values
(1, 1, 100, 3);
update orders
set total_price = total_price + (100 * 3)
where id = 1;
在添加订单时,您必须记住要同时更新总数。您可以即时执行此操作...
insert into order_products (order_id, product_id, quantity, rate) values
(1, 1, 100, 3);
update orders
set total_price = (
select sum(op.rate * op.quantity) + orders.ship_chr
from order_products op
where orders.id = op.order_id
group by op.order_id
)
where id = 1;
但是在每次更改后重新计算整个订单可能更安全。
guess = int(input("Which door would you like to pick? "))
from random import randint
car = randint (1,3)
print ("You picked Door" + str (guess))
goat1 = input
goat2 = input
goat3 = input
if car and guess == 1:
goat1= str(input("There is a goat behind door 2, would you like to switch? "))
if goat1== "yes":
print ("The car was behind door 2. You lost.")
elif goat1 == "no":
print ("You win!")
elif car and guess == 2:
goat2 = str(input("There is a goat behind door #3, would you like to change your pick?"))
if goat2 == "yes":
print ("The car was behind door #2! You lost.")
elif goat2 == "no":
print ("The car was begind door #2! You win.")
elif car and guess == 3:
goat3 = input(str("There is a goat behind door #1. Would you like to change your pick?"))
if goat3 == "yes":
print ("The car was behind door #3. You lost.")
elif goat3 == "no":
print ("The car was behind door #3. You win!")
if car == 1 and guess == 2:
print ("Sorry, you lost. There is a goat behind Door #3")
elif car ==1 and guess == 3:
print ("Sorry, you lost. There is a goat behind Door #2")
elif car == 2 and guess == 1:
print ("Sorry, you lost. There is a goat behind Door #3")
elif car == 2 and guess == 3:
print ("Sorry, you lost. There is a goat behind Door #1")
elif car == 3 and guess == 1:
print ("Sorry, you lost. There is a goat behind Door #2")
elif car == 3 and guess == 2:
print ("Sorry, you lost. There is a goat behind Door #1")