1

My first post so here goes, been doing some basic SQL but having to learn a bit more due to more responsibilities.

Query 1 below gives me a year to date value of all codes in our Financial System, these run from Objective AAAAA to ZZZZZ and Subjective 0000 to 9999, not all codes are in use, at present this is at week 28 in year 15.

Query 2 below gives me the total value of journals (in three journal tables) that have been input this week (29), they have a field called jnl_processed that can be selected with the line AND JNL_PROCESSED = 'N', this adds up the value of the no processed journals as per the relevant Objective and Subjective codes, there are are lot less codes on this table compared to table 1.

I am looking ahead to year end and a worthwhile query would be to see the value of all the Year_To_Date (YTD) in this instance week 28 added to the journal value week 29 to create and new column total.

I have the two queries separately below, but struggling to merge the two, can anybody assist?

Query 1

SELECT OBJ_FULL,SUBJ_FULL,ACT_YTD_TY 
FROM DB2ADM2.TFINCATP
WHERE OBJ_FULL = 'TBBBB'
AND ACT_YTD_TY <> 0.00

Produces this:

OBJ_FULL    SUBJ_FULL   ACT_YTD_TY
TBBBB           3420            12,000.00 
TBBBB           3473            18,453.02 
TBBBB           3524            2,480.40 
TBBBB           3704            585.00 
TBBBB           7704            -4,142.71 
TBBBB           8199            -25.00 
TBBBB           9400            -168,363.10 
TBBBB           9403            -457.56 
TBBBB           9404            -9,666.73 
TBBBB           9405            -13,098.05 

Query 2

SELECT  JNL_OBJ, JNL_SUBJ, SUM(JNL_VALUE)
FROM DB2ADM2.JNLFILE
WHERE JNL_PROCESSED = 'N' AND JNL_YEAR = '15' 
GROUP BY JNL_OBJ, JNL_SUBJ
UNION
SELECT  JNL_OBJ, JNL_SUBJ, SUM(JNL_VALUE)
FROM DB2ADM2.JNLRFILE
WHERE JNL_PROCESSED = 'N'  AND JNL_YEAR = '15'
GROUP BY JNL_OBJ, JNL_SUBJ
UNION
SELECT  JNL_OBJ, JNL_SUBJ, SUM(JNL_VALUE)
FROM DB2ADM2.JNLYFILE
WHERE JNL_PROCESSED = 'N'  AND JNL_YEAR = '15' 
GROUP BY JNL_OBJ, JNL_SUBJ

Produces this:

JNL_OBJ JNL_SUBJ    VALUE
TBBBB   9404            -547.78 
TBBBB   9405            -24.39 
TBBCA   9404            547.78 
TBBCC   9400            24.39 

Ideally the result should return (using four codes as an example)

OBJ     SUBJ    YTD           VALUE        TOTAL
TBBBB   3420    12,000.00   0.00       12,000.00 
TBBBB   9403    -457.56     0.00       -457.56 
TBBBB   9404    -9,666.73   -547.78    -10,214.51 
TBBBB   9405    -13,098.05  -24.39     -13,122.44 

Any assistance greatly appreciated, I don't know if there is a simpler way to merge the three journal tables rather than using UNION, the columns are the same apart from JNL_SOURCE which is JA, JC and JD for each of the tables.

This is the current data being returned at 15:30 on 21 Oct 15

OBJ         SUBJ     YTD        VALUE   TOTAL
TBBBB   9404    -9666.73    -547.78 -10214.51
TBBBB   9405    -13098.05   -24.39  -13122.44

There are several other codes with a value in the YTD column but the VALUE column is zero, I need to include all codes.

Paul E
  • 17
  • 5

2 Answers2

0

You have two perfectly good queries worked out. Now you need to use them as if they were tables -- think of them as virtual tables -- and join them together.

Something like this will do the trick.

SELECT query1.OBJ_FULL, query1.SUBJ_FULL, 
       query1.ACT_YTD_TY, query2.VALUE,
       (query1.ACT_YTD_TY + query2.VALUE) AS TOTAL 
  FROM ( 
          /* your first query */
       ) query1
  JOIN (
          /* your second query */
       ) query2 ON query1.OBJ_FULL = query2.JNL_OBJ
               AND query1.SUBJ_FULL = query2.JNL_SUBJ

That does the trick of laminating your two result sets together.

O. Jones
  • 103,626
  • 17
  • 118
  • 172
  • Struggling to get that to work, get the following error – Paul E Oct 21 '15 at 11:08
  • BIC000004. DAL01008. An error occured while accessing the database. DB2 SQL error: SQLCODE: -206, SQLSTATE: 42703, SQLERRMC: QUERY2.JNL_VALUE; DB2 SQL error: SQLCODE: -727, SQLSTATE: 56098, SQLERRMC; 2;-206;42703;QUERY2.JNL_VALUE – Paul E Oct 21 '15 at 11:12
  • I'm sorry, I don't have the slightest idea how to interpret IBM db2 error messages. I thought you said this was using the MySQL database. – O. Jones Oct 21 '15 at 17:45
  • No worries, my lack of understanding doesn't help, thanks for your reply though. – Paul E Oct 23 '15 at 10:21
0

Try ...

SELECT EOYQuery.OBJ_FULL AS OBJ,
       EOYQuery.SUBJ_FULL AS SUBJ,
       EOYQuery.ACT_YTD_TY AS YTD,
       JournalsQuery.JournalsValue AS VALUE,
       ( EOYQuery.ACT_YTD_TY + JournalsQuery.JournalsValue ) AS TOTAL
FROM ( SELECT OBJ_FULL,
              SUBJ_FULL,
              ACT_YTD_TY
       FROM DB2ADM2.TFINCATP
       WHERE OBJ_FULL = 'TBBBB'
         AND ACT_YTD_TY <> 0.00 ) EOYQuery,
     ( SELECT JNL_OBJ,
              JNL_SUBJ,
              SUM( JNL_VALUE ) AS JournalsValue
       FROM DB2ADM2.JNLFILE
       WHERE JNL_PROCESSED = 'N' AND
             JNL_YEAR = '15' 
       GROUP BY JNL_OBJ,
                JNL_SUBJ
       UNION SELECT JNL_OBJ,
                    JNL_SUBJ,
                    SUM( JNL_VALUE ) AS JournalsValue
             FROM DB2ADM2.JNLRFILE
             WHERE JNL_PROCESSED = 'N' AND
                   JNL_YEAR = '15'
             GROUP BY JNL_OBJ,
                      JNL_SUBJ
       UNION SELECT JNL_OBJ,
                    JNL_SUBJ,
                    SUM( JNL_VALUE ) AS JournalsValue
             FROM DB2ADM2.JNLYFILE
             WHERE JNL_PROCESSED = 'N' AND
                   JNL_YEAR = '15'
                               GROUP BY JNL_OBJ,
                      JNL_SUBJ ) JournalsQuery
WHERE EOYQuery.OBJ_FULL  = JournalsQuery.JNL_OBJ
  AND EOYQuery.SUBJ_FULL = JournalsQuery.JNL_SUBJ;

If this is or is not a suitable answer, then please feel free to comment accordingly.

Please Note : Without knowing for sure the structure of the tables being referenced as well as a representative (imitation) sample of the data stored in those tables, I am unable to test my answer.

I could try to deduce these from the samples you have supplied, but constructing the script necessary to replicate these in MySQL would take more time than I currently have. If you have or can construct such script yourself and you are allowed to post it, and do so, then I will be able to test my answer and hunt down any bugs that may be present.

toonice
  • 2,211
  • 1
  • 13
  • 20
  • Toonice, I had to change the following line JournalsQuery.VALUE AS VALUE to JournalsQuery.JNL_VALUE as AMT and change to (YTD + AMT) as TOTAL, however got the following message. – Paul E Oct 21 '15 at 11:02
  • BIC000004. DAL01008. An error occured while accessing the database. DB2 SQL error: SQLCODE: -206, SQLSTATE: 42703, SQLERRMC: JOURNALSQUERY.JNL_VALUE; DB2 SQL error: SQLCODE: -727, SQLSTATE: 56098, SQELRRMC: 2:-206;42703;JOURNALSQUERY.JNL_VALUE – Paul E Oct 21 '15 at 11:02
  • My apolgies - I should have paid more attention to your quoted query. Please note : My usage of JournalsQuery.VALUE was based on your "output" of Query 2. I may have spotted the source of your error also, and I shall edit my answer accordingly. Am I correct in assuming that you are not allowed to reproduce any more of the tables structures and data than you have already shown? – toonice Oct 21 '15 at 11:47
  • The following should shed some light on the nature of the error - http://stackoverflow.com/questions/24257615/db2-sql-error-sqlcode-206-sqlstate-42703. – toonice Oct 21 '15 at 12:13
  • still can't get my head round it, next error message below – Paul E Oct 21 '15 at 13:24
  • BIC000004. DAL01008. An error occured while accessing the database. DB2 SQL error: SQLCODE: -206, SQLSTATE: 42703, SQLERRMC: YTD; DB2 SQL error: SQLCODE: -727, SQLSTATE: 56098, SQLERRMC: 2;-206;42703;YTD – Paul E Oct 21 '15 at 13:24
  • I can share all the table formats etc but don't know how to on here, could I email them? – Paul E Oct 21 '15 at 13:25
  • I have made another edit which will hopefully solve the problem. If it does not, then I will have to check the forum's protocols regarding email correspondence. As for posting the scripts / information, there are 2 options. The first is to edit your question and add the text as you did your queries and sample output. This is more in line with the forum's protocols, and my preferred format, but can result in a very long question which some people might find offputting. (Continues in next comment). – toonice Oct 21 '15 at 14:05
  • The other alternative is to place the code on an external site, such as jsfiddle (https://jsfiddle.net/) or a number of others. I'm still new to these sites and am still learning both their strengths and their weaknesses. The main strength thusfar is that you can use the right site to present a functioning sample of your problem. (Continues) – toonice Oct 21 '15 at 14:10
  • The main weakness is that this forums protocols recommend against using external sites exclusively to present code / data since, if the external website goes down in the future, then anybody trying to access your code / data afterwards will not be able to do so, thus compromising their ability to learn from your example, which is one of the main points of this forum. Next time I have a break, I might try to find and bookmark the relevant protocol so I can reproduce the link to it as needed. – toonice Oct 21 '15 at 14:13
  • That has run this time, I have now added the following line of code as it duplicated some lines AND EOYQuery.SUBJ_FULL = JournalsQuery.JNL_SUBJ; the next problem is (continued) – Paul E Oct 21 '15 at 14:29
  • Data has now been returned for the two lines as there are only two journals in the system this week, it is ignoring any VALUE that equals zero, I will copy and paste the results in my question, nearly there! – Paul E Oct 21 '15 at 14:31
  • Please see my comment that is attached to your question above. – toonice Oct 21 '15 at 15:48
  • Got that all up and running now, copy and pasted the actual working query, thanks for your help. – Paul E Oct 23 '15 at 10:21
  • No worries - glad to help. in regards to the edits you proposed, I rejected the proposed changes to the indenting of the first SELECT line and to the first two GROUP BY lines of JournalsQuery. I prefer to keep each GROUP BY in line with its corresponding SELECT, FROM, WHERE, etc. Doing so helps with debugging a query, especially when that query involves alot of nesting, brackets, etc. By all means adapt the layout of your implementation according to your protocols, I'm all for that, but I shall keep the layout of my answer as is. – toonice Oct 23 '15 at 11:29
  • I was, of course, happy to alter my final WHERE clause to be more in line with what you were looking for. Incidentally, if you are interested in knowing more about my layout protocols I can investigate posting them in an appropriate forum. – toonice Oct 23 '15 at 11:34
  • Sorry, I should have edited your SQL rather than copy and pasting. – Paul E Oct 23 '15 at 13:08