⑴ 查询2016年购买商品数量最多的10个用户,并说明应如何优化sql
SELECTTOP10CustomerID--前10条
,Amount
FROM(
SELECTCustomerID
,SUM(Amount)ASAmount
FROMSales
GROUPBYCustomerID
)D
ORDERBYAmountDESC--倒序
优化:加上时间范围
⑵ 有一张表存储了用户获得各种礼物的数量,如果我要查询出各种礼物获得最多的用户,sql应该怎样写谢谢
----建立表结构:
create table tableTest
(
id int identity(1,1) primary key,
name nvarchar(50),
type nvarchar(50),
num int
)
insert into tableTest values('a','花',10);
insert into tableTest values('b','烟',50);
insert into tableTest values('c','酒',13);
insert into tableTest values('d','花',18);
insert into tableTest values('e','烟',20);
insert into tableTest values('f','糖',11);
insert into tableTest values('a','糖',11);
----具体执行代码如下:
create table #table1
(
name varchar(255),
type varchar(255),
num int
)
create table #table2
(
num int,
type varchar(255)
)
create table #table3
(
name varchar(255),
type varchar(255),
num int
)
declare @name varchar(255)
declare @number int
declare @type varchar(255)
declare se_type cursor for
select distinct type from tableTest
open se_type
fetch se_type into @type
while(@@fetch_status=0)
begin
insert into #table1 select name,type,sum(num) from tableTest group by type,name having type=@type
insert into #table2 select max(num),type from #table1 group by type having type=@type
insert into #table3 select #table1.name,#table1.type,#table1.num from #table1 inner join #table2 on #table1.num=#table2.num and #table1.type=#table2.type where #table1.type=@type
fetch se_type into @type
end
close se_type
deallocate se_type
select * from #table3
完全按照你的要求去做的,希望能帮助到你。如有更简便方法,望讨论交流!
⑶ sql 用户充值金额为恒定值且超过俩小时取出用户id
首先第一点,关于恒定值,你咋知道这个是不是恒定值,用什么来判断?最简单就是要设置一个字段:更新时间
前置条件:要有一个时间字段,每一次对数据进行修改的时候会对该字段进行更新。
假设data_date是该时间字段
然后sql语句这样写:select user_id from user where data_date< date_sub(now(), interval 2 hour);
⑷ 如何高效率的统计一段时间范围内的新增充值人数的SQL写法
如何高效率的统计一段时间范围内的新增充值人数的SQL写法
电池供电的开始时间查询
select * from 表 where (curDt>开始时间 and curDt< 结束时间 ) and PowerState =电池
电池供电的结束时间查询
select * from 表 where (curDt>开始时间 and curDt< 结束时间) and PowerState =市电
⑸ 求写SQL语句:取2015年每个月消费金额都大于100的用户信息
select * from c_cons cs where exists(
select pay_amt,pay_ym a_pay ay where cs.cons_id=ay.cons_id and cs.pay_amt > 100 group cs.by pay_ym,cs.pay_ym
)
⑹ 请教如何用SQL语句实现查出表中某时间段内消费最多的5个人,谢谢大师们!
SELECT top 5 code,SUM(xfje) 消费金额合计
FROM A表 JOIN B表 ON A表.kahao=B表.kahao
where A表.date between 开始日期 and 结束日期 and A表.time between 开始时间 and 结束时间
group by code
order by 消费金额合计 desc
⑺ 使用SQL如何查询不同人员的最后一条充值信息
表:客户编号,客户冲值金额,冲值日期
select 客户编号,客户冲值金额,冲值日期 from 表
where
客户编号='1'
and
冲值日期=(select max(冲值日期) from 表 where 客户编号='1')
客户编号设置个变量,然后递交过来查询,再把查询结果显示出来
⑻ SQL 列出本月消费最多的10位客户 语句怎么写
select top 10 * from 表 order by 消费 desc按照消费降序排列,也就是由高到低排,然后再找出前10条。
⑼ sql server 2005 查找并统计前10名数量最多的用户
例表tab_test ,数量列名:sl
selecttop10*fromtab_testorderbyddesc
语句执行,先将结果集按 sl 的降序 排序,然后使用 top 10 选择前10行数据。
如果单一用户有多条记录,那需要将用户分组,计算数量
例表tab_test ,数量列名:sl,用户列名: user
selecttop10user,sum(sl)as'sl_xj'fromtab_test
groupbyuserorderbysl_xjdesc
⑽ 用sql语句查询消费的金额都大于100的用户
MYSQL语法,请参考:
SELECTt.`user`FROMtabletGROUPBYt.`user`HAVINGMIN(t.money)>=100