Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

SQL Join to join 2 select statments

Posted on 2010-08-17
13
487 Views
Last Modified: 2012-05-10
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.

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]

Open in new window


can somebody help me please.

Thanks results
0
Comment
Question by:Jason Yousef, MS
  • 6
  • 5
  • 2
13 Comments
 
LVL 16

Expert Comment

by:vdr1620
ID: 33457166
can you provide sample data from the tables along with table structure ??
0
 
LVL 21

Author Comment

by:Jason Yousef, MS
ID: 33457283
Hi vdr1620:

thanks for your reply.
here you go attached.

sample-data-x.txt
0
 
LVL 2

Expert Comment

by:ajisasaggi
ID: 33457285
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.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 21

Author Comment

by:Jason Yousef, MS
ID: 33457309
ajisasaggi:  thanks for stepping in, union gives me duplicates, doesn't sum all the same service departments.

can't figure it out !
0
 
LVL 2

Expert Comment

by:ajisasaggi
ID: 33457463
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?
0
 
LVL 16

Expert Comment

by:vdr1620
ID: 33457511
Thanks for data..but go to go.. will work on it as soon as i come back
0
 
LVL 21

Author Comment

by:Jason Yousef, MS
ID: 33457542
ajisasaggi:  yes, need to sum all the departments that match and other too.

as it's a combined 2 statements together
0
 
LVL 2

Expert Comment

by:ajisasaggi
ID: 33457801
Please try the attached query.
It can be cleaned up quite a bit (percentage values from inner queries can be removed).

query.sql
0
 
LVL 21

Author Comment

by:Jason Yousef, MS
ID: 33458076
ajisasaggi:  thank you, but it gave me error, attached.

I'm using sql server 2008 dev

error.gif
0
 
LVL 2

Expert Comment

by:ajisasaggi
ID: 33458457
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
0
 
LVL 21

Author Comment

by:Jason Yousef, MS
ID: 33458548
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 ')'
0
 
LVL 2

Accepted Solution

by:
ajisasaggi earned 500 total points
ID: 33458862
My bad, I didn't validate the syntax.

Please try with this. I have modified the query to return the top 10 departments that have a combined higher total and an additional row with the accumulated total for all other departments.
SELECT [Service Code], [Department Total]
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%') + (Select SUM([Total Charges]) from [ExctemedDB].[dbo].[XACTIMED-CARE] Where [Fac Name] like '%Michaels%') AS [Percentage]
    FROM
    (
        SELECT [Service Code], [Total Charges]
        from dbo.[XACTIMED-COMM]
        Where [Fac Name] like '%Michaels%'
        union all
        SELECT [Service Code], [Total Charges]
        from dbo.[XACTIMED-CARE]
        Where [Fac Name] like '%Michaels%'
    ) AS ALL_DEPT
    GROUP BY [Service Code]
    ORDER BY [Department Total] DESC
) AS TOP_TEN

UNION ALL

SELECT 'Other' [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%') + (Select SUM([Total Charges]) from [ExctemedDB].[dbo].[XACTIMED-CARE] Where [Fac Name] like '%Michaels%') AS [Percentage]
FROM
(
    SELECT [Service Code], [Total Charges]
    from dbo.[XACTIMED-COMM]
    Where [Fac Name] like '%Michaels%'
    union all
    SELECT [Service Code], [Total Charges]
    from dbo.[XACTIMED-CARE]
    Where [Fac Name] like '%Michaels%'
) AS ALL_DEPT
WHERE [Service Code] NOT IN (
    SELECT TOP 10 [Service Code]
    FROM
    (
        SELECT [Service Code], [Total Charges]
        from dbo.[XACTIMED-COMM]
        Where [Fac Name] like '%Michaels%'
        union all
        SELECT [Service Code], [Total Charges]
        from dbo.[XACTIMED-CARE]
        Where [Fac Name] like '%Michaels%'
    ) AS ALL_DEPT
    GROUP BY [Service Code]
    ORDER BY [Department Total] DESC
)

Open in new window

0
 
LVL 21

Author Closing Comment

by:Jason Yousef, MS
ID: 33559261
Thanks for trying, but still a lot of syntax errors, I did it in a different way though.
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
tempdb log contention 16 49
Why I am having this error in my query? 2 25
Stored Proc - Rewrite 42 56
Parse this column 6 24
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question