<!DOCTYPE html>
    <html lang="vi" xmlns="http://www.w3.org/1999/xhtml" prefix="og: http://ogp.me/ns#">
    <head>
<title>Hàm SUM trong SQL Server</title>
<meta name="description" content="Hàm SUM trong SQL Server - Savefile - Tin Tức - https&#x3A;&#x002F;&#x002F;www.nguoicodonvn2008.info&#x002F;vi&#x002F;news&#x002F;savefile&#x002F;kien-thuc-may-tinh&#x002F;ham-sum-trong-sql-server-9669.html">
<meta name="author" content=".: Nguoicodonvn2008.info - Cõi lòng người cô đơn :.">
<meta name="copyright" content=".: Nguoicodonvn2008.info - Cõi lòng người cô đơn :. [admin@nguoicodonvn2008.info]">
<meta name="robots" content="index, archive, follow, noodp">
<meta name="googlebot" content="index,archive,follow,noodp">
<meta name="msnbot" content="all,index,follow">
<meta name="generator" content="NukeViet v4.5">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta property="og:title" content="Hàm SUM trong SQL Server">
<meta property="og:type" content="website">
<meta property="og:description" content="Savefile - Tin Tức - https&#x3A;&#x002F;&#x002F;www.nguoicodonvn2008.info&#x002F;vi&#x002F;news&#x002F;savefile&#x002F;kien-thuc-may-tinh&#x002F;ham-sum-trong-sql-server-9669.html">
<meta property="og:site_name" content=".&#x3A; Nguoicodonvn2008.info - Cõi lòng người cô đơn &#x3A;.">
<meta property="og:url" content="https://www.nguoicodonvn2008.info/vi/news/savefile/kien-thuc-may-tinh/ham-sum-trong-sql-server-9669.html">
<link rel="shortcut icon" href="https://nguoicodonvn2008.info/favicon.ico">
<link rel="canonical" href="https://www.nguoicodonvn2008.info/vi/news/savefile/kien-thuc-may-tinh/ham-sum-trong-sql-server-9669.html">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/" title="Tin Tức" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/karaoke-dual/" title="Tin Tức - Karaoke Dual" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/nhac-tre/" title="Tin Tức - Nhạc trẻ" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/tru-tinh/" title="Tin Tức - Trữ tình" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/nuoc-ngoai/" title="Tin Tức - Nước ngoài" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/remix/" title="Tin Tức - Remix" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/tam-su-tinh-yeu/" title="Tin Tức - Tâm sự tình yêu" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/tho-suu-tam/" title="Tin Tức - Thơ sưu tầm" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/cuoc-song/" title="Tin Tức - Cuộc sống" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/phan-mem/" title="Tin Tức - Phần mềm" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/kien-thuc-may-tinh/" title="Tin Tức - Kiến thức máy tính" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/hoc-tap/" title="Tin Tức - Học tập" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/tai-lieu/" title="Tin Tức - Tài liệu" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/de-thi/" title="Tin Tức - Đề thi" type="application/rss+xml">
<link rel="preload" as="style" href="https://nguoicodonvn2008.info/assets/css/font-awesome.min.css" type="text/css">
<link rel="preload" as="style" href="https://nguoicodonvn2008.info/themes/default/css/bootstrap.non-responsive.css" type="text/css">
<link rel="preload" as="style" href="https://nguoicodonvn2008.info/themes/default/css/style.css" type="text/css">
<link rel="preload" as="style" href="https://nguoicodonvn2008.info/themes/default/css/style.non-responsive.css" type="text/css">
<link rel="preload" as="style" href="https://nguoicodonvn2008.info/themes/default/css/news.css" type="text/css">
<link rel="preload" as="style" href="https://nguoicodonvn2008.info/themes/default/css/custom.css" type="text/css">
<link rel="preload" as="script" href="https://nguoicodonvn2008.info/assets/js/jquery/jquery.min.js" type="text/javascript">
<link rel="preload" as="script" href="https://nguoicodonvn2008.info/assets/js/language/vi.js" type="text/javascript">
<link rel="preload" as="script" href="https://nguoicodonvn2008.info/assets/js/DOMPurify/purify3.js" type="text/javascript">
<link rel="preload" as="script" href="https://nguoicodonvn2008.info/assets/js/global.js" type="text/javascript">
<link rel="preload" as="script" href="https://nguoicodonvn2008.info/assets/js/site.js" type="text/javascript">
<link rel="preload" as="script" href="https://nguoicodonvn2008.info/themes/default/js/news.js" type="text/javascript">
<link rel="preload" as="script" href="https://nguoicodonvn2008.info/themes/default/js/main.js" type="text/javascript">
<link rel="preload" as="script" href="https://nguoicodonvn2008.info/themes/default/js/custom.js" type="text/javascript">
<link rel="preload" as="script" href="https://nguoicodonvn2008.info/themes/default/js/bootstrap.min.js" type="text/javascript">
<link rel="stylesheet" href="https://nguoicodonvn2008.info/assets/css/font-awesome.min.css">
<link rel="stylesheet" href="https://nguoicodonvn2008.info/themes/default/css/bootstrap.non-responsive.css">
<link rel="stylesheet" href="https://nguoicodonvn2008.info/themes/default/css/style.css">
<link rel="stylesheet" href="https://nguoicodonvn2008.info/themes/default/css/style.non-responsive.css">
<link rel="StyleSheet" href="https://nguoicodonvn2008.info/themes/default/css/news.css">
<link rel="stylesheet" href="https://nguoicodonvn2008.info/themes/default/css/custom.css">
<style type="text/css">
	body{background: #fff;}
</style>
    </head>
    <body>
<div id="print">
	<div id="hd_print">
		<h2 class="pull-left">.&#x3A; Nguoicodonvn2008.info - Cõi lòng người cô đơn &#x3A;.</h2>
		<p class="pull-right"><a title=".&#x3A; Nguoicodonvn2008.info - Cõi lòng người cô đơn &#x3A;." href="https://nguoicodonvn2008.info/">https://nguoicodonvn2008.info</a></p>
	</div>
	<div class="clear"></div>
	<hr />
	<div id="content">
		<h1>Hàm SUM trong SQL Server</h1>
		<ul class="list-inline">
			<li>Thứ ba - 14/01/2025 22:35</li>
			<li class="hidden-print txtrequired"><em class="fa fa-print">&nbsp;</em><a title="In ra" href="javascript:;" onclick="window.print()">In ra</a></li>
			<li class="hidden-print txtrequired"><em class="fa fa-power-off">&nbsp;</em><a title="Đóng cửa sổ này" href="javascript:;" onclick="window.close()">Đóng cửa sổ này</a></li>
		</ul>
		<div class="clear"></div>
		<div id="hometext">
		</div>
				<div class="imghome">
			<img alt="Hàm SUM trong SQL Server" src="https://st.quantrimang.com/photos/image/2018/03/30/ms-sql-server-kieu-du-lieu-size-80x80-znd.jpg" width="460" class="img-thumbnail" />
		</div>
		<div class="clear"></div>
		<div id="bodytext" class="clearfix">
			<p style="text-align: justify;"><strong>Hàm SUM trong SQL Server</strong>&nbsp;là gì?&nbsp;<strong>Cách dùng hàm SUM SQL Server&nbsp;</strong>như thế nào? Hãy cùng Quantrimang.com tìm hiểu nhé!</p>

<p style="text-align: justify;">SQL Server là kiến thức bạn nhất định nên tìm hiểu nếu muốn trở thành một lập trình viên chuyên nghiệp bởi nó được sử dụng phổ biến trong lĩnh vực này.</p>

<p style="text-align: justify;">Nhìn chung, học SQL Server không khó. Bạn dễ dàng tìm thấy những khóa học online hay hướng dẫn tự học trên Internet, Quantrimang.com là một trong số đó. Quantrimang sẽ cung cấp cho bạn những kiến thức cơ bản về SQL Server. Ở bài viết này, chúng ta hãy cùng nhau tìm hiểu về hàm tính tổng trong SQL Server nhé!</p>

<h2 style="text-align: justify;">Câu lệnh SUM trong SQL Server là gì?</h2>

<p style="text-align: justify;">Nếu đang tìm cách tính tổng SQL, nhất định bạn phải biết cách dùng hàm SUM.&nbsp;<strong>Hàm SUM trong SQL Server&nbsp;</strong>là một kiểu hàm tổng hợp. Hàm này được dùng để tính giá trị tổng trong một nhóm dữ liệu của cột hay biểu thức được chỉ định.</p>

<p style="text-align: justify;"><strong>Câu lệnh SUM trong SQL Server&nbsp;</strong>là một hàm có sẵn, chấp nhận một tham số đơn lẻ mà có thể là cột hoặc biểu thức hợp lệ và trả về một kết quả duy nhất để tóm tắt nhập bộ dữ liệu đầu vào. Hàm này bỏ qua giá trị NULL. Tuy nhiên, nó trả về NULL khi nhóm kết quả không có hàng. Nó cũng hoạt động với mệnh đề WHERE, GROUP BY, ORDER BY và HAVING.</p>

<h2 style="text-align: justify;">Cú pháp</h2>

<p style="text-align: justify;">Để sử dụng hàm SUM trong SQL Server, ta dùng cú pháp như sau:</p>

<pre id="pre0" style="text-align: justify;">
SELECT SUM(cot)FROM bang&#91;WHERE dieukien&#93;;</pre>

<p style="text-align: justify;"><strong>Tham số</strong>:</p>

<ul>
	<li style="text-align: justify;"><em>cot:</em>&nbsp;cột hoặc giá trị tính toán, biểu thức mà bạn muốn tính tổng</li>
	<li style="text-align: justify;"><em>bang:</em>&nbsp;bảng dùng để lấy bản ghi. Phải có ít nhất 1 bảng trong mệnh đề FROM.</li>
	<li style="text-align: justify;"><em>dieukien:</em>&nbsp;tùy chọn. Điều kiện mà bản ghi phải đáp ứng để được chọn.</li>
</ul>

<p style="text-align: justify;"><strong>Lưu ý</strong>:</p>

<ul>
	<li style="text-align: justify;">Hàm SUM có thể được sử dụng trong các phiên bản sau của SQL Server: SQL Server 2017, SQL Server 2016, SQL Server 2014, SQL Server 2012, SQL Server 2008 R2, SQL Server 2008, SQL Server 2005.</li>
</ul>

<h2 style="text-align: justify;">Ví dụ</h2>

<p style="text-align: justify;">Hãy xem và khám phá một số ví dụ về hàm SUM trong SQL Server.</p>

<p style="text-align: justify;">Giả sử, ta có bảng dữ liệu như sau:</p>

<p style="text-align: justify;"><img alt="" data-i="0" data-src="https://st.quantrimang.com/photos/image/2019/02/22/ham-sum-1.jpg" data-was-processed="true" height="245" src="https://st.quantrimang.com/photos/image/2019/02/22/ham-sum-1.jpg" width="500" /></p>

<p style="text-align: justify;"><strong>Ví dụ 1: Tính tổng số lượng số bài viết của website Quantrimang</strong></p>

<pre id="pre1" style="text-align: justify;">
SELECT SUM(Sobai) AS &quot;Tong so bai&quot;FROM QuantrimangWHERE Sobai &gt; 100;Result: Tong so bai 641</pre>

<p style="text-align: justify;">Ở ví dụ này ta cần đầu ra là giá trị tổng số bài của website, nên kết quả được tính bằng cách cộng tất cả giá trị ở cột Sobai có dữ liệu khác rỗng và lớn hơn 100 lại với nhau.</p>

<p style="text-align: justify;"><strong>Ví dụ 2: Sử dụng DISTINCT</strong></p>

<pre id="pre2" style="text-align: justify;">
SELECT SUM(DISTINCT Sobai) AS &quot;Tong so bai&quot;FROM QuantrimangWHERE Sobai &gt; 100;Result: Tong so bai 540</pre>

<p style="text-align: justify;">Ví dụ này dùng từ khóa DISTINCT, nên những giá trị lặp lại chỉ được tính một lần. Trong bảng dữ liệu cho trước, giá trị “101” xuất hiện 2 lần, nhưng chỉ được tính một lần, cho nên tổng số dòng được tính sẽ là 4, kết quả được tính như sau:</p>

<pre id="pre3" style="text-align: justify;">
152 + 101 + 122 + 165 = 540</pre>

<p style="text-align: justify;"><strong>Ví dụ 3: Tính giá trị tổng theo cột được chỉ định của các dòng được chọn</strong></p>

<p style="text-align: justify;">Để tính giá trị tổng theo cột được chỉ định của các dòng được chọn, ta sử dụng mệnh đề GROUP BY.</p>

<p style="text-align: justify;">Ví dụ sau tính giá trị tổng của tất cả bản ghi liên quan tới một chuyên mục lớn và bạn sẽ thực hiện như sau:</p>

<pre id="pre4" style="text-align: justify;">
SELECT Chuyenmuclon, SUM(Sobai) AS &quot;Tong so bai&quot;FROM QuantrimangGROUP BY Chuyenmuclon;Result:Chuyenmuclon          Tong so baiLaptrinh              324Mang xa hoi           317Trinh duyet web       94</pre>

<p style="text-align: justify;"><strong>Hàm SUM SQL Server với mệnh đề WHERE</strong></p>

<p style="text-align: justify;">Mệnh đề WHERE được dùng để lọc các bản ghi một bảng. Ví dụ này sẽ dùng mệnh đề WHERE cùng hàm SUM() để tính tổng lương của nhân viên làm việc nhiều hơn 9 tiếng một ngày.</p>

<pre id="pre5" style="text-align: justify;">
SELECT SUM(salary) AS &quot;Total Salary&quot;
FROM employee_info
WHERE working_hours&gt;9;</pre>

<p style="text-align: justify;">Khi chạy truy vấn này, nó trả về kết quả sau:</p>

<p style="text-align: justify;"><img alt="Hàm SUM SQL với WHERE" data-i="1" data-src="https://st.quantrimang.com/photos/image/2023/03/14/sql-server-sum-function4.png" data-was-processed="true" height="164" src="https://st.quantrimang.com/photos/image/2023/03/14/sql-server-sum-function4.png" width="292" /></p>

<p style="text-align: justify;"><strong>Hàm SUM SQL Server với ORDER BY</strong></p>

<p style="text-align: justify;">ORDER BY được dùng để sắp xếp dữ liệu trong bảng theo thứ tự tăng hoặc giảm dần. Ví dụ bên dưới sẽ dùng hàm SUM() với ORDER BY, tính tổng số lương của nhân viên dựa trên từng công việc, rồi phân loại kết quả theo thứ tự tăng dần:</p>

<pre id="pre6">
<code>SELECT occupation, SUM(salary) AS &quot;Total Salary&quot;
FROM employee_info
GROUP BY occupation
ORDER BY SUM(salary);</code></pre>

<p style="text-align: justify;">Kết quả:</p>

<p style="text-align: justify;"><img alt="Hàm SUM SQL với ORDER BY" data-i="2" data-src="https://st.quantrimang.com/photos/image/2023/03/14/sql-server-sum-function7.png" data-was-processed="true" height="250" src="https://st.quantrimang.com/photos/image/2023/03/14/sql-server-sum-function7.png" width="377" /></p>

<p style="text-align: justify;"><strong>Hàm SUM với OVER()</strong></p>

<p style="text-align: justify;">Mệnh đề OVER() xác định số hàng từ truy vấn được áp dụng cho hàm đó, theo thứ tự hàm đánh giá chúng và khi các phép tính của hàm được khởi động lại. Truy vấn bên dưới cung cấp tổng doanh số tích lũy cho từng công việc ở bảng employee_info:</p>

<pre id="pre7">
<code>SELECT DISTINCT occupation,
SUM(salary) OVER (PARTITION BY occupation) AS &quot;Total Salary&quot;
FROM employee_info;</code></pre>

<p style="text-align: justify;"><img alt="Dùng hàm SUM với Over" data-i="3" data-src="https://st.quantrimang.com/photos/image/2023/03/14/sql-server-sum-function9.png" data-was-processed="true" height="232" src="https://st.quantrimang.com/photos/image/2023/03/14/sql-server-sum-function9.png" width="462" /></p>

<h2 style="text-align: justify;">Dùng trong trường hợp cột có giá trị NULL</h2>

<p style="text-align: justify;">Với bất kỳ ai đã quen dùng hàm SUM() SQL đều biết, nó bỏ qua giá trị NULL từ cột số bất kỳ trong suốt quá trình tính toán. Ví dụ sau sẽ cho bạn thấy rõ điều đó. Đầu tiên, chèn một số hàng bổ sung chứa giá trị NULL, sau đó liệt kê tất cả giá trị từ bảng này để xác minh kết quả đầu ra tiếp theo.</p>

<p style="text-align: justify;">Lệnh đầu tiên hiện toàn bộ giá trị từ bảng Sales và truy vấn thứ hai bổ sung tất cả giá trị được lưu ở cột Price &amp; Quantity để chứng minh hàm tính tổng SQL Server bỏ qua các giá trị NULL.</p>

<pre id="pre8" style="text-align: justify;">
SELECT * FROM Sales
GO
SELECT SUM(Price) AS &#91;Total Sales&#93;,
SUM(Quantity) AS &#91;Total units&#93;
FROM Sales</pre>

<p style="text-align: justify;">Bạn có thể thấy giá trị&nbsp;<strong>NULL</strong>&nbsp;ở cả hai cột được trả về bởi truy vấn đầu tiên và thứ hai đã trả về tổng các giá trị được lưu ở cột Price &amp; Quantity. Bạn có thể thấy giá trị NULL không được tính tới trong khi trả về kết quả này:</p>

<p style="text-align: justify;"><img alt="Ví dụ về cách dùng lệnh tính tổng SQL Server" data-adbro-processed="true" data-i="4" data-src="https://st.quantrimang.com/photos/image/2023/06/15/sql-server-sum.jpg" data-was-processed="true" height="444" src="https://st.quantrimang.com/photos/image/2023/06/15/sql-server-sum.jpg" width="497" /></p>

<div style="text-align: justify;">&nbsp;</div>
		</div>
				<div id="author">
						<p>
				<strong>Nguồn tin:</strong>
				Quantrimang.com:
			</p>
		</div>
	</div>
	<div id="footer" class="clearfix">
		<div id="url">
			<strong>URL của bản tin này: </strong><a href="https://www.nguoicodonvn2008.info/vi/news/savefile/kien-thuc-may-tinh/ham-sum-trong-sql-server-9669.html" title="Hàm SUM trong SQL Server">https://www.nguoicodonvn2008.info/vi/news/savefile/kien-thuc-may-tinh/ham-sum-trong-sql-server-9669.html</a>

		</div>
		<div class="clear"></div>
		<div class="copyright">
			&copy; .&#x3A; Nguoicodonvn2008.info - Cõi lòng người cô đơn &#x3A;.
		</div>
		<div id="contact">
			<a href="mailto:admin@nguoicodonvn2008.info">admin@nguoicodonvn2008.info</a>
		</div>
	</div>
</div>
        <div id="timeoutsess" class="chromeframe">
            Bạn đã không sử dụng Site, <a onclick="timeoutsesscancel();" href="https://nguoicodonvn2008.info/#">Bấm vào đây để duy trì trạng thái đăng nhập</a>. Thời gian chờ: <span id="secField"> 60 </span> giây
        </div>
        <div id="openidResult" class="nv-alert" style="display:none"></div>
        <div id="openidBt" data-result="" data-redirect=""></div>
		</script>
		<div class="car-top">
  <span><img src="https://nguoicodonvn2008.info/themes/default/images/car.png" alt=""></span>
</div>
<script src="https://nguoicodonvn2008.info/assets/js/jquery/jquery.min.js"></script>
<script>var nv_base_siteurl="/",nv_lang_data="vi",nv_lang_interface="vi",nv_name_variable="nv",nv_fc_variable="op",nv_lang_variable="language",nv_module_name="news",nv_func_name="savefile",nv_is_user=0, nv_my_ofs=-4,nv_my_abbr="EDT",nv_cookie_prefix="nv4c_e856T",nv_check_pass_mstime=1738000,nv_area_admin=0,nv_safemode=0,theme_responsive=0,nv_recaptcha_ver=2,nv_recaptcha_sitekey="",nv_recaptcha_type="image",XSSsanitize=1;</script>
<script src="https://nguoicodonvn2008.info/assets/js/language/vi.js"></script>
<script src="https://nguoicodonvn2008.info/assets/js/DOMPurify/purify3.js"></script>
<script src="https://nguoicodonvn2008.info/assets/js/global.js"></script>
<script src="https://nguoicodonvn2008.info/assets/js/site.js"></script>
<script src="https://nguoicodonvn2008.info/themes/default/js/news.js"></script>
<script src="https://nguoicodonvn2008.info/themes/default/js/main.js"></script>
<script src="https://nguoicodonvn2008.info/themes/default/js/custom.js"></script>
<script type="application/ld+json">
        {
            "@context": "https://schema.org",
            "@type": "Organization",
            "url": "https://nguoicodonvn2008.info",
            "logo": "https://nguoicodonvn2008.info/uploads/angel.gif"
        }
        </script>
<script src="https://nguoicodonvn2008.info/themes/default/js/bootstrap.min.js"></script>
<script type="text/javascript">
var $scrolltop = $('.car-top');
$scrolltop.on('click', function () {
    $('html,body').animate({
        scrollTop: 0
    }, 800);
    $(this).addClass("car-run");
    setTimeout(function(){ $scrolltop.removeClass('car-run');}, 1000);
    return false;
});
$(window).on('scroll', function ()
{ 
    if($(window).scrollTop() >= 200)
    {
        $scrolltop.addClass("show");
        $scrolltop.addClass("car-down");
    }
    else
    {
       $scrolltop.removeClass("show");
       setTimeout(function(){ $scrolltop.removeClass('car-down');}, 300);
    }
});
</script>
</body>
</html>