# sql統計語句

a1 50 A a1
b2 100 A b2
b3 100 B e3
a4 200 B a4

A 150
B 200

...全文
44 点赞 收藏 4

4 条回复

select 機種,sum(isnull(數量,0)) from
(select 表二.* ,表一.数量
from 表二 left join 表一
on 表一.工單=表二.工單)
grou by 機種

dawugui 2008-03-06
``````create table 表一(工單 varchar(10) , 數量 int)
insert into 表一 values('a1', 50 )
insert into 表一 values('b2', 100)
insert into 表一 values('b3', 100)
insert into 表一 values('a4', 200)
create table 表二(機種 varchar(10) , 工單 varchar(10))
insert into 表二 values('A', 'a1')
insert into 表二 values('A', 'b2')
insert into 表二 values('B', 'e3')
insert into 表二 values('B', 'a4')
go

select b.機種 , sum(a.數量) 數量
from 表一 a , 表二 b
where a.工單 = b.工單
group by b.機種

drop table 表一,表二

/*

---------- -----------
A          150
B          200
（所影响的行数为 2 行）
*/``````

pt1314917 2008-03-06
``````
select 機種,sum(數量) from
(select a.工單,a.數量,b.機種 from 表1 a, 表2 b
where a.工單=b.工單)c group by 機種
``````

dawugui 2008-03-06
``````select b.機種 , sum(a.數量) 數量
from 表一 a , 表二 b
where a.工單 = b.工單
group by b.機種``````

3.3w+

MS-SQL Server相关内容讨论专区
{"ft_lang_label":"__label__zh","ft_lang_prob":0.52455115,"math_prob":0.8563086,"size":613,"snap":"2021-43-2021-49","text_gpt3_token_len":320,"char_repetition_ratio":0.24466339,"word_repetition_ratio":0.0,"special_character_ratio":0.4290375,"punctuation_ratio":0.17834395,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.98582494,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-10-27T04:54:34Z\",\"WARC-Record-ID\":\"<urn:uuid:93fc8e8a-a127-4e1d-ad2f-2c091cde2056>\",\"Content-Length\":\"126666\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:7f94662f-b247-4af5-b27c-bc1e200d0852>\",\"WARC-Concurrent-To\":\"<urn:uuid:03cb0fdd-453c-4b02-8987-521585f70ce5>\",\"WARC-IP-Address\":\"47.95.50.136\",\"WARC-Target-URI\":\"https://bbs.csdn.net/topics/210011054\",\"WARC-Payload-Digest\":\"sha1:EG2B2OLYN4GTST7W7HY3O37S2FUHTMD2\",\"WARC-Block-Digest\":\"sha1:SK65BHE6T5EZVAUZIHXWMLHLGV5K6EXK\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-43/CC-MAIN-2021-43_segments_1634323588053.38_warc_CC-MAIN-20211027022823-20211027052823-00640.warc.gz\"}"}