Avatar of djkontroll
djkontroll
Flag for United States of America asked on

SQL SUBQUERY SYNTAX ISSUE

First off happy holidays to you all and I hope if anyone else is working this weekend like me everything is going smoothly. so here is this situation. I have two separate queries I am trying to merge into a sub-query however I can’t seem to get it to work properly and I know it is a simple syntax issue that someone with more skills can identify.

Here are the 2 queries:

QUERY 1

SELECT   YEAR(araclstdte) AS YEAR,
         MONTH(araclstdte) AS MONTH,
         SUM(AFACINIAMT) AS TOTAL_PLACED
FROM     SQLUser.STFIELDAUD STF,
         SQLUser.ARRELATIONSHIP ARR,
         SQLUser.ARACCOUNT ARA,
         SQLUser.AFACCOUNT AFA
WHERE    STF.STAUDKEY=ARR.ARRELID
AND      ARA.ARACID=ARR.ARRELACID
AND      AFA.AFACKEY=ARR.ARRELAFID
AND      ARR.ARRELCLTID = 'LATX1'
AND      STF.STAUDFLDNAME = 'ARRELSTATUSID'
AND      STF.STAUDFLDEXTNEW like 'BAD%'
AND      STF.STAUDFLDEXTOLD like 'NEWACCT'
GROUP BY YEAR(araclstdte), MONTH(araclstdte)
ORDER BY YEAR(araclstdte), MONTH(araclstdte)

Which returns:
YEAR      MONTH      TOTAL_PLACED
2012      8      3101542.03
2012      9      285786.43
2012      10      1224473.49
2012      11      327823.32
2012      12      315477.44

QUERY 2



SELECT   YEAR(araclstdte) AS YEAR,
         MONTH(araclstdte) AS MONTH,
         SUM(AFACINIAMT) AS TOTAL_LOCATED
FROM     SQLUser.STFIELDAUD STF,
         SQLUser.ARRELATIONSHIP ARR,
         SQLUser.ARACCOUNT ARA,
         SQLUser.AFACCOUNT AFA
WHERE    STF.STAUDKEY=ARR.ARRELID
AND      ARA.ARACID=ARR.ARRELACID
AND      AFA.AFACKEY=ARR.ARRELAFID
AND      ARR.ARRELCLTID = 'LATX1'
AND      STF.STAUDFLDNAME = 'ARRELSTATUSID'
AND      STF.STAUDFLDEXTNEW NOT like 'BAD%'
AND      STF.STAUDFLDEXTOLD like 'BAD%'
AND      ARR.ARRELPHASE = 'REGULAR'
GROUP BY YEAR(araclstdte), MONTH(araclstdte)
ORDER BY YEAR(araclstdte), MONTH(araclstdte)

WHICH RETURNS:
YEAR      MONTH      TOTAL_LOCATED
2012      8      2905070.72
2012      9      202220.81
2012      10      462872.94
2012      11      77451.27
2012      12      22331.84

Both return the values expected.  The problem I have is when I try to nest (sub-query) the 2nd query into the first with this query:


SELECT   YEAR(araclstdte) AS YEAR,
         MONTH(araclstdte) AS MONTH,
         SUM(AFACINIAMT) AS TOTAL_PLACED,
(SELECT SUM(AFACINIAMT)
FROM     SQLUser.STFIELDAUD STF,
         SQLUser.ARRELATIONSHIP ARR,
         SQLUser.ARACCOUNT ARA,
         SQLUser.AFACCOUNT AFA
WHERE    STF.STAUDKEY=ARR.ARRELID
AND      ARA.ARACID=ARR.ARRELACID
AND      AFA.AFACKEY=ARR.ARRELAFID
AND      ARR.ARRELCLTID = 'LATX1'
AND      STF.STAUDFLDNAME = 'ARRELSTATUSID'
AND      STF.STAUDFLDEXTNEW NOT like 'BAD%'
AND      STF.STAUDFLDEXTOLD like 'BAD%'
AND      ARR.ARRELPHASE = 'REGULAR')AS TOTAL_LOCATED
FROM     SQLUser.STFIELDAUD STF,
         SQLUser.ARRELATIONSHIP ARR,
         SQLUser.ARACCOUNT ARA,
         SQLUser.AFACCOUNT AFA
WHERE    STF.STAUDKEY=ARR.ARRELID
AND      ARA.ARACID=ARR.ARRELACID
AND      AFA.AFACKEY=ARR.ARRELAFID
AND      ARR.ARRELCLTID = 'LATX1'
AND      STF.STAUDFLDNAME = 'ARRELSTATUSID'
AND      STF.STAUDFLDEXTNEW like 'BAD%'
AND      STF.STAUDFLDEXTOLD like 'NEWACCT'
GROUP BY YEAR(araclstdte), MONTH(araclstdte)
ORDER BY YEAR(araclstdte), MONTH(araclstdte)

I get the following which is incorrect:

YEAR      MONTH      TOTAL_PLACED      TOTAL_LOCATED
2012      8      3101542.03      3669947.58
2012      9      285786.43      3669947.58
2012      10      1224473.49      3669947.58
2012      11      327823.32      3669947.58
2012      12      315477.44      3669947.58

Please help if you can!.  Thanks and Happy Holidays
DatabasesMicrosoft SQL Server

Avatar of undefined
Last Comment
awking00

8/22/2022 - Mon
jmdl1983

You should look at doing the UNION ALL command as it will allow the joining of two result sets.

http://www.w3schools.com/sql/sql_union.asp
Guy Hengel [angelIII / a3]

I would rather do a JOIN of the 2 subquerys...

SELECT q1.YEAR, q1.MONTH, q1.TOTAL_PLACED, q2.TOTAL_LOCATED
   FROM  ( <yourquery1 ) q1
   JOIN  ( <yourquery2) q2
      ON q1.YEAR = q2.YEAR
     AND q1.MONTH = q2.MONTH

Open in new window

jmdl1983

A join is dependant on relationship between the two tables, whereas a UNION ALL allows completely distinct queries to be independent of each other.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
djkontroll

ASKER
Just because I forgot to mention I am running these queries from AQT on a Cache database via ODBC.   That said 'angelIII' would your solution still be suitable?
djkontroll

ASKER
@jmdl1983 - I think you are definitley pointing me in the right direction.  I tried union when I first started working on this and then had someone point me towards subqueries.  I have just tried a few variations and remembered the individual queries with unions must contain the final count of columns so I tried this and no luck.  I'm sure I'm close... Thoughts?

SELECT   YEAR(araclstdte) AS YEAR,
         MONTH(araclstdte) AS MONTH,
         SUM(AFACINIAMT) AS TOTAL_PLACED,'' as TOTAL_LOCATED
FROM     SQLUser.STFIELDAUD STF,
         SQLUser.ARRELATIONSHIP ARR,
         SQLUser.ARACCOUNT ARA,
         SQLUser.AFACCOUNT AFA
WHERE    STF.STAUDKEY=ARR.ARRELID
AND      ARA.ARACID=ARR.ARRELACID
AND      AFA.AFACKEY=ARR.ARRELAFID
AND      ARR.ARRELCLTID = 'LATX1'
AND      STF.STAUDFLDNAME = 'ARRELSTATUSID'
AND      STF.STAUDFLDEXTNEW like 'BAD%'
AND      STF.STAUDFLDEXTOLD like 'NEWACCT'
UNION ALL
SELECT   YEAR(araclstdte) AS YEAR,
         MONTH(araclstdte) AS MONTH,'' as TOTAL_PLACED,
         SUM(AFACINIAMT) AS TOTAL_LOCATED
FROM     SQLUser.STFIELDAUD STF,
         SQLUser.ARRELATIONSHIP ARR,
         SQLUser.ARACCOUNT ARA,
         SQLUser.AFACCOUNT AFA
WHERE    STF.STAUDKEY=ARR.ARRELID
AND      ARA.ARACID=ARR.ARRELACID
AND      AFA.AFACKEY=ARR.ARRELAFID
AND      ARR.ARRELCLTID = 'LATX1'
AND      STF.STAUDFLDNAME = 'ARRELSTATUSID'
AND      STF.STAUDFLDEXTNEW NOT like 'BAD%'
AND      STF.STAUDFLDEXTOLD like 'BAD%'
AND      ARR.ARRELPHASE = 'REGULAR'
GROUP BY YEAR(araclstdte), MONTH(araclstdte)
ORDER BY YEAR(araclstdte), MONTH(araclstdte)
jmdl1983

What is the error you are receiving?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
djkontroll

ASKER
Error during Prepare
 37000(25)[Cache ODBC][State : 37000][Native Code 25]
[C:\Program Files (x86)\Advanced Query Tool v9\aqtv9.exe]
[SQLCODE: <-25>:<Input encountered after end of query>]
[Cache Error: <<SYNTAX>errdone+2^%qaqqt>]
[Details: <Prepare>]
[%msg: < SQL ERROR #25: Input (() encountered after end of query^SELECT YEAR ( araclstdte ) AS YEAR , MONTH ( araclstdte ) AS MONTH , SUM ( AFACINIAMT ) AS TOTAL_PLACED , :%qpar(1) as TOTAL_LOCATED FROM SQLUser . STFIELDAUD STF , SQLUser . ARRELATIONSHIP ARR , SQLUser . ARACCOUNT ARA
djkontroll

ASKER
if I remove:
GROUP BY YEAR(araclstdte), MONTH(araclstdte)
ORDER BY YEAR(araclstdte), MONTH(araclstdte)

I of course get the details I need but not the summary.  This is the bigger reason I pushed to subquerry  so I could apply the grouping one time.  If I try to apply in both "legs" (for lack of a better term) of the union query I get an error SQL ERROR #25: Input (UNION) encountered after end of query
djkontroll

ASKER
I tried just Union instead of Union all and removed the Group/Sort and now much closer than I was however the numbers are now wrong?  See below:

SELECT   YEAR(araclstdte) AS YEAR,
         MONTH(araclstdte) AS MONTH,
         SUM(AFACINIAMT) AS TOTAL_PLACED,'' as TOTAL_LOCATED
FROM     SQLUser.STFIELDAUD STF,
         SQLUser.ARRELATIONSHIP ARR,
         SQLUser.ARACCOUNT ARA,
         SQLUser.AFACCOUNT AFA
WHERE    STF.STAUDKEY=ARR.ARRELID
AND      ARA.ARACID=ARR.ARRELACID
AND      AFA.AFACKEY=ARR.ARRELAFID
AND      ARR.ARRELCLTID = 'LATX1'
AND      STF.STAUDFLDNAME = 'ARRELSTATUSID'
AND      STF.STAUDFLDEXTNEW like 'BAD%'
AND      STF.STAUDFLDEXTOLD like 'NEWACCT'
UNION
SELECT   YEAR(araclstdte) AS YEAR,
         MONTH(araclstdte) AS MONTH,'' as TOTAL_PLACED,
         SUM(AFACINIAMT) AS TOTAL_LOCATED
FROM     SQLUser.STFIELDAUD STF,
         SQLUser.ARRELATIONSHIP ARR,
         SQLUser.ARACCOUNT ARA,
         SQLUser.AFACCOUNT AFA
WHERE    STF.STAUDKEY=ARR.ARRELID
AND      ARA.ARACID=ARR.ARRELACID
AND      AFA.AFACKEY=ARR.ARRELAFID
AND      ARR.ARRELCLTID = 'LATX1'
AND      STF.STAUDFLDNAME = 'ARRELSTATUSID'
AND      STF.STAUDFLDEXTNEW NOT like 'BAD%'
AND      STF.STAUDFLDEXTOLD like 'BAD%'
AND      ARR.ARRELPHASE = 'REGULAR'

RETURNS:

YEAR      MONTH      TOTAL_PLACED      TOTAL_LOCATED
2012      8      5255102.71      
2012      9      5255102.71      
2012      10      5255102.71      
2012      11      5255102.71      
2012      12      5255102.71      
2012      8      0                        3669947.58
2012      9      0                        3669947.58
2012      10      0                        3669947.58
2012      11      0                        3669947.58
2012      12      0                        3669947.58
Your help has saved me hundreds of hours of internet surfing.
fblack61
keyu

I think SQL join should definatly work for you....

please refer below link might helps you....

http://www.sqlnewsgroups.net/sqlserver/t11142-urgent-slow-join-across-sql-linked-server-intersystems-cache.aspx

if you still wants to follow unian all option i think you should try pivot table option....

http://blog.sqlauthority.com/2008/06/07/sql-server-pivot-and-unpivot-table-examples/
djkontroll

ASKER
@: keyu

The only issue with join like I was saying above is this is not a SQL database, I am using  ODBC to query an Intersystem's Cache DB.  That said I can't join queries as tables like you can with SQL server or Microsoft Access.  If I could use Access I would but one of the tables I am using in this query has 40 keys (That's the whole purpose of that table to be the center relationship for this DB) and that is beyond the maximum keys Acess allows per table.
Scott Pletcher

Can you use CASE expressions in your SQL?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
djkontroll

ASKER
Yes  I can
ASKER CERTIFIED SOLUTION
Scott Pletcher

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
djkontroll

ASKER
@ ScottPletcher:

Aren't you just the superstar!  Great job!  Thank you for the Xmas present and have a happy new year.

T
awking00

I think the case statement should work with a little tweaking -
SELECT   YEAR(araclstdte) AS YEAR,
         MONTH(araclstdte) AS MONTH,
         SUM(CASE WHEN STF.STAUDFLDEXTNEW like 'BAD%'
                  AND  STF.STAUDFLDEXTOLD = 'NEWACCT'
                  THEN AFACINIAMT ELSE 0
             END) AS TOTAL_PLACED,
         SUM(CASE WHEN STF.STAUDFLDEXTNEW NOT like 'BAD%'
                  AND  STF.STAUDFLDEXTOLD like 'BAD%'
                  AND  ARR.ARRELPHASE = 'REGULAR'
                  THEN AFACINIAMT ELSE 0
             END) AS TOTAL_LOCATED
FROM     SQLUser.STFIELDAUD STF,
         SQLUser.ARRELATIONSHIP ARR,
         SQLUser.ARACCOUNT ARA,
         SQLUser.AFACCOUNT AFA
WHERE    STF.STAUDKEY=ARR.ARRELID
AND      ARA.ARACID=ARR.ARRELACID
AND      AFA.AFACKEY=ARR.ARRELAFID
AND      ARR.ARRELCLTID = 'LATX1'
AND      STF.STAUDFLDNAME = 'ARRELSTATUSID'
GROUP BY YEAR(araclstdte), MONTH(araclstdte)
ORDER BY YEAR(araclstdte), MONTH(araclstdte);
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
awking00

Took too long testing and typing :-(
Out of curiosity, do the two queries ScottPletcher's and mine return the same results?
Scott Pletcher

I can't test it either, since I don't have data, but I can tell you: yes, they do produce the same results, albeit that yours could execute more slowly, depending on table indexes and the sophistication of the SQL optimizer.

Look closely at the first condition in each set of tests: they are mutually exclusive:

STF.STAUDFLDEXTNEW like 'BAD%'
--vs.--
STF.STAUDFLDEXTNEW NOT like 'BAD%'

Thus, you don't need to do additional checks to distinguish between them when adding the amounts.
djkontroll

ASKER
Scott's Runs in 5.09 sec

AWKINGS runs in 6.08
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
awking00

>>STF.STAUDFLDEXTNEW like 'BAD%'
--vs.--
STF.STAUDFLDEXTNEW NOT like 'BAD%'<<
ScottPletcher, missed that obviously off-setting condition. Thanks for pointing that out.
djkontroll, Thanks for running my query. At least it's good to know it worked.