sql-order-by

ORDER BY items must appear in the select list if SELECT DISTINCT is specified

♀尐吖头ヾ 提交于 2019-12-02 08:15:38
I have sql query in which i want return rows with distinct value order by particular column. like say,i want disntict batchno from ordertable order by locationid. i have tried google also but not able to find out solution ORDER BY items must appear in the select list if SELECT DISTINCT is specified. that what i got every time i tried. when not using distinct i got output but with duplicate rows using this query Select Batchno,LocationId from Ordertbl order by case when[LocationId] =3 THEN 0 ELSE 1 END, [LocationId] if i use distinct it gives me error these are query i tried out. Select

order by rand() gets trouble for large posts

走远了吗. 提交于 2019-12-02 07:33:16
问题 I have this code, this code gets trouble and so much time to be executed to show RANDOM posts from DB: $totalrows = 10; $sql = "SELECT posts.Tags as tags, posts.OwnerUserId as postsid, posts.Id as postid, posts.Body as body, posts.Title as title, users.Id as userid, users.DisplayName as usersname FROM posts JOIN users ON posts.OwnerUserId = users.Id WHERE posts.Title != '' order by rand() asc limit " . $totalrows; $r = mysql_query($sql) or die(mysql_error()); Please tell me what should I

order by rand() gets trouble for large posts

帅比萌擦擦* 提交于 2019-12-02 07:16:25
I have this code, this code gets trouble and so much time to be executed to show RANDOM posts from DB: $totalrows = 10; $sql = "SELECT posts.Tags as tags, posts.OwnerUserId as postsid, posts.Id as postid, posts.Body as body, posts.Title as title, users.Id as userid, users.DisplayName as usersname FROM posts JOIN users ON posts.OwnerUserId = users.Id WHERE posts.Title != '' order by rand() asc limit " . $totalrows; $r = mysql_query($sql) or die(mysql_error()); Please tell me what should I change to show random posts more quikly? Regards,Dan rand() is very expensive. This will help: http://www

How Can I Make Oracle Query Sort Order Dynamic?

六眼飞鱼酱① 提交于 2019-12-02 06:51:44
I have a Oracle procedure inside a package like this PROCEDURE getEmployee ( pinLanguage IN VARCHAR2, pinPage IN NUMBER, pinPageSize IN NUMBER, pinSortColumn IN VARCHAR2, pinSortOrder IN VARCHAR2, poutEmployeeCursor OUT SYS_REFCURSOR ) AS BEGIN OPEN poutEmployeeCursor FOR SELECT * FROM ( SELECT EMPLOYEE_ID, USERNAME, FULL_NAME, DATE_OF_BIRTH, EMP.GENDER_ID, GEN_TR.GENDER, EMP.WORK_TYPE_ID, WT_TR.WORK_TYPE, SALARY, EMAIL, PROFILE_IMAGE, ROW_NUMBER() OVER (ORDER BY EMPLOYEE_ID ASC) RN FROM EMPLOYEES EMP INNER JOIN GENDERS GEN ON EMP.GENDER_ID = GEN.GENDER_ID LEFT JOIN GENDERS_MLD GEN_TR ON GEN

Using Order By in codeigniter

蓝咒 提交于 2019-12-02 06:12:46
This is my normal mysql query: $sql = "SELECT * FROM item order by "; if(my_condition) { $sql. = "FIELD(`category`, 'Auto & Accessories', 'Couch', 'Bed', 'Desk & Office', 'Bike & Scooter', 'Tools', 'Leisure', 'Iron & Wood', 'Cabinet', 'Kitchen & Accessories', 'Refrigerator & Appliances', 'Toys & Games', 'Chair', 'Table', 'Garden & Terrace', 'TV, HIFI & Computers', 'General Item')"; } else { $sql .= "category asc"; } I need it in CI in active record. I tried in following way: if(my_condition) { $this->db->order_by("FIELD(`category`, 'Auto & Accessories', 'Couch', 'Bed', 'Desk & Office', 'Bike &

SQL: How to sort / order query result in somewhat arbitrary manner?

China☆狼群 提交于 2019-12-02 06:07:51
问题 I need to sort the results of a query in an UNCONVENTIONAL order. Say, I do a search for "Chair". I want all the records STARTING with Chair to be listed first alphabetically, then other records that CONTAIN "chair" to be listed afterwards in alphabetical order. So "Blue Chair" would be listed after "Chair Blue" for example. Please note the STARTING and CONTAINING conditions. How should I implement this sort? Thanks. 回答1: How does this look? SELECT *, 1 as sort_pref FROM table WHERE column

MySQL: GROUP_CONCAT with an ORDER BY COUNT?

梦想与她 提交于 2019-12-02 05:53:32
Is this possible ? Let's say I have a list of addresses, with a lot of duplicate entries. I need to filter out all the duplicates, because some addresses have slightly different names but the same postalcode and telephone number. First I do a GROUP BY on postalcode and telephone. SELECT name, address, postalcode, place, phone FROM addresses GROUP BY postalcode, phone But then I get random names. I would like to get the best name, that is, the name with the most entries per postalcode/phone. So I thought of the following. Here I use the SUBSTRING_INDEX function to only get the first item in the

Ordering sub-items within ordered items in a Linq to Entities Query

一曲冷凌霜 提交于 2019-12-02 05:21:34
I have a few tables, Listings, ListingImages and a few others related to Listings. ListingImages is related to Listings so that you can have many ListingImages per Listing. When I query this table I do; IQueryable<Listing> ListingToSendToView = (from x in DBEntities.ListingSet.Include("ListingImages") .Include("OtherTables") where x.Listing.ID == (int)LID orderby x.ID descending select x).First(); Now this is fine. However, I now want to sort the ListingImages independently within each Listing (by an ImageOrder column I have in that table). How can I do this and pass all my Includes(...).

Group by year in date field in MySQL

早过忘川 提交于 2019-12-02 04:19:58
I have a MySQL database which has a customer table. Some dummy data is: customer_id date 000001 2008-10-10 000002 2008-11-11 000003 2010-01-02 000004 2007-04-03 000005 2010-05-05 I want to run a query which will give me a result like so: year customer_count 2007 1 2008 2 2010 2 I know I need to use group by, however I am unable to wrap my head around how to group based on year value of a date field, and how to have them in an order. Extract the year from the date and group by it select year(date) as year, count(customer_id) as customers from your_table group by year order by year asc Use the

ORDER BY suddenly conflicting with VARCHAR concatenation in TSQL

五迷三道 提交于 2019-12-02 03:22:40
I have code in Sql Server 2008 which concatenates some strings from a query into a variable using the tried-and-true SELECT @VAR = @VAR + FIELD FROM TABLE ORDER BY OTHERFIELD syntax. This is my exact SQL: SELECT @SQL = @SQL + ISNULL(FORMULA, CASE WHEN USEMAP = 1 THEN 'dbo.getFieldTranslation('+CONVERT(VARCHAR,ROWID)+', [' + ISNULL(ENCOMPASSFIELD,'') + '])' ELSE '[' + ISNULL(ENCOMPASSFIELD,'') + ']' END ) + ' AS "' + FILECOLNAME + '",' + @CRLF FROM dbo.EXPORTMAP_EX WHERE WAREHOUSEID = @WHSID ORDER BY ORDERIDX This was working beautifully and perfectly. Then suddenly today, it stopped working.