星期一, 4月 28, 2014

[LINQ] GroupBy

論壇討論 - DataTable 日期不重複篩選問題,看到有前輩用 Distinct 去解,就想到說用 GroupBy 應該也可以作到同樣的效果,拿來練習一下
// 引用 namespace
using System.Data;
using System.Data.SqlClient;

// 建立測試資料
DataTable dt = new DataTable();
dt.Columns.Add("Date",typeof(DateTime));
dt.Rows.Add(new DateTime(2014, 1, 1, 11, 10, 10));
dt.Rows.Add(new DateTime(2014, 1, 2, 12, 10, 10));
dt.Rows.Add(new DateTime(2014, 1, 2, 13, 10, 10));
dt.Rows.Add(new DateTime(2014, 1, 3, 14, 10, 10));
dt.Rows.Add(new DateTime(2014, 1, 4, 15, 10, 10));

// 方法一
var result = (from data in dt.AsEnumerable()
     select data.Field<DateTime>("Date").Day).Distinct();

// 方法二
var result = dt.AsEnumerable().Select(Data => Data.Field<DateTime>("Date").Day).Distinct();

// 方法三
var result = from data in dt.AsEnumerable()
    group data by data.Field<DateTime>("Date").Day into o
    select o.Key;

// 方法四
var result = dt.AsEnumerable().GroupBy(data => data.Field<DateTime>("Date").Day).Select(o => o.Key);
[LINQ] GroupBy

星期五, 4月 25, 2014

[SQL] TempDB

TempDB 顧名思義就是暫時性資料庫,存放系統自動建立或使用者建立的物件。

TempDB 的 Database_ID 是 2
SELECT DB_ID('TempDB') AS Database_ID
TempDB 中建立區域物件、全域物件 - 以 Table 說明

Table 名稱前有 # 代表區域 Table、## 代表全域 Table,區域 Table 只有建立它的 session 可以存取,全域 Table 則是全部的 session 都可以存取。
CREATE TABLE #Local(ID int)
GO
CREATE TABLE ##Global(ID int)
GO
[SQL] TempDB-1

在 T-SQL 經過複雜運算後,可以把結果存在暫存 Table 內,方便之後再使用,但會建議執行完畢後,把暫存 Table 刪除
IF OBJECT_ID('TempDB..#Local') IS NOT NULL
    DROP TABLE #Local

IF OBJECT_ID('TempDB..##Global') IS NOT NULL
    DROP TABLE ##Global

星期二, 4月 22, 2014

HP LaserJet Pro 400 M401 韌體更新

使用者告知大量列印時,常常會出現出現問題,想說查查官網有沒有新的韌體和驅動程式,也許更新過後就不會發生,希望啦

官網更新韌體的步驟:
  1. Download the flash firmware updater file from HP.com to the pc desktop.
  2. Turn the printer off and then back on to clear main printer memory.
  3. Double click on the flash firmware update utility file. An HP firmware update window will open.
  4. Select the appropriate MFP in the dialog window. If you assigned a different name to the HP MFP, choose the name that represents the correct printer.
  5. Click on "Send Firmware" when ready to proceed.
  6. The progress bar will begin to move to the right indicating information being transferred.
  7. DO NOT power cycle the product or the pc during this process or the product may become unstable and or unusable without further attention from HP. The flash firmware update can take up to 5 minutes to complete. Please be patient.
  8. During the product flash firmware update, the display on the product control panel will display "Erasing - Programming - Complete - Hewlett-Packard - Ready - Initializing - Ready". Once the download is finished the printer will be in the Ready state.
  9. At the conclusion of the firmware update, the dialog window will show a smiley face. Click on exit.
  10. The flash firmware update process is complete.

星期一, 4月 21, 2014

[C#] SqlDataAdapter Wizard

最近開始學習 C# Winform,找到的網路範例,明明都有看見拖曳 SqlDataAdapter Wizard,就可以直接產生 SqlDataAdapter 控件來使用,但為什麼我的 Visual Studio 內就沒有,原以為是 Express 版沒有,沒想到非 Express 版內也沒有,後來才發現必須手動加入

工具箱 => 任一分類滑鼠右鍵 => 選擇項目

SqlDataAdapter Wizard-1

.NET Framework 元件 => SqlDataAdapter

SqlDataAdapter Wizard-2

就可以在工具箱內看見 SqlDataAdapter,也可以拖曳至工具箱內任一分類

SqlDataAdapter Wizard-3

星期五, 4月 18, 2014

[SQL] WHERE 和 HAVING 差異

WHERE 和 HAVING 都具有篩選資料的功能,但使用時機和方式,卻是有差異

MSDN WHERE 定義
指定查詢所傳回之資料列的搜尋條件。
MSDN HAVING 定義
指定群組或彙總的搜尋條件。 HAVING 只能搭配 SELECT 陳述式使用。 HAVING 通常用在 GROUP BY 子句中。當未使用 GROUP BY 時,HAVING 的行為會如同 WHERE 子句
WHERE 和 HAVNG 差異
  1. SELECT、FROM、WHERE、GROUP BY 和 HAVING 的 T-SQL 邏輯順序來看,順序分別為
    1. FROM
    2. WHERE
    3. GROUP BY
    4. HAVING
    5. SELECT
    WHERE 在 GROUP BY 之前,而 HAVING 在 GROUP BY 之後
  2. WHERE 無法對彙總欄位進行篩選,但 HAVING 可以
  3. HAVING 通常搭配 GROUP BY 來對彙總資料進行篩選
MSDN 範例

找出金額超過 100,000 的訂單
USE AdventureWorks2012
GO

SELECT 
  SalesOrderID, 
  SUM(LineTotal) AS SubTotal
FROM Sales.SalesOrderDetail
GROUP BY SalesOrderID
HAVING SUM(LineTotal) > 100000.00
ORDER BY SalesOrderID

星期一, 4月 14, 2014

星期五, 4月 11, 2014

[SQL] AdventureWork T-SQL 練習

論壇上看見針對 AdventureWorks 的作業練習題,內容如下

資料:AdventureWorks2008R2 的 Sales.SalesOrderHeader Table
需求:業務經理想要分析銷售區域代碼是 7、8、9 的這三個區域中各個業務員的銷售數據
條件:每個銷售區域、每個銷售員(包含無銷售員的網路訂單)的以下各項資料
  1. 訂單的筆數(根據有幾筆資料計算)
  2. 現金交易的筆數(根據 CreditCardID 欄位計算),CreditCardID 欄位內是NULL值的代表現金交易,提示:總交易筆數 = 現金交易的筆數 + 信用卡交易的筆數
  3. 信用卡交易的筆數(根據 CreditCardID 欄位計算)
  4. 客戶使用不同信用卡的張數(根據 CreditCardID 欄位判斷)
  5. 含稅的總銷售金額(由貨品小計( SubTotal 欄位)加稅額( TaxAmt 欄位)計算)
  6. 平均每筆銷售訂單的運費(根據 Freight 欄位計算)
  7. 輸出的結果要包含"區域代碼"( TerritoryID 欄位)、"業務員代號"( SalesPersonID 欄位)、"訂單筆數"、"現金交易筆數"、"信用卡交易筆數"、"信用卡數"、"總交易金額(含稅)"、"平均每筆運費"八個欄位
  8. 輸出時,先按區域代碼由小到大排列,區域代碼相同時,再按業務員代號從最小到最大排列
T-SQL 解法
SELECT 
    TerritoryID AS [區域代碼],
    SalesPersonID AS [業務員代號],
    COUNT(*) AS [訂單筆數],
    SUM(CASE WHEN CreditCardID IS NULL THEN 1 ELSE 0 END) AS [現金交易筆數] ,
    SUM(CASE WHEN CreditCardID IS NOT NULL THEN 1 ELSE 0 END) AS [信用卡交易筆數] ,
    COUNT(DISTINCT CreditCardID) AS [信用卡張數] ,
    SUM(SubTotal + TaxAmt) AS [總交易金額(含稅)],
    AVG(Freight) AS [平均每筆運費]
FROM Sales.SalesOrderHeader
WHERE TerritoryID IN (7,8,9)
GROUP BY TerritoryID , SalesPersonID
ORDER BY TerritoryID , SalesPersonID
[SQL] AdventureWork T-SQL 練習

沒有解答可以參考,也不知道對不對 ~~ ^_^''

星期五, 4月 04, 2014

[SQL] smalldatetime 和 datetime

要把工業機台產生的 txt 檔案內容,塞進 SQL Server 2005,該 Table 是把 smalldatetime 欄位設為 Primary Key,塞資料時出現違反 Primary Key 的錯誤訊息

在 C# Console 內把資料秀出來檢查,資料都是唯一的

[SQL] smalldatetime 和 datetime-1