473,378 Members | 1,427 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

3 queries into 1

1
Expand|Select|Wrap|Line Numbers
  1.  
  2. select ob.amount from BRDB_SU_OPENING_BALANCE ob
  3. where ob.BRANCH_ACCOUNTING_CODE=654321
  4. AND ob.STOCK_UNIT='UT1'
  5. AND ob.TRADING_PERIOD=101
  6. AND ob.BALANCE_PERIOD=1
  7. AND ob.PROD_ID=654
  8.  
  9. select nvl(ds.amount,0) from BRDB_DAILY_CUMULATIVE_SUMMARY ds
  10. where ds.BRANCH_ACCOUNTING_CODE=654321
  11. AND ds.STOCK_UNIT='UT1'
  12. AND ds.TRADING_PERIOD=101
  13. AND ds.BALANCE_PERIOD=1
  14. AND ds.PROD_ID=654
  15. AND ds.TRADING_DATE=(SELECT MAX(TRADING_DATE) from BRDB_DAILY_CUMULATIVE_SUMMARY
  16. where BRANCH_ACCOUNTING_CODE=654321
  17. AND STOCK_UNIT='UT1'
  18. AND TRADING_PERIOD=101
  19. AND BALANCE_PERIOD=1)
  20.  
  21. select nvl(sum(sd.amount),0) from BRDB_RX_REP_SESSION_DATA sd
  22. where sd.BRANCH_ACCOUNTING_CODE=654321
  23. AND sd.STOCK_UNIT='UT1'
  24. AND sd.TRADING_PERIOD=101
  25. AND sd.BALANCE_PERIOD=1
  26. AND sd.PROD_ID=654
  27. AND sd.TRADING_DATE=
  28.  
  29.  
I want to convert these three queries into one. So that I can add the amount of all the three queries and return one column back. How should I go about doing this?

The first query will definitely return me one record and only one.

The second query may or may not return any records. But if there are records for the combination given in the query then I want to fetch the one having the latest date (trading_date) and that is why I have a subquery. Can this be achieved without using sub-query?

The third query may or may not return any records. But if there any records then it should retrieve them where trading_date is > the max of trading date found in second query. But if there are no records from the second query then it should retrieve records where trading_date >= insert_timestamp from the first query table. Currently I don’t even how I can do this separately and that is the reason I have left the last AND as blank.

I think if we combine the three queries into one then we need outer-joins for the tables given in second and third queries (BRDB_DAILY_CUMULATIVE_SUMMARY, BRDB_RX_REP_SESSION_DATA).
Nov 30 '07 #1
0 1004

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Andreas Lauffer | last post by:
I changed from Access97 to AccessXP and I have immense performance problems. Details: - Access XP MDB with Jet 4.0 ( no ADP-Project ) - Linked Tables to SQL-Server 2000 over ODBC I used...
5
by: WertmanTheMad | last post by:
Ive been playing with this for a few days and thought I might thow it out for seggestions. I have Several Queries that need counts returned The Queries are Mutually Exclusive meaning whatever...
1
by: Roger Green | last post by:
I have inherited a complex database that has many dozens of queries that derive data from a people table. I now need to be able to run these queries (from within a significant number of forms)...
9
by: Dom Boyce | last post by:
Hi First up, I am using MS Access 2002. I have a database which records analyst rating changes for a list of companies on a daily basis. Unfortunately, the database has been set up (by my...
3
by: NeilAnderson | last post by:
I'm a fairly new user of access & I've never had any training, so I'm wondering if I'm doing the right thing here, or if it matter at all. I'm building a database for room booking purposes and I'm...
5
by: Jerry Hull | last post by:
I'm working with a database developed by an untrained person over several years - and on a network that has recently been upgraded with a new server installed and MS office upgraded from 2K (I...
0
by: MHenry | last post by:
Hi, I know virtually nothing about creating Macros in Access. I would appreciate some help in creating a Macro or Macros that automatically run(s) 14 Queries (three Make Table Queries, and 11...
44
by: Greg Strong | last post by:
Hello All, Is it better to create a query in DAO where a report has 4 sub-reports each of whose record source is a query created at runtime and everything is in 1 MDB file? From what I've...
1
by: loosecannon_1 | last post by:
Hello everyone, I am hoping someone can help me with this problem. I will say up front that I am not a SQL Server DBA, I am a developer. I have an application that sends about 25 simultaneous...
5
by: marshmallowww | last post by:
I have an Access 2000 mde application which uses ADO and pass through queries to communicate with SQL Server 7, 2000 or 2005. Some of my customers, especially those with SQL Server 2005, have had...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.