资料库SQL语句 表格式转换

资料库SQL语句 表格式转换,第1张

资料库SQL语句 表格式转换 declare @b table(barcode varchar(10), size varchar(5), quantity int, requantity int)

declare @a table(barcode varchar(10), s1 int, s2 int, s3 int,s4 int,s5 int)

insert into @a

select '1009823',0,0,1,0,2 union

select '1009824',3,0,1,0,3 union

select '1009825',-1,2,0,3,0

select * from @a

select * from (

select barcode, 's1' as size,case when s1>0 then s1 else 0 end quantity, case when s1<0 then s1 else 0 end requantity from @a union

select barcode, 's2' as size,case when s2>0 then s2 else 0 end quantity, case when s2<0 then s2 else 0 end requantity from @a union

select barcode, 's3' as size,case when s3>0 then s3 else 0 end quantity, case when s3<0 then s3 else 0 end requantity from @a union

select barcode, 's4' as size,case when s4>0 then s4 else 0 end quantity, case when s4<0 then s4 else 0 end requantity from @a union

select barcode, 's5' as size,case when s5>0 then s5 else 0 end quantity, case when s5<0 then s5 else 0 end requantity from @a

) t

where not exists ( select 1 from (

select barcode, 's1' as size,case when s1>0 then s1 else 0 end quantity, case when s1<0 then s1 else 0 end requantity from @a union

select barcode, 's2' as size,case when s2>0 then s2 else 0 end quantity, case when s2<0 then s2 else 0 end requantity from @a union

select barcode, 's3' as size,case when s3>0 then s3 else 0 end quantity, case when s3<0 then s3 else 0 end requantity from @a union

select barcode, 's4' as size,case when s4>0 then s4 else 0 end quantity, case when s4<0 then s4 else 0 end requantity from @a union

select barcode, 's5' as size,case when s5>0 then s5 else 0 end quantity, case when s5<0 then s5 else 0 end requantity from @a

) t1

where t1.quantity=0 and t1.requantity=0

and t.barcode = t1.barcode and t.size=t1.size

)

order by barcode

-----------------------------------

(3 个资料列受到影响)

barcode s1 s2 s3 s4 s5

---------- ----------- ----------- ----------- ----------- -----------

1009823 0 0 1 0 2

1009824 3 0 1 0 3

1009825 -1 2 0 3 0

(3 个资料列受到影响)

barcode size quantity requantity

---------- ---- ----------- -----------

1009823 s3 1 0

1009823 s5 2 0

1009824 s1 3 0

1009824 s3 1 0

1009824 s5 3 0

1009825 s1 0 -1

1009825 s2 2 0

1009825 s4 3 0

(8 个资料列受到影响)

资料库SQL语句

给你一个参巧一下:

查询房费大于200的可以尝试这样写SQL语句:

select * from guest where Money>200 and Details='房费'

然后是执行该SQL语句.应该可以获取到想要的效果.其它的效果就模拟这个样式写就好了,要注意的一点是 字串内容查询需要把 字串 使用单引号 括起来 比如说 '房费'这里.

以前在SQL 中写SP 时,如比较复杂时,喜欢中间使用临时表来暂存相关记录,这样的好处有很多,提高效率,提高程式的可读性等。当然后临时表的使用,一般均会使用使用者临时表,即 #TempTable, 但有一些情况下,偶尔也会使用系统临时表,即 ##TempTable。

两种临时表的的使用语法差不多,可用几种方法来建立,可 Create ,也可 Select Into 。

当然关键的是系统临时表和使用者临时表的区别:(如下)

1)使用者临时表:使用者临时表的名称以单个数字元号(#)开头

使用者临时表只对建立这个表的使用者的Session可见,对其他程序是不可见的.

当建立它的程序消失时这个临时表就自动删除.

2)系统临时表:系统临时表的名称以数字符号(##)开头

全域性临时表对整个SQL Server例项都可见,但是所有访问它的Session都消失的时候,它也自动删除.

明白了这些就知道了他们的用途和限制,但有一些地方还是容易出问题,故在此专门列出。

1, 在使用 Exec(SQLScript) 执行Script 时,其间也相当于单独有一个程序处理,故执行期间内如果建立使用者临时表的话,在执行完成后也就结束了,即执行完成后,你不可以使用在 SQLScript 中生成的使用者临时表,可以用系统临时表代替。

2, 在使用使用者临时表时,有一个问题要注意,就是最好在建立时指定其使用者为 dbo ,以避免可能的问题;

3, 在使用系统临时表时,一定要考虑到,不可以将其用于多使用者使用的环境功能或系统中,否则就可能出现冲突的问题,导致结果不可预料。

如果在多使用者使用的环境中使用系统临时表,则可能会出现多个使用者同时对同一系统临时表进行处理,从而导致冲突和资料的错误。以前没有注意这一点,我就因此而浪费过不少的时间。

有时需要使用Exec(SQLScript)方式产生资料,但其中只能使用系统临时表,如何处理呢?

1, 可以预先定义好一个使用者临时表,然后使用 Insert #TempTable Exec(SQLScript) 的方式,即可将Exec 产生的结果记录加入使用者临时表,从而避免使用系统临时表;

2, 从根本上避免使用 Exec() ,可用其它方式代替。

Exec() 的使用是因为有一些 Script 比较复杂,其中需要一些组合字元,如 in ('','','') 或其它可能的情况,在此情况,无法直接使用一般的Script 产生记录,只能先产生一个组合的Script ,然后用Exec 执行

1 avg=avg(gmark)

2 student a,s-c

3 class=''10

4 sno

5 distinct sname

6 student a

7 ()

8 sno,o

9 sname Desc

10 gmark,sname

11 s-c.sno=student.sno. group by sno

12 =

13 distinct(max(mark))

14 <

15 not null

1 select * from 表名 where 部门ID = 20

2 select 员工号,员工名,部门号 from 表名 where 工种=CLERK

3 select * from 表名 where COMM>SAL

4 select * from 表名 where COMM>(SAL*0.2)

5 select * from 表名 where (部门ID = 10 and 工种=MANAGER) or(部门ID=20 and 工种=CLERK)

6 select * from 表名 where 工种!=MANAGER and 工种!=CLERK and 工资>1999

7 select 工种 from 表名 where 奖金 != null

select r.读者编号,姓名 from Readers r

inner join Brrowinf b on b.读者编号=r.读者编号

where 借期='2012-1-1'

没执行过这语句,你试试,有错误再贴出来我再改改

drop database 资料库名 --删除资料库的

drop table 表名--删除表的

delete from 表名 where 条件 --删除资料的

truncate table 表名 也是删除资料库的.但是他可以裁断序列 这个你跟DELETE 对照试一下就知道了

资料库sql语句

1:

select ft.s_name

from student ft

where ft.s_id not in (select distinct

a.s_id

from chosen_class a

left join class b on a.c_id = b.c_id

where b.c_name like '%C1%'

)

2:

select b.c_name,

avg(a.grade) as grade_avg

from chosen_class a

left join class b on a.c_id = b.c_id

group by b.c_name

order by grade_avg desc

3:

select b.s_name,

count(distinct a.c_id) as class_num

from chosen_class a

left join student b on a.s_id = b.s_id

group by b.s_name

having count(distinct a.c_id) >= 2

这个没办法做,只有=999 显示或隐藏一行。列没地方设定。

如果一定要做,那你只能拼字串,先查一列是否有符合的,有的话就不放在select 语句中。

select count(*) from (selectworkerId, workerName, t1.departmentId, departmentName, telephone from worker t1 left join department t2on t1.departmentId = t2.departmentId)

Year(@time), Month(@time) 2 个函数即可。

然后用它们来组装字符串也就简单了。

CONVERT(VarChar(7), @time, 120)会得到YYYY-MM。调用。

用转成字符串,,但此时他已经不是时间类型。

CONVERT(char(7),"你的时间例",120)


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/10818308.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-11
下一篇 2023-05-11

发表评论

登录后才能评论

评论列表(0条)

保存