如何将两个查询的结果组合成一个单一的数据集(How to combine results of tw

2019-07-21 01:23发布

我有两个疑问:查询简体排除加盟

Query 1 : select ProductName,NumberofProducts (in inventory) from Table1.....;
Query 2 : select ProductName, NumberofProductssold from Table2......;

我想知道我怎样才能得到一个输出:

ProductName NumberofProducts(in inventory)  ProductName NumberofProductsSold

用于获取每个查询的输出的关系是不同的。 我需要输出这种方式为我的SSRS报告。

(我试过联合声明,但它不为我想看到的输出工作。)

Answer 1:

这里是做两个完全不相干的表之间的联合的示例:学生和产品表。 它产生一输出即4列:

select
        FirstName as Column1,
        LastName as Column2,
        email as Column3,
        null as Column4
    from
        Student
union
select
        ProductName as Column1,
        QuantityPerUnit as Column2,
        null as Column3,
        UnitsInStock as Column4
    from
        Products

很明显,你会调整这个为自己的环境...



Answer 2:

我觉得你这样的事情后, (使用row_number()CTE和执行FULL OUTER JOIN

例如拨弄

;with t1 as (
  select col1,col2, row_number() over (order by col1) rn
  from table1 
),
t2 as (
  select col3,col4, row_number() over (order by col3) rn
  from table2
)
select col1,col2,col3,col4
from t1 full outer join t2 on t1.rn = t2.rn

表和数据:

create table table1 (col1 int, col2 int)
create table table2 (col3 int, col4 int)

insert into table1 values
(1,2),(3,4)

insert into table2 values
(10,11),(30,40),(50,60)

结果:

|   COL1 |   COL2 | COL3 | COL4 |
---------------------------------
|      1 |      2 |   10 |   11 |
|      3 |      4 |   30 |   40 |
| (null) | (null) |   50 |   60 |


Answer 3:

怎么样,

select
        col1, 
        col2, 
        null col3, 
        null col4 
    from Table1
union all
select 
        null col1, 
        null col2,
        col4 col3, 
        col5 col4 
    from Table2;


Answer 4:

如果你的意思是,这两个ProductName字段具有相同的值,则:

SELECT a.ProductName,a.NumberofProducts,b.ProductName,b.NumberofProductsSold FROM Table1 a, Table2 b WHERE a.ProductName=b.ProductName;

或者,如果你想要的ProductName所显示的列只有一次,

SELECT a.ProductName,a.NumberofProducts,b.NumberofProductsSold FROM Table1 a, Table2 b WHERE a.ProductName=b.ProductName;

否则,如果表1中的任何行从表2(即使我真的不知道为什么anyone'd想这样做)的任何行相关联,你可以给这个看看。



Answer 5:

问题是,除非你的表是相关的,你不能确定如何加入他们,所以你必须任意加入他们的行列,导致笛卡尔积:

select Table1.col1, Table1.col2, Table2.col3, Table2.col4
from Table1
cross join Table2

如果你有,例如,下面的数据:

col1  col2
a     1
b     2

col3  col4
y     98
z     99

你最终会得到如下:

col1  col2  col3  col4
a     1     y     98
a     1     z     99
b     2     y     98
b     2     z     99

这是你在找什么? 如果没有,你有关于表的一些手段,那么你需要包括在两个表拼接,例如:

select Table1.col1, Table1.col2, Table2.col3, Table2.col4
from Table1
inner join Table2
on Table1.JoiningField = Table2.JoiningField

这将拉开东西放在一起你到然而,数据是相关的,给你你的结果。



Answer 6:

加载每个查询到数据表:

http://www.dotnetcurry.com/ShowArticle.aspx?ID=143

加载两个数据表到数据集:

http://msdn.microsoft.com/en-us/library/aeskbwf7%28v=vs.80%29.aspx



Answer 7:

这是你可以做什么。 假设你的ProductName列有共同的价值观。

SELECT 
     Table1.ProductName, 
     Table1.NumberofProducts, 
     Table2.ProductName, 
     Table2.NumberofProductssold
FROM Table1
INNER JOIN Table2
ON Table1.ProductName= Table2.ProductName


Answer 8:

试试这个:

SELECT ProductName,NumberofProducts ,NumberofProductssold
   FROM table1 
     JOIN table2
     ON table1.ProductName = table2.ProductName


Answer 9:

试试这个:

获取记录CURRENT_MONTH,LAST_MONTH和ALL_TIME并将其合并为单一ARRAY

$analyticsData = $this->user->getMemberInfoCurrentMonth($userId);
$analyticsData1 = $this->user->getMemberInfoLastMonth($userId);
$analyticsData2 = $this->user->getMemberInfAllTime($userId);                        

    foreach ($analyticsData2 as $arr) {                
        foreach ($analyticsData1 as $arr1) {
            if ($arr->fullname == $arr1->fullname) {
                $arr->last_send_count = $arr1->last_send_count;
                break;
            }else{
                $arr->last_send_count = 0;
            }
        }
        foreach ($analyticsData as $arr2) {
            if ($arr->fullname == $arr2->fullname) {
                $arr->current_send_count = $arr2->current_send_count;
                break;
            }else{
                $arr->current_send_count = 0;
            }
        }
    }
    echo "<pre>";
    print_r($analyticsData2);die;


文章来源: How to combine results of two queries into a single dataset