⑴ 查詢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