select subset of data with labels in specific column balanced in TSQL

select subset of data with labels in specific column balanced in TSQL

I have about 4M records in a table. Each record in this table is distinct. One of the columns 'labels' has two distinct values L1 and L2. I want to randomly select 100000 records such that 50% of this data has label L1 and the rest has L2.

I have about 4M records in a table. Each record in this table is distinct. One of the columns 'labels' has two distinct values L1 and L2. I want to randomly select 100000 records such that 50% of this data has label L1 and the rest has L2.

Eg : If the following table is the mastertable and has about 10 rows

s_key  C1    C2    labels
s1     0.2   0.1    L1
s2     0.3   0.3    L1
s3     0.25  0.32   L2
s4     1.4   0.5    L1
s5     2.3   9.0    L1
s6     1.2   0.8    L1
s7     0.8   0.4    L1
s8     2.6   9.5    L2
s9     3.7   7.1    L2
s10    2.7   8.3    L2

I want to randomly select 4 records from the above table such that 50 % that is 2 records have labels L1 and 2 records have labels L2 and looks like the following

s_key  C1    C2    labels
s1     0.2   0.1    L1
s2     0.3   0.3    L1
s3     0.25  0.32   L2
s10    2.7   8.3    L2

How can I do this in Tsql/Sql? Appreciate any input.

sql t-sql

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Introduction to Structured Query Language SQL pdf

SQL stands for Structured Query Language. SQL is a scripting language expected to store, control, and inquiry information put away in social databases. The main manifestation of SQL showed up in 1974, when a gathering in IBM built up the principal model of a social database. The primary business social database was discharged by Relational Software later turning out to be Oracle.

Introduction to Structured Query Language SQL pdf

SQL stands for Structured Query Language. SQL is a scripting language expected to store, control, and inquiry information put away in social databases. The main manifestation of SQL showed up in 1974, when a gathering in IBM built up the principal model of a social database. The primary business social database was discharged by Relational Software later turning out to be Oracle.

Welcome Back the T-SQL Debugger with SQL Complete – SQL Debugger

Debug SQL stored procedures and develop your SQL database project with dbForge SQL Complete, a new add-in for Visual Studio and SSMS. When you develop large chunks of T-SQL code with the help of the SQL Server Management Studio tool, it is essential to test the “Live” behavior of your code by making sure that each small piece of code works fine and being able to allocate any error message that may cause a failure within that code.

The Easy Guide on How to Use Subqueries in SQL Server

This article provides an outlook on various types of subqueries in SQL such as select or other T-SQL statements and caveats when using them.

Transact-SQL: The Building Blocks to SQL Server Programming by Gregory A. Larsen

Transact SQL (TSQL) is the languaged used to query and update data stored in a SQL Server. This book, written by Simple Talk author Greg Larsen, will give developers an understanding of the basics of the TSQL language. Programmers will have the building blocks necessary to quickly and easily build applications that use SQL Server.