發表文章

18 10 01 自我研讀(四)

COUNT 會忽略空值 LEFT JOIN = LEFT (OUTER) JOIN GROUP BY ROLLUP 運算子可用來產生包含小計與總數的報告。ROLLUP 運算子將產生一個類似於 CUBE 運算子產生之結果集的結果集。如需詳細資訊,請參閱<使用 CUBE 來摘要資料>。 以下是 CUBE 與 ROLLUP 之間的特定差異: CUBE 將產生一個結果集,顯示出選定資料行之所有值組合的彙總。 ROLLUP 將產生一個結果集,顯示出選取的資料行中值階層的彙總。 INTERSECT except(差集) select * from A except select * from B (任何欄位值不同,皆會被列出,列出A存在但B不存在的資料) =================== DateTime 跟 DateTimeOffset都是一個struct DateTimeOffset跟DateTime很相似,都是用來表示日期跟時間 但是DateTimeOffset多提供了GMT time的offset,用來明確表示特定時間 當根據時區表示明確的時間點時,使用DateTimeOffset ex: now, transaction times, file change times, logging event times 不需要表示特訂時區時,使用DateTime 不需要特訂時間時,使用DateTime。 例如:生日,並且把時間訂為 00:00:00 不要在DateTimeOffset中使用DateTimeKind 只有時間沒有日期時,使用TimeSpan for system_time as of  '2017-01-01'  從TEMP TABLE取得一段時間 117

TSQL Lab 09 習題

LAB 09 Exercise 1 --------------------------------------------------------------------- -- Task 2 --  -- Write a SELECT statement that will return groups of customers that made a purchase. -- The SELECT clause should include the custid column from the Sales.Orders table  -- and the contactname column from the Sales.Customers table.  -- Group by both columns and filter only the orders from the sales employee  -- whose empid equals five. --------------------------------------------------------------------- 寫法1 select c.custid,c.contactname from Sales.Customers c inner join Sales.Orders o on c.custid = o.custid where o.empid = 5 group by c.custid,c.contactname 寫法2 select distinct(o.custid), c.contactname,o.empid from Sales.Orders o inner join Sales.Customers c on o.custid = c.custid where o.empid = 5 order by o.custid asc --------------------------------------------------------------------- -- Task 3 --  -- Copy the T-SQL statement ...

18 10 01 自我研讀(三)

=== IIF ( boolean_expression, true_value, false_value ) 範例: select productid,unitprice, iif(unitprice>50 ,'high','low') as pricepoint from Production.Products === PARSE、TRY_PARSE -- 繁體中文,日期的順序是:ymd  SELECT PARSE('01/02/03' AS  datetime2 USING 'zh-TW') N'日期' GO  -- 英文 us_english,日期的順序是:mdy SELECT PARSE('01/02/03' AS  datetime2 USING 'en-US') N'日期' GO == CHOOSE(index,condition 1,condition 2...) select CHOOSE(3,'Japan','China','Taiwan') -- result  'Taiwan'  == CAST CONVERT 區別 CONVERT是專對SQL Server使用的,使日期與時間值,小數之間轉換具有更寬的靈活性。 CAST是兩種功能中更具ANSI標準的功能,即雖然更具便攜性(比如,使用CAST的函數能更容易的被其它資料庫軟體使用),但功能相對弱一些。不過,當小數轉化為數值,並保留原始運算式中的小數數值時,仍然需要使用CAST == NULLIF COALESCE 區別 NULLIF是專對SQL Server使用的,盡可能使用規範中所支援的標準函數COALESCE

TSQL Lab 08 習題

LAB 08 Exercise 1 --------------------------------------------------------------------- -- Task 1 --  -- Write a SELECT statement against the Production.Products table to retrieve a calculated column named productdesc. The calculated column should be based on the columns productname and unitprice and look like this: --  The unit price for the Product HHYDP is 18.00 $. -- -- Execute the written statement and compare the results that you got with the desired results shown in the file 52 - Lab Exercise 1 - Task 1 Result.txt.  -- -- Did you use the CAST or the CONVERT function? Which one do you think is more appropriate to use? --------------------------------------------------------------------- select productname,'The unit price for the Product ' + SUBSTRING(productname,9,LEN(productname)) + ' is ' + convert(varchar(10),unitprice) + ' $.' as productdesc from Production.Products YES,因為有金額欄位接成字串需要做轉換 --------------------------------------------...

18 09 26 自我研讀(二)

圖片
== ISNULL(判斷欄位,要替代的值) EX: ISNull(NULL, 'A')   ---A ISNull(12345, 'A')   ---12345 ISNull(NULL, NULL)   ---NULL == COALESCE(函數可為多個參數,回傳第一個不是NULL值的參數,至少要有兩個參數) 以下為例 select ID, NAME, p1,p2,p3, coalesce(p2,p3) from shop_Price --從p2,p3挑出非NULL值(若都為NULL還是會顯示NULL) select ID, NAME, p2,p3, coalesce(p2,p3,0) from shop_Price --從p2,p3,0挑出非NULL值 == EOMONTH(回傳指定日期當月的最後一天) select DATEADD(DAY,1,EOMONTH(GETDATE(),-1)) ; 當月第一天 select EOMONTH(getdate()); 當月最後一天 == ISNUMERIC (判斷是否為有效數值,TRUE為1,FALSE為0) == ISDATE  (判斷是否為 有效日期 ,TRUE為1,FALSE為0) select ISNUMERIC(100),ISNUMERIC(1.5),ISNUMERIC(-10.5),ISNUMERIC('abc'); -- result 1,1,1,0 select ISDATE('2018-09-11'),ISDATE('199033'),ISDATE('abc'); -- result 1,0,0 == TRY_CONVERT(嘗試轉換,若轉換失敗則為NULL) SELECT isitdate,TRY_CONVERT(datetime,isitdate) as converteddate FROM Sales.Somedates; == LIKE 用法 + 萬用字元 1. % : 表任一或多個字元 2. _ : 表任一個字元 3. [ ] :表示括弧內...

TSQL Lab 06 習題

LAB 06 Exercise 1 --------------------------------------------------------------------- -- Task 1 -- -- -- Write a SELECT statement to return columns that contain: --  The current date and time. Use the alias currentdatetime. --  Just the current date. Use the alias currentdate. --  Just the current time. Use the alias currenttime. --  Just the current year. Use the alias currentyear. --  Just the current month number. Use the alias currentmonth. --  Just the current day of month number. Use the alias currentday. --  Just the current week number in the year. Use the alias currentweeknumber. --  The name of the current month based on the currentdatetime column. Use the alias currentmonthname. -- -- Execute the written statement and compare the results that you got with the desired results shown in the file 52 - Lab Exercise 1 - Task 1 Result.txt. Your results will be different because of the current date and time value. -- -- Ca...

大綱

遠古的魔法師們在人類的世界裡隱密的生存著,雖然只是少數人,卻掌握著人類的命脈。 魔物無所不在,更甚者會奪人性命,對於人類來說就是幽靈一般的存在。隨著魔物科學進步的現代,出現一種新的人造魔物,他們本來是人,卻因不明原因被注射魔化劑,可以聽從主人指示,攻擊甚至殺害魔法師。他們需要借由不斷汲取魔法師的魔力才得以生存,只有強烈求生意志的人類才有辦法忍受魔化的痛苦。 魔法族的能力全是來自於晶石 家族 赤  - 強烈的攻擊力 青  - 強大的防護力及治癒能力 白  - 每個時期只有一個時間掌管者,擁有操控時間的能力 紫 - 混種人。能存活的人不是非常強不然就是連人類都比不上。兩股矛盾的力量在身體中互斥,存活率非常低而且性格乖戾,一般人口中的狂人、瘋子。 寒 - 白族的後裔,父親為白族時間掌控家,在紅藍家族戰中犧牲,從此不知去向。本身擁有劃開時間的力量但不自知,只有十分危急的時候才意外使出。 個性冷靜,不常表現任何情感,平時也不多話。跟妹妹媽媽同住。 依 - 原先只是普通人類。在捷運爆炸事件中倖存,被 紫混種人 犽 注射魔物劑化為人造魔物。幻化獸為白狐。屬於敏捷性的魔物。 大綱 1. 黑洞 - 劃開時間的起點 在回家的路上,被捲入青赤家族的紛爭,隨後被依化為魔物的攻擊,在情急之下劃開時空,也拉著依一起跌入黑洞。 2. 會面 - 校園再次見面 依醒來後,回到自己的高中時期。雖然外貌是自己高中的模樣,卻是10年後的靈魂,仍遭受毒物的控制。在某天碰巧遇見原來同高中的寒,抓住她問她事情的經過。 3. 神秘人 - 找回回到未來的方法 兩人毫無頭緒,在一次體育課依因毒性發作而受傷送到保健室,一醒來護理師靜(青族)拿著劍要殺她,發現依的魔物力量要殺掉她,被寒阻止。道出原委後達成協議,將依和寒將自身當成研究材料,並換取回去的方法。 這段期間,靜將自身的青之力給予依作為食物減緩毒性發作的時間,高中生也要顧功課。依的程度沒有寒來的好,只好巴著寒不放,求寒幫他補習。寒百般不願意,但也只能接受。兩人接受靜認識學校的教官進行體力訓練。魔力訓練則由靜擔當。 某日依去寒家補習時,在雜物堆中找到一本塵封已久的古書,裡面一顆水晶石項鍊,這顆透明晶石能讓她掌握操作時間能力的方法。發現魔法一族的秘密。造物者在遠古發現晶石,...