㈠ sql查詢分組怎麼做
select user_id
,sum(case when trade_type='4' then amount else 0 end) as trade_type4
,sum(case when trade_type='5' then amount else 0 end) as trade_type5
,sum(case when trade_type='11' then amount else 0 end) as trade_type11
,sum(case when trade_type='12' then amount else 0 end) as trade_type12
from tab_name
group by user_id
㈡ sql分組查詢及遍歷
sql server 2005+下分組:
WITH AreaAll AS(
SELECT id=ID,
pArea = area,
area = area
FROM area
WHERE parentid is null
UNION ALL
SELECT id= T1.ID,
pArea = T2.area,
area = T1.area
FROM area T1
JOIN AreaAll T2
ON T1.parentid = T2.id
)
SELECT pArea,area FROM AreaAll where pArea!=area order by id,pArea,area asc
㈢ 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
㈣ sql分組查詢
select 品名,時間 ,sum(數量),sum(數量1) from table group by 時間,品名
㈤ sql 分組查詢問題,
分組,使用Case When 語句將特定數據橫向展開,具體語句如下
Select street,
1號點=Max(Case When Name='1號點' Then 經緯度 Else null End),
2號點=Max(Case When Name='2號點' Then 經緯度 Else null End),
3號點=Max(Case When Name='3號點' Then 經緯度 Else null End),
4號點=Max(Case When Name='4號點' Then 經緯度 Else null End),
5號點=Max(Case When Name='5號點' Then 經緯度 Else null End)
From yourTable Group By street
㈥ SQL對查詢結果進行分組
如果你的UID是int型先進行轉換,然後截取前兩位,在用聚合函數獲取總數,進行分組查詢就好了,語句如下(把表名替換下可以直接用):select SUBSTRING(CONVERT(varchar(50),UID),0,3),COUNT(*) from 表 group by SUBSTRING(CONVERT(varchar(50),UID),0,3)
㈦ 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分組查詢語句
只是查詢出來么?
select store_no 商品,in_code 識別碼,quantity 數量,store_no 倉庫,supplier_no 供貨商 from (表明) where in_date=『指定的日期』
(⊙﹏⊙)b,你的商品,倉庫,供貨商應該都是關聯了字典表的
㈨ sql多條件分組查詢,求sql語句。
分組是用來聚集匯總的,如求平均、求總和、求最大等
你這個不需要分組,直接排序就可以了
selectname,datefromtableorderbynameasc,datedesc
如果每組要按date的降序排列,也挺簡單
with t1 as (select name, max(date) md from table group by name,
t2 as (select table.name name, table.date date, t1.md from table join t1 on table.name=t1.name
order by t1.md)
select name,date from t2
㈩ SQL多表分組查詢
selectt1.*,t2.English
from
(selecta.id,a.name,a.address,MAX(b.Chinese)Chinesefromstudenta,Scoreb
wherea.id=b.idgroupbya.id,a.name,a.address)t1,
scoret2
wheret1.id=t2.idandt1.Chinese=t2.Chinese