如何将两个查询的结果组合到一个数据集中

时间:2013-03-05 17:31:55

标签: sql sql-server sql-server-2008 tsql

我有两个问题: 查询简化,不包括联接

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

我想知道如何获得输出:

ProductName NumberofProducts(in inventory)  ProductName NumberofProductsSold

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

(我试过了union语句,但它对我想看的输出不起作用。)

10 个答案:

答案 0 :(得分:48)

这是一个在两个完全不相关的表之间建立联合的示例:Student和Products表。它生成一个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

显然你会为自己的环境调整一下......

答案 1 :(得分:26)

我认为你是在追求这样的事情; (将row_number()CTE一起使用并执行FULL OUTER JOIN

Fiddle example

;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 |

答案 2 :(得分:11)

怎么样,

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

答案 3 :(得分:3)

如果您的意思是两个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;

否则,如果Table1的任何行可以与Table2中的任何行相关联(尽管我真的想知道为什么有人想要这样做),你可以给this看看。

答案 4 :(得分:2)

问题在于,除非您的表格相关,否则您无法确定如何加入它们,因此您必须随意加入它们,从而产生笛卡尔积:

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

这会把你的东西拉到一起,但数据是相关的,给你结果。

答案 5 :(得分:1)

古老的问题,但是其他人使用JOIN将不相关的查询合并到一个表中的行时,这是我的解决方案,将不相关的查询合并到一行中,例如:

withContext(Dispatchers.IO)

给出以下单行输出:

coroutineScope.launch(newSingleThreadContext("MyOwnThread")

(这告诉我,我们的Web服务器当前在290个会话中使用了14个Oracle会话;我在没有头的情况下将此输出记录在每隔几分钟运行的sqlplus脚本中)

答案 6 :(得分:0)

试试这个:

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

答案 7 :(得分:0)

将每个查询加载到数据表中:

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

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

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

答案 8 :(得分:0)

  

这是你可以做的。假设您的ProductName列具有共同的值。

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

答案 9 :(得分:-3)

试试这个:

获取CURRENT_MONTH,LAST_MONTH和ALL_TIME的记录并将其合并到单一阵列

$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;
相关问题