2012年5月23日 星期三

[TSQL] 分組求前幾筆資料的問題

以下是table的結構:

model code tatol
A1 E1 13
A2 E2 11
A3 E7 8
A4 J5 4
A5 K5 120
.....

G4 H2 200
其中model不重複, model code不重複, 現在的問題是按modelcode數量最大的前三筆記錄,
: model A1 來說就是: K5, E1, E2
--按某一字段分組取最大()值所在行的數據(2007-10-23於浙江杭州)
/*
數據如下:
name val memo
a    2   a2(a的第二個值)
a    1   a1--a的第一個值
a    3   a3:a的第三個值
b    1   b1--b的第一個值
b    3   b3:b的第三個值
b    2   b2b2b2b2
b    4   b4b4
b    5   b5b5b5b5b5
*/
--創建表並插入數據:
create table tb(name varchar(10),val int,memo varchar(20))
insert into tb values('a',    2,   'a2(a的第二個值)')
insert into tb values('a',    1,   'a1--a的第一個值')
insert into tb values('a',    3,   'a3:a的第三個值')
insert into tb values('b',    1,   'b1--b的第一個值')
insert into tb values('b',    3,   'b3:b的第三個值')
insert into tb values('b',    2,   'b2b2b2b2')
insert into tb values('b',    4,   'b4b4')
insert into tb values('b',    5,   'b5b5b5b5b5')
go
--一、按name分組取val最大的值所在行的數據。
--方法1
select a.* from tb a where val = (select max(val) from tb where name = a.name) order by a.name
--方法2
select a.* from tb a where not exists(select 1 from tb where name = a.name and val > a.val)
--方法3
select a.* from tb a,(select name,max(val) val from tb group by name) b where a.name = b.name and a.val = b.val order by a.name
--方法4
select a.* from tb a inner join (select name , max(val) val from tb group by name) b on a.name = b.name and a.val = b.val order by a.name
--方法5
select a.* from tb a where 1 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name
/*
name       val         memo                 
---------- ----------- -------------------- 
a          3           a3:a的第三個值
b          5           b5b5b5b5b5
*/
--二、按name分組取val最小的值所在行的數據。
--方法1
select a.* from tb a where val = (select min(val) from tb where name = a.name) order by a.name
--方法2
select a.* from tb a where not exists(select 1 from tb where name = a.name and val < a.val)
--方法3
select a.* from tb a,(select name,min(val) val from tb group by name) b where a.name = b.name and a.val = b.val order by a.name
--方法4
select a.* from tb a inner join (select name , min(val) val from tb group by name) b on a.name = b.name and a.val = b.val order by a.name
--方法5
select a.* from tb a where 1 > (select count(*) from tb where name = a.name and val < a.val) order by a.name
/*
name       val         memo                 
---------- ----------- -------------------- 
a          1           a1--a的第一個值
b          1           b1--b的第一個值
*/
--三、按name分組取第一次出現的行所在的數據。
select a.* from tb a where val = (select top 1 val from tb where name = a.name) order by a.name
/*
name       val         memo                 
---------- ----------- -------------------- 
a          2           a2(a的第二個值)
b          1           b1--b的第一個值
*/
--四、按name分組隨機取一條數據。
select a.* from tb a where val = (select top 1 val from tb where name = a.name order by newid()) order by a.name
/*
name       val         memo                 
---------- ----------- -------------------- 
a          1           a1--a的第一個值
b          5           b5b5b5b5b5
*/
--五、按name分組取最小的兩個(N)val
select a.* from tb a where 2 > (select count(*) from tb where name = a.name and val < a.val ) order by a.name,a.val
select a.* from tb a where val in (select top 2 val from tb where name=a.name order by val) order by a.name,a.val
select a.* from tb a where exists (select count(*) from tb where name = a.name and val < a.val having Count(*) < 2) order by a.name
/*
name       val         memo                 
---------- ----------- -------------------- 
a          1           a1--a的第一個值
a          2           a2(a的第二個值)
b          1           b1--b的第一個值
b          2           b2b2b2b2
*/
--六、按name分組取最大的兩個(N)val
select a.* from tb a where 2 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name,a.val
select a.* from tb a where val in (select top 2 val from tb where name=a.name order by val desc) order by a.name,a.val
select a.* from tb a where exists (select count(*) from tb where name = a.name and val > a.val having Count(*) < 2) order by a.name
/*
name       val         memo                 
---------- ----------- -------------------- 
a          2           a2(a的第二個值)
a          3           a3:a的第三個值
b          4           b4b4
b          5           b5b5b5b5b5
*/

2007年3月14日 星期三

SQLServer 孤兒使用者的問題

當您將使用者資料庫由一台SQL Server 伺服器,移至另一台SQL Server 伺服器時,將會發生 master 資料庫中的
登入帳號,與使用者資料庫中的使用者之安全識別碼 (SID) 之間會不相符的問題。將會產生『孤兒使用者』的問
題。

造成影響:
使用者無法登入該伺服器,就算建立一模一樣的帳號名稱,也是沒辦法

-- Step01. 變更目前資料庫的擁有者為 sa。
/* 先切換的指定的資料庫
功能:解決 dbo 為 孤兒使用者的問題。*/
USE [資料庫名稱]
GO
EXEC sp_changedbowner 'sa'

-- Step02.顯示目前資料庫中,SID有遺失的帳號資訊。
USE [資料庫名稱]
GO
EXEC sp_change_users_login 'Report'

-- Step03.自動將使用者對應到登入,並且視需要自動建立新的登入使用者。
/*請注意:
在 Step02 中,若是出現一個以上得的帳號資訊,則本步驟 Step3 必須一個一個帳號來處理執行喔。

引數說明:
@UserNamePattern -- 使用 Step02 取得帳號名稱
@Password -- 輸入 原先 密碼
@Action -- 使用 'Auto_Fix',但必須確認新的伺服器中沒有相同名稱的登入帳號
其餘引數不改變

請參考以下的範例:
此範例會顯示如何使用 『Auto_Fix』 選項,將現有的使用者對應到擁有相同名稱的使用者,
或是在登入 Mary 不存在時,自動建立 SQL Server 登入 Mary (包含密碼 B3r12-36)。*/

EXEC sp_change_users_login
@Action='Auto_Fix',
@UserNamePattern='QQZZ_apuser',
@LoginName=NULL,
@Password='123'

-- Step04.再做一次檢查,是否有孤兒帳號
EXEC sp_change_users_login 'Report'

[C#] OutLook In C#


要怎麼在 C# 中操作 OutLook 呢? 雖然 C# 並沒有直接的提供收信的元件,但是我們仍然可以透過
OFFice 提供的 Outlook 元件來存取、操作 OutLook ,首先要把元件加入參考,如下圖所示:




我們會在參考中發現到多了一個 Outlook 的參考


現在我們就可以開始操作囉....







Outlook.Application Mailapp= new Outlook.ApplicationClass();
Outlook.NameSpace mailspace=Mailapp.GetNamespace("MAPI");
Outlook.MAPIFolder mfolder=mailspace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
mfolder.Items.Sort("[ReceivedTime]",false);
string MailSubject="";
long mailcount=mfolder.Items.Count;

for (int i=1;i<mailcount;i++)
{
object item = mfolder.Items[i];


Outlook.MailItem mi = item as Outlook.MailItem;
if(mi != null)
{

if (mi.Subject ==null)
{MailSubject="";}
else
{MailSubject=mi.Subject.ToString();}
textBox1.Text+=mi.ReceivedTime.ToString()+" - "+MailSubject+"\r\n";
if (MailSubject=="給王小明的主旨")
{
Outlook.Attachments ii=mi.Attachments;  //檢查有無附件
for (int x=1;x<=ii.Count;x++)
{
Outlook.Attachment aa=(Outlook.Attachment)ii[x];
aa.SaveAsFile(@"d:\"+aa.FileName);  //有的話就存檔
}



break;
}
}
}


// 取得Folder 的另一種方法
// foreach(object item in mfolder.Items)
// {
// Outlook.MailItem mi = item as Outlook.MailItem;
//
// if(mi != null)
// {
// if (mi.Subject ==null)
// {MailSubject="";}
// else
// {MailSubject=mi.Subject.ToString();}
//
// //textBox1.Text+=mi.ReceivedTime.ToString()+" - "+MailSubject+"\r\n";
// if (MailSubject=="給王小明的主旨")
// {
// Outlook.Attachments ii=mi.Attachments;
// for (int x=1;x<=ii.Count;x++)
// {
// Outlook.Attachment aa=(Outlook.Attachment)ii[x];
// aa.SaveAsFile(@"d:\"+aa.FileName);
// }
//
//
//
// break;
// }
//
//
//
//
// }


// }

mailspace.Logoff();
mfolder=null;
mailspace=null;
Mailapp =null;









以上和大家分享...


[JavaScript] 動態增加下拉式選單

下面三種都是動態增加下拉式選單 選項的語法...雖然都能達到相同的效果
但是寫的方式不同, 其效率也有很大的差別,給大家參考一下

function option(){
var opt;
var start;
var end;

start=new Date();
selContainer.innerHTML="";
selContainer.innerHTML="<select id='selShow' onchange='change(this);'></select>";

for(i=0;i<opttext.length;i++)
{ opt=new Option();
//or you may code like below:
//opt=document.createElement("OPTION");
opt.text=opttext[i];
opt.value=optvalue[i];
selShow.options.add(opt);
}

end=new Date();
optionTime.innerText="The Operation Took Time:"+(end.getTime()-start.getTime())+" milliseconds";

}

function object()
{
var start;
var end;
var str="<select id='selShow' onchange='change(this);'>";

start=new Date();
selContainer.innerHTML="";

for(i=0;i<opttext.length;i++)
{
str+="<option value='"+optvalue[i]+"'>"+opttext[i]+"</option>";
}

str+="</select>";
selContainer.innerHTML=str;

end=new Date();
objectTime.innerText="The Operation Took Time:"+(end.getTime()-start.getTime())+" milliseconds";
}

function join()
{
var len=opttext.length;
var arr=new Array(len);
var start;
var end;

start=new Date();
selContainer.innerHTML="";
joinTime.innerText="";

for(i=0;i<len;i++)
{
arr[i]="<option value='"+optvalue[i]+"'>"+opttext[i]+"</option>";
}
selContainer.innerHTML="<select id='selShow' onchange='change(this);'>"+arr.join()+"</select>";

end=new Date();
joinTime.innerText="The Operation Took Time:"+(end.getTime()-start.getTime())+" milliseconds";
}

# option() 最慢 ,object() 次之,join() 最快


附上完整範例在附件中,供大家參考


來源:某不知名大陸網友發表.....

2007年3月12日 星期一

[C#] DataTable to CSV

有時候可能會需要將某的Table全部匯出成 CSV , 雖然可以只接用 SQL Server 直接匯出來但是可能有時候會需要程式中轉出來,還有一點要注意在匯出完要存檔的時候,記得要將編碼轉成BIG5的,不然會拿去匯入資料庫的時候,中文可能會出現亂碼的情形,因為.NET預設的編碼是Unicode

StreamWriter CSVsw =new StreamWriter(Sourcefile,false,System.Text.Encoding.Default);

private string CSVExplor(DataTable tb)
{
Mydatas="";
foreach (DataRow row in tb.Rows)
{
foreach (DataColumn column in tb.Columns)
{ if (column.DataType==System.Type.GetType("System.DateTime"))
{
IFormatProvider culture = new System.Globalization.CultureInfo("en-US", true);
Mydatas += Convert.ToDateTime(row[column].ToString()).ToUniversalTime().ToString(culture) + ","; }
else
{
Mydatas += row[column].ToString() + ",";
}
}
Mydatas += "\r\n"; } return Mydatas;
}