Sql server 2008 返回叠加在二月数据之上的一月数据比并排返回更容易。我将更新我的帖子以进行说明。谢谢thomas,最后一个查询将返回两个查询中的所有行。现在我想知道的是。。。。。。我可以在联合后对数据进行分组吗。。。。。。。。。。你能告诉我哪里可以学习高级查询技术吗-谢谢。

Sql server 2008 返回叠加在二月数据之上的一月数据比并排返回更容易。我将更新我的帖子以进行说明。谢谢thomas,最后一个查询将返回两个查询中的所有行。现在我想知道的是。。。。。。我可以在联合后对数据进行分组吗。。。。。。。。。。你能告诉我哪里可以学习高级查询技术吗-谢谢。,sql-server-2008,date,Sql Server 2008,Date,返回叠加在二月数据之上的一月数据比并排返回更容易。我将更新我的帖子以进行说明。谢谢thomas,最后一个查询将返回两个查询中的所有行。现在我想知道的是。。。。。。我可以在联合后对数据进行分组吗。。。。。。。。。。你能告诉我哪里可以学习高级查询技术吗-谢谢。@Shahidul-关于什么的小组?您可以将整个Union All查询放在派生表中(例如,Select…From()作为Foo)。 Query1: ---------- SELECT t1.a1, t1.a2, t2.b1,t2.b2 fro


返回叠加在二月数据之上的一月数据比并排返回更容易。我将更新我的帖子以进行说明。谢谢thomas,最后一个查询将返回两个查询中的所有行。现在我想知道的是。。。。。。我可以在联合后对数据进行分组吗。。。。。。。。。。你能告诉我哪里可以学习高级查询技术吗-谢谢。@Shahidul-关于什么的小组?您可以将整个Union All查询放在派生表中(例如,
Select…From()作为Foo
)。
Query1:
----------
SELECT t1.a1, t1.a2, t2.b1,t2.b2 
from (SELECT a1,a2 from xyz WHERE (date BETWEEN '2011-01-01' AND '2011-01-30')
AND id = 70 GROUP BY a1 a2)t1,
(SELECT a1,a2 from xyz WHERE (date BETWEEN '2011-01-01' AND '2011-01-30')
AND id = 70 GROUP BY a1 a2)t2, t3

where t1.a1=t3.a1
and t2.a1=t3.a1


output 1:
---------------
a1 a2  b1  b2
---------------
1   2  7   4
1   3  4   2
1   6  5   1
query2:
---------------
SELECT t1.a1, t1.a2, '' as b1,'' as b2 
FROM(SELECT a1,a2 from xyz WHERE (date BETWEEN '2011-01-01' AND '2011-01-31')
AND id = 70 GROUP BY a1 a2)t1, t3
where t1.a1=t3.c1

UNION ALL

SELECT '' AS a1, '' AS a2, t2.b1,t2.b2 
FROM(SELECT a1,a2 from xyz WHERE (date BETWEEN '2011-02-01' AND '2011-02-30')
AND id = 70 GROUP BY a1 a2)t1, t3
where t2.b1=t3.c1

output 2:
---------------
a1 a2  b1  b2
---------------
1   2      4
1   3      2
1   6  5
4   8  3
Select t1.a1
    , t1.a2
    , t2.b1
    ,t2.b2 
From    (
        Select a1,a2 
        From xyz 
        Where date Between '2011-01-01' And '2011-01-30'
            And id = 70 
        Group By a1,a2
        ) As t1
    Join t3
        On t3.a1 = t1.a1
    Join    (   
            Select a1,a2 
            From xyz 
            Where date Between '2011-02-01' And '2011-02-28'
                And id = 70 
            Group By a1, a2
            ) As t2
        On t2.a1 = t3.a1
Select T1.a1, T1.a2
    , T2.a1 As b1, T2.a2 As b2
From    (
        Select a1, a2
        From xyz
            Join t3
                On t3.a1 = xyz.a1
        Where xyz.id = 70
            And xyz.date >= '2011-01-01' And xyz.date < '2011-02-01' 
        Group By a1, a2
        ) As T1
    Full Join   (
                Select a1, a2
                From xyz
                    Join t3
                        On t3.a1 = xyz.a1
                Where xyz.id = 70
                    And xyz.date >= '2011-02-01' And xyz.date < '2011-03-01' 
                Group By a1, a2
                ) As T2
        On T2.a1 = T1.a1
Select 'Jan' As Month, a1, a2
From xyz
    Join t3
        On t3.a1 = xyz.a1
Where xyz.id = 70
    And xyz.date >= '2011-01-01' And xyz.date < '2011-02-01' 
Group By a1, a2
Union All
Select 'Feb', a1, a2
From xyz
    Join t3
        On t3.a1 = xyz.a1
Where xyz.id = 70
    And xyz.date >= '2011-02-01' And xyz.date < '2011-03-01' 
Group By a1, a2