mklink/?
建立符號連結。
MKLINK [[/D] | [/H] | [/J]] Link Target
/D 建立目錄符號連結。預設是檔案符號連結。
/H 建立永久連結而不是符號連結。
/J 建立目錄連接。
Link 指定新符號連結名稱。
Target 指定新連結參照的路徑 (相對或絕對)。
2014年2月21日 星期五
2013年1月11日 星期五
MS SQL 使用Rowid 更新資料方法
--Form: http://www.databasejournal.com/features/mssql/article.php/3572301/RowNumber-function-in-SQL-Server-2005.htm
Jan 4, 2006
Row_Number() function in SQL Server 2005
As we all know, SQL Server 2005 has new features when compared to SQL Server 2000. One of the features that we are going to discuss in this article is the Row_Number() function. SQL Server Database administrators and developers have been longing for this function for a long time--now the wait is over.
Traditionally developers and Database administrators used temporary tables and co-related sub-queries to generate calculated row numbers in a query. Now SQL Server 2005 provides a function, which replaces all of the additional resources we used to generate row numbers.
Let us assume that we have the following database [EMPLOYEE TEST] and the following table [EMPLOYEE] in the database. You can use the below query to create the database, table and all the corresponding rows.
Illustration 1.0
Traditionally in SQL Server 2000, in order to create row numbers based on the rows available in a table, we used to use the following query.
Let us query the table by using the following query.
Illustration 1.1
In this illustration it is clear that the table has a duplicate row with EMPID = 2021110.
To delete the duplicate row with EMPID = 2021110, we have to delete the row in employee2 table and I cannot delete the duplicate row directly from the Employee table.
SQL Server 2005 provides a new function, Row_Number(), for generating row numbers. In order to delete the duplicate row from the original table we can use the features, Common Table Expression and Row_Number() together.
Let us generate the ROWID using the Row_Number() function based on EMPID.
Illustration 1.2
In this result set, we can identify the duplicate row for the EMPID 2021110.
Let us display the duplicate row using the Common Table expression and Row_Number() function by using the following query.
Illustration 1.3
This duplicate row can be deleted using the Common Table expression and Row_Number() function by using the following query.
Illustration 1.4
In this illustration, we can see that the duplicate row has been deleted.
» See All Articles by Columnist MAK
Traditionally developers and Database administrators used temporary tables and co-related sub-queries to generate calculated row numbers in a query. Now SQL Server 2005 provides a function, which replaces all of the additional resources we used to generate row numbers.
Let us assume that we have the following database [EMPLOYEE TEST] and the following table [EMPLOYEE] in the database. You can use the below query to create the database, table and all the corresponding rows.
USE [MASTER] GO IF EXISTSLet us browse the table Employee by using the following SQL Query.(SELECT NAME FROM SYS.DATABASES WHERE NAME = N'EMPLOYEE TEST') DROP DATABASE [EMPLOYEE TEST] GO CREATE DATABASE [EMPLOYEE TEST] GO USE [EMPLOYEE TEST] GO IF EXISTS
(SELECT * FROM SYS.OBJECTS
WHERE OBJECT_ID =
OBJECT_ID(N'[DBO].[EMPLOYEE]') AND TYPE IN (N'U')) DROP TABLE [DBO].[EMPLOYEE] GO CREATE TABLE EMPLOYEE (EMPID INT, FNAME VARCHAR(50), LNAME VARCHAR(50)) GO INSERT INTO EMPLOYEE (EMPID, FNAME, LNAME) VALUES (2021110, 'MICHAEL', 'POLAND') GO INSERT INTO EMPLOYEE (EMPID, FNAME, LNAME) VALUES (2021110, 'MICHAEL', 'POLAND') GO INSERT INTO EMPLOYEE (EMPID, FNAME, LNAME) VALUES (2021115, 'JIM', 'KENNEDY') GO INSERT INTO EMPLOYEE (EMPID, FNAME, LNAME) VALUES (2121000, 'JAMES', 'SMITH') GO INSERT INTO EMPLOYEE (EMPID, FNAME, LNAME) VALUES (2011111, 'ADAM', 'ACKERMAN') GO INSERT INTO EMPLOYEE (EMPID, FNAME, LNAME) VALUES (3015670, 'MARTHA', 'LEDERER') GO INSERT INTO EMPLOYEE (EMPID, FNAME, LNAME) VALUES (1021710, 'MARIAH', 'MANDEZ') GO
SELECT EMPID, FNAME, LNAME FROM EMPLOYEEThe results of the above query look like illustration 1.0.
| 2021110 | MICHAEL | POLAND |
| 2021110 | MICHAEL | POLAND |
| 2021115 | JIM | KENNEDY |
| 2121000 | JAMES | SMITH |
| 2011111 | ADAM | ACKERMAN |
| 3015670 | MARTHA | LEDERER |
| 1021710 | MARIAH | MANDEZ |
Illustration 1.0
SELECT ROWID=IDENTITY(int,1,1) , EMPID, FNAME, LNAME INTO EMPLOYEE2 FROM EMPLOYEE ORDER BY EMPIDThis query created a new table using the identity function in order to generate RowId.
Let us query the table by using the following query.
SELECT RowID, EMPID, FNAME, LNAME FROM EMPLOYEE2The results of the above query would look like illustration 1.1.
| 1 | 1021710 | MARIAH | MANDEZ |
| 2 | 2011111 | ADAM | ACKERMAN |
| 3 | 2021110 | MICHAEL | POLAND |
| 4 | 2021110 | MICHAEL | POLAND |
| 5 | 2021115 | JIM | KENNEDY |
| 6 | 2121000 | JAMES | SMITH |
| 7 | 3015670 | MARTHA | LEDERER |
Illustration 1.1
To delete the duplicate row with EMPID = 2021110, we have to delete the row in employee2 table and I cannot delete the duplicate row directly from the Employee table.
SQL Server 2005 provides a new function, Row_Number(), for generating row numbers. In order to delete the duplicate row from the original table we can use the features, Common Table Expression and Row_Number() together.
Let us generate the ROWID using the Row_Number() function based on EMPID.
SELECT ROW_NUMBER() OVER (ORDER BY EMPID ASC) AS ROWID, * FROM EMPLOYEEThe results of the above query would look like illustration 1.2.
| 1 | 1021710 | MARIAH | MANDEZ |
| 2 | 2011111 | ADAM | ACKERMAN |
| 3 | 2021110 | MICHAEL | POLAND |
| 4 | 2021110 | MICHAEL | POLAND |
| 5 | 2021115 | JIM | KENNEDY |
| 6 | 2121000 | JAMES | SMITH |
| 7 | 3015670 | MARTHA | LEDERER |
Illustration 1.2
Let us display the duplicate row using the Common Table expression and Row_Number() function by using the following query.
WITH [EMPLOYEE ORDERED BY ROWID] AS (SELECT ROW_NUMBER() OVER (ORDER BY EMPID ASC) AS ROWID, * FROM EMPLOYEE) SELECT * FROM [EMPLOYEE ORDERED BY ROWID] WHERE ROWID =4The results of the above query would look like illustration 1.3.
| 4 | 2021110 | MICHAEL | POLAND |
Illustration 1.3
WITH [EMPLOYEE ORDERED BY ROWID] AS (SELECT ROW_NUMBER() OVER (ORDER BY EMPID ASC) AS ROWID, * FROM EMPLOYEE) DELETE FROM [EMPLOYEE ORDERED BY ROWID] WHERE ROWID =4Let us query the table using the following query.
SELECT * FROM EMPLOYEEThe results of the above query would look like illustration 1.4.
| 2021110 | MICHAEL | POLAND |
| 2021115 | JIM | KENNEDY |
| 2121000 | JAMES | SMITH |
| 2011111 | ADAM | ACKERMAN |
| 3015670 | MARTHA | LEDERER |
| 1021710 | MARIAH | MANDEZ |
Illustration 1.4
Conclusion
In this article, we have discussed the new features of Row_Number() function and Common Table Expression and used both the features together to delete duplicate rows.» See All Articles by Columnist MAK
2012年4月6日 星期五
what is difference between wmsys.wm_concat and ListAgg
This thread is to discussion what is difference between wmsys.wm_concat and ListAgg for 11G r2.
*************************************************************************
difference1
wmsys.wm_concat allows distinct option.
ListAgg does not allows it.
*************************************************************************
difference2
ListAgg allows to decide string concat order.
wmsys.wm_concat does not allows it.
*************************************************************************
difference3
ListAgg allows to decide delimiter.
wmsys.wm_concat does not allows it.
*************************************************************************
difference4
wmsys.wm_concat allows to be used OLAP function with order by
ListAgg does not allows it.
ListAgg allows only OLAP function without order by.
*************************************************************************
difference5
wmsys.wm_concat allows to be used KEEP
ListAgg does not allows it.
*************************************************************************
difference1
wmsys.wm_concat allows distinct option.
ListAgg does not allows it.
create table diffT(sortKey,Val) as
select 1,'aa' from dual union all
select 2,'bb' from dual union all
select 3,'aa' from dual union all
select 4,'dd' from dual;
col concatV for a20
select wmsys.wm_concat(distinct Val) as concatV from diffT;
concatV
--------
aa,bb,dd
*************************************************************************
difference2
ListAgg allows to decide string concat order.
wmsys.wm_concat does not allows it.
select ListAgg(Val,',')
within group(order by sortKey desc) as concatV
from diffT;
CONCATV
------------
dd,aa,bb,aa
*************************************************************************
difference3
ListAgg allows to decide delimiter.
wmsys.wm_concat does not allows it.
select ListAgg(Val,'***')
within group(order by sortKey desc) as concatV
from diffT;
CONCATV
-----------------
dd***aa***bb***aa
*************************************************************************
difference4
wmsys.wm_concat allows to be used OLAP function with order by
ListAgg does not allows it.
ListAgg allows only OLAP function without order by.
select sortKey,wmsys.wm_concat(Val)
over(order by sortKey) as concatV
from diffT;
SORTKEY CONCATV
------- -----------
1 aa
2 aa,bb
3 aa,bb,aa
4 aa,bb,aa,dd
*************************************************************************
difference5
wmsys.wm_concat allows to be used KEEP
ListAgg does not allows it.
select wmsys.wm_concat(Val)
Keep(Dense_Rank First order by Val) as concatV
from diffT;
CONCATV
-------
aa,aa
2011年7月15日 星期五
Ms Sql 重複資料查詢與刪除
--檢查重複排序
WITH Get_Last_cmd
AS
(
SELECT
*,GroupID = ROW_NUMBER() OVER (PARTITION BY dbowner,custid,facisno ORDER BY cmopendate desc)
FROM
dbo.table
)
Select * FROM Get_Last_cmd order by facisno,cmopendate desc;
--刪除重復
WITH Get_Last_cmd
AS
(
SELECT
GroupID = ROW_NUMBER() OVER (PARTITION BY dbowner,custid,facisno ORDER BY cmopendate desc)
FROM
dbo.table
)
delete * FROM Get_Last_cmd WHERE GroupID > 1;
WITH Get_Last_cmd
AS
(
SELECT
*,GroupID = ROW_NUMBER() OVER (PARTITION BY dbowner,custid,facisno ORDER BY cmopendate desc)
FROM
dbo.table
)
Select * FROM Get_Last_cmd order by facisno,cmopendate desc;
--刪除重復
WITH Get_Last_cmd
AS
(
SELECT
GroupID = ROW_NUMBER() OVER (PARTITION BY dbowner,custid,facisno ORDER BY cmopendate desc)
FROM
dbo.table
)
delete * FROM Get_Last_cmd WHERE GroupID > 1;
2010年12月7日 星期二
2010年8月5日 星期四
訂閱:
文章 (Atom)
