Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何显示两行中的一行_Sql_Sql Server_Sql Server 2000 - Fatal编程技术网

Sql 如何显示两行中的一行

Sql 如何显示两行中的一行,sql,sql-server,sql-server-2000,Sql,Sql Server,Sql Server 2000,我的表中有type列,type列的值是HOT和NOT。因此,我希望在一行中显示列的HOT和NOT值 范例 表1 Period ID Total 11/2011 101 250 12/2011 102 350 11/2011 103 450 .... 表2 Period ID Type Value 11/2011 101 NOT 500 11/2011 101 HOT 200 12/2011 102 NOT 300 12/2011 102 HOT 200 .... 我想在一行中显示类型(H

我的表中有
type
列,
type
列的值是
HOT
NOT
。因此,我希望在一行中显示列的
HOT
NOT

范例

表1

Period ID Total

11/2011 101 250
12/2011 102 350
11/2011 103 450
....
表2

Period ID Type Value

11/2011 101 NOT 500
11/2011 101 HOT 200
12/2011 102 NOT 300
12/2011 102 HOT 200
....
我想在一行中显示类型(
Hot
Not

预期产量

Period ID NOT HOT Total

11/2011 101 500 200 250
12/2011 102 300 200 350
11/2011 103 300 400 450
....
如何进行查询。

您尝试过这个吗

select base.period, base.id, sum( notchild.value ) as notsum, sum( hotchild.value ) as hotsum, base.total
from table1 base
left outer join table2 notchild 
    on base.period = notchild.period and base.id = notchild.id and notchild.type = 'NOT'
left outer join table2 hotchild 
    on base.period = hotchild.period and base.id = hotchild.id and hotchild.type = 'HOT'
group by base.period, base.id, base.total
你试过这个吗

select base.period, base.id, sum( notchild.value ) as notsum, sum( hotchild.value ) as hotsum, base.total
from table1 base
left outer join table2 notchild 
    on base.period = notchild.period and base.id = notchild.id and notchild.type = 'NOT'
left outer join table2 hotchild 
    on base.period = hotchild.period and base.id = hotchild.id and hotchild.type = 'HOT'
group by base.period, base.id, base.total

您可以使用
JOIN
(检查)来执行此操作。连接两个表并在其上执行
选择。

您可以使用
连接
(检查)来执行此操作。连接两个表并对其执行
选择

如果我可以假设表1的主键为(句点,ID),表2的主键为(句点,ID,类型),那么您可以执行以下操作:

select
    t1.period
    , t1.id
    , t2n.value [not]
    , t2h.value [hot]
    , t1.total
from
    table1 t1
    left join table2 t2n
        on t1.period = t2n.period
        and t1.id = t2n.id
        and t2n.type = 'Not'
    left join table2 t2h
        on t1.period = t2h.period
        and t1.id = t2h.id
        and t2h.type = 'Hot'

这将检索表1中的所有行及其对应的“not”和“hot”对应项,分别对应于上面的t2n和t2h。

如果我可以假设表1的主键为(Period,ID),而表2的主键为(Period,ID,Type),那么您可以执行以下操作:

select
    t1.period
    , t1.id
    , t2n.value [not]
    , t2h.value [hot]
    , t1.total
from
    table1 t1
    left join table2 t2n
        on t1.period = t2n.period
        and t1.id = t2n.id
        and t2n.type = 'Not'
    left join table2 t2h
        on t1.period = t2h.period
        and t1.id = t2h.id
        and t2h.type = 'Hot'

这将检索表1中的所有行,以及它们对应的“not”和“hot”对应项,分别对应于上面的t2n和t2h。

您可能忘记添加
notchild.type='not'
hotchild.type='hot'
。您完全正确!很抱歉看到了原来的海报。我会立即修改答案。您可能忘记在某个地方添加
notchild.type='NOT'
hotchild.type='HOT'
(很可能是加入条件)。您完全正确!很抱歉看到了原来的海报。我马上修改答案。也有可能
(ID)
表1
的主键和
(ID,Type)
表2
,和
表2.Period
是一个重复
表1的冗余列。Period
@CD Jorgensen,它在工作,但在表2中,我的Period比表1多,所以我想显示表2和表2中的所有其他时间段table1@RemoRose:尝试完全联接而不是左联接;要获取
期间
id
的正确结果,请使用
大小写
表达式或
合并()。也有可能
(ID)
表1
的主键和
(ID,Type)
表2
,和
表2.Period
是一个重复
表1的冗余列。Period
@CD Jorgensen,它在工作,但在表2中,我的Period比表1多,所以我想显示表2和表2中的所有其他时间段table1@RemoRose:尝试完全联接而不是左联接;要获取
期间
id
的正确结果,请使用
大小写
表达式或
合并()。