面試的兩道SQL題

來源:互聯網
上載者:User
1沒有做出來,下午回來後,慢慢弄出來了。跟大家分享一下,看有無更好的解決方案
表一test
name result type

 張三 95 語文
 張三 85 數學
 張三 87 英語
 李四 67 數學
 李四 85 英語
 王五 86 語文
 王五 78 數學

所有成績都在80分以上的

SELECT *
FROM (SELECT name, COUNT(*) AS a
        FROM test
        WHERE (result > 80)
        GROUP BY name)
WHERE (a = 3)

表2 test2
id goodsid scount type

 1 1 10 進
 2 2 20 進
 3 3 20 出
 4 1 45 出
 5 2 30 進
 6 3 40 出

要得出
goodsid 進貨 出庫 庫存
1 10 45 -35
2 50 0 50
3 0 60 -60
這樣的結果
select isnull(a.goodsid,b.goodsid),isnull(a.sumjin,0),isnull(b.sumchu,0),isnull(a.sumjin,0)-

isnull(b.sumchu,0) as kucun from
(SELECT GoodsId, SUM(SCount) AS sumjin
FROM test2
WHERE (Type = '進')
GROUP BY GoodsId) as a full join
(SELECT GoodsId, SUM(SCount) AS sumchu
FROM test2
WHERE (Type = '出')
GROUP BY GoodsId) as b on a.goodsid=b.goodsid

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.