如何在SQL服务器上没有“WITH”或子查询的情况下简化此查询?(How can I simplify this query without “WITH” or Subquery on SQL server?)
我有一个查询,可以在我的仓库和商店中筛选我的表格以查看积极的股票。
我只需要采取积极的价值,如果其中一个值是积极的,我只拿这个值。
WITH Resultat AS ( SELECT AR_Ref,StockDepots,StockStores, CASE WHEN StockDepots > 0 AND StockStores > 0 THEN StockDepots + StockStores WHEN StockDepots > 0 THEN StockDepots WHEN StockStores > 0 THEN StockStores ELSE StockDepots + StockStores END AS StockMulti FROM ArticleStock ) SELECT * FROM Resultat WHERE StockMulti > 0 ORDER BY AR_Ref
小提琴: http : //rextester.com/RMSU6120
我的查询结果是好的,我需要知道如何不使用“WITH”或子查询来简化此查询?
编辑**
为什么我不需要WITH或SubQuery是因为我认为在这种情况下是过度杀伤性的,我认为可以在没有WITH或Subquery的情况下进行查询。
I've a query that filter my table for positive stock in my warehouse and stores.
I need to take only positive value and if one of values is positive I take this value only.
WITH Resultat AS ( SELECT AR_Ref,StockDepots,StockStores, CASE WHEN StockDepots > 0 AND StockStores > 0 THEN StockDepots + StockStores WHEN StockDepots > 0 THEN StockDepots WHEN StockStores > 0 THEN StockStores ELSE StockDepots + StockStores END AS StockMulti FROM ArticleStock ) SELECT * FROM Resultat WHERE StockMulti > 0 ORDER BY AR_Ref
Fiddle : http://rextester.com/RMSU6120
My query result is Ok, I need to know How can I simplify this query without "WITH" or Subquery ?
EDIT**
Why I don't need WITH or SubQuery is because I think is overkill in this case and I think it's possible to make this query without WITH or Subquery.
原文:https://stackoverflow.com/questions/43778165
最满意答案
如您所知,要将两个数字一起添加,您可以使用
+
函数:(+ 2 1) ;;=> 3
要将两个向量一起添加,可以使用
mapv
函数将两个向量压缩在一起,使用+
:(mapv + [1 0] [1 0]) ;;=> [2 0]
要在矢量序列中执行此矢量添加的左边折叠,可以使用
reduce
:(reduce #(mapv + %1 %2) [[0 0] [1 0] [1 0] [1 0]]) ;;=> [3 0]
或者,用
partial
的用法替换该函数文字:(reduce (partial mapv +) [[0 0] [1 0] [1 0] [1 0]]) ;;=> [3 0]
要获得这个左边折叠的所有中间步骤,可以使用
reductions
:(reductions (partial mapv +) [[0 0] [1 0] [1 0] [1 0]]) ;;=> ([0 0] [1 0] [2 0] [3 0])
最后,要仅返回此序列中的唯一元素,可以使用
set
:(set (reductions (partial mapv +) [[0 0] [1 0] [1 0] [1 0]])) ;;=> #{[0 0] [1 0] [3 0] [2 0]}
To add two numbers together, you can use the
+
function, as you know:(+ 2 1) ;;=> 3
To add two vectors together, you can use the
mapv
function to zip together the two vectors using+
:(mapv + [1 0] [1 0]) ;;=> [2 0]
To perform a left fold of this vector addition across a sequence of vectors, you can use
reduce
:(reduce #(mapv + %1 %2) [[0 0] [1 0] [1 0] [1 0]]) ;;=> [3 0]
Or, replacing that function literal with a usage of
partial
:(reduce (partial mapv +) [[0 0] [1 0] [1 0] [1 0]]) ;;=> [3 0]
To obtain all the intermediate steps of this left fold, you can use
reductions
:(reductions (partial mapv +) [[0 0] [1 0] [1 0] [1 0]]) ;;=> ([0 0] [1 0] [2 0] [3 0])
Finally, to return only the unique elements from this sequence, you can use
set
:(set (reductions (partial mapv +) [[0 0] [1 0] [1 0] [1 0]])) ;;=> #{[0 0] [1 0] [3 0] [2 0]}
相关问答
更多-
(->> '([2.0 3.0] [2.0 4.0] [2.0 4.0] [2.0 12.0] [2.0 23.0] [2.0 6.0] [2.0 8.0] [1.0 5.0] [1.0 9.0] [2.0 8.0] [2.0 16.0] [2.0 19.0] [2.0 13.0] [2.0 18.0] [1.0 23.0] [2.0 24.0] [2.0 22.0] [2.0 33.0] [2.0 27.0] [1.0 29.0] [2.0 32.0] [1.0 ...
-
Mapcat对于映射,每个元素可以扩展为0个或更多输出元素,如下所示: (mapcat #(if (vector? (first %)) % [%]) data) 虽然我不确定(vector? (first %))是否足以测试您的数据。 Mapcat is useful for mapping where each element can expand into 0 or more output elements, like this: (mapcat #(if (vector? (first %)) % ...
-
正如评论中所阐明的那样 ,OP的目标是根据使用的字符串位置找出使用哪些单词,而不是使用哪些字母 - 所以字/子串THROW基本上是无关紧要的。 这是一个很短的版本: from itertools import chain wordlist = ["ONE","TWO","THREE","FOUR"] string = ''.join(wordlist) # "ONETWOTHREEFOUR" stringpositions = [6,7,8,0,4] # construct a list that ma ...
-
itertools.izip_longest在您的情况下可能会非常方便: a = [1, 2, 3, 4] b = [1, 2, 3, 4, 5, 6] c = [1, 2] lists = (a, b, c) result = [sum(el) for el in itertools.izip_longest(*lists, fillvalue=0)] 在这里你得到了你想要的东西: >>> result [3, 6, 6, 8, 5, 6] 它所做的只是简单地将你的列表拉到一起,通过用0填充空值。 ...
-
Clojure,总结一个向量列表,记录沿途的位置(Clojure, sum a list of vectors, recording positions along the way)[2021-08-01]
如您所知,要将两个数字一起添加,您可以使用+函数: (+ 2 1) ;;=> 3 要将两个向量一起添加,可以使用mapv函数将两个向量压缩在一起,使用+ : (mapv + [1 0] [1 0]) ;;=> [2 0] 要在矢量序列中执行此矢量添加的左边折叠,可以使用reduce : (reduce #(mapv + %1 %2) [[0 0] [1 0] [1 0] [1 0]]) ;;=> [3 0] 或者,用partial的用法替换该函数文字: (reduce (partial mapv +) ... -
你在一个合理的算法中做了两件昂贵的事情: 您正在为列表重新创建每次迭代的矩阵; 这可能很慢 您重复计算整行的总和,实际上您只需要计算边际变化 这是另一种选择。 我们重建原始矩阵一次,然后只添加边缘列。 fun_brodie <- function(mtx_list) { mtx <- do.call(cbind, mtx_list) base <- mtx[, 1] res <- list(base) for(i in seq(ncol(mtx))[-1]) res[[i]] <- ...
-
在clojure中的向量的变构列表中映射每个元素(mapping over each element in a destructured list of vectors in clojure)[2021-12-01]
数据自然是从学生编号(字符串)到成绩矢量的地图: (def grades-1 {"s18129" [100 70 85 71 85] "s18121" [80 75 85 81 85] "r18131" [75 60 80 56 75]}) ...以及要应用于上述的权重向量: (def grade-weights [10 20 15 25 30]) ...从学生编号到最终成绩的地图。 我们怎么能这个因素呢? 根据给定 ... -
根据每个元素的平方和来排列向量列表(Arranging a list of vectors according to the sum of square of each element)[2022-12-11]
您可以遍历列表以计算每个向量的平方和,并使用order()以升序获取值的索引。 然后,您可以使用它们对初始列表x进行排序: x[order(sapply(x, function(v) sum(v ** 2)))] 结果是: [[1]] [1] 1 1 [[2]] [1] 1 2 [[3]] [1] 1 4 You can go iterate over your list to calculate the sum of squares of each vector and use order() t ... -
我们可以使用Map从'mypos'的索引中提取'mylist'的相应元素 Map(`[`, mylist, mypos) 在OP的代码中,在每个list元素中重复'mypos',从而从索引中提取所有元素。 相反,它可以按顺序循环 lapply(seq_along(mylist), function(x) mylist[[x]][mypos[[x]]]) We can use Map to extract the corresponding elements of 'mylist' from the in ...
-
试试这个(它给你一个数据框,每一行都是一个独特的向量,有两个元素从原始向量交换,有28个这样的独特向量,包括原始的一个): v <- c(1,1,1,2,2,2,3,3,3) unique(t(apply(t(combn(1:length(v), 2)), 1, function(x) {v[x] <- v[rev(x)]; v}))) 输出: [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [1,] 1 1 1 2 2 ...