简体   繁体   English

如何计算某个维度使用了多少次 TABLEAU/SQL

[英]How do I count how many times a certain dimension is used TABLEAU/SQL

Im trying to count how many times my barcodes come up in multiple places.我试图计算我的条形码出现在多个地方的次数。 Im trying to bucket barcodes into two categories 1) barcodes that only come up in ONE business unit 2) barcodes that show up in multiple business units我试图将条形码分为两类 1) 仅出现在一个业务单元中的条形码 2) 出现在多个业务单元中的条形码

在此处输入图像描述

I dont care about how many times they are shown at multiple business units, just simply if it does show up in more than one我不在乎它们在多个业务部门出现了多少次,只要它确实出现在多个业务部门中就行了

SOS求救

Calculate a count of how many fields are not null.计算有多少字段不是 null。

SELECT *, Abs(NOT IsNull(CH101) + NOT IsNull(CH311) + etc) AS CntBU FROM table;

Otherwise, normalize data structure so an aggregate query can be used.否则,规范化数据结构以便可以使用聚合查询。 UNION can rearrange data to normalized structure then use UNION query in an aggregate query. UNION 可以将数据重新排列为标准化结构,然后在聚合查询中使用 UNION 查询。

SELECT BarCode, RecordType, ProductCo, CH101 AS Data, "CH101" AS BU FROM table
UNION SELECT BarCode, RecordType, ProductCo, CH311, "CH311" FROM table
etc

Limit of 50 SELECT lines in UNION. UNION 中的 SELECT 行限制为 50 条。

One solution is to define a set based on the barcode (or mint if preferred) field.一种解决方案是基于条形码(或薄荷,如果首选)字段定义一个集合。 Right click on barcode (or mint) in the data pane sidebar and create set.右键单击数据窗格侧栏中的条形码(或薄荷)并创建集。 Define membership in the set with the condition使用条件定义集合中的成员资格

countd([Business Unit]) > 1

Barcodes belong to the set if they appear with multiple business units.如果条形码与多个业务单位一起出现,则它们属于该集合。 You can use the set on any shelf or calculation您可以在任何架子或计算上使用该集合

For an even simpler approach to understand how COUNTD() works.为了更简单的方法来理解 COUNTD() 的工作原理。 In a new worksheet, put one discrete field on the Rows shelf, say Barcode, then put Business Unit on Columns.在新工作表中,将一个离散字段放在“行”架子上,例如条形码,然后将“业务单位”放在“列”上。 Then right click on Business Unit on the columns shelf, and convert it from a dimension to a measure using the COUNTD function.然后右键单击列架上的业务单元,并使用 COUNTD function 将其从维度转换为度量。

You should then see a mark for each of your Barcodes showing how many distinct Business Units that Barcode appears with in the data.然后,您应该会看到每个条码的标记,显示条码与数据中出现的不同业务单位的数量。 For the Barcodes where COUNTD([Business Unit]) = 1, those are the ones where they only appear in the data with one particular [Business Unit]对于 COUNTD([Business Unit]) = 1 的条形码,它们仅出现在具有一个特定 [Business Unit] 的数据中

What the set does is distinguish the barcodes that appear with multiple Business Units from those that appear only with one.该集合所做的是将与多个业务单位一起出现的条码与仅与一个业务单位一起出现的条码区分开来。 One way to think of the set is as a boolean function based on Barcodes.将集合视为基于条码的 boolean function 的一种方式。

You can use sets for all kinds of things in Tableau, well described in the help and training, but they are not essential for your problem (just convenient) The function that solves your problem is COUNTD().您可以在 Tableau 中将集合用于各种事物,在帮助和培训中进行了很好的描述,但它们对于您的问题并不是必需的(只是方便)解决您的问题的 function 是 COUNTD()。 Its worth the time to make sure you understand it well.值得花时间确保你理解它。 In SQL, it is equivalent to "COUNT DISTINCT"在 SQL 中,相当于“COUNT DISTINCT”

Last point, COUNTD() is useful and important, but it is more computationally intensive than other aggregation functions like COUNT() or SUM().最后一点,COUNTD() 有用且重要,但它比其他聚合函数(如 COUNT() 或 SUM())计算量更大。 So if you have very large datasets and see performance problems, see if calls to COUNTD may be the culprit.因此,如果您有非常大的数据集并看到性能问题,请查看对 COUNTD 的调用是否可能是罪魁祸首。 You can then look at ways to shape the data differently to avoid the need for COUNTD().然后,您可以研究以不同方式塑造数据的方法,以避免对 COUNTD() 的需要。 Still, for most small or medium size datasets, the convenience of COUNTD() outweighs any performance costs尽管如此,对于大多数中小型数据集, COUNTD() 的便利性超过了任何性能成本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM