sql的分组查询语句
❶ sql语句如何按年龄段和性别分组查询,麻烦给个具体例子,年龄字段是age,性别字段是sex
1、建表:
CREATETABLE[dbo].[Users](
[Name][nvarchar](50)NULL,
[Age][int]NULL,
[Sex][nchar](10)NULL
)ON[PRIMARY]
GO
2、插入数据:
❷ 求SQL大神给写一个分组查询语句
select 公司编号,部门编号,
max(case when substring(部门分组编号,1,1)='A' then 部门分组编号 else '-' end) 部门分组A,
max(case when substring(部门分组编号,1,1)='B' then 部门分组编号 else '-' end) 部门分组B,
max(case when substring(部门分组编号,1,1)='C' then 部门分组编号 else '-' end) 部门分组C,
max(case when substring(部门分组编号,1,1)='D' then 部门分组编号 else '-' end) 部门分组D
from table group by 公司编号,部门编号
❸ 分组查询SQL怎么写
整个要用到动态SQL语句。
首先用B表做游标,然后使用 case when 把每种类型都统计出来作为一个字段。
示例如下:
--建立示例表
--create table tab_a ( 单位 varchar(100) )
--create table tab_b ( 类型 varchar(10) )
--create table tab_c ( 单位 varchar(100),类型 varchar(10),完成情况 varchar(10) )
insert tab_a ( 单位 )
select '科技局'
union select '交通局'
union select '能源局'
union select '人事局'
insert tab_b ( 类型 )
select '招考'
union select '面试'
union select '税收'
union select '其他'
insert tab_c ( 单位 , 类型 , 完成情况 )
select '科技局' ,'招考','完成'
union all select '科技局' ,'招考','未完成'
union all select '科技局' ,'税收','未完成'
union all select '科技局' ,'税收','未完成'
union all select '科技局' ,'税收','未完成'
union all select '交通局','招考','完成'
union all select '交通局','招考','完成'
union all select '交通局','招考','完成'
union all select '能源局','税收','完成'
union all select '能源局','税收','完成'
union all select '人事局','其他','未完成'
--建立示例表完成
-- 处理数据开始
declare @c_type varchar(10)
declare @c_sql varchar(max)
set @c_sql = ' select 单位, '
declare cur_type cursor for
select 类型 from tab_b
open cur_type
fetch cur_type into @c_type
while @@fetch_status = 0
begin
set @c_sql = @c_sql + ' sum ( case when 类型 = ''' + @c_type + ''' and 完成情况 = ''完成'' then 1 else 0 end ) ' + @c_type + '_完成,'
set @c_sql = @c_sql + ' sum ( case when 类型 = ''' + @c_type + ''' and 完成情况 = ''未完成'' then 1 else 0 end ) ' + @c_type + '_未完成,'
fetch cur_type into @c_type
end
close cur_type
deallocate cur_type
set @c_sql = left(@c_sql,len(@c_sql) - 1 )
set @c_sql = @c_sql + ' from tab_c group by 单位 '
exec ( @c_sql )
❹ sql 分组查询语句
这个很简单啊,就是四个表之间串一下,上sql
select A.NAME 影片名,B.RUNTIME 放映时间,C.TIME 订单时间,C.NUM 顾客编号
from FILM A,RUN B,ORDER C,RESERVATIONS D
WHERE D.TIME=C.TIME AND D.FMN=A.FMN AND A.FMN=B.FMN
试试吧
❺ sql语句 分组查询 急用,在线等啊
你可以通过where条件来限制只查询哪条记录。
比如,我要查出nama=1的记录,可以使用sql语句
select
*
from
table1
where
name='1'
如果我要查询name=1或者name=2的记录,可以使用sql语句
select
*
from
table1
where
name='1'
or
name='2'
或者
select
*
from
table1
where
name
in
('1','2')
❻ SQL语句分组查询
是SQL SERVER吗?就是想要怎样把查询结果导出到文本文档是吧?
EXEC master..xp_cmdshell 'BCP "select XLBH,CMBH,SUM(XSJE) from MDFSLSK group by XLBH,CMBH" queryout D:\XXX.TXT -c -T'
好像要在同一行才能正确执行,MDFSLSK表前可能要加上数据库名.用户名. 比如这样:数据库名.DBO.MDFSLSK
-T是使用信任连接 当然可以换成 -U"sa" -P"password",要详细了解就查一下BCP
❼ SQL对查询结果进行分组(sql分组查询)
1.打开plsql并转到登录页面。
2.以管启胡搭理员身份登录数据库。
3.登录后,创建一个新的SQL窗口。
4,输入,以下语做游句选择a.file#,a.name,a.bytes/1024/1024CurrentMB,ceilHWM*a.block_size/1024/,a.bytes-HWM*a.block_size/1024/1024releaseMB;
5.单击“执行”按钮以执行查询操作。
6,查询后,可以悄拿在查询结果中看到每个数据库文件的具体路径。
❽ sql分组查询语句
只是查询出来么?
select store_no 商品,in_code 识别码,quantity 数量,store_no 仓库,supplier_no 供货商 from (表明) where in_date=‘指定的日期’
(⊙﹏⊙)b,你的商品,仓库,供货商应该都是关联了字典表的
❾ sql分组查询的完整语句
分组查询 group by 主要是对(count,sum,avg,min,max)
例如
表A
a b c
一 1 2
一 1 3
二 2 5
三 7 7
二 9 9
select a,sum(b),sum(c) from A group by a
对分组数据进行限制
select a,sum(b),sum(c) from A group by a having sum(b)>5