㈠ sql一列如何分成多列在視圖中顯示出來
分隔字元串代碼 用','分隔
用下面的函數
create function f_splitstr(@SourceSql varchar(8000),@StrSeprate varchar(100)) returns @temp table(F1 varchar(100)) as begin declare @ch as varchar(100) set @SourceSql=@SourceSql+@StrSeprate while(@SourceSql<>'') begin set @ch=left(@SourceSql,charindex(',',@SourceSql,1)-1) insert @temp values(@ch) set @SourceSql=stuff(@SourceSql,1,charindex(',',@SourceSql,1),'') end return end
㈡ sql 一列如何按條件分成多列
------------------------------------------------------------------------
-- author:jc_liumangtu(【DBA】小七)
-- date: 2010-03-26 09:37:30
-- version:
-- Microsoft SQL Server 2005 - 9.00.1399.06 (Intel X86)
-- Oct 14 2005 00:33:37
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 3)
--
------------------------------------------------------------------------use test
set nocount on
if object_id('test4','U') is not null
drop table test4
go
create table test4
(item int ,date char(8),type char(2),quty int)
insert into test4
select 1000 , '20100101', 'A' , 100 union all
select 2000 , '20100101' ,'B' , 200 union all
select 1000 , '20100101' ,'C' , 100 union all
select 2000 , '20100101' , 'D' , 100
select * from test4select item ,date
,max(case type when 'A' then quty else 0 end) [typeA]
,max(case type when 'B' then quty else 0 end) [typeB]
,max(case type when 'C' then quty else 0 end) [typeC]
,max(case type when 'D' then quty else 0 end) [typeD]
from test4 group by item,date
item date type quty
----------- -------- ---- -----------
1000 20100101 A 100
2000 20100101 B 200
1000 20100101 C 100
2000 20100101 D 100item date typeA typeB typeC typeD
----------- -------- ----------- ----------- ----------- -----------
1000 20100101 100 0 100 0
2000 20100101 0 200 0 100
㈢ 怎樣將SQL查出的欄位分成兩列解決辦法
select
substr(欄位名稱,1,x),substr(欄位名稱,x+1)
from
表名;
其中X代表你要截取的該欄位要設置為第一列的長度。
例如:
select
substr(empno,1,2)第一列,substr(empno,3)第二列
from
emp;
這樣查詢出來顯示的就把
empno
欄位分為了2列。
㈣ sql中將一列按照不同長度分成幾列
用CASE WHEN來判斷,分解:
selectCASEWHENLen(a.menu_code)=2THENa.menu_codeELSE''ENDASMenu1
,CASEWHENLen(a.menu_code)=4THENa.menu_codeELSE''ENDASMenu2
,CASEWHENLen(a.menu_code)=6THENa.menu_codeELSE''ENDASMenu3
,a.menu_name
,c.role_name,e.user_in_station_mc,e.user_name,e.bm
frommenu_nameasa
leftjoinmenu_userasbonb.menu_code=a.menu_code
leftjoinuser_roleasconc.role_code=b.user_code
leftjoinuser_role_dyasdond.role_code=c.role_code
leftjoinuser_rightaseone.user_code=d.user_code
wheree.user_state='正常'
anda.menu_namelike'%交款%'
㈤ SQL 如何將一列分成若干列
這個..我試試吧..
先重建一個表吧,不過這次把這個列分為5列,然後我們要做的就是把你原來的數據倒過去
建表的語句就不寫了,這個表為tb_re,你原來的表為tb_or
insert into tb_re select **** from tb_or b;
****這里是關鍵,它包含了你要倒進tb_re的列的內容,當然了,select
之後的結果要和tb_re對應,難點應該就是怎麼拿那5個列出來
我假設你/分割的都只是個位數
現在:第一列 substr(b.aa,1,1)
2 substr(b.aa,3,1)
3 substr(b.aa,5,1)
4 substr(b.aa,7,1)
5 substr(b.aa,9,1)
不是個位數也行,不過就麻煩多了,其實如果樓主是在oracle中就容易做很多,在sql就難多了
感覺是數據表設計問題,以後不要這樣了...
㈥ sql 按行和按列分組
select 省份,sum(case 區域 when '華東' then 1 else 0 end)as '華東',
sum(case 區域 when '華南' then 1 else 0 end)as '華南',
sum(case 區域 when '西南' then 1 else 0 end)as '西南',
sum(case 區域 when '華中' then 1 else 0 end)as '華中',
...
from 表 group by 省份
㈦ sql如何將一張表中的一列數據分為兩列顯示
目前找到一個辦法。
select id,max(money),sum(money)-max(money)
from tt
group by id
㈧ sql查詢中,如何將某列 分成 兩列。
SELECT PAccM33g02,
CASE PAccM33g02
WHEN 0 THEN PAccM33g02 END PAccM33g02_J,
CASE PAccM33g02
WHEN 1 THEN PAccM33g02 END PAccM33g02_C
FROM PAccM3307
㈨ MSSql查詢數值的分布情況
創建一個表WeightLevel,填充數據樣例如下,樣例中只包含了其中四條數據。
SelectLevelName,Count(Weight)
FromWeightLevel,表A
WhereWeight>=MinScoreAndWeight<=MaxScore
GroupByLevelName
OrderByLevelName
㈩ SQL 中如何分列
我來回答吧,下面代碼我測試過,完全運行無誤,而且效率非常高。
你先創建一個 sql函數 ,日期格式轉換 如:12-01-09 為 09-01-12
然後在更新 資料庫。
詳細代碼如下:
go
create function ConvertDateStr(@str varchar(10))
returns varchar(10)
as
begin
-- 12-01-09
declare @d varchar(2),@m varchar(2),@y varchar(4)
set @d = left(@str,2)
set @str = substring(@str,4,len(@str))
set @m = left(@str,2)
set @str = substring(@str,4,len(@str))
set @y = @str
return @y + '-'+ @m + '-' + @d
end
go
-- select dbo.ConvertDateStr('12-01-09')
update 你的表 set 要轉換列 = dbo.ConvertDateStr(要轉換列)