MS SQL 2008 Studio creating syntax error - sql

I have a project where I create a query string dynamically. It's not too complicated, just complex. I specifically have a problem adding a select into the mix. Studio adds an "as" when I save the view.
SELECT TOP (100) PERCENT dbo.Users.ID AS UserId, dbo.Users_Info.Home_Area_Code, dbo.Users_Info.Work_Area_Code, dbo.Users_Info.State AS HomeState,
(CASE WHEN (dbo.Users_Info.Address1 = '' OR
dbo.Users_Info.Address1 IS NULL) THEN 0 ELSE 1 END) AS has_home_address, (CASE WHEN dbo.users.ID NOT IN
(SELECT UserID
FROM Users_Client_Companies) THEN 1 ELSE 0 END) AS Eliminate_TSI_Clients, (CASE WHEN dbo.users.ID IN
(SELECT individual_number
FROM qUsersPresented_ES) THEN 1 ELSE 0 END) AS Presented_ES, (CASE WHEN dbo.users.ID IN
(SELECT individual_number
FROM qUsersPlaced_ES
WHERE isnull(date_of_placement, '') > '') THEN 1 ELSE 0 END) AS Placed_ES, (CASE WHEN dbo.users.ID IN
(SELECT individual_number
FROM qUsersPresented_CS) THEN 1 ELSE 0 END) AS Presented_CS, (CASE WHEN dbo.users.ID IN
(SELECT individual_number
FROM qUsersPlaced_CS
WHERE isnull(date_of_placement, '') > '') THEN 1 ELSE 0 END) AS Placed_CS, (CASE WHEN dbo.users.ID IN
(SELECT individual_number
FROM QEliteAdCandidatesPanel
WHERE isnull(date_of_placement, '') > '') THEN 1 ELSE 0 END) AS Placed_EJA, (CASE WHEN dbo.users.ID IN
(SELECT individual_number
FROM QEliteAdCandidatesPanel) THEN 1 ELSE 0 END) AS Presented_EJA, (CASE WHEN dbo.users.id IN
(SELECT ID
FROM qUsers_TTMember) THEN 1 ELSE 0 END) AS TT_Member, (CASE WHEN dbo.users.id IN
(SELECT ID
FROM qUsers_HasHomeEmail) THEN 1 ELSE 0 END) AS Has_Home_Email, (CASE WHEN dbo.users.id IN
(SELECT ID
FROM qUsers_HasWorkEmail) THEN 1 ELSE 0 END) AS Has_Work_Email, dbo.Companies.Sales_Level, dbo.Users_Salary.ISIPCT AS Individual_Letter_Per,
dbo.Users_Salary.ISCPCT AS Corporate_Letter_Per, (CASE WHEN (dbo.Users_Info.Basic_JobAd = 1 OR
dbo.Users_Info.Custom_JobAd = 1 OR
dbo.Users_Info.Elite_JobAd = 1 OR
dbo.Users_Info.EJA_Candidate = 1) THEN 1 ELSE 0 END) AS job_board, (CASE WHEN dbo.Users_Info.Current_Company_Number IN
(SELECT company_id
FROM Companies_Adv) THEN 1 ELSE 0 END) AS Comp_Adv, (CASE WHEN dbo.Users_Info.Current_Company_Number IN
(SELECT company_id
FROM Companies_BJA) THEN 1 ELSE 0 END) AS Comp_BJA, (CASE WHEN dbo.Users_Info.Current_Company_Number IN
(SELECT company_id
FROM Companies_CJA) THEN 1 ELSE 0 END) AS Comp_CJA, (CASE WHEN dbo.Users_Info.Current_Company_Number IN
(SELECT company_id
FROM Companies_CS) THEN 1 ELSE 0 END) AS Comp_CS, (CASE WHEN dbo.Users_Info.Current_Company_Number IN
(SELECT company_id
FROM Companies_EJA) THEN 1 ELSE 0 END) AS Comp_EJA, (CASE WHEN dbo.Users_Info.Current_Company_Number IN
(SELECT company_id
FROM Companies_ES) THEN 1 ELSE 0 END) AS Comp_ES, (CASE WHEN dbo.Users_Info.Current_Company_Number IN
(SELECT company_id
FROM Companies_Intern) THEN 1 ELSE 0 END) AS Comp_Intern, (CASE WHEN dbo.Users_Info.Current_Company_Number IN
(SELECT company_id
FROM Companies_OrgAssess) THEN 1 ELSE 0 END) AS Comp_OrgAssess, (CASE WHEN dbo.Users_Info.Current_Company_Number IN
(SELECT company_id
FROM Companies_SB) THEN 1 ELSE 0 END) AS Comp_SB,
(SELECT MAX(date_purchased) AS EJA_Date_Purchased
FROM dbo.QEliteContacts
WHERE (contact_id = dbo.Users.ID)) AS Date_Purchased_EJA,
dbo.Users_Salary.ISTEC1 AS Technical_Federal_Per,
dbo.Users_Salary.ISTEC2 AS Technical_StateLocal_Per,
The view QEliteContacts is as follows:
SELECT bd.date_purchased, bd.ID, csc.contact_id
FROM dbo.Board_Details AS bd LEFT OUTER JOIN
dbo.Client_Services_Contacts AS csc ON csc.service_id = bd.ID
WHERE (bd.ad_type LIKE '%Elite%')
Near the bottom of the first block of code is the statement "(SELECT MAX(date_purchased) AS EJA_Date_Purchased". This is the new code I have added to the view. I get and error "invalid syntax near 'as'". If I remove the alias from the max(date_purchased) studio will add 'as expr1' when I save the view. If I remove the alias from the select, studio will add 'as expr1' when I save the view.
To tell the truth, I'm not sure I'm attacking the problem of getting the max date properly.

Remove the second 'as'.ie, remove the portion ' AS Date_Purchased_EJA ' and give that instead of ' AS EJA_Date_Purchased '.In effect there should be only the first 'AS' statement.Then execute the query in Query Designer window and not in View Designer Window.The code is shown below.
SELECT TOP (100) PERCENT dbo.Users.ID AS UserId, dbo.Users_Info.Home_Area_Code, dbo.Users_Info.Work_Area_Code, dbo.Users_Info.State AS HomeState,
(CASE WHEN (dbo.Users_Info.Address1 = '' OR
dbo.Users_Info.Address1 IS NULL) THEN 0 ELSE 1 END) AS has_home_address, (CASE WHEN dbo.users.ID NOT IN
(SELECT UserID
FROM Users_Client_Companies) THEN 1 ELSE 0 END) AS Eliminate_TSI_Clients, (CASE WHEN dbo.users.ID IN
(SELECT individual_number
FROM qUsersPresented_ES) THEN 1 ELSE 0 END) AS Presented_ES, (CASE WHEN dbo.users.ID IN
(SELECT individual_number
FROM qUsersPlaced_ES
WHERE isnull(date_of_placement, '') > '') THEN 1 ELSE 0 END) AS Placed_ES, (CASE WHEN dbo.users.ID IN
(SELECT individual_number
FROM qUsersPresented_CS) THEN 1 ELSE 0 END) AS Presented_CS, (CASE WHEN dbo.users.ID IN
(SELECT individual_number
FROM qUsersPlaced_CS
WHERE isnull(date_of_placement, '') > '') THEN 1 ELSE 0 END) AS Placed_CS, (CASE WHEN dbo.users.ID IN
(SELECT individual_number
FROM QEliteAdCandidatesPanel
WHERE isnull(date_of_placement, '') > '') THEN 1 ELSE 0 END) AS Placed_EJA, (CASE WHEN dbo.users.ID IN
(SELECT individual_number
FROM QEliteAdCandidatesPanel) THEN 1 ELSE 0 END) AS Presented_EJA, (CASE WHEN dbo.users.id IN
(SELECT ID
FROM qUsers_TTMember) THEN 1 ELSE 0 END) AS TT_Member, (CASE WHEN dbo.users.id IN
(SELECT ID
FROM qUsers_HasHomeEmail) THEN 1 ELSE 0 END) AS Has_Home_Email, (CASE WHEN dbo.users.id IN
(SELECT ID
FROM qUsers_HasWorkEmail) THEN 1 ELSE 0 END) AS Has_Work_Email, dbo.Companies.Sales_Level, dbo.Users_Salary.ISIPCT AS Individual_Letter_Per,
dbo.Users_Salary.ISCPCT AS Corporate_Letter_Per, (CASE WHEN (dbo.Users_Info.Basic_JobAd = 1 OR
dbo.Users_Info.Custom_JobAd = 1 OR
dbo.Users_Info.Elite_JobAd = 1 OR
dbo.Users_Info.EJA_Candidate = 1) THEN 1 ELSE 0 END) AS job_board, (CASE WHEN dbo.Users_Info.Current_Company_Number IN
(SELECT company_id
FROM Companies_Adv) THEN 1 ELSE 0 END) AS Comp_Adv, (CASE WHEN dbo.Users_Info.Current_Company_Number IN
(SELECT company_id
FROM Companies_BJA) THEN 1 ELSE 0 END) AS Comp_BJA, (CASE WHEN dbo.Users_Info.Current_Company_Number IN
(SELECT company_id
FROM Companies_CJA) THEN 1 ELSE 0 END) AS Comp_CJA, (CASE WHEN dbo.Users_Info.Current_Company_Number IN
(SELECT company_id
FROM Companies_CS) THEN 1 ELSE 0 END) AS Comp_CS, (CASE WHEN dbo.Users_Info.Current_Company_Number IN
(SELECT company_id
FROM Companies_EJA) THEN 1 ELSE 0 END) AS Comp_EJA, (CASE WHEN dbo.Users_Info.Current_Company_Number IN
(SELECT company_id
FROM Companies_ES) THEN 1 ELSE 0 END) AS Comp_ES, (CASE WHEN dbo.Users_Info.Current_Company_Number IN
(SELECT company_id
FROM Companies_Intern) THEN 1 ELSE 0 END) AS Comp_Intern, (CASE WHEN dbo.Users_Info.Current_Company_Number IN
(SELECT company_id
FROM Companies_OrgAssess) THEN 1 ELSE 0 END) AS Comp_OrgAssess, (CASE WHEN dbo.Users_Info.Current_Company_Number IN
(SELECT company_id
FROM Companies_SB) THEN 1 ELSE 0 END) AS Comp_SB,
(SELECT MAX([EJA_Date_Purchased]) from
(SELECT date_purchased AS [EJA_Date_Purchased]
FROM dbo.QEliteContacts
WHERE (contact_id = dbo.Users.ID))t),
dbo.Users_Salary.ISTEC1 AS Technical_Federal_Per,
dbo.Users_Salary.ISTEC2 AS Technical_StateLocal_Per,

Related

Count the number of column having non zero values

I have the following script,
SELECT COUNT(*) AS Total,
SUM(CASE WHEN TypeId ='4' THEN 1 ELSE 0 END) AS 'TotalCount1',
SUM(CASE WHEN TypeId ='6' THEN 1 ELSE 0 END) AS 'TotalCount2',
SUM(CASE WHEN TypeId ='1' THEN 1 ELSE 0 END) AS 'TotalCount3',
SUM(CASE WHEN TypeId ='10' THEN 1 ELSE 0 END) AS 'TotalCount4',
SUM(CASE WHEN TypeId ='5' THEN 1 ELSE 0 END) AS 'TotalCount5',
SUM(CASE WHEN TypeId ='8' THEN 1 ELSE 0 END) AS 'TotalCount6'
FROM [Party]
Please refer the screenshot as the output of the above script.
What I want:
I want a column after the Total as the total number of the column having nonzero values.
Like in the picture the values should be 2 as TotalCount1 and Totalcount3 have non zero values.
SELECT COUNT(*) AS Total,
...
...
CASE ( WHEN SUM(CASE WHEN TypeId ='4' THEN 1 ELSE 0 END) > 0 THEN 1 ELSE 0 END) +
CASE ( WHEN SUM(CASE WHEN TypeId ='6' THEN 1 ELSE 0 END) > 0 THEN 1 ELSE 0 END) +
CASE ( WHEN SUM(CASE WHEN TypeId ='1' THEN 1 ELSE 0 END) > 0 THEN 1 ELSE 0 END) +
CASE ( WHEN SUM(CASE WHEN TypeId ='10' THEN 1 ELSE 0 END) > 0 THEN 1 ELSE 0 END) +
CASE ( WHEN SUM(CASE WHEN TypeId ='5' THEN 1 ELSE 0 END) > 0 THEN 1 ELSE 0 END) +
CASE ( WHEN SUM(CASE WHEN TypeId ='8' THEN 1 ELSE 0 END) > 0 THEN 1 ELSE 0 END)
as SumOfNonZeros
FROM [Party]
Or maybe simpler
SELECT COUNT(*) AS Total,
COUNT(CASE WHEN TypeId ='4' THEN 1 END) AS 'TotalCount1',
COUNT(CASE WHEN TypeId ='6' THEN 1 END) AS 'TotalCount2',
COUNT(CASE WHEN TypeId ='1' THEN 1 END) AS 'TotalCount3',
COUNT(CASE WHEN TypeId ='10' THEN 1 END) AS 'TotalCount4',
COUNT(CASE WHEN TypeId ='5' THEN 1 END) AS 'TotalCount5',
COUNT(CASE WHEN TypeId ='8' THEN 1 END) AS 'TotalCount6',
COUNT( DISTINCT CASE WHEN TypeId IN ('4', '6', '1', '10', '5', '8')
THEN TypeId
END ) as CountOfNonZeros
FROM [Party]
Using CASE expression over the selecting result, you can do this:
SELECT (
CASE WHEN TotalCount1 > 0 THEN 1 ELSE 0 END +
CASE WHEN TotalCount2 > 0 THEN 1 ELSE 0 END +
CASE WHEN TotalCount3 > 0 THEN 1 ELSE 0 END +
CASE WHEN TotalCount4 > 0 THEN 1 ELSE 0 END +
CASE WHEN TotalCount5 > 0 THEN 1 ELSE 0 END +
CASE WHEN TotalCount6 > 0 THEN 1 ELSE 0 END) AS Result
FROM (
SELECT COUNT(*) AS Total,
SUM(CASE WHEN TypeId ='4' THEN 1 ELSE 0 END) AS 'TotalCount1',
SUM(CASE WHEN TypeId ='6' THEN 1 ELSE 0 END) AS 'TotalCount2',
SUM(CASE WHEN TypeId ='1' THEN 1 ELSE 0 END) AS 'TotalCount3',
SUM(CASE WHEN TypeId ='10' THEN 1 ELSE 0 END) AS 'TotalCount4',
SUM(CASE WHEN TypeId ='5' THEN 1 ELSE 0 END) AS 'TotalCount5',
SUM(CASE WHEN TypeId ='8' THEN 1 ELSE 0 END) AS 'TotalCount6'
FROM [Party]
) A

Counting columns with a where clause

Is there a way to count a number of columns which has a particular value for each rows in Hive.
I have data which looks like in input and I want to count how many columns have value 'a' and how many column have value 'b' and get the output like in 'Output'.
Is there a way to accomplish this with Hive query?
One method in Hive is:
select ( (case when cl_1 = 'a' then 1 else 0 end) +
(case when cl_2 = 'a' then 1 else 0 end) +
(case when cl_3 = 'a' then 1 else 0 end) +
(case when cl_4 = 'a' then 1 else 0 end) +
(case when cl_5 = 'a' then 1 else 0 end)
) as count_a,
( (case when cl_1 = 'b' then 1 else 0 end) +
(case when cl_2 = 'b' then 1 else 0 end) +
(case when cl_3 = 'b' then 1 else 0 end) +
(case when cl_4 = 'b' then 1 else 0 end) +
(case when cl_5 = 'b' then 1 else 0 end)
) as count_b
from t;
To get the total count, I would suggest using a subquery and adding count_a and count_b.
Use lateral view with explode on the data and do the aggregations on it.
select id
,sum(cast(col='a' as int)) as cnt_a
,sum(cast(col='b' as int)) as cnt_b
,sum(cast(col in ('a','b') as int)) as cnt_total
from tbl
lateral view explode(array(ci_1,ci_2,ci_3,ci_4,ci_5)) tbl as col
group by id

How to use having condition in SQL query

SELECT
userid,
CASE
WHEN (COUNT(CASE
WHEN onlinesportsgamewagers != 0
THEN 1
ELSE null
END)
+ COUNT(CASE
WHEN depositmade_amt != 0
THEN 1
ELSE null
END)) >= 10
THEN "VIP"
ELSE "NON-VIP"
END as VIPcheck
FROM
player_activity
WHERE
userid = 2023410
GROUP BY
year(txndate), month(txndate)
This query determines the user's VIP status for each month.
Ultimately, I want to have a query that determines if the user achieved VIP status for at least 3 months (including the current month). For the time being, it's only user 2023410, but eventually I want to run this for the whole database.
Therefore my ultimate output would be:
User - VIPcheck (3 different months w/ active status)
(one row per userID)
HAVING COUNT(CASE WHEN (COUNT(CASE WHEN onlinesportsgamewagers != 0
THEN 1
ELSE null
END)
+ COUNT(CASE WHEN depositmade_amt != 0
THEN 1
ELSE null
END)) >= 10
THEN 1
ELSE 0
END)
Tried the above having statement, but it didn't work. Any suggestions?
If I understand correctly, this gets the VIP status for one user by month:
SELECT userid, year(txndate), month(txndate),
(CASE WHEN SUM(CASE WHEN onlinesportsgamewagers <> 0 THEN 1 ELSE 0 END) +
SUM(CASE WHEN depositmade_amt <> 0 THEN 1 ELSE 0 END) >= 10
THEN 'VIP'
ELSE 'NON-VIP'
END) as VIPcheck
FROM player_activity
GROUP BY userid, year(txndate), month(txndate);
Another aggregation will get what you want:
SELECT userid,
(CASE WHEN SUM(VIPcheck = 'VIP') >= 3 THEN 'SUPER-VIP'
WHEN SUM(VIPcheck = 'VIP') >= 1 THEN 'VIP'
ELSE 'HOI POLLOI'
END) as status
FROM (SELECT userid, year(txndate), month(txndate),
(CASE WHEN SUM(CASE WHEN onlinesportsgamewagers <> 0 THEN 1 ELSE 0 END) +
SUM(CASE WHEN depositmade_amt <> 0 THEN 1 ELSE 0 END) >= 10
THEN 'VIP'
ELSE 'NON-VIP'
END) as VIPcheck
FROM player_activity
GROUP BY userid, year(txndate), month(txndate)
) uym
GROUP BY userid;

missing expression error

Any reason why i'm getting a missing expression error?
select
CASE WHEN
(
SELECT COUNT(*) AS COUNT
FROM TRANSPLANT_ORGANS
INNER JOIN ORGAN_TYPE ON TRANSPLANT_ORGANS.TX_ORG_ID = ORGAN_TYPE.ORG_RECORD_ID
WHERE COUNT > 0
)
THEN
(
SELECT SUBSTR(CONCAT(CASE SUM(CASE WHEN ORGAN_TYPE.ORG_ID BETWEEN 660 AND 662 THEN 1 ELSE 0 END) WHEN 0 THEN '' ELSE '/KIDNEY' END,
CONCAT(CASE SUM(CASE WHEN ORGAN_TYPE.ORG_ID BETWEEN 640 AND 650 THEN 1 ELSE 0 END) WHEN 0 THEN '' ELSE '/LIVER' END,
CONCAT(CASE SUM(CASE WHEN ORGAN_TYPE.ORG_ID IN (620,622) THEN 1 ELSE 0 END) WHEN 0 THEN '' ELSE '/PANCREAS' END,
CONCAT(CASE SUM(CASE WHEN ORGAN_TYPE.ORG_ID IN (621) THEN 1 ELSE 0 END) WHEN 0 THEN '' ELSE '/PANCREAS ISLETS' END,
CONCAT(CASE SUM(CASE WHEN ORGAN_TYPE.ORG_ID IN (700) THEN 1 ELSE 0 END) WHEN 0 THEN '' ELSE '/HEART' END,
CONCAT(CASE SUM(CASE WHEN ORGAN_TYPE.ORG_ID IN (600) THEN 1 ELSE 0 END) WHEN 0 THEN '' ELSE '/LUNGS' END,
CONCAT(CASE SUM(CASE WHEN ORGAN_TYPE.ORG_ID BETWEEN 601 AND 607 THEN 1 ELSE 0 END) WHEN 2 THEN '/LUNGS' WHEN 0 THEN '' ELSE '/LUNG' END,
CONCAT(CASE SUM(CASE WHEN ORGAN_TYPE.ORG_ID IN (680) THEN 1 ELSE 0 END) WHEN 0 THEN '' ELSE '/INTESTINES' END,
CONCAT(CASE SUM(CASE WHEN ORGAN_TYPE.ORG_ID BETWEEN 685 AND 686 THEN 1 ELSE 0 END) WHEN 0 THEN '' ELSE 'SMALL INTESTINE' END,
CONCAT(CASE SUM(CASE WHEN ORGAN_TYPE.ORG_ID BETWEEN 683 AND 684 THEN 1 ELSE 0 END) WHEN 0 THEN '' ELSE 'LARGE INTESTINES' END,
CONCAT(CASE SUM(CASE WHEN ORGAN_TYPE.ORG_ID IN (681) THEN 1 ELSE 0 END) WHEN 0 THEN '' ELSE '/DUODENUM' END,
CASE SUM(CASE WHEN ORGAN_TYPE.ORG_ID IN (682) THEN 1 ELSE 0 END) WHEN 0 THEN '' ELSE '/STOMACH' END))))))))))),2,255)
FROM TRANSPLANT_ORGANS
INNER JOIN ORGAN_TYPE ON TRANSPLANT_ORGANS.TX_ORG_ID = ORGAN_TYPE.ORG_RECORD_ID
)
END
First off, you are retrieving a COUNT from transplant_organs, but then never doing a comparison. WHERE COUNT > 0 can be removed and then with the ending parenthesis you can put ) > 0.
Second, your outer select doesn't have a FROM clause. At the very least use FROM dual.
Example:
SELECT CASE
WHEN (SELECT 1 FROM dual) > 0
THEN (SELECT 'Something' FROM dual)
END
FROM dual;

Sum data for many different results for same field

I am trying to find a better way to write this sql server code 2008. It works and data is accurate. Reason i ask is that i will be asked to do this for several other reports going forward and want to reduce the amount of code to upkeep going forward.
How can i take a field where i sum for the yes/no/- (dash) in each field without doing an individual sum as i have in code. Each table is a month of detail data which i sum using in a CTE. i changed the table name for each month and Union All to put data together. Is there a better way to do this. This is a small sample of code. Thanks for the help.
WITH H AS (
SELECT 'August' AS Month_Name
, SUM(CASE WHEN G.FFS = '-' THEN 1 ELSE 0 END) AS FFS_Dash
, SUM(CASE WHEN G.FFS = 'Yes' THEN 1 ELSE 0 END) AS FFS_Yes
, SUM(CASE WHEN G.FFS = 'No' THEN 1 ELSE 0 END) AS FFS_No
, SUM(CASE WHEN G.DNA = '-' THEN 1 ELSE 0 END) AS DNA_Dash
, SUM(CASE WHEN G.DNA = 'Yes' THEN 1 ELSE 0 END) AS DNA_Yes
, SUM(CASE WHEN G.DNA = 'No' THEN 1 ELSE 0 END) AS DNA_No
FROM table08 G )
, G AS (
SELECT 'July' AS Month_Name
, SUM(CASE WHEN G.FFS = '-' THEN 1 ELSE 0 END) AS FFS_Dash
, SUM(CASE WHEN G.FFS = 'Yes' THEN 1 ELSE 0 END) AS FFS_Yes
, SUM(CASE WHEN G.FFS = 'No' THEN 1 ELSE 0 END) AS FFS_No
, SUM(CASE WHEN G.DNA = '-' THEN 1 ELSE 0 END) AS DNA_Dash
, SUM(CASE WHEN G.DNA = 'Yes' THEN 1 ELSE 0 END) AS DNA_Yes
, SUM(CASE WHEN G.DNA = 'No' THEN 1 ELSE 0 END) AS DNA_No
FROM table07 G )
select * from H
UNION ALL
select * from G
How about:
SELECT Month_Name,
SUM(CASE WHEN G.FFS = '-' THEN 1 ELSE 0 END) AS FFS_Dash,
SUM(CASE WHEN G.FFS = 'Yes' THEN 1 ELSE 0 END) AS FFS_Yes,
SUM(CASE WHEN G.FFS = 'No' THEN 1 ELSE 0 END) AS FFS_No,
SUM(CASE WHEN G.DNA = '-' THEN 1 ELSE 0 END) AS DNA_Dash,
SUM(CASE WHEN G.DNA = 'Yes' THEN 1 ELSE 0 END) AS DNA_Yes,
SUM(CASE WHEN G.DNA = 'No' THEN 1 ELSE 0 END) AS DNA_No
FROM ((select 'July' as Month_Name, G.*
from table07 G
) union all
(select 'August', H.*
from table08 H
)
) gh
GROUP BY Month_Name;
However, having tables with the same structure is usually a sign of poor database design. You should have a single table with a column representing the month.