简体   繁体   English

SQL查询堆积条形图

[英]SQL query for stacked bar chart

In my project I'm trying to write a query to fetch data for a stacked bar graph. 在我的项目中,我试图编写一个查询以获取堆积条形图的数据。 Instead of explaining the schema of my DB (which would be lengthy), I found a similar analogy which I can port to my DB later. 我没有解释数据库的架构(这很冗长),而是找到了一个类似的类比,以后可以移植到数据库。

On this page , run the query 在此页面上 ,运行查询

SELECT * FROM [Products]

In the table, if I want to select data for stacked bar graph as below, how do I form the query? 在表格中,如果要选择如下所示的堆积条形图数据,该如何形成查询?

  • one bar for each CategoryID 每个CategoryID一格
  • within each CategoryID, I want the number of products that use bags, bottles or jars (Substring in Unit column) 在每个CategoryID中,我想要使用袋子,瓶子或罐子的产品数(单位列中的子字符串)

Sorry, I'm not able to demonstrate substantial research effort in this, as I'm not even able to visualize what the SELECT clause would look like. 抱歉,我无法在此方面进行大量研究,因为我什至无法可视化SELECT子句的外观。 I tried to check if we can use AS in WHERE clause and then do a COUNT on it. 我试图检查是否可以在WHERE子句中使用AS,然后对其进行计数。 But that does not seem to be correct syntax. 但这似乎不是正确的语法。

I'm going to be running the actual query on MS SQL Server through PowerBI 我将通过PowerBI在MS SQL Server上运行实际查询

The first step will be to create a flag for bags, bottles, and jars. 第一步将为袋,瓶和罐子创建一个标志。 This can be done in either SQL or Power BI. 这可以在SQL或Power BI中完成。

For SQL, use a query like this: 对于SQL,请使用如下查询:

SELECT *
    ,   CASE
            WHEN Products.Unit LIKE '%bag%' THEN 'Bag'
            WHEN Products.Unit LIKE '%bottle%' THEN 'Bottle'
            WHEN Products.Unit LIKE '%jar%' THEN 'Jar'
            ELSE 'Other'
        END AS 'UnitFlag'
FROM dbo.Products

For Power BI, use DAX to create a new column with this formula: 对于Power BI,使用DAX通过以下公式创建新列:

UnitFlag = IF(IFERROR(SEARCH("bag", Products[Unit]), -1) > 1, "Bag",
    IF(IFERROR(SEARCH("Bottle", Products[Unit]), -1) > 1, "Bottle",
        IF(IFERROR(SEARCH("Jar", Products[Unit]), -1) > 1, "Jar",
            "Other"
        )
    )
)

Then just configure your stacked chart as shown below. 然后只需配置您的堆叠图表,如下所示。

图形

From here, you can change a couple of settings to make the graph look a little nicer; 在这里,您可以更改一些设置,以使图形看起来更好一些; for instance, changing the X-Axis to be treated as Categorical instead of Continuous and hiding the "Other" category if you don't want that visible. 例如,将X轴更改为“分类”而不是“连续”,并在不希望显示的情况下隐藏“其他”类别。

It sounds like you want to learn about aggregation: 听起来您想了解聚合:

select categoryID, 
       reverse(left(reverse(unit), charindex(' ',reverse(unit))-1)) as Unit_type, -- gets the last word of `UNIT`
       count(*) as Total_Products
from Products
group by categoryID, 
         reverse(left(reverse(unit), charindex(' ',reverse(unit))-1))

What this will do is count all of the rows and group by Unit type and categoryID. 这将根据单位类型和类别ID对所有行和组进行计数。 Try it, see what happens. 试试看,看看会发生什么。

I would suggest that you learn about aggregation sooner rather than later, it's incredibly powerful. 我建议您尽早了解聚合,它非常强大。

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

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