将两个不同记录的结果合二为一

时间:2013-12-26 19:22:16

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

我有一个SQL数据存储在一个表中,但有不同的类型,我想在下面的例子中将结果合并到一个班轮显示中。任何人都可以给出这个SQL查询的建议吗?

我的SQL查询得到了这个结果:

SELECT NumValue, Label, Type
FROM (CustomPollerStatusTable
      INNER JOIN CustomPollers ON (CustomPollerStatusTable.PollerID = 
                                   CustomPollers.CustomPollerID)) 
INNER JOIN Nodes ON (CustomPollerStatusTable.NodeID = Nodes.NodeID)
WHERE ((Nodes.Caption = 'fqdn') AND 
       (CustomPollers.Type = 'Student Info') AND 
       (CustomPollerStatusTable.NumValue > 89) AND  
       (NOT (CustomPollerStatusTable.Label LIKE '%.snapshot%')) AND
       (NOT (CustomPollerStatusTable.Label LIKE '%aggr%')) AND
       (NOT (CustomPollerStatusTable.Label = '/vol/scratch/'))
      )


====================================
NumValue | Label     |    Type
====================================
90       | Student 1 | Student Info
10       | Student 1 | Student Class
====================================

我想要达到的结果:

========================================================================== 
NumValue.Info | NumValue.Class | Label     | Type.Info    | Type.Class
========================================================================== 
90            | 10             | Student 1 | Student Info | Student Class
==========================================================================

2 个答案:

答案 0 :(得分:2)

这应该这样做。与nrathaus的答案略有不同。显然,您可以使用as将列名更改为您想要的任何名称。

select
    s1.NumValue,
    s2.NumValue,
    s1.Label,
    s1.Type,
    s2.Type
from Student s1 inner join Student s2
on s1.Label = s2.Label
where s1.Type like '%Info'
and s2.Type like '%Class'

编辑:既然您已发布了您的select语句,我认为这可能不起作用。你可以张贴表结构吗?

EDIT2:这可能有效。

INSERT INTO TempTable(
    NumValInfo,
    Label,
    TypeInfo)
SELECT
    c.NumValue,
    c.Label,
    p.Type
FROM (CustomPollerStatusTable c INNER JOIN CustomPollers p
ON (c.PollerID = p.CustomPollerID)) 
    INNER JOIN Nodes n
ON (c.NodeID = n.NodeID)
WHERE n.Caption = 'fqdn'
AND p.Type = 'Student Info'
AND c.NumValue > 89
AND NOT (c.Label LIKE '%.snapshot%')
AND NOT (c.Label LIKE '%aggr%')
AND NOT (c.Label = '/vol/scratch/')
AND p.Type like '%Info'

UPDATE TempTable set
    NumValClass = c.NumValue,
    TypeClass = p.Type
FROM (CustomPollerStatusTable c INNER JOIN CustomPollers p
ON (c.PollerID = p.CustomPollerID)) 
    INNER JOIN Nodes n
ON (c.NodeID = n.NodeID)
    INNER JOIN TempTable t
ON t.Label = c.Label
WHERE n.Caption = 'fqdn'
AND p.Type = 'Student Info'
AND c.NumValue > 89
AND NOT (c.Label LIKE '%.snapshot%')
AND NOT (c.Label LIKE '%aggr%')
AND NOT (c.Label = '/vol/scratch/')
AND p.Type like '%Class'

SELECT * FROM TempTable

答案 1 :(得分:-1)

我认为这样做:

SELECT tblInfo.NumValue AS `NumValue.Info`, 
       tblClass.NumValue AS `NumValue.Class`,
       Table.Label AS Label,
       tblInfo.Type AS `Type.Info`,
       tblClass.Type AS `Type.Class`
FROM (Table)
LEFT JOIN Table AS tblInfo ON tblInfo.Label = Table.Label ON tblInfo.Type = 'Student Info'
LEFT JOIN Table AS tblClass ON tblClass.Label = Table.Label ON tblClass.Type = 'Student Class'
GROUP BY Table.Label