- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个像这样的表架构。
mysql> desc material_out;
+-------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+-------+
| id | int(11) | NO | | 0 | |
| barcode | varchar(20) | NO | | NULL | |
| name | varchar(100) | NO | | NULL | |
| qty | double | YES | | NULL | |
| unit | varchar(20) | YES | | NULL | |
| num_letter | varchar(30) | YES | | NULL | |
| date | date | YES | | NULL | |
| destination | varchar(50) | YES | | NULL | |
+-------------+--------------+------+-----+---------+-------+
8 rows in set (0.00 sec)
mysql> desc material_in;
+-------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+-------+
| id | int(11) | NO | | 0 | |
| barcode | varchar(20) | NO | | NULL | |
| name | varchar(100) | NO | | NULL | |
| qty | double | YES | | NULL | |
| unit | varchar(20) | YES | | NULL | |
| num_letter | varchar(30) | YES | | NULL | |
| date | date | YES | | NULL | |
| destination | varchar(50) | YES | | NULL | |
+-------------+--------------+------+-----+---------+-------+
8 rows in set (0.00 sec)
mysql> desc goods;
+-------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+-------+
| id | int(11) | NO | | 0 | |
| barcode | varchar(20) | NO | | NULL | |
| name | varchar(100) | NO | | NULL | |
| unit | varchar(20) | YES | | NULL | |
| category | varchar(25) | YES | | NULL | |
| first_stok | double ) | YES | | NULL | |
+-------------+--------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
在表material_out中,我有10,000行数据。拥有多达350种条码。在表 Material_in 中,我有大约 15,000 行的数据。条码种类多达200种。
我的查询是这样的
SELECT br.barcode,
COALESCE(tNEW.total_out,0) AS total_out, COALESCE(tNEW.total_in,0) as total_in,
COALESCE(tNEW.total_in,0)-COALESCE(tNEW.total_out,0) AS result,
COALESCE(tOLD.total_out,0) AS total_out_old, COALESCE(tOLD.total_in,0) AS total_in_old
FROM (
SELECT barcode from goods where category=1
) as br
LEFT JOIN (
SELECT goods.barcode,
COALESCE(SUM(tOUT.qty),0) AS total_out,
COALESCE(SUM(tIN.qty),0) AS total_in
FROM goods
LEFT JOIN material_out AS tOUT ON tOUT.barcode=goods.barcode
LEFT JOIN material_in AS tIN ON tIN.barcode=goods.barcode
WHERE goods.category=1
AND tOUT.date >='2013-05-01' AND tOUT.date <='2013-08-31'
AND tIN.date >= '2013-05-01' AND tIN.date <= '2013-08-31'
GROUP BY goods.barcode
) AS tNEW ON tNEW.barcode=br.barcode
LEFT JOIN (
SELECT goods.barcode,
SUM(tOUT.qty) AS total_out,
SUM(tIN.qty) AS total_in
FROM goods
LEFT JOIN material_out AS tOUT ON tOUT.barcode=goods.barcode
LEFT JOIN material_in AS tIN ON tIN.kode=goods.barcode
WHERE goods.category=1
AND tOUT.date BETWEEN '2013-01-01' AND '2013-04-31'
AND tIN.date BETWEEN '2013-01-01' AND '2013-04-31'
GROUP BY goods.barcode
) AS tOLD ON tOLD.barcode=br.barcode
我曾经使用过这样的查询。结果进展顺利。但是当我尝试输入新数据时。然后我重新查询你的显示。但结果与输入的数据不符。例如,当我输入您的查询时。结果如下
+--------------+-----------+----------+------------+---------------+-------------------+
| barcode | total_out | total_in | result | total_out_old | total_in_old |
+--------------+-----------+----------+------------+---------------+-------------------+
| TNWET021 | 6195 | 15000 | 8805 | 20085 | 46200 |
| TNWET020 | 3420 | 7650 | 4230 | 4860 | 23925 |
| TNWET019 | 8370 | 25200 | 16830 | 11610 | 47175 |
| TNWET018 | 18690 | 44100 | 25410 | 13800 | 54150 |
| TNWET017 | 1140 | 3750 | 2610 | 3690 | 16200 |
| TNWET016 | 19500 | 56100 | 36600 | 31725 | 111300 |
| TNWET015 | 5145 | 18150 | 13005 | 6510 | 23400 |
| TNWET014 | 33300 | 65250 | 31950 | 96300 | 262500 |
| TNWET013 | 1170 | 5625 | 4455 | 3690 | 13200 |
| TNWET012 | 720 | 2700 | 1980 | 3870 | 13800 |
| TNWET011 | 0 | 0 | 0 | 180 | 450 |
| TNWET010 | 0 | 0 | 0 | 405 | 1125 |
| TNWET009 | 0 | 0 | 0 | 0 | 0 |
当我进行手动求和时,结果如下。
mysql> select sum(qty) from material_in where barcode='TNWET021' and date BETWEEN '2013-05-01' AND '2013-08-31';
+-------------+
| sum(qty) |
+-------------+
| 750 |
+-------------+
1 row in set (0.00 sec)
mysql> select sum(qty) from material_in where barcode='TNWET020' and date BETWEEN '2013-05-01' AND '2013-08-31';
+-------------+
| sum(qty) |
+-------------+
| 450 |
+-------------+
1 row in set (0.00 sec)
当数据对于报告非常重要时,为什么结果可能会有很大不同。请帮助我。
我之前用过这个,但是执行起来花了很长时间。也许你可以帮助总结这个查询以快速执行
SELECT COALESCE(tIN.total_in,0) + COALESCE(production.total_prod,0) AS incoming, COALESCE(tOUT.total_out,0) AS expenditure,br.barcode as barcode, br.name,br.initial_stock,br.unit,COALESCE(adj.total,0) AS adjusment,COALESCE(tIN.total_in,0) + COALESCE(production.total_prod,0) + COALESCE(adj.total,0) + COALESCE(br.intial_stock,0) - COALESCE(tOUT.total_out,0) as final_stok,so.stock_opname from (
select barcode,name,initial_stock,unit from barang where category=1
) as br
LEFT JOIN (
select (select sum(qty) from material_out where date >= '2013-05-01' AND date <='2013-08-31' and barcode=a.barcode) as total_out,a.barcode from material_out a group by a.barcode
) as tOUT
ON tOUT.barcode=br.barcode
LEFT JOIN
(
SELECT(
SELECT SUM(qty) FROM adjusment
WHERE status = '+' AND date >= '2013-05-01' AND date <= '2013-08-31'
) - (
SELECT SUM(qty) FROM adjusment
WHERE status = '-' AND date >= '2013-05-01' AND date <= '2013-08-31'
) AS total,barcode FROM adjusment
GROUP BY barcode
) AS adj
ON br.barcode = adj.barcode
LEFT JOIN (
select (select sum(qty) from material_in where date >= '2013-05-01' AND date <='2013-08-31' and barcode=a.barcode) as total_in,a.barcode,a.nama from material_in a group by a.barcode
) as tIN
ON br.barcode=tIN.barcode
LEFT JOIN (
select (select sum(qty) from view_production where date >= '$start' AND date <='$end' and kode=a.kode) as total_prod,a.barcode from view_production a group by a.barcode
) as production
ON br.barcode=production.barcode
LEFT JOIN (
select(select sum(qty) from stock_opname where date >= '2013-04-01' AND date <= '2013-05-31' AND barcode=a.barcode) as stok_opname,a.barcode from pencacahan a group by a.barcode
) as so
ON br.barcode=so.barcode
最佳答案
您应该编写tIN.barcode=goods.barcode
,而不是tIN.kode=goods.barcode
。
你能试试这个吗?
SELECT goods.barcode,
tOUT.qty,
tIN.qty
FROM goods
LEFT JOIN material_out AS tOUT ON tOUT.barcode=goods.barcode
LEFT JOIN material_in AS tIN ON tIN.barcode=goods.barcode
WHERE goods.category=1
AND tOUT.date >='2013-05-01' AND tOUT.date <='2013-08-31'
AND tIN.date >= '2013-05-01' AND tIN.date <= '2013-08-31'
我有一个假设。 (见评论。)您将看到相同数量的次数比您预期的要多。
如果我是对的,那么你应该首先聚合所有部分结果,然后再加入它们。
关于mysql - 结果总和不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19333272/
我基本上有三个表: hunt_c_usershunt_c_collected_eggshunt_c_achievements 我目前只使用 hunt_c_users 和 hunt_c_collecte
我已经计算了不同表中计数的总和。这会执行两次,每个 performanceID 一次。现在我想得到两个总和的总和。 下面是我目前做的两个总和的代码: SELECT SUM((COUNT (Bo
我有一个对 2 个值求和的脚本。我计划添加更多值(value),但首先我需要让它发挥作用。人们告诉我给他们 NUMBER 值,所以我这样做了,但现在它甚至没有给出输出。 base = 0; $("#F
我正在尝试计算在我们的数据库中跟踪的花费总额。每个订单文档包含一个字段“total_price” 我正在尝试使用以下代码: db.orders.aggregate({ $group: {
给定 Excel 2013(或更高版本)中的 2 个命名表: tbl发票 ID InvRef Total 1 I/123 45 2 I/234
希望你们一切都好。我来这里是因为我从今天早上开始就试图解决一个问题,我再也受不了了。 这就是上下文:我有一个 excel 工作簿,其中有不同的工作表,其中包含不同国家/地区的不同商业计划。我的目标是制
我有一份报告显示客户订购的产品及其价格: CompanyA Product 7 14.99 CompanyA Product 3 45.95 CompanyA Prod
我使用此python客户端: https://github.com/ryananguiano/python-redis-timeseries 如何汇总所有匹配? ts = TimeSeries(cli
希望创建一个总和和计数公式,该公式将自动调整以适应范围内插入的新行。 例如,如果我在单元格 D55 中有公式 =SUM(D17:D54)。每次我在该范围内插入新行时,我都需要更改公式的顶部范围来解释它
所以,我需要聚合日期相同的行。 到目前为止,我的代码返回以下内容: date value source 0 2018-04-08 15:52:26.1
我有数字输入 数量约为 30 我需要将它们全部汇总到一个字段 我拥有的在下面 查看:
您好,我正在尝试根据以下数据计算过去三个月中出现不止一次的不同帐户 ID 的数量;我想要 2 作为查询结果,因为 test1@gmail.com 和 test2@gmail.com 出现超过 1 次。
我有两个带有以下字段的表: ... orders.orderID orders.orderValue 和 payments.orderID payments.payVal 在 payments.pay
我想按 image_gallery 和 video_gallery 两列的 DESC 进行排序。 SELECT b.*, c.title as category, (S
实际上我的原始数据库为 SELECT sum(data1,data2) as database_value,sum(data3,data4) as database_not_value from t
我试图获取三个分数中每一个的值并将它们相加并显示在“总计:”中。我的问题是,我不知道如何做到这一点,以便每次其中一个分数值发生变化时,相应的总分值也会随之变化。 我可以在某处调用“onchange”来
如何获得按第一个值分组的元组列表中第二个和第三个值的总和? 即: list_of_tuples = [(1, 3, 1), (1, 2, 4), (2, 1, 0), (2, 2, 0)] expec
我正在尝试将我的列表中的整数转换为列表的总和和平均值,并说明任何低于冰点 F<32 的温度。每当我尝试获取总和或平均值时,我都会收到错误提示“+: 'int' 和 'str' 不支持的操作数类型”。我
在我的 ios 项目中,我使用了两个实体 (CoreData):具有一对多关系的 Person 和 Gifts 我知道如何计算给一个人的礼物总和: NSDecimalNumber *orderSum=
我有两个表(输入和类别): CREATE TABLE categories ( iId INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, sNam
我是一名优秀的程序员,十分优秀!