简体   繁体   English

全部来自表 A 并为表 B 中的每个集合包含一行,即使表 B 中没有相关记录

[英]All from table A and include a row for each of a set from table B even if there is no related record in Table B

I had a hard job describing this simple problem...我很难描述这个简单的问题......

I want to return a series of data points for a stacked bar chart for each stack Label and Bar, whether or not there is a record for the Label/Bar datapoint, a null value being returned where there is no data record.我想为每个堆栈 Label 和 Bar 的堆积条形图返回一系列数据点,无论是否有标签/条形数据点的记录,在没有数据记录的情况下返回 null 值。 The 'missing' Label must also be included:还必须包括“缺失”的 Label:

    Label   Bar  Value
    a       X    99
    a       Y    23
    a       Z    97
    b       X    null
    b       Y    32
    b       Z    null

I have table DataLabel that contains the Labels for the each type of datapoint in the bar stack and a table DataSeries that contains the X-axis Labels and the Y-axis values to be calculated for each datapoint.我有一个表 DataLabel,其中包含条形堆栈中每种类型数据点的标签,还有一个表 DataSeries,其中包含要为每个数据点计算的 X 轴标签和 Y 轴值。

    create table DataLabel
    (id number
    , label varchar2(10));

    Insert into DataLabel values (1, 'a');
    Insert into DataLabel values (2, 'b');

    Create table DataSeries
    (id number
    , DataLabel_id number
    , val number
    , DataSeriesName varchar2(5));

    Insert into DataSeries values (1, 1, 99, 'X');
    Insert into DataSeries values (11, 1, 23, 'Y');
    Insert into DataSeries values (12, 2, 32, 'Y');
    Insert into DataSeries values (21, 1, 97, 'Z');

The problem arises, where the DataSeries table be does not contain a corresponding value for a DataLabel for a given datapoint.问题出现了,其中 DataSeries 表不包含给定数据点的 DataLabel 的相应值。

I have managed an inelegant solution that does return the desired result in SQLfiddle using a Union join, but is there a better solution....?我已经管理了一个不优雅的解决方案,它使用联合连接在SQLfiddle中返回了所需的结果,但是有更好的解决方案......?

Use a cross join to generate the rows and then a left join to bring in the values:使用cross join来生成行,然后使用left join来引入值:

select dl.label, dsn.dataseriesname,
       ds.val
from datalabel dl cross join
     (select distinct dataseriesname from dataseries
     ) dsn left join
     dataseries ds
     on ds.datalabel_id = dl.id and
        ds.dataseriesname = dsn.dataseriesname
order by dl.label, dsn.dataseriesname;

Here is the SQL Fiddle.是 SQL 小提琴。

A cross join among tables containing a correlated subquery might be an option:包含相关子查询的表之间的cross join可能是一种选择:

select distinct label, DataSeriesName as bar,
      (select val 
         from DataSeries 
        where DataSeriesName=s.DataSeriesName 
          and DataLabel_id=l.id) as value
  from DataSeries s
 cross join DataLabel l 
 order by label, bar

Demo 演示

暂无
暂无

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

相关问题 显示表A中的所有记录,并跳过表B中的匹配记录 - Show all record From table A with skipping matching record in Table B 插入表A中,从表A中选择,在满足B中条件的每个记录中,从B中选择条件B。 - insert into table A, select from table A join B where condition from B, for each record that meets condition in B 对于表A中的每个项目,返回表B中所有不活动的项目 - Return all inactive items from Table B for each item in Table A SQL 查询来自两个表和一个链接表,以返回表 c 的所有表 a 的每一行,其中表 b 中没有链接 - SQL query from Two tables and a link table to return all of table c for each row of table a with nulls where the is no link in table b 选择表A中与表B的所有项目结合的行 - Select rows in table A that junction to all of a set of items from table B 从表a中选择记录不在表b和表c中的位置 - select from table a where record is not in table b and table c SSIS:如何将数据从表A传输到表B,然后为每行更新表A标志列 - SSIS: How to transfer data from table A to table B and then update table A flag column for each row Oracle:触发以替换具有相等ID的记录的从相关表B到空间表A的行 - Oracle: Trigger to replace row from related table B to spatial table A for records with equal IDs 从表 A 中获取与表 B 上的数据相关的记录 - Fetch records from table A related to data on table B 从一个表中检索所有记录,即使在相关表中找不到相关记录 - Retrieve all records from one table even if a related record can't be found in a related table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM