Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
.NET
.NET Core
.NET MAUI
.NET Standard
Active Directory
ADO.NET
Agile Development
AI
AJAX
AlbertAGPT
Alchemy
Alexa Skills
Algorand
Algorithms in C#
Android
Angular
ArcObject
ASP.NET
ASP.NET Core
Augmented Reality
Avalanche
AWS
Azure
Backbonejs
Big Data
BizTalk Server
Blazor
Blockchain
Bootstrap
Bot Framework
Business
Business Intelligence(BI)
C#
C# Corner
C# Strings
C, C++, MFC
Career Advice
Careers and Jobs
Chapters
ChatGPT
Cloud
Coding Best Practices
Cognitive Services
COM Interop
Compact Framework
Copilot
Cortana Development
Cosmos DB
Cryptocurrency
Cryptography
Crystal Reports
CSS
Current Affairs
Custom Controls
Cyber Security
Data Mining
Data Science
Databases & DBA
Databricks
Design Patterns & Practices
DevExpress
DevOps
DirectX
Dynamics CRM
Enterprise Development
Entity Framework
Error Zone
Exception Handling
F#
Files, Directory, IO
Flutter
Games Programming
GDI+
General
Generative AI
GO
Google Cloud
Google Development
Graphics Design
Graphite Studio
Hardware
Hiring and Recruitment
HoloLens
How do I
HTML 5
Infragistics
Internet & Web
Internet of Things
Ionic
Java
Java and .NET
JavaScript
JQuery
JSON
JSP
Knockout
Kotlin
Langchain
Leadership
Learn .NET
Learn iOS Programming
LINQ
Machine Learning
Metaverse
Microsoft 365
Microsoft Fabric
Microsoft Office
Microsoft Phone
Microsoft Teams
Mobile Development
MongoDB
MuleSoft
MySQL
NEAR
NetBeans
Networking
NFT
NoCode LowCode
Node.js
Office Development
OOP/OOD
Open Source
Operating Systems
Oracle
Outsourcing
Philosophy
PHP
Polygon
PostgreSQL
Power Apps
Power Automate
Power BI
Power Pages
Printing in C#
Products
Progress
Progressive Web Apps
Project Management
Public Speaking
Python
Q#
QlikView
Quantum Computing
R
React
React Native
Reports using C#
Robotics & Hardware
RPA
Ruby on Rails
RUST
Salesforce
Security
Servers
ServiceNow
SharePoint
Sharp Economy
SignalR
Smart Devices
Snowflake
Software Architecture/Engineering
Software Testing
Solana
Solidity
Sports
SQL
SQL Server
Startups
Stratis Blockchain
Swift
SyncFusion
Threading
Tools
TypeScript
Unity
UWP
Visual Basic .NET
Visual Studio
Vue.js
WCF
Wearables
Web API
Web Design
Web Development
Web3
Windows
Windows Controls
Windows Forms
Windows PowerShell
Windows Services
Workflow Foundation
WPF
Xamarin
XAML
XML
XNA
XSharp
Register
Login
4
Answers
SQL Query for union all the months
Kasuni Abegunawardana
6y
650
1
Reply
Hi all,
I need to get data to all months for the selected year like this picture
this is what i hv done. please send me an answer
Declare
@
Temp
Table
(
CompanyID
int
,
ID nvarchar(250)
NOT
NULL
,
Name
nvarchar(250) ,
ParentAccID nvarchar(250),
ParentAccName nvarchar(250),
ParentDisplayID nvarchar(250),
DisplayID nvarchar (250),
Actual
decimal
(18,2),
Budget
decimal
(18,2),
Classification nvarchar(250),
AccLevel
int
,
AccOrder
int
);
INSERT
INTO
@
Temp
select
4011111111115156122,NEWID(),
'GROSS PROFIT'
,
''
,
'GP'
,
'7-0000'
,
'7-0000'
,0,0,
'GrossProfit'
,1,3
union
select
4011111111115156122,NEWID(),
'OPERATING PROFIT'
,
null
,
'OP'
,
'10-000'
,
'10-000'
,0,0,
'OperatingProfit'
,1,5
union
select
4011111111115156122,NEWID(),
'NET PROFIT / LOSS'
,
''
,
'P/L'
,
'11-000'
,
'11-000'
,0,0,
'NetProfit/Loss'
,1,8
INSERT
INTO
@
Temp
select
[CompanyID],
[UID],
[
Name
],
[ParentAccountId],
[ParentAccountName],
[ParentDisplayID],
[DisplayID],
0,
0,
[Classification],
[AccountLevel],
(
case
when
Classification =
'Income'
then
1
when
Classification=
'CostOFsales'
then
2
when
Classification=
'Expense'
then
4
when
Classification=
'OtherIncome'
then
6
when
Classification=
'OtherExpense'
then
7
else
0
end
)
as
AccOrder
from
Account111
where
CompanyID=4011111111115156122
AND
Classification
in
(
'Income'
,
'Expense'
,
'CostOfSales'
,
'OtherIncome'
,
'OtherExpense'
)
-- Actual for Month
update
Tupdate
set
Actual =
isnull
((
select
sum
(Activity)
from
AccountReg
where
CompanyID = 4011111111115156122
and
Year
= 2012
and
Month
=1
and
convert
(
varchar
(250),UID) = Tupdate.ID) ,0)
from
@
Temp
as
Tupdate
---- Budget for Month
update
Tupdate
set
Budget =
isnull
((
select
sum
(Amount)
from
AccountBud
where
CompanyID = 4011111111115156122
and
Year
= 2012
and
month
=1
and
convert
(
varchar
(250),AccountUID) = Tupdate.ID) ,0)
from
@
Temp
as
Tupdate
update
Tupdate
set
Actual = ((
select
Actual
from
@
Temp
where
DisplayID =
'4-0000'
AND
CompanyID = 4011111111115156122 ) - (
select
Actual
from
@
Temp
where
DisplayID =
'5-0000'
and
CompanyID = 4011111111115156122)),
budget = ((
select
Budget
from
@
Temp
where
DisplayID =
'4-0000'
AND
CompanyID = 4011111111115156122 ) - (
select
Budget
from
@
Temp
where
DisplayID =
'5-0000'
and
CompanyID = 4011111111115156122))
from
@
Temp
as
Tupdate
where
DisplayID =
'7-0000'
and
CompanyID = 4011111111115156122
update
Tupdate
set
Actual = ((
select
Actual
from
@
Temp
where
DisplayID =
'7-0000'
AND
CompanyID = 4011111111115156122 ) - (
select
Actual
from
@
Temp
where
DisplayID =
'6-0000'
and
CompanyID = 4011111111115156122)),
budget = ((
select
Budget
from
@
Temp
where
DisplayID =
'7-0000'
AND
CompanyID = 4011111111115156122 ) - (
select
Budget
from
@
Temp
where
DisplayID =
'6-0000'
and
CompanyID = 4011111111115156122))
from
@
Temp
as
Tupdate
where
DisplayID=
'10-000'
and
CompanyID = 4011111111115156122
update
Tupdate
set
Actual = ((
select
Actual
from
@
Temp
where
DisplayID =
'4-0000'
AND
CompanyID = 4011111111115156122 ) - (
select
Actual
from
@
Temp
where
DisplayID =
'5-0000'
and
CompanyID = 4011111111115156122) - (
select
Actual
from
@
Temp
where
DisplayID =
'6-0000'
and
CompanyID = 4011111111115156122) + (
select
Actual
from
@
Temp
where
DisplayID =
'8-0000'
and
CompanyID = 4011111111115156122)- (
select
Actual
from
@
Temp
where
DisplayID =
'9-0000'
and
CompanyID = 4011111111115156122) ),
budget = ((
select
Budget
from
@
Temp
where
DisplayID =
'4-0000'
AND
CompanyID = 4011111111115156122 ) - (
select
Budget
from
@
Temp
where
DisplayID =
'5-0000'
and
CompanyID = 4011111111115156122) - (
select
Budget
from
@
Temp
where
DisplayID =
'6-0000'
and
CompanyID = 4011111111115156122) + (
select
Budget
from
@
Temp
where
DisplayID =
'8-0000'
and
CompanyID = 4011111111115156122)- (
select
Budget
from
@
Temp
where
DisplayID =
'9-0000'
and
CompanyID = 4011111111115156122))
from
@
Temp
as
Tupdate
where
DisplayID=
'11-000'
and
CompanyID = 4011111111115156122
select
*
from
@
Temp
order
by
AccOrder, AccLevel,ID
thank you
Post
Reset
Cancel
Answers (
4
)
Next Recommended Forum
Display Excel data into HTML table in MVC using Oledb
How to add drop down list in data gridview