免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12345
最近访问板块 发新帖
楼主: tigerfish
打印 上一主题 下一主题

我所收集整理的一些语句! [复制链接]

论坛徽章:
0
41 [报告]
发表于 2006-10-21 22:55 |只看该作者
不错,谢谢分享!!!

论坛徽章:
0
42 [报告]
发表于 2006-10-21 23:28 |只看该作者
辛苦了....神龟先生

论坛徽章:
0
43 [报告]
发表于 2006-10-26 00:02 |只看该作者
0039--复杂行例转换

一条看似简单,但是复杂的SQL题目?
Consider the following account and customer tables:

表1:cust_tbl
cust_id title e_first_name e_last_name address1 .
0 MR Martin Ma .
1 MR Kirs Cheung .
2 MR Ricky Chan .
3 MR Tom Kwan .
4 MR Corporate Default Corporate Default .
5 MRS Mary Mok .


表2:acc_grp_cust_tbl
acc_group Cust_id1 Cust_id2 Cust_id3 Cust_id4
1400 0 1 2 .
1500 3 4 . .
1600 5 . . .


The acc_grp_cust_tbl table is responsible to store the customer ID, and the cust_tbl table is responsible to store customer personal information such as name, address, etc… Please write a SQL query in order to provide following result.

ACC_GROUP PAYEENAMES
1400 Ma Martin/Cheung Kris/Chan Ricky
1500 Kwan Tom/Corporate Default Corporate Default
1600 Mok Mary


要注意PAYEENAMES显示的结果方式
请问Answer: 是什么?








if object_id(\'tempdb..#cust_tbl\') is not null
drop table #cust_tbl
GO

create table #cust_tbl([cust_id] [char](1) , [e_first_name] [varchar](100) , [e_last_name] [varchar](100))
insert into #cust_tbl(cust_id,e_first_name,e_last_name) values(\'0\',\'Martin\',\'Ma\')
insert into #cust_tbl(cust_id,e_first_name,e_last_name) values(\'1\',\'Kirs\',\'Cheung\')
insert into #cust_tbl(cust_id,e_first_name,e_last_name) values(\'2\',\'Ricky\',\'Chan\')
insert into #cust_tbl(cust_id,e_first_name,e_last_name) values(\'3\',\'Tom\',\'Kwan\')
insert into #cust_tbl(cust_id,e_first_name,e_last_name) values(\'4\',\'Corporate Default Corporate Default \',\'\')
insert into #cust_tbl(cust_id,e_first_name,e_last_name) values(\'5\',\'Mary\',\'Mok\')

if object_id(\'tempdb..#a\') is not null
drop table #a
GO
select cust_id , e_last_name + \' \' + e_first_name as PAYEENAMES into #a from #cust_tbl

if object_id(\'tempdb..#acc_grp_cust_tbl\') is not null
drop table #acc_grp_cust_tbl
GO
create table #acc_grp_cust_tbl([acc_group] [char](4) , [Cust_id1] [char](1) , [Cust_id2] [char](1) , [Cust_id3] [char](1))
insert into #acc_grp_cust_tbl(acc_group,Cust_id1,Cust_id2,Cust_id3) values(\'1400\',\'0\',\'1\',\'2\')
insert into #acc_grp_cust_tbl(acc_group,Cust_id1,Cust_id2,Cust_id3) values(\'1500\',\'3\',\'4\',\'\')
insert into #acc_grp_cust_tbl(acc_group,Cust_id1,Cust_id2,Cust_id3) values(\'1600\',\'5\',\'\',\'\')

if object_id(\'tempdb..#b\') is not null
drop table #b
GO
select * into #b from
(
select acc_group,Cust_id1 as Cust_id from #acc_grp_cust_tbl where Cust_id1 <> \'\'
union all
select acc_group,Cust_id2 as Cust_id from #acc_grp_cust_tbl where Cust_id2 <> \'\'
union all
select acc_group,Cust_id3 as Cust_id from #acc_grp_cust_tbl where Cust_id3 <> \'\'
)t

if object_id(\'tempdb..tb\') is not null
drop table tb
GO
select #b.acc_group ,#a.PAYEENAMES into tb from #a,#b where #a.cust_id = #b.cust_id

if object_id(\'tempdb..fn_合并\') is not null drop function fn_合并
go
create function fn_合并(
@acc_group varchar(20)
)
returns varchar(300)
as
begin
declare @r varchar(300)
set @r=\'\'
select top 3 @r=@r+\'/\'+PAYEENAMES from tb where acc_group=@acc_group
if @r<>\'\'
set @r=stuff(@r,1,1,\'\')
return @r
end
go

--调用
select acc_group,dbo.fn_合并(acc_group) as PAYEENAMES from tb group by acc_group


acc_group PAYEENAMES
--------- -------------
1400 Ma Martin/Cheung Kirs/Chan Ricky
1500 Kwan Tom/ Corporate Default Corporate Default
1600 Mok Mary

(所影响的行数为 3 行)

论坛徽章:
0
44 [报告]
发表于 2006-10-26 00:03 |只看该作者
0040--怎么列出1-100的列表

select a+10*b+1 as n
from (
select 0 as a
union all select 1
union all select 2
union all select 3
union all select 4
union all select 5
union all select 6
union all select 7
union all select 8
union all select 9
) as t1,(
select 0 as b
union all select 1
union all select 2
union all select 3
union all select 4
union all select 5
union all select 6
union all select 7
union all select 8
union all select 9
) as t2
order by n

论坛徽章:
0
45 [报告]
发表于 2006-10-26 00:03 |只看该作者
0041 -- 怎麼樣用SQL語句把TXT文檔導入到數據庫
我現在TXT文檔的格式為:
1720061016000057208
1720061016000057208
2620061016000054221
2620061016000053042
2220061016000154955
2620061016000154083

要求導入後分成四個字段
Col01  Col02      Col03  Col04
17     20061016   0000   57209

請問.怎麼樣才能實現>?



SELECT
        Col01 = SUBSTRING(F1, 1, 2),
        Col02 = SUBSTRING(F1, 3, ,
        Col03 = SUBSTRING(F1, 11, 4),
        Col04 = SUBSTRING(F1, 15, 5)
FROM(
SELECT F1 = CONVERT(varchar, CONVERT(decimal(3, F1))
FROM OPENROWSET(\'MICROSOFT.JET.OLEDB.4.0\'
        ,\'Text;HDR=NO;DATABASE=C:\\\'        --c:\\是目录
        ,a#txt)                                        --aa#txt是文本文件名aa.txt
)A

论坛徽章:
0
46 [报告]
发表于 2006-10-26 00:05 |只看该作者
0042--SQL分类汇总的问题(SQL,Group by)

表a(dw,sl,bz)单位代码,数量,标志
表b(dw,mc)单位代码,名称
a表中数据如下
dw                        sl                        bz
1001                        1                        0
1002                        25                        2
1003                        5                        1
1001                        20                        1
1002                        10                        3
b表中数据如下
dw                        mc
1001                        公司a
1002                        公司b
1003                        公司c
1004                        公司d
1005                        公司e
现在想汇总a表中的数据实现下面内容.
dw        sl        tzs(bz=0)        zs(bz=1)        jzzs(bz=2)        jjbg(bz=3)
公司a        21        1                1                0                0
公司b        35        0                0                1                1
公司c        5        0                1                0                0
公司d        0        0                0                0                0
公司e        0        0                0                0                0

用一个sql语句怎么写出来?



--显示不同条件的分组结果记录集
--建立测试环境
create table #a(dw varchar(20),sl int,bz int)
insert into #a
select \'1001\',1,0 union all
select \'1002\',25,2 union all
select \'1003\',5,1 union all
select \'1001\',20,1 union all
select \'1002\',10,3

create table #b(dw varchar(20),mc nvarchar(100))
insert into #b
select \'1001\',\'公司a\' union all
select \'1002\',\'公司b\' union all
select \'1003\',\'公司c\' union all
select \'1004\',\'公司d\' union all
select \'1005\',\'公司e\'
--测试
select B.mc
,isnull((select sum(sl) tzs from #a A where A.dw=B.dw group by dw),0) \'sl\'
,isnull((select sum(sl) tzs from #a A where bz=0 and A.dw=B.dw group by dw),0) \'tzs(bz=0)\'
,isnull((select sum(sl) tzs from #a A where bz=1 and A.dw=B.dw group by dw),0) \'tzs(bz=1)\'
,isnull((select sum(sl) tzs from #a A where bz=2 and A.dw=B.dw group by dw),0) \'tzs(bz=2)\'
,isnull((select sum(sl) tzs from #a A where bz=3 and A.dw=B.dw group by dw),0) \'tzs(bz=3)\'
from #b B
--显示结果
/*
dw        sl        tzs(bz=0)        zs(bz=1)        jzzs(bz=2)        jjbg(bz=3)
公司a        21        1                1                0                0
公司b        35        0                0                1                1
公司c        5        0                1                0                0
公司d        0        0                0                0                0
公司e        0        0                0                0                0
*/
--删除测试环境
drop table #a
drop table #b

论坛徽章:
0
47 [报告]
发表于 2006-10-28 08:29 |只看该作者
不错,值得收藏!

经过N多的Ctrl + C,ctrl+V,有空时慢慢消化再做成CHM
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP