sql-order-by

TSQL Order BY on occasion doesn't order correctly

杀马特。学长 韩版系。学妹 提交于 2019-12-25 19:38:04
问题 TSQL MSSQL 2008r2 I'm re-writing the question to try and make it clear what the issue is that I'm trying to explain. I've got a stored proc that takes 3 parameters. VehicleKey, StartDate and EndDateTime. I'm querying a Data Warehouse db. So the data shouldn't change. When the proc is called with the same parameters then most of the time the results will be as expected but on some random occasions, with those same parameters, the results differ. I'm querying a Data WH so the data doesn't

SQL: group by from other table and invert result

丶灬走出姿态 提交于 2019-12-25 17:10:29
问题 I have some problem with my SQL query I have table message and table recipient message is ID author date -------------------- 0 1 2013-07-08 05:38:47 1 1 2013-07-13 05:38:47 2 1 2013-07-15 05:38:47 3 1 2013-07-15 05:38:47 4 2 2013-07-17 05:38:47 5 1 2013-07-28 05:38:47 recipient is ID m_id recipient -------------------- 0 0 2 1 1 2 2 2 3 3 3 2 4 4 1 5 5 2 I need return rows from table message with group by recipient column from table recipient with last date in message table I'll try this

SQL - ORDER BY - Threads & Messages

筅森魡賤 提交于 2019-12-25 14:25:10
问题 I have three tables: Message_Threads: ( thread_ID ) Message_Users: ( thread_ID, user_ID, notify ) Messages: ( thread_ID, user_ID, message_ID, date_created ) I am trying to list out just the threads in order of most recent message within the thread, but I want to show the ones that have notify=1 above the rest (notify is set to 1 for all other users on a thread when a new message is posted and set to 0 when they visit that thread). Anyone got a monster select statement for me? 回答1: Your data

MySQL order by primary key dynamically in DESC order

╄→гoц情女王★ 提交于 2019-12-25 09:29:31
问题 I am trying to query a table in descending order based on its primary key column. Here is the query: SELECT * FROM fdmsus_demo.vitalstats ORDER BY ( SELECT `COLUMN_NAME` FROM `information_schema`.`COLUMNS` WHERE (`TABLE_SCHEMA` = 'fdmsus_demo') AND (`TABLE_NAME` = 'vitalstats') AND (`COLUMN_KEY` = 'PRI') ) DESC LIMIT 10; I am expecting this query to return rows in descending order. However it is not working as expected. I feel DESC is not being applied to the query. The reason I am writing

LINQ for RANK() and CASE WHEN in VB.NET

自闭症网瘾萝莉.ら 提交于 2019-12-25 08:29:47
问题 I apologize, if a similar question is already asked in VB.Net for the RANK() and CASE WHEN . I have two tables. The script with the required columns are below: Table 1: SELECT S.* INTO dbo.LoanHeader FROM ( SELECT 1 LoanHeaderId, 1234 LoanNumber, GETDATE() EffectiveOn UNION ALL SELECT 2 LoanHeaderId, 2234 LoanNumber, GETDATE() EffectiveOn UNION ALL SELECT 3 LoanHeaderId, 3234 LoanNumber, GETDATE() EffectiveOn )S Table 2: SELECT S.* INTO dbo.LoanHeaderHistory FROM ( SELECT 1 LoanHeaderId, 1

group by and order by in mysql query

廉价感情. 提交于 2019-12-25 07:49:22
问题 I am working on dating website with php and mysql as backend. I've two tables. First table is user table where all user info will be stored and other table is visituser table where if one user view another users profile then there will be an entry in visituser table. Every time if one user visits another users profile then there will be an entry in visituser table. There will be multiple entry for one user visit to another user. My problem is that i've to show the visitor one time with latest

MySQL Sorting by turns query

こ雲淡風輕ζ 提交于 2019-12-25 07:38:19
问题 I want to sort the following data items in the order they are presented below : enter image description here id msisdn locker is_selected classification nomor ---------------------------------------------------------------------- 1282 62811409xxx Kendari 0 1 01 1138 62811409xxx Kendari 0 1 01 3264 62811556xxx Samarinda 0 1 02 3323 62811562xxx Pontianak 0 1 02 3450 62811481xxx Jayapura 0 1 03 3435 62811481xxx Jayapura 0 1 03 Query : (SELECT *, CONCAT(is_selected, 1) AS nomor FROM reference

“ORDER BY 'column_name' DESC”, but numbers before letters requires two statements?

[亡魂溺海] 提交于 2019-12-25 06:26:44
问题 I want to select data in my database and order it alphabetically but numbers first and then letters. Sample of my table: watch_date 474 1442437 2181257 2015-12-20 Modern Family 3 13 Little Bo Bleep 475 1442437 2189485 2015-12-21 Modern Family 3 14 Me? Jealous? 476 1442437 2209159 2015-12-22 Modern Family 3 15 Aunt Mommy 477 1442437 2237415 2015-12-22 Modern Family 3 16 Virgin Territory 478 1442437 2244103 2015-12-22 Modern Family 3 17 Leap Day 479 1442437 2239817 2015-12-24 Modern Family 3 18

ORACLE SQL | Modifying data in ORDER BY

半城伤御伤魂 提交于 2019-12-25 05:14:50
问题 following structure in a ORACLE table: FILE_NAME ----------- 12345_l.tif 12345_m.tif 12345_r.tif 12345_x.tif 12345_y.tif Need the following result: First *_m* Then *_l* Then *_r* Then * (everything else) Trying with: SELECT FILE_NAME FROM TABLE WHERE FILE_NAME LIKE '12345%' ORDER BY regexp_replace(FILE_NAME, '_m', '_1'), regexp_replace(FILE_NAME, '_l', '_2'), regexp_replace(FILE_NAME, '_r', '_3') But this gives me a wrong result. Anybody with a hint? TIA Matt 回答1: Change your ORDER BY to

MySQL union and order by help

限于喜欢 提交于 2019-12-25 04:27:24
问题 The below code works but when i change Order by id to Order by s.id, i get this error Unknown column 's.id' in 'order clause' $construct = "SELECT child.* FROM products child LEFT JOIN products parent on parent.name=child.parent INNER JOIN subscribe s ON (s.productid = parent.id) WHERE s.username='$logged' AND s.type='0' UNION SELECT child.* FROM products child LEFT JOIN products parent on parent.sid=child.sid INNER JOIN subscribe s ON (s.productid = parent.id) WHERE s.username='$logged' AND