根据多个条件构建postgresql查询(Build postgresql query based on multiple conditions)
我有这个查询,它根据定义的时间间隔选择每个“消息代码”的计数。
SELECT coalesce(count(case when messagecode = 'F-100' then 'F-100' end), 0) as fixed, coalesce(count(case when messagecode = 'H-100' then 'H-100' end), 0) as hope, coalesce(count(case when messagecode = 'G-100' then 'G-100' end), 0) as good, coalesce(count(case when messagecode = 'T-100' then 'T-100' end), 0) as todo FROM messages WHERE messagetime >= current_timestamp - ('5' * interval '1 minute')
输入条目
fixed: 115, hope: 334, good: 1045, todo: 6000
我想做类似的事情但是要选择具有此消息代码的唯一ID的数量,我知道我应该从做起
SELECT coalesce(count(DISTINCT id), 0) ...
但我的问题是,我还不知道如何在同一查询的多个条件下执行此操作。 预期产量:
fixed: 13, (13 unique ids exist) hope: 45, (45 unique ids exist) good: 110, (110 unique ids exist) todo: 414 (414 unique ids exist)
我怎样才能实现它?
I have this query which selects the count of each "messagecode" based on a defined time interval.
SELECT coalesce(count(case when messagecode = 'F-100' then 'F-100' end), 0) as fixed, coalesce(count(case when messagecode = 'H-100' then 'H-100' end), 0) as hope, coalesce(count(case when messagecode = 'G-100' then 'G-100' end), 0) as good, coalesce(count(case when messagecode = 'T-100' then 'T-100' end), 0) as todo FROM messages WHERE messagetime >= current_timestamp - ('5' * interval '1 minute')
Output for entries
fixed: 115, hope: 334, good: 1045, todo: 6000
I want to make something similar but to select the amount of unique ID's are having this messagecode, I know I should start by doing
SELECT coalesce(count(DISTINCT id), 0) ...
But my issue is, I don't know yet how to do this for multiple conditions on the same query. Expected output :
fixed: 13, (13 unique ids exist) hope: 45, (45 unique ids exist) good: 110, (110 unique ids exist) todo: 414 (414 unique ids exist)
How will I be able to achieve it?
原文:https://stackoverflow.com/questions/36016533
最满意答案
此模式将起作用:
[^\\]+\\[^\\]+$
它说:任意数量的非正斜杠字符,然后是正斜杠,然后是更多非正斜杠,以及输入或行的结尾。
This pattern will work:
[^\\]+\\[^\\]+$
It says: any number of non-forward-slash characters, and then a forward slash, and then more non-forward-slashes, and the end of input or the line.
相关问答
更多-
这就是你要追求的吗? (?
.*?(?:\/|^))assets\/(? [A-Za-z0-9\-_\/.]+)\.(? jpg|jpeg|bmp|tiff|png|gif|js|css|mov|mp4|ogg|avi|mp3) 你可以在这里试试这个: http : //regexstorm.net/tester或者这里: https : //regex101.com/r/b8XxcF/1 注意:在上面的正则表达式中,我逃脱了正斜 ... -
此模式将起作用: [^\\]+\\[^\\]+$ 它说:任意数量的非正斜杠字符,然后是正斜杠,然后是更多非正斜杠,以及输入或行的结尾。 This pattern will work: [^\\]+\\[^\\]+$ It says: any number of non-forward-slash characters, and then a forward slash, and then more non-forward-slashes, and the end of input or the line.
-
您可以使用此正则表达式匹配所有文件名: ([\w._-]*bkp[\S]*?\.ctp) 假设只使用._-来分割文件,您可能需要向该字符类添加更多内容。 此正则表达式还假定文件始终以ctp结尾。 演示: https : //regex101.com/r/tB9nM9/1 编辑:如果您希望匹配任何扩展名,可以使用\w{3}扩展扩展名。 如果长度为3,则可以根据需要在此处添加方差,但更具体的通常更好。 ([\w._-]*bkp[\S]*\.\w{3}) 演示: https : //regex101.com/ ...
-
我检查了你的代码并且它工作正常,但前提是你之前已经访问过路径(因此可以缓存凭据),或者当前用户可以访问共享。 我建议你在你的Test-Path调用中添加-Credential参数。 I have checked your code and it works fine, but only if you have accessed the path before (so it could cache credentials), or if your current user can access the sha ...
-
使用Windows文件夹路径的Python正则表达式中的原始字符串(Raw string in Python regular expression using Windows folder path)[2023-01-24]
re.sub解释替换字符串( docs )中的转义序列。 在\ f之前添加额外的反斜杠以逃避反斜杠似乎可以解决问题: import re print re.sub(r'^[a-zA-Z]:\\.+(\\Data.+)', r'D:\\folder\1', r'C:\Some\Path\Data\File.txt') 如果您的替换字符串是动态的,您可以始终使用另一个regexp来转义反斜杠,或者使用str.encode('unicode-escape')。 re.sub interprets escape ... -
Nodemon依赖于文件模式而不是正则表达式来忽略某些文件。 试试这个: nodemon --ignore 'modules/**/client/angularfiles/*' Nodemon relies on file patterns rather than regular expressions for ignoring some files. Try this instead: nodemon --ignore 'modules/**/client/angularfiles/*'
-
使用文件名作为键,您应该具有所需的输出: (如果没有,请尝试: pathsEmails(Replace(fle.Path,weekFolder.Path,vbNullString)) = book.Worksheets(1).Range("C15").Value ) Option Explicit Sub SO() 'clear the existing list here -- not implemented '... Dim wS As Worksheet Dim Las ...
-
更换 [^\\]+\\?$ 没什么。 这适用于C:\Temp\Dir1 (没有尾部反斜杠)之类的路径。 它对于驱动器的根目录失败。 Replace [^\\]+\\?$ by nothing. This works for paths like C:\Temp\Dir1 (no trailing backslash) as well. It fails for the root directory of a drive.
-
如何使用正则表达式或javascript验证文件夹路径?(How to validate folder path using regex or javascript? [duplicate])[2024-03-06]
怎么样 ^[a-zA-Z]:\\(\w+\\)*\w*$ 然后在JS中,您可以检查字符串是否匹配 /^[a-zA-Z]:\\(\w+\\)*\w*$/.test(yourString) How about ^[a-zA-Z]:\\(\w+\\)*\w*$ Then in JS, you could check if a string matches by /^[a-zA-Z]:\\(\w+\\)*\w*$/.test(yourString) -
我会使用类似下面的东西,不需要递归,让BCL为你做: // I didn't recount the parenetheses... Regex re = new Regex("MENS|WOMENS|MENS\sDROPBOX|WOMENS\sDROPBOX)\\((((COLOR\sCHIPS)|(ALL\sMENS\sCOLORS)))|((\d{4})\\(\w+)\\(FINAL\sART|FINAL\sARTWORK)\\(\d{3})))"); var dirs = from dir in ...