Click here to Skip to main content
15,884,628 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
dear all,

i have 3 SQL statement i want to merge them into one but each of them has its own where condition how can i merge them
1st:
SQL
SELECT
	ProjectCode,
	SUM (- 1 *(CreditAm) +(DebitAm)) AS Balance
FROM
	[dbo].[AcDetail]
WHERE
	AcCode IN (
		401000,
		402000,
		403000,
		404000,
		405000,
		406000,
		407000,
		499999
	)
AND BranchCode = 'WP'
AND TDate >= '2016-01-01'
AND TDate <= '2016-12-31'
GROUP BY
	ProjectCode


2nd
SQL
SELECT
	ProjectCode,
	SUM (- 1 *(CreditAm) +(DebitAm)) AS Materials
FROM
	[dbo].[AcDetail]
WHERE
	AcCode IN (600001, 600002, 600151)
AND BranchCode = 'WP'
AND TDate >= '2016-01-01'
AND TDate <= '2016-12-31'
GROUP BY
	ProjectCode


3rd:
SQL
SELECT
	ProjectCode,
	SUM (- 1 *(CreditAm) +(DebitAm)) AS Salaries
FROM
	[dbo].[AcDetail]
WHERE
	AcCode IN (
		650001,
		650005,
		650006,
		650007,
		650008,
		650009,
		650010,
		650020,
		650021,
		650022,
		650023,
		650024,
		650025,
		650026,
		650027
	)
AND BranchCode = 'WP'
AND TDate >= '2016-01-01'
AND TDate <= '2016-12-31'
GROUP BY
	ProjectCode
ORDER BY
	ProjectCode


What I have tried:

SELECT
	ProjectCode,
	SUM (- 1 *(CreditAm) +(DebitAm)) AS Balance
FROM
	[dbo].[AcDetail]
WHERE
	AcCode IN (
		401000,
		402000,
		403000,
		404000,
		405000,
		406000,
		407000,
		499999
	)
AND BranchCode = 'WP'
AND TDate >= '2016-01-01'
AND TDate <= '2016-12-31'
GROUP BY
	ProjectCode
Posted
Updated 21-Jan-17 0:39am

1 solution

You can use UNION resp. UNION ALL statement to concatenate as many single queries as you need as long as all return exactly the same columns in the same order.
 
Share this answer
 
Comments
Eng Abdull A-ziz 21-Jan-17 7:21am    
could you please write the query for me
NightWizzard 21-Jan-17 12:21pm    
You won't learn anything if someone else does your job. Look here for more about UNION: https://msdn.microsoft.com/de-de/library/ms180026.aspx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900