`
yanyanquan
  • 浏览: 450521 次
  • 性别: Icon_minigender_1
  • 来自: 江门
社区版块
存档分类
最新评论

MS-SQL Case when

    博客分类:
  • SQL
阅读更多

显示时间:

select   getdate()   as   日期,case   month(getdate())   
  when   11   then   '十一'   
  when   12   then   '十二'   
  else   substring('一二三四五六七八九十',   month(getdate()),1)   
  end+'月'   as   月份

  

GROUP BY 子句中的 CASE when

SELECT 'Number of Titles', Count(*) 
FROM titles 
GROUP BY 
    CASE 
        WHEN price IS NULL THEN 'Unpriced' 
        WHEN price < 10 THEN 'Bargain' 
        WHEN price BETWEEN 10 and 20 THEN 'Average' 
        ELSE 'Gift to impress relatives' 
    END 
GO 

 

  ORDER BY 的Case when

SELECT 
    CASE 
        WHEN price IS NULL THEN 'Unpriced' 
        WHEN price < 10 THEN 'Bargain' 
        WHEN price BETWEEN 10 and 20 THEN 'Average' 
        ELSE 'Gift to impress relatives' 
    END AS Range, 
     Title 
FROM titles 
GROUP BY 
    CASE 
        WHEN price IS NULL THEN 'Unpriced' 
        WHEN price < 10 THEN 'Bargain' 
        WHEN price BETWEEN 10 and 20 THEN 'Average' 
        ELSE 'Gift to impress relatives' 
    END, 
     Title 
ORDER BY 
    CASE 
        WHEN price IS NULL THEN 'Unpriced' 
        WHEN price < 10 THEN 'Bargain' 
        WHEN price BETWEEN 10 and 20 THEN 'Average' 
        ELSE 'Gift to impress relatives' 
    END, 
     Title 
GO  

 

分享到:
评论

相关推荐

    ms-sql函數,用於計算個人所的稅

    SELECT @Tax = CASE WHEN ISNULL(@Income, 0) &lt;= 500 THEN -- 500 HU ISNULL(@Income, 0) * 5 / 100.0 WHEN ISNULL(@Income, 0) &gt; 500 AND ISNULL(@Income, 0) &lt;= 2000 THEN -- 500 -- 2000 ISNULL(@Income, 0) ...

    PostgreSQL_与_MS_SQLServer比较

    MS SQL Server 支持简单 CASE 和搜索 CASE 函数,而 PostgreSQL 的 CASE 结构更为灵活,例如: - **MS SQL Server**: ```sql CASE sex WHEN '1' THEN '男' WHEN '2' THEN '女' ELSE '其他' END ``` - **...

    sql学习之CASE WHEN THEN ELSE END的用法

    而在MS SQL Server中,`CASE`语句提供了更复杂逻辑的支持,例如: ```sql SELECT id, CASE WHEN bz = '1' THEN xx WHEN bz = '2' THEN yy ELSE zz END AS tt FROM xxx ``` 在上述例子中,当`bz`字段等于'1...

    微软内部资料-SQL性能优化2

    The following definitions will help you understand how SQL Server interacts with the operating system when allocating and using memory. Virtual Address Space A set of memory addresses that are mapped...

    MS SQL Server数据库快捷生成表结构语句

    (CASE WHEN c身份验证 = 1 THEN ' IDENTITY(' + CAST(ISNULL(c.seed, 0) AS VARCHAR(10)) + ',' + CAST(ISNULL(c.increment, 1) AS VARCHAR(10)) + ')' ELSE '' END) FROM sys.columns c INNER JOIN sys.types...

    ms sql 行列转换

    @sql = @sql + ' , max(case Subject when ''' + Subject + ''' then Result else 0 end) [' + Subject + ']' from (select distinct Subject from tb) as a set @sql = @sql + ' from tb group by name' exec(@...

    MS SQL 存储过程解密

    SET @sql1 = (CASE WHEN @number &gt; 1 THEN 'ALTER PROCEDURE ' + @objectName + ';' + RTRIM(@k) + ' WITH ENCRYPTION AS' ELSE 'ALTER PROCEDURE ' + @objectName + ' WITH ENCRYPTION AS' END) ... ``` **逐段...

    MSSQL性能监控SQL语句

    ((CASE WHEN statement_end_offset = -1 THEN (LEN(CONVERT(nvarchar(max), s2.text)) * 2) ELSE statement_end_offset END) - statement_start_offset) / 2 + 1)) AS sql_statement, execution_count, plan_...

    SQLServer日期时间函数

    SELECT @m = CASE WHEN @m BETWEEN 1 AND 3 THEN 1 WHEN @m BETWEEN 4 AND 6 THEN 4 WHEN @m BETWEEN 7 AND 9 THEN 7 ELSE 10 END; SELECT @time = DATENAME(year, GETDATE()) + '-' + CONVERT(varchar(10), @m...

    SQL数据字典查询

    CASE WHEN COLUMNPROPERTY(a.id, a.name, 'IsIdentity') = 1 THEN '√' ELSE '' END AS 标识, CASE WHEN EXISTS (SELECT 1 FROM dbo.sysindexes si INNER JOIN dbo.sysindexkeys sik ON si.id = sik.id AND...

    Sql效率查询

    - **总CPU时间ms**:表示执行这些SQL所消耗的总CPU时间。 - **物理读取次数**:表示从磁盘读取数据块的次数。 - **每执行一次逻辑读取次数**:表示每次执行SQL时从缓存读取数据页的平均次数。 - **逻辑读取次数**:...

    获取SQL-Server中的所有数据库用户,数据表,列,备注说明,主外键.docx

    CASE WHEN pk.is_nullable = 1 THEN '' ELSE 'PK' END, CASE WHEN fk.is_nullable = 1 THEN '' ELSE ',FK' END ) AS [ 主/外键], pkc.name AS 主键字段, fkc.name AS 外键字段, pkc.system_type_id AS 数据...

    使用SQL查询 生成sql server数据库结构字典 生成html

    CASE WHEN col.colorder = 1 THEN '&lt;/table&gt;&lt;h3&gt;' + obj.name + ':' + COALESCE(epTwo.value, '') + '&lt;/h3&gt;&lt;table class="gridtable"&gt; 序号&lt;/th&gt;&lt;th&gt;列名&lt;/th&gt;&lt;th&gt;列说明&lt;/th&gt;&lt;th&gt;数据类型&lt;/th&gt;&lt;th&gt;长度&lt;/th&gt;&lt;th&gt;...

    SqlServer2000&2005数据库字典及表结构

    CASE WHEN COLUMNPROPERTY(a.id, a.name, 'IsIdentity') = 1 THEN '是' ELSE '否' END AS IsIdentity, CASE WHEN EXISTS ( SELECT 1 FROM dbo.sysindexes si INNER JOIN dbo.sysindexkeys sik ON si.id = sik....

    与时间相关的SQL语句

    CASE WHEN DATEPART(hour, GETDATE()) BETWEEN 0 AND 11 THEN '上午' WHEN DATEPART(hour, GETDATE()) BETWEEN 12 AND 17 THEN '下午' ELSE '晚上' END) AS TimeInfo; ``` 这个脚本首先使用`DATENAME`函数获取...

    用SQL查询表中字段的属性2000和2005

    TableDesc=ISNULL(CASE WHEN C.column_id=1 THEN PTB.[value] END,N''), Column_id=C.column_id, ColumnName=C.name, PrimaryKey=ISNULL(IDX.PrimaryKey,N''), [IDENTITY]=CASE WHEN C.is_identity=1 THEN ...

    快速定位MSSQL占用CPU高的sql方法

    WHEN deqs.statement_end_offset = -1 THEN DATALENGTH(dest.text) ELSE deqs.statement_end_offset END - deqs.statement_start_offset ) / 2 + 1) AS [执行SQL], dest.text as [完整SQL], db_name(dest....

    获取SQL-Server中的所有数据库用户,数据表,列,备注说明,主外键.pdf

    CASE WHEN c.CHARACTER_MAXIMUM_LENGTH IS NULL THEN '' ELSE CAST(c.CHARACTER_MAXIMUM_LENGTH AS VARCHAR(50)) END AS 字段长度, ISNULL(coldesc.[value], '') AS 字段说明 FROM INFORMATION_SCHEMA.COLUMNS ...

    sql server 时间查询

    SET @thruDate = DATEADD(day, CASE WHEN DATEPART(weekday, @thruDate) % 7 &lt;= 1 THEN -1 - DATEPART(weekday, @thruDate) % 7 ELSE 0 END, @thruDate) SET @result = DATEDIFF(hour, @fromDate, @thruDate) / 24...

    慢sql监控mysql及oracle.pdf

    SUBSTRING(st.text, (qs.statement_start_offset/2) + 1, ((CASE statement_end_offset WHEN -1 THEN DATALENGTH(st.text) ELSE qs.statement_end_offset END - qs.statement_start_offset)/2) + 1) N'执行语句', ...

Global site tag (gtag.js) - Google Analytics