row-number

t-SQL to find Top 10 Records for Each Group

[亡魂溺海] 提交于 2020-01-14 08:12:26
问题 I am trying to figure out how to return the top 10 records for each group of Trans.TranSID. SELECT a.ABID, a.ABName, t.TranSID, SUM(IIF(TranTypeID = 'CO', td.Qty * CAST(td.Price AS money) * - 1, td.Qty * CAST(td.Price AS money))) AS TotalSales FROM Trans t INNER JOIN TransDetail td ON t.TranID = td.TranID INNER JOIN ABook a ON t.TranABID = a.ABID WHERE (t.TranDate BETWEEN CONVERT(DATETIME, '2012-01-01 00:00:00', 102) AND CONVERT(DATETIME, '2013-01-01 00:00:00', 102)) AND t.TranTypeID in ('SO'

t-SQL to find Top 10 Records for Each Group

非 Y 不嫁゛ 提交于 2020-01-14 08:12:00
问题 I am trying to figure out how to return the top 10 records for each group of Trans.TranSID. SELECT a.ABID, a.ABName, t.TranSID, SUM(IIF(TranTypeID = 'CO', td.Qty * CAST(td.Price AS money) * - 1, td.Qty * CAST(td.Price AS money))) AS TotalSales FROM Trans t INNER JOIN TransDetail td ON t.TranID = td.TranID INNER JOIN ABook a ON t.TranABID = a.ABID WHERE (t.TranDate BETWEEN CONVERT(DATETIME, '2012-01-01 00:00:00', 102) AND CONVERT(DATETIME, '2013-01-01 00:00:00', 102)) AND t.TranTypeID in ('SO'

How to get row number in SQLite?

こ雲淡風輕ζ 提交于 2020-01-11 09:16:52
问题 I've read many articles regarding how to use a row number in SQLite but none of them gave me the answer I need. I know how to select row number using this query: SELECT (SELECT COUNT() FROM table WHERE title < t.title OR (title = t.title AND id<t.id)) as rowIndex, t.title FROM table AS t ORDER BY t.title; but if I add COLLATE NOCASE (which I need) at the end of the query then the result is completely different. 回答1: Your query contains an error: the alias "ja" is not defined. Try it like this

sql loop and set properties

倖福魔咒の 提交于 2020-01-06 05:36:27
问题 In SQL, I would like to query a list, in order by pageNumber SELECT * FROM `comics` WHERE 1 ORDER BY pageNumber ASC Then, I would like to set their pageNumbers based on their index in the query (starting with 1 instead of 0). Here is my pseudo code for the functionality as desired; where list is the return value of the Select Query above. for(var n:int = 0; n<list.length; n++){ if(list[n].pageNumber != n+1){ list[n].pageNumber = n+1 } } For example I might have pageNumbers 5, 17, 23, 24, 18,

Multiple columns in OVER ORDER BY

爷,独闯天下 提交于 2020-01-03 08:43:08
问题 Is there a way to specify multiple columns in the OVER ORDER BY clause? SELECT ROW_NUMBER() OVER(ORDER BY (A.Col1)) AS ID FROM MyTable A The above works fine, but trying to add a second column does not work. SELECT ROW_NUMBER() OVER(ORDER BY (A.Col1, A.Col2)) AS ID FROM MyTable A Incorrect syntax near ','. 回答1: The problem is the extra parentheses around the column name. These should all work: -- The standard way SELECT ROW_NUMBER() OVER(ORDER BY A.Col1) AS ID FROM MyTable A SELECT ROW_NUMBER

Equivalent of Oracle's RowID in SQL Server

一世执手 提交于 2019-12-27 12:23:09
问题 What's the equivalent of Oracle's RowID in SQL Server? 回答1: From the Oracle docs ROWID Pseudocolumn For each row in the database, the ROWID pseudocolumn returns the address of the row. Oracle Database rowid values contain information necessary to locate a row: The data object number of the object The data block in the datafile in which the row resides The position of the row in the data block (first row is 0) The datafile in which the row resides (first file is 1). The file number is relative

Equivalent of Oracle's RowID in SQL Server

邮差的信 提交于 2019-12-27 12:21:50
问题 What's the equivalent of Oracle's RowID in SQL Server? 回答1: From the Oracle docs ROWID Pseudocolumn For each row in the database, the ROWID pseudocolumn returns the address of the row. Oracle Database rowid values contain information necessary to locate a row: The data object number of the object The data block in the datafile in which the row resides The position of the row in the data block (first row is 0) The datafile in which the row resides (first file is 1). The file number is relative

SQL, using entered time to find session with most visitors

戏子无情 提交于 2019-12-25 05:28:14
问题 I have a query that I need to write. Lab assistants will enter the information for students coming to the lab for help. the field are: Visit_ID, Student_ID, Visit_Date, Time_In, Time_Out, Course, Asst_ID. There are 3 sessions for time_in entry Session 1 9-12 Session 2 12-3 Session 3 3-6 The query needs to determine which session on the current day had the most visitors. This is what I have so far: with cte as ( select case when cast(Time_In as time) >'12:00:00' and cast(Time_In as time) <='15

The data reader is incompatible with the specified class

a 夏天 提交于 2019-12-25 03:54:35
问题 so there are a few other similar type posts about, but none matching the simple task I am attempting. just running a stored proc through EF6 code first in the following manner var results = context.DataBase.SqlQuery<MyClass>("exec spGetStuff @param1, @param2", new SqlParameter[] {new SqlParameter("param1",value), new SqlParameter("param2", value2)}); I have used this method on many occasions with no issue. the class I am mapping the results to is pretty nasty with many properties, but all

How to eliminate duplicate rows but the maximum value in the rownumber(),of these rows be in the result?

ぐ巨炮叔叔 提交于 2019-12-25 00:43:22
问题 My query : SELECT CHR,CHNO,CHSQ, ROW_NUMBER () OVER(PARTITION BY CHNO ORDER BY CHSQ DESC ) TEMP, CHSB,CHVR,CHRD FROM WRPDAT.WSCLHP WHERE CHADT > '20180901' AND CHSB ='R' AND CHB1 in ('L1', 'R2') ORDER BY CHSQ The value of TEMP must be the highest(ie 2 or 3 or 4) etc amongst the duplicate rows, and rest must be eliminated. If no duplicate occurs, the TEMP=1 must be in result Sample Data CHR CHNO CHSQ TEMP CHSB CHVR CHRD F140 R11671 A11671 1 R 0 4 F140 R11671 A11671 2 R 1 4 T181 90391R A90391 1