Jason Yousef
asked on
SQL Join to join 2 select statments
Hello all,
sorry for my query, it's a long one
basically it consists of 2 select statements on 2 tables, each select statement gets the top 10 and self join it to the rest 90 as others.
now I need to join the 2 statements together full join, as there's of course different top 10s in each of them.
here's my query but it gives me weird results !! only results from the second statement and 3 NULL columns !!
please tell me if sample data is needed.
can somebody help me please.
Thanks
sorry for my query, it's a long one
basically it consists of 2 select statements on 2 tables, each select statement gets the top 10 and self join it to the rest 90 as others.
now I need to join the 2 statements together full join, as there's of course different top 10s in each of them.
here's my query but it gives me weird results !! only results from the second statement and 3 NULL columns !!
please tell me if sample data is needed.
SELECT a.[Service Code]
, a.[Department Total]
, a.[Percentage]
from
(SELECT [Service Code]
, [Department Total]
, [Percentage]
from(
SELECT TOP 10
[Service Code]
,Sum([Total Charges]) AS [Department Total]
,Sum([Total Charges])/(Select SUM([Total Charges]) from [ExctemedDB].[dbo].[XACTIMED-COMM] Where [Fac Name] like '%Michaels%') AS [Percentage]
from dbo.[XACTIMED-COMM]
Where [Fac Name] like '%Michaels%'
GROUP BY
[Service Code]
ORDER BY
[Department Total] DESC
) AS Q1
UNION ALL
SELECT
'Other' AS [Service Code]
,Sum([Total Charges]) AS [Department Total]
,Sum([Total Charges])/(Select SUM([Total Charges]) from [ExctemedDB].[dbo].[XACTIMED-COMM]) AS [Percentage]
from dbo.[XACTIMED-COMM]
Where [Fac Name] like '%Michaels%'
and
[Service Code] NOT IN (SELECT TOP 10
[Service Code]
from dbo.[XACTIMED-COMM]
Where [Fac Name] like '%Michaels%'
GROUP BY
[Service Code]
ORDER BY
Sum([Total Charges]) DESC))a
full join
(SELECT [Service Code]
, [Department Total]
, [Percentage]
from(
SELECT TOP 10
[Service Code]
,Sum([Total Charges]) AS [Department Total]
,Sum([Total Charges])/(Select SUM([Total Charges]) from [ExctemedDB].[dbo].[XACTIMED-CARE] Where [Fac Name] like '%Michaels%') AS [Percentage]
from dbo.[XACTIMED-CARE]
Where [Fac Name] like '%Michaels%'
GROUP BY
[Service Code]
ORDER BY
[Department Total] DESC
) AS Q2
UNION ALL
SELECT
'Other' AS [Service Code]
,Sum([Total Charges]) AS [Department Total]
,Sum([Total Charges])/(Select SUM([Total Charges]) from [ExctemedDB].[dbo].[XACTIMED-CARE]) AS [Percentage]
from dbo.[XACTIMED-CARE]
Where [Fac Name] like '%Michaels%'
and
[Service Code] NOT IN (SELECT TOP 10
[Service Code]
from dbo.[XACTIMED-CARE]
Where [Fac Name] like '%Michaels%'
GROUP BY
[Service Code]
ORDER BY
Sum([Total Charges]) DESC))d
on d.[Service Code] = a.[Service Code]
can somebody help me please.
Thanks
can you provide sample data from the tables along with table structure ??
ASKER
Could you please state your requirement? Do the individual statements produce expected results?
If it is combining the results of the 2 separate queries, then you should do a UNION of the statements instead of JOIN.
If it is combining the results of the 2 separate queries, then you should do a UNION of the statements instead of JOIN.
ASKER
ajisasaggi: thanks for stepping in, union gives me duplicates, doesn't sum all the same service departments.
can't figure it out !
can't figure it out !
So do you want to sum the totals, [Service Code] wise, from both statements?
What about 'Other'? Do you want a single row with combined totals?
What about 'Other'? Do you want a single row with combined totals?
Thanks for data..but go to go.. will work on it as soon as i come back
ASKER
ajisasaggi: yes, need to sum all the departments that match and other too.
as it's a combined 2 statements together
as it's a combined 2 statements together
Please try the attached query.
It can be cleaned up quite a bit (percentage values from inner queries can be removed).
query.sql
It can be cleaned up quite a bit (percentage values from inner queries can be removed).
query.sql
ASKER
Please try the attached query.
The idea is to combine the results of the 4 inner SELECT statements and apply grouping on the resultset.
query.sql
The idea is to combine the results of the 4 inner SELECT statements and apply grouping on the resultset.
query.sql
ASKER
ajisasaggi: thanks for your help, but what version of sql you're using? how come it works for you and i keep getting errors !
I got punch of Incorrect syntax near ')'
I got punch of Incorrect syntax near ')'
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for trying, but still a lot of syntax errors, I did it in a different way though.