SQL Server iff函数的使用

本文介绍了一种使用SQL的条件查询技巧,通过selectiff函数可以根据不同条件返回不同的结果值,例如根据年龄判断是否成年,并重新命名查询结果的列名。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

select iff(条件,1,2) from table_name

条件:带有字段值的判断表达式,结果为 true || false

1:当条件判断为true时选择的结果值

2:当条件判断为false时选择的结果值

例:select iff(age < 18,‘未成年’,‘已成年’) as ‘是否成年’ from person

从person表查询age列,判断某个person的年龄,小于18,输出未成年,大于等于18,

输出已成年,重新命名查询结果的列名为 ‘是否成年’

As described in Restrict Access at the Data Row Level, this approach requires the database to contain the field you want to use for filtering. For this example, the data includes a reference table called People, which contains two columns: Region and Manager. Names in the Manager field match Tableau Cloud or Tableau Server user names, and we’ll use this field for filtering. You can follow along using the Superstore data that comes with Tableau Desktop, although the fields and values do not match exactly. Connect to the data and set up the user filter In Tableau Desktop, open the workbook and connect to the data you want to filter. In this example, we use a table called Orders. Double click the Orders table to open the join canvas. Add the reference table (People) with a left join. For this example, join on the field Region. On the worksheet, select Analysis > Create Calculated Field, and create the following field, named "User is a manager". USERNAME() = [Manager] This calculation returns TRUE if the user name of the person signed in to the server exists in the manager column of the People table. Add the User is a manager field to the Filters shelf. In the Filter dialog box, select True, and then click OK. This sets the filter so that only people who are managers can see the data in the view. If you are not listed in the Manager field, your view while authoring might appear as a blank canvas. See how the view looks to a particular person: in the lower-right, open the Filter as User menu, and select someone you know is a manager. Important: As with a manual user filter, you need to take steps to Secure user filters on published content. Filter a data source Rather than maintain user filters and special permissions on each published workbook, you can filter a data source, and then publish the data source to make it available as a shared, one-to-many resource for anyone who uses that data. This procedure builds from the dynamic-filter approach described in th
03-26
<think>好的,我需要帮助在Tableau中设置行级安全性,使用参考表和USERNAME函数实现动态过滤。首先,我应该了解一下Tableau的行级安全(RLS)是如何工作的。通常,RLS允许根据用户身份限制他们能看到的数据行。用户过滤通常涉及将用户的登录信息与数据中的某个字段匹配,比如部门、区域等。 用户提到要使用参考表和USERNAME函数。参考表应该是一个包含用户名和对应权限的表,比如每个用户可以访问哪些部门的数据。USERNAME函数在Tableau中返回当前登录用户的用户名,这通常是用户的电子邮件或网络ID,具体取决于Tableau Server或Online的配置。 步骤可能包括:首先,将参考表与主数据表连接,确保每个用户的权限信息能与数据关联。然后创建计算字段,使用USERNAME()来动态过滤数据。例如,只有当参考表中的用户名与当前用户匹配时,才显示相关数据行。 需要注意的点是数据混合的方式是否正确,可能需要使用数据混合而不是传统的连接,特别是如果参考表和主数据来自不同数据源。此外,确保在发布到Tableau Server时,权限设置正确,用户只能访问他们被授权的视图。 可能遇到的问题包括USERNAME函数返回的值与参考表中的用户名格式不一致,比如大小写或域名部分的问题,需要处理字符串匹配。另外,性能问题如果参考表很大,可能需要优化数据模型。 还需要考虑测试方法,如何验证不同用户登录后只能看到他们应该看到的数据。可能需要创建多个测试账户,或者使用参数模拟不同用户。 最后,记得参考Tableau的官方文档,查看是否有最佳实践或特定配置需要注意,比如使用行级安全筛选器或用户过滤器功能。</think>在Tableau中通过参考表和USERNAME函数实现行级安全的步骤如下: 1. **准备参考表** 创建包含两列的权限表: - `Username`:与Tableau Server/Online登录名一致的用户标识 - `Allowed_Field`:用于过滤的字段(如部门代码、区域ID等) ```sql -- 示例结构 SELECT 'user1@company.com' AS Username, 'Sales' AS Allowed_Field UNION ALL SELECT 'user2@company.com', 'Finance' ``` 2. **建立数据关联** - 将主数据表与权限表进行左连接,连接字段为`Allowed_Field` - 若数据源不同,使用`数据混合`并设置基于`Allowed_Field`的关联 3. **创建动态过滤计算字段** 新建计算字段`[行级权限验证]`: ```tableau [Username] = USERNAME() // 引用混合数据源中的权限表字段 AND NOT ISNULL([Allowed_Field]) // 确保权限匹配成功 ``` 4. **应用过滤器** - 将`[行级权限验证]`字段拖入"筛选器"卡,选择`True`值 - 右键该筛选器 → 选择"编辑筛选器" → 勾选"所有值" - 在筛选器菜单选择"上下文筛选器"(重要步骤) 5. **发布设置** - 发布工作簿时勾选"包括外部文件"确保权限表同步 - 在Tableau Server上为用户分配"查看"权限而非"交互"权限 $$ \text{权限验证逻辑表达式:} \\ \forall d \in D,\ u \in U,\ \text{Visible}(d,u) \iff \exists r \in R\ \text{where}\ r.u = u_{\text{current}} \land r.f = d.f $$ 其中$D$为主数据集,$U$为用户集,$R$为参考表,$f$为过滤字段[^1] **关键注意事项**: - 用户名匹配需区分大小写,建议使用`LOWER()`函数统一格式 - 如需多级权限控制,可在参考表中添加权重字段,并使用`MAX()`聚合函数确定最高权限 - 性能优化建议为权限表创建单独的数据提取并建立索引[^2]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值