SQL CASE:WHEN语句的顺序是否重要?(SQL CASE: Does the order of the WHEN statements matter?)
我使用PHP生成需要以自定义方式排序的SQL查询。 我正在生成一个
CASE
块,其中包含许多WHEN
语句,用于指定数字排名。 包含的WHEN
语句取决于我可用于查询的信息量。 例如,如果我有可用的电话,我将生成三个WHEN
语句:
WHEN phone = '(202) 555-5555' AND last_name = 'Smith' and first_name = 'John' THEN 1
WHEN phone = '(202) 555-5555' AND last_name = 'Smith' THEN 2
WHEN phone = '(202) 555-5555' THEN 3
所以我根据匹配的接近程度进行排名,并且我对参数的评估是相同的(电话,名字和姓氏上的匹配应该在与地址,名字和姓氏匹配的同一层上排名)。 但是,在为多条信息(电话,地址等)生成这些语句后,我的
WHEN
语句将全部乱序;THEN 1
终止条款将彼此分开,对于THEN 2
相同,依此类推。 我可以把它们整理好,但这会让我的PHP更加冗长和丑陋。TL; DR?
所以简短的问题是:
CASE
语句中WHEN
语句的顺序是否重要? SQL(我使用的是Oracle)是第一次匹配还是评估所有可能性并分配最高排名?I am using PHP to generate a SQL query that needs to be ordered in a custom way. I am generating a
CASE
block with a number ofWHEN
statements that assign a number ranking. TheWHEN
statements that are included are contingent on how much information I have available for querying. For example, if I have a phone available, I will generate threeWHEN
statements:
WHEN phone = '(202) 555-5555' AND last_name = 'Smith' and first_name = 'John' THEN 1
WHEN phone = '(202) 555-5555' AND last_name = 'Smith' THEN 2
WHEN phone = '(202) 555-5555' THEN 3
So I am ranking based on how close the match is, and I am valuing the parameters the same (a match on phone, first name, and last name should be ranked on the same tier as a match on address, first name and last name). However, after generating these statements for multiple pieces of information (phone, address, etc), my
WHEN
statements will be all out of order; theTHEN 1
terminated clauses will be separated from each other, same forTHEN 2
and so on. I could put them in order, but that would make my PHP more verbose and ugly.TL;DR?
So the short question is: Does the order of the
WHEN
statements in aCASE
statement matter? Does SQL (I'm using Oracle) go with the first match or does it evaluate all of the possibilities and assign the highest ranking?
原文:https://stackoverflow.com/questions/22640981
最满意答案
尝试这个。
def formatheader(): print( "Code | Name | Producer | Description | Quantity |\n" "-----+-------------+---------------+------------------+-------------|") def sortbycode(): device_file = open('devices.txt', 'r') formatheader() devices = [] for line in device_file: devices.append([i for i in line.strip("\n").split(":")]) devices.sort(key=lambda x:x[0]) for device in devices: print("{0:5}|{1:13}|{2:15}|{3:18}|{4:5}".format(*device)) sortbycode()
try this.
def formatheader(): print( "Code | Name | Producer | Description | Quantity |\n" "-----+-------------+---------------+------------------+-------------|") def sortbycode(): device_file = open('devices.txt', 'r') formatheader() devices = [] for line in device_file: devices.append([i for i in line.strip("\n").split(":")]) devices.sort(key=lambda x:x[0]) for device in devices: print("{0:5}|{1:13}|{2:15}|{3:18}|{4:5}".format(*device)) sortbycode()
相关问答
更多-
以下代码将按照您询问的方式对列表进行排序: from operator import itemgetter def sort_and_format(): contents = [] with open('table1.txt', 'r+') as f: for line in f: l = line.split(',') l[1:]=map(int,l[1:]) contents.append(l) ...
-
with open("dnaseq.txt") as data, open("ouput.txt", "w") as output: lines = data.readlines() for line in lines: #do whatever processing to line output.write(line) data.readlines()返回一个行列表。 只需遍历列表项并逐行写出。 with open("dnaseq.txt") as data, ...
-
请试试这个 f = open("my.txt" , "w") def a(): return (2+3) def x(): b = a() print("\n" ,b) return str(b) f.write(x()) f.close() 因为函数x不会返回任何东西,所以你不能将它打印到文件中 please try this f = open("my.txt" , "w") def a(): return (2+3) def x(): b = a() ...
-
就像budder说的那样,尝试将你的元素转换为int或float 。 假设您正在使用整数,您可以在sorted()函数中更改您的key : sort = sorted(csv1, key=lambda x : int(x[1])) Like budder says, try to convert your elements to int or float. Assuming that your are working with integers you can change your key in sor ...
-
尝试这个。 def formatheader(): print( "Code | Name | Producer | Description | Quantity |\n" "-----+-------------+---------------+------------------+-------------|") def sortbycode(): device_file = open('devices.txt', 'r' ...
-
问题是你为每一行(即在循环内)调用一次,而不是整个行集。 试试这个: f1 = open('file.txt','r') a=sorted(f1.readlines(), key=lambda l:l.split('|')[1]) r=open('file.txt','w') r.writelines(a) f1.close The problem is that you are calling sorted once for each line (i.e. inside the loop) ...
-
如何在python中以数字方式对file.txt的内容进行排序(How to sort contents of a file.txt in python numerically)[2022-06-02]
您可以在创建sorted_data之后在tab标签上strip()新行\n和split() ,然后使用" ".join()将拆分字符串重新连接在一起,并用空格字符分隔。 然后你可以再次打印: with open("newfile.txt", "r") as f: sorted_data = sorted(f.readlines(), key=lambda item: int(item.strip().split("\t")[-1])) sort = [" ".join(line.strip( ... -
看一下pandas库,它对数据流非常有用。 http://pandas.pydata.org/ 或者你可以直接这样做: list1 = [] list2 = [] list3 = [] with open('test.txt', 'r') as f: content = f.readlines() for x in content: row = x.split() list1.append(int(row[0])) list2.append(i ...
-
Unicode解码尝试从Python中的.txt文件读取数据时出错(Unicode Decode Error when trying to read data from a .txt file in Python)[2022-08-17]
假设原始文本文件是ANSI编码的(默认使用Acrobat Reader的“另存为文本”选项),此命令将其转换为utf-8 : iconv -f "iso-8859-1" -t "utf-8" sjsuclassdata.txt -o sjsuclassdata-utf8.txt Assuming that the original text file is ANSI encoded (default with Acrobat Reader's 'Save As Text' option), this co ... -
使用sorted()方法按字母排序列表。 进行计算以找到标记的平均值并根据平均标记创建学生列表 演示: import pickle d = {'WILLIAMS GABRIELLE': [10, 6, 2], 'JONES CHLOE': [7, 5, 5],\ 'MILLER NOEL': [1, 8, 6], 'CLARK BRANDON': [8, 6, 1],\ 'NATHAN WILSON': [7, 9, 6], 'SMITH ISABELLE': [2, 9, 6],\ ...