1. sql 怎麼根據一個欄位的值把數據拆分成多條數據
可以這樣來查詢:
1
2
3
4
5
6
7
8
9
10
<a href="https://www..com/s?wd=select&tn=44039180_cpr&fenlei=-EUBtkn16zPHbsrjRYrjmLPjcdPHfs" target="_blank" class="-highlight">select</a> s.a01, s.<a href="https://www..com/s?wd=a02&tn=44039180_cpr&fenlei=-EUBtkn16zPHbsrjRYrjmLPjcdPHfs" target="_blank" class="-highlight">a02</a>, 1.000 <a href="https://www..com/s?wd=as&tn=44039180_cpr&fenlei=-EUBtkn16zPHbsrjRYrjmLPjcdPHfs"target="_blank" class="-highlight">as</a> a03, s.a04
<a href="https://www..com/s?wd=from&tn=44039180_cpr&fenlei=-EUBtkn16zPHbsrjRYrjmLPjcdPHfs" target="_blank" class="-highlight">from</a> table_name s,
(
<a href="https://www..com/s?wd=select&tn=44039180_cpr&fenlei=-EUBtkn16zPHbsrjRYrjmLPjcdPHfs" target="_blank" class="-highlight">select</a> 1.000 <a href="https://www..com/s?wd=as&tn=44039180_cpr&fenlei=-EUBtkn16zPHbsrjRYrjmLPjcdPHfs" target="_blank" class="-highlight">as</a> val
union
<a href="https://www..com/s?wd=select&tn=44039180_cpr&fenlei=-EUBtkn16zPHbsrjRYrjmLPjcdPHfs" target="_blank" class="-highlight">select</a> 2.000
union
select 3.000
) c
where s.a03 >= c.val
2. sql 將一個欄位的數據分列顯示
SQL2000不支持開窗函數row_number() ,實現這種效果可以藉助存儲過程。
CREATE PROCEDURE 存儲過程2
AS
set nocount on
/* 創建一個臨時表,利用identity 添加一個從1開始的連續標識列 */
/* x欄位我設置為變長字元串型,請更改為與原始欄位類型相同 */
create table #temp (id int identity,X varchar(50))
/* 將原始表中數據插入臨時表 */
insert into #temp(x) select colName from tableName order by colName
/* 輸出希望得到列表效果 */
/* 思路:id除4求模數根據結果將X值分別放到欄位a,b,c */
/* 用(case id%4 when 1 then id + 3 when 2 then id +2 when 3 then id +1 else id end)將每4條記錄標識為一組 */
/* 最後通過求分組最大值得辦法得到最終列表 */
select
max(case (case id%4 when 1 then 1 when 2 then 2 when 3 then 3 else 0 end) when 1 then x else null end) as a,
max(case (case id%4 when 1 then 1 when 2 then 2 when 3 then 3 else 0 end) when 2 then x else null end) as b,
max(case (case id%4 when 1 then 1 when 2 then 2 when 3 then 3 else 0 end) when 3 then x else null end) as c,
max(case (case id%4 when 1 then 1 when 2 then 2 when 3 then 3 else 0 end) when 0 then x else null end) as d
from #temp
group by (case id%4 when 1 then id + 3 when 2 then id +2 when 3 then id +1 else id end)
set nocount off
上面代碼源自下面存儲過程,相對容易理解,但是由於過程里使用多一次操作查詢(update)其效率也許會慢一些(但是本人未證實)
CREATE PROCEDURE 存儲過程1
AS
set nocount on
create table #temp (id int identity,X nvarchar(50),idd int, flag smallint)
insert into #temp(x) select colName from tableName order by colName
update #temp set flag=case id%4 when 1 then 1 when 2 then 2 when 3 then 3 else 0 end,
idd=case id%4 when 1 then id + 3 when 2 then id +2 when 3 then id +1 else id end
select
max(case flag when 1 then x else null end) as a,
max(case flag when 2 then x else null end) as b,
max(case flag when 3 then x else null end) as c,
max(case flag when 0 then x else null end) as d
from #temp group by idd
set nocount off
3. SQL統計根據同一個欄位的不同值,統計其他欄位和,分列顯示,求高手解答!
selectmonthas'月份',sum(score1)as'score1月小計',sum(score2)as'score2月小計',sum(score3)as'score3月小計'
from表格名稱
whereyear=2017
groupbymonth;
4. SQL查同一個欄位輸出兩列
你好,按照你問題是需要按year分組,並且組內按照計算code開頭為1的quantity之和以及code開頭為2的quantity之和的數據。首先分析,既然需要按照year分組,那麼就需要用到group by 那麼 就可以寫出如下:select year , xx, xx from 表名 group by year如何計算組內code開頭為1的quantity之和以及code開頭為2的quantity之和的數據,那麼需要用到sum,並且分別刷選出code開頭為1和code開頭為2的數據即可。完整SQL如下:select year, sum (case when quantitiy like '%1' then quantitiy else 0 end as ) as 開頭為1的quantity之和為一列, sum (case when quantitiy like '%2' then quantitiy else 0 end as ) as 開頭為2的quantity之和為一列 from 表名 group by year
5. SQL語句怎樣使查詢同一表同一列多條數據分列顯示
declare @sql varchar(8000)
set @sql = 'select 編碼,名稱'
select @sql = @sql+', max(case [項目序號] when '''+cast(項目序號 as varchar)+''' then 數額 else 0 end ) AS [項目序號'+cast(項目序號 as varchar)+']'
from (select distinct 項目序號 from 表名) as a
set @sql = @sql +' from 表名 group by 編碼,名稱'
select @sql
exec (@sql)
6. sql server里select中將一個欄位中的內容分成兩個列查出
select FName=left(name,4),sname
=right(name,4) from Table
顯示就是
FName SName
mick sorft
7. SQL 同一欄位不同內容進行分組顯示的
SELECT
姓名
住院號
SUM(case
when
費別=『西葯費』
then
金額
else
0
end
)
as
西葯費
SUM(case
when
費別=『CT費』
then
金額
else
0
end
)
as
CT費
SUM(case
when
費別=『電診費』
then
金額
else
0
end
)
as
電診費
SUM(case
when
費別=『X光費』
then
金額
else
0
end
)
as
X光費
SUM(case
when
費別=『
葯費』
then
金額
else
0
end
)
as
重要費
from
table
group
by
姓名
住院號
8. SQL根據欄位內容進行分列,sql語句怎麼寫
這種不好弄。。
--
select id,replace(col,'2015-','|2015-') from table_name;
用得到的這個結果,根據|這個分隔符重新導入一個新表,之後就好弄了
9. SQL同一個欄位依照條件分列顯示
select case when 成績>=55 then 成績 else 0 end '大於55',
case when 成績<55 then 成績 else 0 end '小於55'
from tab
10. 怎樣將SQL查出的欄位分成兩列解決辦法
select substr(欄位名稱,1,x),substr(欄位名稱,x+1) from 表名;
其中X代表你要截取的該欄位要設置為第一列的長度。
例如:
select substr(empno,1,2)第一列,substr(empno,3)第二列 from emp;
這樣查詢出來顯示的就把 empno 欄位分為了2列。