t-sql 笔记(2)

1.用标点符号分隔的字符串,转换成表

--    SELECT * FROM dbo.split('581:579:519:279:406:361:560',':')
CREATE FUNCTION [dbo].[Split] (@Sql VARCHAR (8000), @Splits VARCHAR (10))
   RETURNS @temp TABLE (a VARCHAR (100))
AS
   BEGIN
      DECLARE @i   INT
      SET @Sql = RTrim (LTrim (@Sql))
      SET @i = CharIndex (@Splits, @Sql)

      WHILE @i >= 1
      BEGIN
         INSERT @temp
         VALUES (Left (@Sql, @i - 1))

         SET @Sql = SubString (@Sql, @i + 1, Len (@Sql) - @i)
         SET @i = CharIndex (@Splits, @Sql)
      END

      IF @Sql <> ''
         INSERT @temp
         VALUES (@Sql)

      RETURN
   END

 http://www.cnblogs.com/fang-beny/archive/2013/08/13/3255171.html

2.使用 APPLY

http://technet.microsoft.com/zh-cn/library/ms175156%28v=sql.90%29.aspx

使用 APPLY 运算符可以为实现查询操作的外部表表达式返回的每个行调用表值函数。表值函数作为右输入,外部表表达式作为左输入。通过对右输入求值来获得左输入每一行的 计算结果,生成的行被组合起来作为最终输出。APPLY 运算符生成的列的列表是左输入中的列集,后跟右输入返回的列的列表。

APPLY 有两种形式:CROSS APPLY 和 OUTER APPLY。CROSS APPLY 仅返回外部表中通过表值函数生成结果集的行。OUTER APPLY 既返回生成结果集的行,也返回不生成结果集的行,其中表值函数生成的列中的值为 NULL。

CREATE TABLE Employees
(
  empid   int         NOT NULL,
  mgrid   int         NULL,
  empname varchar(25) NOT NULL,
  salary  money       NOT NULL,
  CONSTRAINT PK_Employees PRIMARY KEY(empid),
)
CREATE TABLE Departments
(
  deptid    INT NOT NULL PRIMARY KEY,
  deptname  VARCHAR(25) NOT NULL,
  deptmgrid INT NULL REFERENCES Employees
)
CREATE FUNCTION dbo.fn_getsubtree(@empid AS INT) RETURNS @TREE TABLE
(
  empid   INT NOT NULL,
  empname VARCHAR(25) NOT NULL,
  mgrid   INT NULL,
  lvl     INT NOT NULL
)
AS
BEGIN
  WITH Employees_Subtree(empid, empname, mgrid, lvl)
  AS
  ( 
    -- Anchor Member (AM)
    SELECT empid, empname, mgrid, 0
    FROM Employees
    WHERE empid = @empid

    UNION all
    
    -- Recursive Member (RM)
    SELECT e.empid, e.empname, e.mgrid, es.lvl+1
    FROM Employees AS e
      JOIN Employees_Subtree AS es
        ON e.mgrid = es.empid
  )
  INSERT INTO @TREE
    SELECT * FROM Employees_Subtree

  RETURN
END
GO
SELECT *
FROM Departments AS D
  CROSS APPLY fn_getsubtree(D.deptmgrid) AS ST
使用 APPLY

 

if object_id('tempdb..#CategoryDetail') is not null
        drop table #CategoryDetail  
create  TABLE  #CategoryDetail (
    [Id] [int],
    [CategoryId] [int],
    [Cry] [varchar](50))
INSERT into #CategoryDetail ([Id], [CategoryId], [Cry]) VALUES (1, 1, N'')
INSERT into #CategoryDetail ([Id], [CategoryId], [Cry]) VALUES (2, 2, N'')
select * from #CategoryDetail;
if object_id('tempdb..#Category') is not null
        drop table #Category  
create  TABLE #Category (
    [Id] [int],
    [Name] [varchar](50))
INSERT into #Category ([Id], [Name]) VALUES (1, N'Cat')
INSERT into #Category ([Id], [Name]) VALUES (2, N'Dog')
INSERT into #Category ([Id], [Name]) VALUES (3, N'Tiger')
select * from #Category;
select * from #Category a cross apply (select * from #CategoryDetail b where b.CategoryId=a.Id) c
select * from #Category a outer apply (select * from #CategoryDetail b where b.CategoryId=a.Id) c

 

http://www.cnblogs.com/A2008A/archive/2011/03/11/1981828.html 

 

3.行转列

 http://www.cnblogs.com/lwhkdash/archive/2012/06/26/2562979.html

WEEK           INCOME
星期一           1000
星期二           2000
星期三           3000
星期四           4000
星期五           5000
星期六           6000
星期日           7000

 

星期一   星期二   星期三   星期四   星期五   星期六   星期日
1000     2000     3000     4000     5000     6000     7000

SELECT  
SUM(CASE WEEK WHEN '星期一' THEN INCOME END) AS [星期一],
SUM(CASE WEEK WHEN '星期二' THEN INCOME END) AS [星期二],
SUM(CASE WEEK WHEN '星期三' THEN INCOME END) AS [星期三],
SUM(CASE WEEK WHEN '星期四' THEN INCOME END) AS [星期四],
SUM(CASE WEEK WHEN '星期五' THEN INCOME END) AS [星期五],
SUM(CASE WEEK WHEN '星期六' THEN INCOME END) AS [星期六],
SUM(CASE WEEK WHEN '星期日' THEN INCOME END) AS [星期日]
FROM WEEK_INCOME



SELECT [星期一],[星期二],[星期三],[星期四],[星期五],[星期六],[星期日]
FROM WEEK_INCOME
PIVOT
(
    SUM(INCOME) for [week] in([星期一],[星期二],[星期三],[星期四],[星期五],[星期六],[星期日])
)TBL

 4.order by case

DECLARE @t1 TABLE 
( 
c1 INT, 
c2 INT 
); 

 

SELECT c1, c2
  FROM @t1
ORDER BY CASE WHEN c1 >= 3 THEN 1 ELSE 2 END,
         CASE WHEN c2 <= 3 THEN 1 ELSE 2 END

 

c1 c2 c1排序 c2排序
4 1 1 1
4 2 1 1
3 6 1 2
3 5 1 2
1 3 2 1
1 2 2 1
2 2 2 1
2 1 2 1
1 4 2 2

 

 

 

x.待续

posted @ 2015-01-06 13:40  PROS  阅读(314)  评论(0编辑  收藏  举报