在SAP B1的应用中,最广泛也最方便的就是用SQL来定制自己的报表。B1本身也很灵活地支持将SQL语句直接存到系统中,产生不同的自定义报表。所以对于一个B1的顾问也好,系统管理员也好,具备一定的SQL知识是必要的。下面就分享一些常用的,也比较巧妙的SQL语句案例,以供大家借鉴和收藏。

首先,先来几句最基本的SQL语句:

1、选择:

select from [表名] where [条件] 注明:号表示返回所有的字段

2、插入:

insert into [表名] (字段1,字段2) values(值1,值2)

3、删除:

delete from [表名] where [条件]

4、更新:

update [表名] set 字段1=值 where [条件]

5、查找:

select * from [表名] where 字段1 like ’%值%’

6、排序:

select * from [表名] order by 字段1,字段2 desc 注意:DESC关键字表示排倒序

7、总数:

select count (*) as totalcount from [表名]

8、求和:

select sum(字段) as sumvalue from [表名]

9、平均:

select avg(字段) as avgvalue from [表名]

10、最大:

select max(字段) as maxvalue from [表名]

11、最小:

select min(字段) as minvalue from [表名]

其次,大家来看一些不错的sql语句

1、复制表(只复制结构,源表名:a 新表名:b)

法一:select into b from a where 1<>1
法二:select top 0
into b from a

2、拷贝表(拷贝数据,源表名:a 目标表名:b)

insert into b(a, b, c) select d,e,f from b;

3、显示文章、提交人和最后回复时间

select a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b

4、between的用法,between限制查询数据范围时包括了边界值,not between不包括

select * from table1 where time between time1 and time2
select a,b,c, from table1 where a not between 数值1 and 数值2

5、in 的使用方法

select * from table1 where a [not] in (‘值1’,’值2’,’值4’,’值6’)

6、两张关联表,删除主表中已经在副表中没有的信息

delete from table1 where not exists ( select * from table2 where table1.field1=table2.field1 )

7、日程安排提前五分钟提醒

select * from 日程安排 where datediff(‘minute’,f开始时间,getdate())>5

8、前10条记录

select top 10 * form table1 where 范围

9、选择在每一组b值相同的数据中对应的a最大的记录的所有信息(类似这样的用法可以用于论坛每月排行榜,每月热销产品分析,按科目成绩排名,等等.)

select a,b,c from tablename ta where a=(select max(a) from tablename tb where tb.b=ta.b)

10、包括所有在 TableA 中但不在 TableB和TableC 中的行并消除所有重复行而派生出一个结果表

(select a from tableA ) except (select a from tableB) except (select a from tableC)

11、删除重复记录

Delete from tablename where id not in (select max(id) from tablename group by col1,col2,…)

12、列出数据库里所有的表名

select name from sysobjects where type=’U’

13、列出表里的所有的字段

select name from syscolumns where id=object_id(‘TableName’)

14、选择从10到15的记录

select top 5 from (select top 15 from table order by id asc) table_别名 order by id desc

读懂上面这些SQL语句,你在逻辑思维上会是一次很愉悦的体验!
会写上面这些SQL语句,你在SBO中完全不用害怕任何的报表了!