日期:2014-05-17 浏览次数:20549 次
---------------------------- -- Author :TravyLee(物是人非事事休,欲语泪先流!) -- Date :2012-10-26 10:34:50 -- Version: -- Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86) -- Jul 9 2008 14:43:34 -- Copyright (c) 1988-2008 Microsoft Corporation -- Developer Edition on Windows NT 6.1 <X86> (Build 7600: ) -- ---------------------------- --> 测试数据:[test] if object_id('[test]') is not null drop table [test] go create table [test]([ID] int,[A1] int,[A2] int) insert [test] select 1,11,100 union all select 2,12,100 union all select 3,21,100 union all select 4,22,100 union all select 5,22,100 union all select 6,23,100 union all select 7,11,101 union all select 8,22,101 union all select 9,12,101 go select a.ID, a.A1, a.A2, b.number as 个位, c.number as 十位 from test a inner join master..spt_values b on RIGHT(LTRIM(a.A1),1)=b.number inner join master..spt_values c on LEFT(LTRIM(a.A1),1)=c.number where b.number between 0 and 9 and b.type='p' and c.number between 0 and 9 and c.type='p' ----------------结果---------------------------- /* ID A1 A2 个位 十位 ----------- ----------- ----------- ----------- ----------- 1 11 100 1 1 2 12 100 2 1 3 21 100 1 2 4 22 100 2 2 5 22 100 2 2 6 23 100 3 2 7 11 101 1 1 8 22 101 2 2 9 12 101 2 1 (9 行受影响) */ 这是我用系统表把数字给你分开来了 但是也要写好大一堆代码才可以实现 不知道别人有什么好的方法 不过我觉得你还是得写代码才可以的
------解决方案--------------------
SELECT [A2],COUNT(*)Total_A1 , SUM(CASE RIGHT([A1],1) WHEN 1 THEN 1 ELSE 0 END ) Total_A1_01, SUM(CASE LEFT([A1],1) WHEN 1 THEN 1 ELSE 0 END )Total_A1_02 ....... FROM #tb GROUP BY [A2]
------解决方案--------------------
SELECT [A2],COUNT(*)Total_A1 , SUM(CASE WHEN RIGHT([A1],1)=1 AND LEFT([A1],1)=1 THEN 1 ELSE 0 END ) Total_A1_01, SUM(CASE WHEN RIGHT([A1],1)=1 AND LEFT([A1],1)=2 THEN 1 ELSE 0 END )Total_A1_02, SUM(CASE WHEN RIGHT([A1],1)=1 AND LEFT([A1],1)=3 THEN 1 ELSE 0 END )Total_A1_03, SUM(CASE WHEN RIGHT([A1],1)=1 AND LEFT([A1],1)=4 THEN 1 ELSE 0 END )Total_A1_04, SUM(CASE WHEN RIGHT([A1],1)=1 AND LEFT([A1],1)=5 THEN 1 ELSE 0 END )Total_A1_05, SUM(CASE WHEN RIGHT([A1],1)=1 AND LEFT([A1],1)=6 THEN 1 ELSE 0 END )Total_A1_06, SUM(CASE WHEN RIGHT([A1],1)=1 AND LEFT([A1],1)=7 THEN 1 ELSE 0 END )Total_A1_07, SUM(CASE WHEN RIGHT([A1],1)=1 AND LEFT([A1],1)=8 THEN 1 ELSE 0 END )Total_A1_08, SUM(CASE WHEN RIGHT([A1],1)=1 AND LEFT([A1],1)=9 THEN 1 ELSE 0 END )Total_A1_09, SUM(CASE WHEN RIGHT([A1],1)=2 AND LEFT([A1],1)=1 THEN 1 ELSE 0 END )Total_A1_01, SUM(CASE WHEN RIGHT([A1],1)=2 AND LEFT([A1],1)=2 THEN 1 ELSE 0 END )Total_A1_02, SUM(CASE WHEN RIGHT([A1],1)=2 AND LEFT([A1],1)=3 THEN 1 ELSE 0 END )Total