您的位置 主页 正文

sql分组查询

一、sql分组查询 -- SQLSERVER 语句 select c.classname, (case c.pid when 0 then (select count(*)from record r1 where r1.cid1 = c.id and r1.status = 0) else (select count(*) from record r1 where r1.cid2 = c.id and r1.status = 0) end) a

一、sql分组查询

-- SQLSERVER 语句 select c.classname, (case c.pid when 0 then (select count(*)from record r1 where r1.cid1 = c.id and r1.status = 0) else (select count(*) from record r1 where r1.cid2 = c.id and r1.status = 0) end) as '及格', (case c.pid when 0 then (select count(*)from record r1 where r1.cid1 = c.id and r1.status = 1) else (select count(*) from record r1 where r1.cid2 = c.id and r1.status = 1) end) as '良', (case c.pid when 0 then (select count(*)from record r1 where r1.cid1 = c.id and r1.status = 2) else (select count(*) from record r1 where r1.cid2 = c.id and r1.status = 2) end) as '优秀' from CLASS c ---ORACLE 写法 select c.classname, (decode(c.cid,0,(select count(*)from record r1 where r1.cid1 = c.id and r1.status= 0), (select count(*) from record r1 where r1.cid2 = c.id and r1.status= 0)))jige, (decode(c.cid,0,(select count(*)from record r1 where r1.cid1 = c.id and r1.status= 1), (select count(*) from record r1 where r1.cid2 = c.id and r1.status= 1)))liang, (decode(c.cid,0,(select count(*)from record r1 where r1.cid1 = c.id and r1.status= 2), (select count(*) from record r1 where r1.cid2 = c.id and r1.status= 2)))youxiu from CLASS c

二、PB查询语句

第一种可以使用数据窗口来实现。数据窗口的参数就是房间类型。然后通过一个查询按钮,获取选择是单人间还是双人间,使用dw_1.retrieve(参数)这样的语句,显示在数据窗口中。

第二种可以使用游标。

假设你有一个窗口,上面有一个选项,使用ddlb控件,里面的选项是单人间,双人间。

然后使用一个Mle控件来显示所有的房间编号以及汇总数量。可以写一个查询按钮,点击事件这么写:

string ls_type //房间类型

string ls_room //房间编号

long ll_count = 0 //统计总数

ls_type = ddlb_1.text //选择的类型

mle_1.text = '' //初始化mle控件

declare cs_room cursor for select 房间编号 from 房间表 where 房间类型 = :ls_type;

open cs_room;

fetch cs_room into :ls_room;

do while sqlca.sqlcode = 0

mle_1.text += ls_room + '~r~n' //行尾加上回车换行,下一个编号就会显示到下一行了。

fetch cs_room into :ls_room;

ll_count += 1 //统计总数累加

loop

close cs_room;

mle_1.text += '累计房间数量' + string(ll_count)

三、sql 模糊查找

SQL模糊查询,使用like比较字,加上SQL里的通配符,请参考以下: 1、LIKE'Mc%' 将搜索以字母 Mc 开头的所有字符串(如 McBadden)。 2、LIKE'%inger' 将搜索以字母 inger 结尾的所有字符串(如 Ringer、Stringer)。 3、LIKE'%en%' 将搜索在任何位置包含字母 en 的所有字符串(如 Bennet、Green、McBadden)。 4、LIKE'_heryl' 将搜索以字母 heryl 结尾的所有六个字母的名称(如 Cheryl、Sheryl)。 5、LIKE'[CK]ars[eo]n' 将搜索下列字符串:Carsen、Karsen、Carson 和 Karson(如 Carson)。 6、LIKE'[M-Z]inger' 将搜索以字符串 inger 结尾、以从 M 到 Z 的任何单个字母开头的所有名称(如 Ringer)。 7、LIKE'M[^c]%' 将搜索以字母 M 开头,并且第二个字母不是 c 的所有名称(如MacFeather)。 ------------------------------------------------- 呵呵,要完整的例句啊。下面这句查询字符串是我以前写的,根据变量 zipcode_key 在邮政编码表 zipcode 中查询对应的数据,这句是判断变量 zipcode_key 为非数字时的查询语句,用 % 来匹配任意长度的字符串,从表中地址、市、省三列中查询包含关键字的所有数据项,并按省、市、地址排序。这个例子比较简单,只要你理解了方法就可以写出更复杂的查询语句。 sql = select * from zipcode where (address like'% & zipcode_key & %') or (city like'% & zipcode_key & %') or (province like'% & zipcode_key & %') order by province,city,address

为您推荐

返回顶部