case

Postgres CASE in ORDER BY using an alias

送分小仙女□ 提交于 2019-12-18 12:16:39
问题 I have the following query which works great in Postgres 9.1: SELECT users.id, GREATEST( COALESCE(MAX(messages.created_at), '2012-07-25 16:05:41.870117'), COALESCE(MAX(phone_calls.created_at), '2012-07-25 16:05:41.870117') ) AS latest_interaction FROM users LEFT JOIN messages ON users.id = messages.user_id LEFT JOIN phone_calls ON users.id = phone_calls.user_id GROUP BY users.id ORDER BY latest_interaction DESC LIMIT 5; But what I want to do is something like this: SELECT users.id, GREATEST(

Combining INSERT statements in a data-modifying CTE with a CASE expression

旧街凉风 提交于 2019-12-18 09:05:49
问题 My question is some kind of extension to Erwin Brandstetter's excellent answer in this thread on the correct use of WITH . My old query looks like this: WITH x AS ( INSERT INTO d (dm_id) SELECT dm_id FROM dm, import i WHERE dm.dm_name = i.dm_name RETURNING d_id ), y AS ( INSERT INTO z (d_id) SELECT d_id FROM x RETURNING z_id ) INSERT INTO port (z_id) SELECT z_id FROM y; This works like a charm. But now, another table ( r ) has been added (same structure as table d ) and with it the

Update query if statement for Oracle

放肆的年华 提交于 2019-12-18 04:45:05
问题 I need to update newly created column in my oracle table. To do so I need to use existing values in row to decide how to populate this column, I am getting error: java.lang.NullPointerException -> See Debug Output for details This is my query: UPDATE SCHEMA_NAME.TABLE_NAME SET OCO= IF CO= 'Y' AND COM='Y' THEN { 'Y' } ELSE { 'N' } END IF; Any suggestions on syntax? 回答1: You could use CASE expression in the SET clause. For example, UPDATE table SET schema.column = CASE WHEN CO= 'Y' AND COM='Y'

Python: getting filename case as stored in Windows?

吃可爱长大的小学妹 提交于 2019-12-18 04:34:06
问题 Though Windows is case insensitive, it does preserve case in filenames. In Python, is there any way to get a filename with case as it is stored on the file system? E.g., in a Python program I have filename = "texas.txt", but want to know that it's actually stored "TEXAS.txt" on the file system, even if this is inconsequential for various file operations. 回答1: Here's the simplest way to do it: >>> import win32api >>> win32api.GetLongPathName(win32api.GetShortPathName('texas.txt'))) 'TEXAS.txt'

SQL use CASE statement in WHERE IN clause

限于喜欢 提交于 2019-12-18 03:05:26
问题 Is it posible to use case in where in clause? Something like this: DECLARE @Status VARCHAR(50); SET @Status='published'; SELECT * FROM Product P WHERE P.Status IN (CASE WHEN @Status='published' THEN (1,3) WHEN @Status='standby' THEN (2,5,9,6) WHEN @Status='deleted' THEN (4,5,8,10) ELSE (1,3) END) This code gives the error : Incorrect syntax near ','. 回答1: No you can't use case and in like this. But you can do SELECT * FROM Product P WHERE @Status='published' and P.Status IN (1,3) or @Status=

How to catch Errno::ECONNRESET class in “case when”?

佐手、 提交于 2019-12-17 22:38:17
问题 My application (Ruby 1.9.2) may raise different exceptions, including net-connection breaks. I rescue Exception => e , then do case/when to handle them in defferent ways, but several errors go through my cases straight to else . rescue Exception => e p e.class case e.class when Errno::ECONNRESET p 1 when Errno::ECONNRESET,Errno::ECONNABORTED,Errno::ETIMEDOUT p 2 else p 3 end end Prints: Errno::ECONNRESET 3 回答1: This is because of how the === operator works on the class Class The case

MySQL Select If Table Exists

我是研究僧i 提交于 2019-12-17 20:27:38
问题 I need to run a count query on a table but only if that table exists, SELECT CASE WHEN (SELECT COUNT(*) FROM information_schema.TABLES WHERE TABLE_SCHEMA = 'DATABASENAME' AND TABLE_NAME = 'testtable') < 1 THEN '0' ELSE (SELECT COUNT(*) FROM testtable) END; The above query should return 0 if the table doesn't exist, but if it does it should get the count. This returns an error saying "testtable" doesn't exist, we know it doesn't exist as the information_schema count returns 0. Is this possible

PHP switch case more than 1 value in the case

依然范特西╮ 提交于 2019-12-17 17:46:26
问题 I have another situation. I have a variable that holds the values ('Weekly', 'Monthly', 'Quarterly', 'Annual'), and I have another variable that holds the values from 1 to 10. switch ($var2) { case 1: $var3 = 'Weekly'; break; case 2: $var3 = 'Weekly'; break; case 3: $var3 = 'Monthly'; break; case 4: $var3 = 'Quarterly'; break; case 5: $var3 = 'Quarterly'; break; // etc. } It isn't beautiful, because my code has a lot of duplicates. What I want: switch ($var2) { case 1, 2: $var3 = 'Weekly';

Cypher Neo4J - CASE Expression with MERGE

天涯浪子 提交于 2019-12-17 12:20:10
问题 I'm trying to implement the logic in Cypher where, based on a particular condition ( CASE Statement), I would create some nodes and relationships; the code is as below MATCH (g:Game)-[:PLAYER]->(u:User)-[r1:AT]->(b1:Block)-[:NEXT]->(b2:Block) WHERE g.game_id='G222' and u.email_id = 'xyz@example.com' and b1.block_id='16' SET r1.status='Skipped', r1.enddate=20141225 WITH u, b2,b1, g, r1 SET b1.test = CASE b2.fork WHEN 'y' THEN MERGE (u)-[r2:STAGE {startdate:20141225, enddate:'99999999', status:

Dynamic order direction

泄露秘密 提交于 2019-12-17 10:57:37
问题 I writing a SP that accepts as parameters column to sort and direction. I don't want to use dynamic SQL. The problem is with setting the direction parameter. This is the partial code: SET @OrderByColumn = 'AddedDate' SET @OrderDirection = 1; … ORDER BY CASE WHEN @OrderByColumn = 'AddedDate' THEN CONVERT(varchar(50), AddedDate) WHEN @OrderByColumn = 'Visible' THEN CONVERT(varchar(2), Visible) WHEN @OrderByColumn = 'AddedBy' THEN AddedBy WHEN @OrderByColumn = 'Title' THEN Title END 回答1: You