sql - Return NULL values last in an ordered set -


I have a set of values ​​in a temporary table, which I want to use to populate three new columns ( Maximum (case ... END) statement given below)

However, I think sometimes in column 1 (or column 1 and column both) NULL values, even if there is a value in column 3.

Other values, for example, Col1 = value, Col2 = value, Col3 =

I have any NULL values For <> zero .

Source code:

  SELECT mc.ID, mc.Date, max (case when sv1.row_num = 1 and mc.Date = sv1.Date then sv1. [ Colonel] And as Call 1, Max (when sv1.row_num = 2 and mc.Date = sv1.Date then sv1. [Colonel] end) as Col2, max (case when sv1.row_num = 3 and mc According to date = sv1 (date) [Date]) # from myTable3 as row_num (date)). As mc.id by mc.ID = sv1.id group on sv1, mc.Date SELECT * FROM # Total ID 7, Date    

Because your values ​​are being shown in that order, and that you use the ROW_NUMBER () function to organize your records (To determine whether a value is located in Col1, Col2, or Col3) change your query to:

  SELECT mc.ID, mc.Date, max (case when Sv1.row_num = 1 and mc.Date = sv1.Date then sv1. As the end) call 1, max (when sv1.row_num = 2 and mc.Date = sv1.Date then sv1. [Curl] end) as col2, max (case when sv1.row_num = 3 and mc As DAT = sv1.Date then join sv1 [Colonel] end 3 as call # 3 in # MyTable3 MC (select #MyTable3. *, At ROW_NUMBER () (id [id] Mc.ID = sv1.id by mc.ID, mc.Date SELECT * FROM on sv1 as ISNULL ([Date], '12 / 31/99 99 ') as the order from # MyTable3 to row_num) # Total 7 order id, date   

ISNULL () function, if [date] f Ld is zero, instead of the date of return in the future that is in the future. Thus, any 'real' dates will appear with the first line number value, and any dates for NULL will be shown the last time.

Or, if you do not like ISNULL () , you can use the case statement, that is,

   as 1 ELSE 0 END, [date])>  ROW_NUMBER () 

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -