如何从2个不同的sql查询中加入数据 [英] How to join data from 2 different sql queries

查看:97
本文介绍了如何从2个不同的sql查询中加入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。

我有问题。我正在按年度进行营业额报告。但是我们已经改变了2016年的数据结构。如果销售年份是2016年或更晚,我想如何区分SQL查询,如何获得每个SalesYear的TOTALSALES。



/ /我的查询在2016年之前获得销售:

Hello everyone
I have a problem. I'm doing a turnover report by year. But we have changed our datastructure per 2016. How do i get our TOTALSALES per SalesYear when i want to differ the SQL Query if the salesyear is 2016 or later.

//My Query that gets sales before 2016:

SELECT        SUM([Cust. Ledger Entry ].[Sales (LCY)]) AS TotalSales, YEAR([Cust. Ledger Entry ].[Posting Date]) AS SalesYear
FROM[Cust. Ledger Entry ] INNER JOIN
Customer ON [Cust. Ledger Entry ].[Customer No.] = Customer.[No.]
WHERE        ([Cust. Ledger Entry ].[Customer No.] <> '80501608') AND (Customer.[Gen. Bus. Posting Group] IN ('OFFSH EU', 'OFFSH Ø', 'OFFSH DK', 'FISK ØV', 'FISK EU', 'FISK DK', 'ØVRIG ØV', 'ØVRIG EU', 'ØVRIG DK', 'SLAM EU', 'SLAM DK', 'SLAM ØV', 'FØDE DK', 'FØDE EU', 'FØDE ØV')) AND 
([Cust. Ledger Entry ].[Document Type] IN ('2', '3')) AND (YEAR([Cust. Ledger Entry ].[Posting Date]) < '2016')
GROUP BY YEAR([Cust. Ledger Entry ].[Posting Date])
ORDER BY SalesYear





// 2016年后获得销售的我的查询:



//My Query that gets sales after 2016:

SELECT        SUM([Credit Amount] - [Debit Amount]) AS TotalSales, YEAR([Posting Date]) AS SalesYear
FROM            [G/L Entry]
WHERE        ([G/L Account No.] IN ('100210', '100220', '100230', '100260', '100270', '100280', '100310', '100320', '100330', '100360', 
                         '100370', '100380', '100410', '100420', '100430'))
GROUP BY YEAR([Posting Date])
ORDER BY SalesYear





我如何获得一起返回的数据?



< b>我尝试了什么:



我试图使用UNION,但我无法正常工作。



How on earth do i get the data returned together?

What I have tried:

I have tried to use UNION but i can't get this working.

推荐答案

似乎你想将两个查询组合成单个结果。所以,请检查: UNION(Transact-SQL) [ ^ ]

Seems you want to combine two queries into single result. So, check this: UNION (Transact-SQL)[^]
[QUERY 1]
UNION ALL
[QUERY 2]





注意:每列的数据类型必须相同!



Note: the data type for each column must be the same!


这篇关于如何从2个不同的sql查询中加入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆