SQL Query to Sum Comma-Separated Values in a Column

preview_player
Показать описание
In this video, learn how to handle comma-separated values stored in a single column and calculate their sum using SQL. We'll walk through creating a table with student marks, writing a query to split and sum the values, and displaying the results. This is a great tutorial for those looking to enhance their SQL skills with practical examples. Perfect for database administrators and developers alike!

--you have a table with students names in one column and their marks as comma separated value in other column.
you need to take sum of those marks and display the result.

-- Step 1: Create the table
CREATE TABLE Students_Marks (
student_id INT PRIMARY KEY,
student_name VARCHAR(50),
marks VARCHAR(100) -- Comma-separated values
);

-- Step 2: Insert sample data
INSERT INTO Students_Marks (student_id, student_name, marks) VALUES
(1, 'John Doe', '75,85,90'),
(2, 'Jane Smith', '88,92,79'),
(3, 'Robert Brown', '65,70,72'),
(4, 'Emily Davis', '80,82,85');

-- Query to calculate the sum of marks

select student_id,student_name,
sum(cast(value as INT)) as Total_Marks
from Students_Marks
CROSS APPLY STRING_SPLIT(marks,',')
group by student_id,student_name

SQL Interview Problem: Splitting Full Names into First, Middle, and Last Names

Top SQL Interview Questions: RANK, DENSE_RANK, ROW_NUMBER Explained

Top 3 Products by Sales & Employees by Salary | SQL Ranking Functions Explained

SQL Interview Challenge: Find Hidden Likes in Your Friends' Favorites!

SQL Magic: Aggregating Marks & Delivery Dates with Advanced Queries!

Top 20% vs Bottom 20% of Students in SQL: NTILE vs TOP PERCENT

SQL Tricks: Find the 3rd Lowest Salary in Each Department with Two Powerful Methods!

SQL Tutorial: Find Employees Who Joined Before Their Managers

SQL Tutorial: Retrieve Specific Rows with OFFSET & ROW_NUMBER - Top 2 Methods Compared!

How to Build a Location-Based Hierarchy in SQL | Recursive Query Tutorial

How to Count Weekends in Any Month Using SQL Server: A Step-by-Step Guide

SQL Date Magic: Find & Format the Last Day of the Previous Month

How to Count Workdays in SQL: Two Effective Methods Explained!

How to Find Employees Earning More Than Their Managers in SQL

SQL Join Techniques: Filtering Data with WHERE vs. ON Clause

Retrieve Specific Rows in SQL: OFFSET-FETCH vs. ROW_NUMBER!

#SQLTutorial
#CommaSeparatedValues
#SUMinSQL
#SQLAggregation
#DatabaseTutorial
#SQLQueryExamples
Рекомендации по теме
welcome to shbcf.ru