- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有三个表(pagu、realisasi 和 perencanaan)。我正在使用左连接。我想在特定日期之前对一个字段求和,而在该日期之前和该日期对另一个字段求和。
我的 pagu 表:
idpagu kode komponen pagu target_kinerja tahun keterangan
1 2217.057.056 Pengembangan Sistem Manajemen Mutu UPTP 52740000 1 2017 tes123
2 2220.051.051 Pelatihan Peningkatan Produktivitas 4732755000 120 2017 tes123
3 2220.052.051 Calon Wirausaha Baru yang Dilatih (RM) 12464938000 240 2017 TES123
我的现实表:
idrealisasi idpagu kode komponen realisasi target_kinerja tgl keterangan
5 1 2217.057.056 Pengembangan Sistem Manajemen Mutu UPTP 52740000 1 1/14/2017 tes
6 2 2220.051.051 Pelatihan Peningkatan Produktivitas 10000000 10 1/14/2017 tes
7 2 2220.051.051 Pelatihan Peningkatan Produktivitas 20000000 20 2/14/2017 tes2
8 2 2220.051.051 Pelatihan Peningkatan Produktivitas 10000000 10 3/14/2017 tes3
9 2 2220.051.051 Pelatihan Peningkatan Produktivitas 10000000 10 6/14/2017 tes4
10 2 2220.051.051 Pelatihan Peningkatan Produktivitas 10000000 10 1/15/2017 tes
我的perencanaan表(无记录):
idperencanaan idpagu kode komponen penarikan target_kinerja tgl keterangan
我的查询:
select p.idpagu,p.kode,p.komponen,p.pagu,p.target_kinerja,
if(pr.penarikan is null,0,sum(pr.penarikan)) as perenuang,
if(pr.target_kinerja is null,0,sum(pr.target_kinerja)) as perenfis,
if(r.realisasi is null,0,sum(r.realisasi)) as realuang,
if(r.target_kinerja is null,0,sum(r.target_kinerja)) as realfis,
p.pagu-if(r.realisasi is null,0,sum(r.realisasi)) as sisauang,
p.target_kinerja-if(r.target_kinerja is null,0,sum(r.target_kinerja)) as sisafis,
if(sum(rr.realisasi) is null,0,sum(rr.realisasi)) as tes
from pagu p
left join realisasi r on p.idpagu=r.idpagu and r.tgl BETWEEN "2017-01-01" and "2017-01-15"
left join perencanaan pr on p.idpagu=pr.idpagu and r.tgl BETWEEN "2017-01-01" and "2017-01-15"
left join realisasi rr on p.idpagu=rr.idpagu and rr.tgl < "2017-01-15"
group by p.idpagu,r.idpagu,rr.idpagu
我的结果:
idpagu kode komponen pagu target_kinerja perenuang perenfis realuang realfis sisauang sisafis tes
1 2217.057.056 Pengembangan Sistem Manajemen Mutu UPTP 52740000 1 0 0 52740000 1 0 0 52740000
2 2220.051.051 Pelatihan Peningkatan Produktivitas 4732755000 120 0 0 20000000 20 4712755000 100 20000000
3 2220.052.051 Calon Wirausaha Baru yang Dilatih (RM) 12464938000 240 0 0 0 0 12464938000 240 0
我的问题:
tes 列是当前日期“2017-01-15”之前的 pagu - realuang,第二行应该是 10.000.000,但我的查询给出了 20.000.000。如何编写查询,以便在第二条记录中,tes 列为 10.000.000?
我的模式:
SET FOREIGN_KEY_CHECKS=0;
DROP TABLE IF EXISTS `pagu`;
CREATE TABLE `pagu` (
`idpagu` int(11) NOT NULL AUTO_INCREMENT,
`kode` varchar(45) NOT NULL,
`komponen` varchar(250) NOT NULL,
`pagu` bigint(20) NOT NULL,
`target_kinerja` int(11) NOT NULL,
`tahun` varchar(4) NOT NULL,
`keterangan` varchar(250) DEFAULT NULL,
PRIMARY KEY (`idpagu`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
INSERT INTO `pagu` VALUES ('1', '2217.057.056', 'Pengembangan Sistem Manajemen Mutu UPTP', '52740000', '1', '2017', 'tes123');
INSERT INTO `pagu` VALUES ('2', '2220.051.051', 'Pelatihan Peningkatan Produktivitas', '4732755000', '120', '2017', 'tes123');
INSERT INTO `pagu` VALUES ('3', '2220.052.051', 'Calon Wirausaha Baru yang Dilatih (RM)', '12464938000', '240', '2017', 'TES123');
DROP TABLE IF EXISTS `perencanaan`;
CREATE TABLE `perencanaan` (
`idperencanaan` int(11) NOT NULL AUTO_INCREMENT,
`idpagu` int(11) NOT NULL,
`kode` varchar(45) NOT NULL,
`komponen` varchar(250) NOT NULL,
`penarikan` bigint(20) NOT NULL,
`target_kinerja` int(11) NOT NULL,
`tgl` date NOT NULL,
`keterangan` varchar(250) DEFAULT NULL,
PRIMARY KEY (`idperencanaan`),
KEY `fk_perencanaan_pagu1_idx` (`idpagu`),
CONSTRAINT `fk_perencanaan_pagu1` FOREIGN KEY (`idpagu`) REFERENCES `pagu` (`idpagu`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `realisasi`;
CREATE TABLE `realisasi` (
`idrealisasi` int(11) NOT NULL AUTO_INCREMENT,
`idpagu` int(11) NOT NULL,
`kode` varchar(45) NOT NULL,
`komponen` varchar(250) NOT NULL,
`realisasi` bigint(20) NOT NULL,
`target_kinerja` int(11) NOT NULL,
`tgl` date NOT NULL,
`keterangan` varchar(250) DEFAULT NULL,
PRIMARY KEY (`idrealisasi`),
KEY `fk_perencanaan_pagu1_idx` (`idpagu`),
CONSTRAINT `fk_perencanaan_pagu10` FOREIGN KEY (`idpagu`) REFERENCES `pagu` (`idpagu`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;
INSERT INTO `realisasi` VALUES ('5', '1', '2217.057.056', 'Pengembangan Sistem Manajemen Mutu UPTP', '52740000', '1', '2017-01-14', 'tes');
INSERT INTO `realisasi` VALUES ('6', '2', '2220.051.051', 'Pelatihan Peningkatan Produktivitas', '10000000', '10', '2017-01-14', 'tes');
INSERT INTO `realisasi` VALUES ('7', '2', '2220.051.051', 'Pelatihan Peningkatan Produktivitas', '20000000', '20', '2017-02-14', 'tes2');
INSERT INTO `realisasi` VALUES ('8', '2', '2220.051.051', 'Pelatihan Peningkatan Produktivitas', '10000000', '10', '2017-03-14', 'tes3');
INSERT INTO `realisasi` VALUES ('9', '2', '2220.051.051', 'Pelatihan Peningkatan Produktivitas', '10000000', '10', '2017-06-14', 'tes4');
INSERT INTO `realisasi` VALUES ('10', '2', '2220.051.051', 'Pelatihan Peningkatan Produktivitas', '10000000', '10', '2017-01-15', 'tes');
最佳答案
根据您的查询和表名和列名进行猜测(pagu = ceiling,realisasi = actual,perencanaan = planned),对于每个上限,您将在特定范围内的几天内计算其实际值、这些天的计划值、它的其中一些日期的实际值,以及一些相关的目标值,将 NULL 值视为 0。
我假设如果上限在某个日期具有实际值,那么它在该日期具有计划值。否则,您的查询将涉及 FULL JOIN,而 LEFT JOIN 有时会错误地形成具有不同日期的行等。所以我怀疑您缺少 FK(外键)。
您的第一个 LEFT JOIN 获取某些日期每个上限的实际值。那么您想要一个上限和日期的任何计划值。所以您缺少 和 r.tgl = pr.tgl
。然后,您需要其中一些日期的上限和日期 的任何实际值。所以您的第三个 LEFT JOIN 缺少 和 r.tgl = rr.tgl
。
您的分组和汇总不正确。如果 SELECT 在聚合调用之外使用一个列,那么它对于 GROUPed 列的每个子行应该是单值的。 (GROUP BY 对 GROUPed 列的每个子行返回一行。)(SET sql_mode = 'ONLY_FULL_GROUP_BY'
不允许 MySQL 的非标准行为。)
select p.idpagu, count(*), p.pagu, p.target_kinerja,
sum(coalesce(pr.penarikan,0)) as pr_penarikan,
sum(coalesce(pr.target_kinerja,0)) as pr_target_kinerja,
sum(coalesce(r.realisasi,0)) as r_realisasi,
sum(coalesce(r.target_kinerja,0)) as r_target_kinerja,
p.pagu-sum(coalesce(r.realisasi,0)) as d_realisasi,
p.target_kinerja-sum(coalesce(r.target_kinerja,0)) as d_target_kinerja,
sum(coalesce(rr.realisasi,0)) as rr_realisasi
from pagu p
left join realisasi r on p.idpagu=r.idpagu and r.tgl BETWEEN "2017-01-01" and "2017-01-15"
left join perencanaan pr on p.idpagu=pr.idpagu and r.tgl = pr.tgl and r.tgl BETWEEN "2017-01-01" and "2017-01-15"
left join realisasi rr on p.idpagu=rr.idpagu and r.tgl = rr.tgl and r.tgl < "2017-01-15"
group by p.idpagu, p.pagu, p.target_kinerja;
idpagu pagu pr_penarikan r_realisasi d_realisasi rr_target_kinerja
count(*) target_kinerja pr_target_kinerja r_target_kinerja d_target_kinerja
1 1 52740000 1 0 0 52740000 1 0 0 52740000
2 2 4732755000 120 0 0 20000000 20 4712755000 100 10000000
3 1 12464938000 240 0 0 0 0 12464938000 240 0
关于MySQL LEFT JOIN 在某个日期之前与 SUM 以及在该日期之前和该日期的 SUM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44902328/
我有这个示例代码: #include #include int main() { Eigen::MatrixXf M = Eigen::MatrixXf::Random(1000, 1000)
我有一个像这样的数据框: +-----+--------+ |count| country| +-----+--------+ | 12| Ireland| | 5|Thailand| +-
我想要 SUM(tot_bill_1+tot_bill_2) AS 总计,但这不起作用 SELECT *, IF(SUM(bill_1) IS NULL, '99', SUM(bill_1)) AS
如果我们有两个矩阵 X 和 Y,都是二维的,现在在数学上我们可以说:sum(X-Y)=sum(X)-总和(Y). Matlab 哪个效率更高?哪个更快? 最佳答案 在我的机器上,sum(x-y) 对于
我正在运行 Hive 1.1.0 并看到对于两个 bigint 列,active_users 和 inactive_users,SUM(active_users + inactive_users) <
是否可以在一个选择查询中求和? 类似这样的事情: SELECT id, SUM(current_price - bought_price)*amount AS profit FROM purchase
这是一个相当奇怪的结果。我希望这些具有相同的产量。 下面还有从数据库中提取的 excel 链接。 https://twentius.opendrive.com/files?89038281_muoyg
我必须对 2 个字段求和,然后再求和。从性能的角度来看,先添加字段还是在对列求和之后添加字段有什么区别? 方法 1 = SELECT SUM(columnA + columnB) 方法 2 = SEL
这是一个经典问题,但我很好奇是否有可能在这些条件下做得更好。 问题:假设我们有一个长度为4*N的排序数组,即每个元素重复4次。请注意,N 可以是任何自然数。此外,数组中的每个元素都受制于 0 A. 执
我正在编写一个 Pig 程序,该程序加载一个用制表符分隔整个文件的文件 例如:名称 TAB 年份 TAB 计数 TAB... file = LOAD 'file.csv' USING PigStora
我有一个包含以下字段的表: EmpID, Code, Amount, TransDate, CM, CMDate 我想要进入数据网格的是 SUM所有的Amount具有相同的 Code和 SUM CM具
我有两个单独的查询用于提取报告信息。一年效果很好。但是,如果一个月超过 1 年,则不会显示正确的响应。 这是我的两个查询: select SUM(rpt_complete.total) total,
我想查询一个团队的积分。通过在列上执行 SUM + 来自具有相同团队 ID 的另一个表的 SUM 来添加这些点。我试着这样写: SELECT k.id, s.fylke, s.
这个问题在这里已经有了答案: How to deal with floating point number precision in JavaScript? (47 个回答) Unexpected
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎与 help center 中定义的范围内的编程无关。 . 关闭 5 年前。 Improve
我已经找了一段时间,但找不到这个问题的答案(也许我没有搜索正确的术语或其他东西)。基本上,我有一个数据库,每个日期有任意数量的条目。我需要取包含条目的最后 X 天的总和(忽略没有条目的天数)。我知道如
我正在尝试获取 B 行中包含 A 行中某个值的所有值中的一些值。我猜这个问题很简单。 这是我的查询: =QUERY('Sheet1'!$A$16:D, "Select sum(D) Where C c
我正在尝试运行以下查询,但出现以下错误: You have an error in your SQL syntax; check the manual that corresponds to your
我有一个 tableA,其中包含以下结构 我将此结构修改为如下所示的tableB,以减少行数,并且类别是固定长度的 假设我在 tableA 中修改为新结构后有 210 万条数据,tableB 仅包含
我的表在 Postgres 中的数据: id user_id sell_amount sell_currency_id buy_amount buy_currency_id type
我是一名优秀的程序员,十分优秀!