我有一个看起来像这样的数据集。
其中business_day
指示是否是transaction_created_date
工作日。我试图line_amount
对假期或周末发生的值进行求和,以便将其添加到下一个工作日,如下所示:
本质上,如果我可以捕获下一个工作日,business_day = 0
那么我就可以对分区进行求和。
鉴于此表:
CREATE TABLE Table1
(
[Classroom] int,
[CourseName] varchar(8),
[Lesson] varchar(9),
[StartTime] char(4),
[EndTime] char(4)
);
然后:
INSERT INTO Table1
(
[Classroom],
[CourseName],
[Lesson],
[StartTime],
[EndTime]
)
VALUES
(1001, 'Course 1', 'Lesson 1', '0800', '0900'),
(1001, 'Course 1', 'Lesson 2', '0900', '1000'),
(1001, 'Course 1', 'Lesson 3', '1000', '1100'),
(1001, 'Course 1', 'Lesson 6', '1100', '1200'),
(1001, 'Course 2', 'Lesson 10', '1100', '1200'),
(1001, 'Course 2', 'Lesson 11', '1200', '1300'),
(1001, 'Course 1', 'Lesson 4', '1300', '1400'),
(1001, 'Course 1', 'Lesson 5', '1400', '1500');
我的查询是:
With A AS
(
SELECT
ClassRoom
CourseName
StartTime
EndTime
PrevCourse = LAG(CourseName, 1, CourseName) OVER (ORDER BY StartTime)
FROM Table1
), B AS (
SELECT
ClassRoom
CourseName
StartTime
EndTime
Ranker = SUM(CASE WHEN CourseName = PrevCourse THEN 0 ELSE 1 END)
OVER (ORDER BY StartTime, CourseName)
FROM A
)
SELECT B.* FROM B;
这给了我以下结果:
ClassRoom CourseName StartTime EndTime Ranker
1001 Course 1 0800 0900 0
1001 Course 1 0900 1000 0
1001 Course 1 1000 1100 0
1001 Course 1 1100 1200 0
1001 Course 2 1100 1200 1
1001 Course 2 1200 1300 1
1001 Course 1 1300 1400 2
1001 Course 1 1400 1500 2
请关注排名列。如果我没有误解,在当前课程与上一课程不同的每一行,然后 sum(1); 下一行,当前课程 == 上一课程,然后是 sum(0),所以我对排名的期望应该是:(0,0,0,0), (1,1), (1,1) 但它给我(0,0,0,0),(1,1),(2,2)。为什么最后它给了我 (2, 2)?还是我错过了什么?
所以我需要对 debit_id 和 transfer_id 相等的值“金额”求和: 表示例:
现在我使用这个查询给了我几乎预期的值
WITH ct AS
( SELECT *,rn = ROW_NUMBER() OVER (PARTITION BY debit_id, transfer_id
ORDER BY id DESC)
FROM transfer_debits
)
SELECT debit_id, transfer_id, amount, debit_type
FROM ct
WHERE rn = 1 AND transfer_id = 861288
ORDER BY debit_id, transfer_id;
我需要这个结果,但是按 debit_id 和 transfer_id 分组的字段金额的总和所需的结果应该是:
我有表Inventory,我想sum(quantity)
按列 gid、版本、仓库分组
+----------+----------+-----------+----------+--------------+
| gid | version | warehouse| quantity | description |
+----------+----------+-----------+----------+--------------+
| gid1 + 1 + 1 + 100 + name 1 +
| gid1 + 1 + 1 + 200 + name 1 +
| gid2 + 1 + 2 + 200 + name 2 +
+----------+----------+-----------+----------+--------------+
以下查询有效,因为它返回 2 条记录的总和:
SELECT gid, version, warehouse, SUM(quantity) as total
FROM Inventory
GROUP BY gid, version, warehouse;
+----------+----------+-----------+----------+
| gid | version | warehouse| quantity |
+----------+----------+-----------+----------+
| gid1 + 1 + 1 + 300 +
| gid2 + 1 + 2 + 200 +
+----------+----------+-----------+----------+
我还尝试description
在记录中添加该列:
SELECT gid, version, warehouse, SUM(quantity) as total, description
FROM Inventory
GROUP BY gid, version, warehouse;
但我收到一条错误消息:
列“描述”在选择列表中无效,因为它既不包含在聚合函数中,也不包含在 GROUP BY 子句中
我搜索了它,最后得到了这个查询:
SELECT T1.gid
,T1.description
,T1.version
,T1.warehouse
,T2.quantity
FROM Inventory T1
INNER JOIN (
SELECT gid, version, warehouse, SUM(quantity) AS quantity
FROM Inventory
GROUP BY gid, version, warehouse
) T2
ON T1.gid = T2.gid
它显示了正确的总和,但它返回 3 条记录:
+----------+----------+-----------+----------+--------------+
| gid | version | warehouse| quantity | description |
+----------+----------+-----------+----------+--------------+
| gid1 + 1 + 1 + 300 + name 1 +
| gid1 + 1 + 1 + 300 + name 2 +
| gid2 + 1 + 2 + 200 + name 2 +
+----------+----------+-----------+----------+--------------+
我怎样才能只得到 2 个箭头?以下是所需的输出:
+----------+----------+-----------+----------+--------------+
| gid | version | warehouse| quantity | description |
+----------+----------+-----------+----------+--------------+
| gid1 + 1 + 1 + 300 + name 1 +
| gid2 + 1 + 2 + 200 + name 2 +
+----------+----------+-----------+----------+--------------+
我的数据库在 SQL Server Express 2008 中。
我有一张工作付款表,当与交易匹配时,需要为同一客户匹配当天的付款,这等于当天的单笔交易。我已经能够匹配,但只返回汇总总数,而不是每一行。
我知道需要加入,但无法正确加入。
D B:
create table jobs(
jobid int(7),
datein date,
total numeric(5,2),
chargeto int(5)
);
create table payments(
payid int(7),
paymentamount numeric(5,2),
paymentjobno int(5),
paymentdate date,
paymenttype int(1)
);
insert into jobs (jobid, total) values (1000, 100, 4);
insert into jobs (jobid, total) values (1001, 50, 4);
insert into jobs (jobid, total) values (1002, 25, 4);
insert into jobs (jobid, total) values (1003, 220, 6);
insert into jobs (jobid, total) values (1004, 200, 7);
insert into payments (payid, paymentamount, paymentjobno, paymentdate, paymenttype) values (1, 100, 1000, 01/10/2019, 1);
insert into payments (payid, paymentamount, paymentjobno, paymentdate, paymenttype) values (2, 50, 1001, 01/10/2019, 1);
insert into payments (payid, paymentamount, paymentjobno, paymentdate, paymenttype) values (3, 25, 1002, 01/10/2019, 1);
insert into payments (payid, paymentamount, paymentjobno, paymentdate, paymenttype) values (4, 220, 1003, 01/10/2019, 1);
insert into payments (payid, paymentamount, paymentjobno, paymentdate, paymenttype) values (5, 200, 1004, 01/10/2019, 1);
我有代码:
$groupmatches = $con->query("
SELECT j1.*, j2.*
FROM jobs AS j1
JOIN (SELECT jobs.jobid, payments.paymentamount, payments.paymentjobno, COUNT(*) AS count
FROM (jobs LEFT JOIN payments ON payments.paymentjobno = jobs.jobid)
WHERE payments.paymentdate = '$dt' AND (payments.paymenttype = 1 OR payments.paymenttype = 2 OR payments.paymenttype = 4)
GROUP BY jobs.chargeto
HAVING SUM(payments.paymentamount) = '$at' AND COUNT(*) > 1) AS j2
ON j1.jobid = j2.paymentjobno;
");
它正在匹配并返回一行,其中包含多少行加起来匹配的计数值,但我无法返回每一行。我究竟做错了什么?
我查看了许多提出和回答的问题,但似乎找不到我的答案,甚至将提供的答案混杂到我的查询中 - 大多数情况下都会失败,其余的会提供垃圾结果。
请注意,这是针对 IBM DB2 v9.7 的! 不是SQL Server 或任何更新的东西。
我有一个包含准不同行的表,但没有键。根据所有字段,找到重复项的几率几乎为零,并且重复的概率随着查询中选择的字段越少而增加。在这一点上,我基本上可以接受。字段比下面列出的要多,我添加了一个键仅供参考...
select sum(max(NumImages)) from table where Source in ('A','B') and Event='190'
Key ID NumImages Source Event
1 1 4 A 190
2 1 - - 247
3 1 2 B 190
4 1 - - 134
5 2 7 C 190
6 2 2 A 190
7 2 1 B 190
8 2 5 A 190
9 3 5 A 190
10 3 - - 247
11 3 - - 134
12 3 - - 815
13 4 2 A 190
14 4 9 A 247
15 4 5 B 190
16 4 4 B 134
Source in ('A','B')
Event = '190'
NumImages is not null
(*我认为前面的参数不会发生这种情况)
ID Sum
1 6
2 6
3 5
4 7
因为:
我是 SQL 的新手,我有两个表,就像我的 PostgreSQL 数据库中的表一样:
表 1 - building_specs
+-----+---------+---------+--------+
| key | wallsId | floorId | roofId |
+-----+---------+---------+--------+
| 701 | 1 | 3 | 2 |
+-----+---------+---------+--------+
| 702 | 1 | 3 | 1 |
+-----+---------+---------+--------+
| 703 | 2 | 1 | 3 |
+-----+---------+---------+--------+
表 2 - spec_value
+-------+-------+-------+------+
| value | walls | floor | roof |
+-------+-------+-------+------+
| 1 | 4,8 | 5,4 | 3,6 |
+-------+-------+-------+------+
| 2 | 7,5 | 3,8 | 7,8 |
+-------+-------+-------+------+
| 3 | 3,6 | 10,1 | 4,1 |
+-------+-------+-------+------+
我需要根据spec_value计算building_specskey
中每个的最终值values
例如,final_value
对于表 1 中的第 701 行:
-> wallsId
= 1 -> 参考表 2 栏中的值4.8walls
-> floorId
= 3 -> 参考表 2 栏中的值10.1floor
-> roofId
= 2 -> 参考表 2 栏中的值7.8roof
final_value
然后,它应该对表 2中的所有参考值求和并获得22.7的 a
所以,我期望的回报是这样的:
+-----+-------------+
| key | final_value |
+-----+-------------+
| 701 | 22,7 |
+-----+-------------+
| 702 | 18,5 |
+-----+-------------+
| 703 | 17 |
+-----+-------------+
我怎样才能做到这一点?
我陷入了一种方式,即如何对列进行求和,比如说表的第一列(来自另一个查询的结果),而不知道列名和列位置 id 之类的东西。
是这样的
select sum(what?), employID from
( select count(*), employID from table1...
union all
select count(*), employID from table2...
union all
select count(*), employID from table3...
)
或者,如果它在单个查询中(使用 sum() 的单个简单选择查询),例如:
select employName, sum(what?), employID from tableX
如何告诉 SUM() 函数根据表中的列位置索引求和SUM(2)
?
注意:我不想使用列别名,是否有可能不基于列名进行 SUM?
我知道我可以使用列名或别名,但我真的想知道不使用这些的可能性,这就是我问这个问题的原因,如果没有可能的方法,我会接受“否”作为正确答案。