- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试找出一个 SQL 查询,它将一个表中的记录均匀分布或分配到另一个表。最好的解释方法是通过一个人为的例子。
假设我有一张员工表,我想从颜色表中为每个员工分配一种颜色。
我想确保颜色均匀分布,但没有任何员工属性可以预测他们会收到哪种颜色。
Sam
John
Jack
April
Sonny
Jill
Jane
Red
Green
Blue
Sam - Red
John - Green
Jack - Blue
April - Red
Sonny - Green
Jill - Blue
Jane - Red
如何在 TSQL 中构造此连接?
最佳答案
我知道这个问题是关于 SQLServer 的,但是对于那些对没有 NTILE 而只使用 row_number 的解决方案感兴趣的人:
-- (PostgreSQL syntax, but can be easly adapted to any DB)
with
-- "uses" a dummy colors table just for testing
colors as
(select 'Red' as color union all
select 'Green' union all
select 'Blue'
)
,
-- "uses" a dummy employees table just for testing
employees as
(select 'John' as employee union all
select 'Jack' union all
select 'April' union all
select 'Sonny' union all
select 'Jill' union all
select 'Jane'
)
,
-- here's the trick:
-- first we define a code_num as row_number for each color
c as
(select *,
row_number() over() as color_num
from colors
),
-- and then we define a code_num for each employee as
-- ((row_number-1) "mod" (colors' table count)) +1
e as
(select *,
(((row_number() over())-1) % (select count(*) from colors))+1 as color_num
from employees
)
-- get both tables joined by color_num
select e.employee,
c.color
from e
join c on c.color_num = e.color_num
输出:
employee color
---------------
John Red
Jack Green
April Blue
Sonny Red
Jill Green
Jane Blue
关于sql-server - 如何在 TSQL 中将一个表中的行均匀分布到另一个表中的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29129712/
CREATE TABLE [dbo].[tblLocations]( [latitude] [float] NOT NULL, [longitude] [float] NOT NULL
有没有办法在相似的表中递归读取记录并按深度级别排序? #table: id int | parent int | value string --------------------
尝试从旧数据库迁移到存储数据的新数据库有点不同。 在一种特定情况下,我有一列用分号分隔的值,我想将其分隔成多行。 这是一个例子: SELECT p.idperson, p.rolepe
我用谷歌搜索并查看了 MSDN TSQL 引用,但无法找到“ .. ”的含义或作用的定义或解释。 示例 if OBJECT_ID('tempdb..#temp') is not null drop t
我需要以下内容: 检查 Public 或 guest 是否被授予对对象(数据库角色和服务器角色)的任何权限 检查是否有任何用户被授予对对象而非角色的权限。 检查用户是否对对象 拥有 with gran
DECLARE @query as varchar(200); SET @query = 'SELECT COUNT(*) FROM table'; 如何执行@query,此外,在分配变量时是否可以直
我正在编写报告,并希望获取年龄列的平均值。问题在于,并非所有行都具有年龄。 如果该列的值为0 2 4,我希望返回3,而不是2。我不能简单地使用WHERE排除零行,因为我正在使用那些行中的其他列。是否存
我尝试在 tsql 中左修剪换行符。所以我想删除领先的 字符(13) + 字符(10) 我在一个字段中的数据。并确保不会删除其他换行符。 我的意思是: ' Kanne Oliver Rosa-Luxe
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: How to execute mathematical expression stored in a var
我认为最好以简单示例的形式提出这个问题。以下 SQL 块会导致“DB-Library Error:20049 Severity:4 Message:Data-conversion导致溢出”消息,但怎么
我们的一位用户在开发服务器中执行了插入语句。插入工作正常。但是在生产服务器执行同样的insert语句时,却遇到了如下错误: Error:.Net SqlClient Data Provider Err
我如何编写 TSQL 来计算以下内容: 当前日期和午夜,例如 2010-12-01 00:00:00.000 当前日期和下午 6 点,例如 2010-12-01 18:00:00.000 谢谢.. 最
我最近有一个关于在 TSQL 中使用游标的争论...... 首先,我不是辩论中的啦啦队长。但是每次有人说 cursor 时,总会有一些笨蛋(或 50 个)用强制性的“光标是邪恶的”咒语来猛扑。我知道
是否可以在不显式定义列的情况下在 sql 中声明变量? 类似 declare @tab as select * from myTable 最佳答案 你可以选择一个临时表......看起来它会做你想要的
我们遇到的问题简化为以下两个语句: select convert(float, (convert(float,5741.61)/convert(float, 196.00)) * convert(fl
我有一个 ADO.NET/TSQL 性能问题。我们的应用程序中有两个选项: 1) 一个具有多个结果集的大型数据库调用,然后在代码中逐步遍历每个结果集并填充我的对象。这会导致一次到数据库的往返。 2)
问题背景 生成和访问固定列布局的数据很容易。您可以预先创建本地临时表,并通过调用存储过程来填充它们。 另一方面,如果要生成具有动态列布局的数据,通常必须动态构建 SQL 语句并使用“exec sp_e
我有一个SQL Server 2005查询,该查询生成较大的结果集(最大为数GB): SELECT * FROM Product FOR XML PATH('Product') 运行查询将生成包含包含
我使用此代码在 sql server 中读取 .mp3 文件字节。当我使用本地连接时,它工作正常。但是,当我更改与服务器的连接(例如: 192.168.1.1 和 windows 身份验证模式 )并运
这不会在 ColdFusion 11 中崩溃,但会在 ColdFusion 2016 中崩溃 SELECT * FROM dbo.Roles WITH (NOLOCK) WHERE Cod
我是一名优秀的程序员,十分优秀!