如何在SQL Server中计算行之间的分布计数
如何使用示例检索输出:如何在SQL Server中计算行之间的分布计数,sql,sql-server,sql-server-2016,Sql,Sql Server,Sql Server 2016,如何使用示例检索输出: CREATE TABLE yourTable ( id varchar(55), code int, count1 int, count2 int ); INSERT INTO yourTable (id, code,count1,count2) VALUES ('no-29', 12345, 4, 1), ('no-29', 32346, 4, 1) 始终count1-count2和结果分布在具有相同id的
CREATE TABLE yourTable
(
id varchar(55),
code int,
count1 int,
count2 int
);
INSERT INTO yourTable (id, code,count1,count2)
VALUES
('no-29', 12345, 4, 1),
('no-29', 32346, 4, 1)
始终count1-count2和结果分布在具有相同id的其他行之间:
示例:4-1=3和3在12345和32346之间的分布
我也希望按id和代码的顺序分布1,1和1,0(因为3是奇数,如果偶数=>1,1和1,1=>countdistribute for codes是2,2)
输出:
id code countdistribute
----------------------------------
'no-29' 12345 2
'no-29' 32346 1
我真的不明白你想在这里描述什么。对于
id
具有相同值的两行,对于count1
和count2
具有不同的值。如果count2
的值大于count1
?如果count2大于count1意味着第一行的值为2-3=-1,第二行的值为0,那么count1
和count2
与此问题有什么关系?count1和count2是stockno解决方案的输入和存在计数?。。。