乙個SQL語句實現的統計功能

2022-02-28 16:19:20 字數 690 閱讀 8486

前幾天,在qq群上有q友問到下面的問題:

客戶    時間       金額  

a     2006-10-1    200

a     2007-5-5     300

b     2006-1-1     400

實現如下結果:

客戶   3個月以內   3-6個月 ... 合計

a         200         300       500

b                     400       400

下面就是sql實現:

select 客戶,

(select sum(金額) from tablea where convert(bigint, 時間) + 90 >= convert(bigint, getdate()) and tablea.客戶 = a.客戶) as [3個月以內],

(select sum(金額) from tablea where convert(bigint, 時間) + 180 >= convert(bigint, getdate()) and convert(bigint, 時間) + 90 < convert(bigint, getdate()) and tablea.客戶 = a.客戶) as [3-6個月]

from (select distinct(客戶) as 客戶 from tablea) a

乙個簡單功能的SQL 實現

1.假設有一張表示cj表 name subject result 張三 語文 80張三 數學 90張三 物理 85李四 語文 85李四 數學 92李四 物理 89要求查詢結果 姓名 語文 數學 物理 張三 80 90 85李四 85 92 89 建立cj表sql create table cj id...

乙個簡單功能的SQL 實現

1.假設有一張表示cj表 name subject result 張三 語文 80張三 數學 90張三 物理 85李四 語文 85李四 數學 92李四 物理 89要求查詢結果 姓名 語文 數學 物理 張三 80 90 85李四 85 92 89 建立cj表sql create table cj id...

乙個不會的SQL語句

12.有兩個表a 和b 均有key 和value 兩個字段,如果b 的key 在a 中也有,就把b 的value 換為a 中對應的value 這道題的sql 語句怎麼寫?update b set value select value from awhere a.key b.key where exi...