2016年10月14日 星期五

[SQL]Remove duplicates

This will also take care of duplicates (return one row for each user_id):


SELECT * FROM (
  SELECT u.*, FIRST_VALUE(u.rowid) OVER(PARTITION BY u.user_id ORDER BY u.date DESC) AS last_rowid
  FROM users u
) u2
WHERE u2.rowid = u2.last_rowid




Ref:
http://stackoverflow.com/questions/121387/fetch-the-row-which-has-the-max-value-for-a-column

2016年7月15日 星期五

[SQL SERVER] convert comma Separated String into rows




DECLARE @str VARCHAR(4000)= '6,7,7,8,10,12,13,14,16,44,46,47,394,396,417,488,714,717,718,719,722,725,811,818,832,833,836,837,846,913,914,919,922,923,924,925,926,927,927,928,929,929,930,931,932,934,935,1029,1072,1187,1188,1192,1196,1197,1199,1199,1199,1199,1200,1201,1202,1203,1204,1205,1206,1207,1208,1209,1366,1367,1387,1388,1666,1759,1870,2042,2045,2163,2261,2374,2445,2550,2676,2879,2880,2881,2892,2893,2894';

SELECT t.c.value('.', 'VARCHAR(1000)')
FROM (
  SELECT x = CAST('' +
   REPLACE(@str , ',', '') + '' AS XML)
 ) a
CROSS APPLY x.nodes('/t') t(c);





Ref:

2016年6月21日 星期二

String.IsNullOrEmpty in JavaScript

Starting with:
return (!value || value == undefined || value == "" || value.length == 0);
Looking at the last condition, if value == "", it's length MUST be 0. Therefore drop it:
return (!value || value == undefined || value == "");
But wait! In JS, an empty string is false. Therefore, drop value == "":
return (!value || value == undefined);
And !undefined is true, so that check isn't needed. So we have:
return (!value);
And we don't need parentheses:
return !value
Q.E.D.


http://codereview.stackexchange.com/questions/5572/string-isnullorempty-in-javascript

2016年6月2日 星期四

[jQuery]add dynamic rows



Ref:
http://stackoverflow.com/questions/2145012/adding-rows-dynamically-with-jquery

http://ssiddique.info/different-ways-to-add-row-to-a-table-using-jquery.html

http://stackoverflow.com/questions/16183231/jquery-append-and-remove-dynamic-table-row

2016年5月27日 星期五

[MS SQL] SPRING JDBC


 -- 使用JDBC連線
http://www.codedata.com.tw/java/java-tutorial-the-3rd-class-2-spring-jdbc/

JDBCDataSource dataSource = new JDBCDataSource();
dataSource.setUrl("jdbc:hsqldb:file:src/main/resources/db/dvd_library");
dataSource.setUser("codedata");
dataSource.setPassword("123456");


-- 呼叫 PROCEDURE
 http://stackoverflow.com/questions/9361538/spring-jdbc-template-for-calling-stored-procedures


SimpleJdbcCall jdbcCall = new SimpleJdbcCall(jdbcTemplate)
    .withSchemaName(schema)
    .withCatalogName(package)
    .withProcedureName(procedure)();
...
jdbcCall.addDeclaredParameter(new SqlParameter(paramName, OracleTypes.NUMBER));
...
jdbcCall.execute(callParams);




-- procedure 回傳 temp table

http://stackoverflow.com/questions/1443663/how-to-return-temporary-table-from-stored-procedure

 -- The "called" SP
  declare
      @returnAsSelect bit = 0;

  if object_id('tempdb..#GetValuesOutputTable') is null
  begin
      set @returnAsSelect = 1;
      create table #GetValuesOutputTable(
         ...   
      );
  end

  -- populate the table

  if @returnAsSelect = 1
      select * from #GetValuesOutputTable;

2016年5月16日 星期一

[JAVA8] truncate java 8 LocalDateTime



LocalDateTime now =  LocalDateTime.now();
LocalDateTime roundFloor =  now.truncatedTo(ChronoUnit.MINUTES);
LocalDateTime roundCeiling =  now.truncatedTo(ChronoUnit.MINUTES).plusMinutes(1);
http://stackoverflow.com/questions/25552023/round-minutes-to-ceiling-using-java-8