如何从2个不同的sql查询中加入数据

编程入门 行业动态 更新时间:2024-10-26 14:28:20
本文介绍了如何从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

我如何获得一起返回的数据? 我尝试了什么: 我试图使用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查询中加入数据

本文发布于:2023-10-17 06:21:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1500027.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据   sql

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!