What is the simplest SQL Query to find the second largest value?
SELECT MAX( col ) FROM table WHERE col < ( SELECT MAX( col ) FROM table )
SELECT MAX( col ) FROM table WHERE col < ( SELECT MAX( col ) FROM table )
To use IN, you must have a set, use this syntax instead: SELECT * FROM Table1 WHERE Table1.principal NOT IN (SELECT principal FROM table2)
Postgres uses \N as substitute symbol for NULL value. But all psql commands start with a backslash \ symbol. You can get these messages, when a copy statement fails, but the loading of dump continues. This message is a false alarm. You have to search all lines prior to this error if you want to … Read more
You can do it this way: UPDATE table_users SET cod_user = (case when user_role=”student” then ‘622057’ when user_role=”assistant” then ‘2913659’ when user_role=”admin” then ‘6160230’ end), date=”12082014″ WHERE user_role in (‘student’, ‘assistant’, ‘admin’) AND cod_office=”17389551″; I don’t understand your date format. Dates should be stored in the database using native date and time types.
By using T-SQL and cursors like this : DECLARE @MyCursor CURSOR; DECLARE @MyField YourFieldDataType; BEGIN SET @MyCursor = CURSOR FOR select top 1000 YourField from dbo.table where StatusID = 7 OPEN @MyCursor FETCH NEXT FROM @MyCursor INTO @MyField WHILE @@FETCH_STATUS = 0 BEGIN /* YOUR ALGORITHM GOES HERE */ FETCH NEXT FROM @MyCursor INTO @MyField … Read more
This: ORDER BY 1 …is known as an “Ordinal” – the number stands for the column based on the number of columns defined in the SELECT clause. In the query you provided, it means: ORDER BY A.PAYMENT_DATE It’s not a recommended practice, because: It’s not obvious/explicit If the column order changes, the query is still … Read more
DECLARE @v XML = (SELECT * FROM <tablename> FOR XML AUTO) Insert the above statement at the point where you want to view the table’s contents. The table’s contents will be rendered as XML in the locals window, or you can add @v to the watches window.
Simpler with the aggregate function string_agg() (Postgres 9.0 or later): SELECT movie, string_agg(actor, ‘, ‘) AS actor_list FROM tbl GROUP BY 1; The 1 in GROUP BY 1 is a positional reference and a shortcut for GROUP BY movie in this case. string_agg() expects data type text as input. Other types need to be cast … Read more
It is really easy to do a bulk insert in Laravel using Eloquent or the query builder. You can use the following approach. $data = [ [‘user_id’=>’Coder 1’, ‘subject_id’=> 4096], [‘user_id’=>’Coder 2’, ‘subject_id’=> 2048], //… ]; Model::insert($data); // Eloquent approach DB::table(‘table’)->insert($data); // Query Builder approach In your case you already have the data within the … Read more
Using LEFT JOIN/IS NULL: DELETE b FROM BLOB b LEFT JOIN FILES f ON f.id = b.fileid WHERE f.id IS NULL Using NOT EXISTS: DELETE FROM BLOB WHERE NOT EXISTS(SELECT NULL FROM FILES f WHERE f.id = fileid) Using NOT IN: DELETE FROM BLOB WHERE fileid NOT IN (SELECT f.id FROM FILES f) Warning Whenever … Read more