• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 478
  • Last Modified:

How to fetch data from two table with comparision between date and column

Dear Sir,
      I have two table No. 1 check Drawn Statement(CDS Table) 2nd One is Bank Statement table.

Table No. 1  field is as below :-
DATE  Office CODE      office NAME       CHEQUENO       AMOUNT
Table 2 field is as below :-
Date      Cheque No.      Amount

Both the table have data of months sep, oct, nov.
As u see both the table have cheque no field is comman.

Now I want a report of unmatched cheque of particular month. Say I want to report of month 9/2010. I want to know that which cheque issued in the month of 9/2010 but they don’t Appear in the month of 9/2010 in the bank statement. Similarly I want which cheque issued in the month of 9/2010 and they present in the bank in the month of 9/2010.
1.      Unmatched cheque report of 9/2010 out three months.
2.      Matched cheque report of 9/2010 out three months.
I want to do this in ms access please tell me the query of these reports.
Thanks

0
Manojtanwar
Asked:
Manojtanwar
  • 4
  • 4
1 Solution
 
incercCommented:
Hello,

1. Unmatched cheques

a) Using subselects :
SELECT * FROM CDS WHERE Date="9/2010" AND CheckNo NOT IN
(Select CheckNo from BankStatement WHERE Date="9/2010");

b) Using LEFT JOIN:
SELECT * FROM CDS LEFT JOIN BankStatement ON CDS.CheckNo = BankStatement.CheckNo
WHERE  BankStatement.CheckNo IS NULL AND CDS.Date=''9/2010" AND BankStatement.Date="9/2010";

2. Matched cheques

SELECT * FROM CDS INNER JOIN BankStatement ON CDS.CheckNo = BankStatement.CheckNo
WHERE  CDS.Date=''9/2010" AND BankStatement.Date="9/2010";

(using subselects, change "NOT IN" with "IN")

You need to modify the above queries in order to compare the month and year of the Date field, using the Month(Date) and Year(Date) functions. It depends also on how you keep the Date data.

Here is more on the Date/Time field manipulation on MS Access :
http://support.microsoft.com/kb/210276

Here is a list of most used MS Access functions :
http://www.techonthenet.com/access/functions/
0
 
ManojtanwarAuthor Commented:
Dear incerc:

i put that query for unmatched cheque but it works find until i have sepmonth data but when i insert sep and oct data in the both table. I put between date parameter on date field of cds table. but it compare sep data of cds table with sep and oct of bank statement table. then i put another between date parameter on  date field of bank statement but after that not data is fetch.    I put below noted query :-

SELECT [OP Divn Sohna CDS].Date, [OP Divn Sohna CDS].DCODE, [OP Divn Sohna CDS].D_NAME, [OP Divn Sohna CDS].CHEQUENO, [OP Divn Sohna CDS].AMOUNT, [Bank Statement OBC].Date
FROM [OP Divn Sohna CDS] LEFT JOIN [Bank Statement OBC] ON [OP Divn Sohna CDS].CHEQUENO = [Bank Statement OBC].[Cheque No#]
WHERE ((([OP Divn Sohna CDS].Date) Between [Start Date] And [End Date]) AND (([Bank Statement OBC].[Cheque No#]) Is Null) AND (([Bank Statement OBC].Date) Between [Start Date] And [End Date]));

Pls solve my problem.

Thanks

0
 
GRayLCommented:
This do it?

SELECT a.ChequeNo, a.[Date], b.[Date] FROM [OP Divn Sohna CDS] a LEFT JOIN [Bank Statement OBC] b ON a.ChequeNo = b.[Cheque No#] Where Format((a.[Date]),"yymm") = Format(b.[Date],"yymm") AND Format(a.[Date],"yymm") IN ("1009","1010","1011");
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
ManojtanwarAuthor Commented:
Dear GRayL:
 This is also not working or i put wrongly. I attach excel file with data this file have two sheet one is with data of issue of cheque and second one is bank statement. i want report of  unmatched but comparing to same months like if cheque issued in sep then i have to compare with sep month data in bank statment. Remember bank statment having two months data sep and oct.

Thanks
sample.xlsx
0
 
incercCommented:
I'm sorry, in my left join select there was an error.

Please try this way :

SELECT [OP Divn Sohna CDS].Date, [OP Divn Sohna CDS].DCODE, [OP Divn Sohna CDS].D_NAME, [OP Divn Sohna CDS].CHEQUENO, [OP Divn Sohna CDS].AMOUNT, [Bank Statement OBC].Date
FROM [OP Divn Sohna CDS] LEFT JOIN [Bank Statement OBC] ON [OP Divn Sohna CDS].CHEQUENO = [Bank Statement OBC].[Cheque No#]
WHERE ((([OP Divn Sohna CDS].Date) Between [Start Date] And [End Date]) AND ((([Bank Statement OBC].[Cheque No#]) Is Null) OR (([Bank Statement OBC].Date) NOT Between [Start Date] And [End Date])));

Basically, the WHERE clause was wrong, in my example it should be :

SELECT * FROM CDS LEFT JOIN BankStatement ON CDS.CheckNo = BankStatement.CheckNo
WHERE CDS.Date=''9/2010" AND (BankStatement.CheckNo IS NULL OR BankStatement.Date <> "9/2010");
0
 
ManojtanwarAuthor Commented:
Dear
incerc:

This query working fine but every data repeat two times.

SELECT [OP Divn Sohna CDS].Date, [OP Divn Sohna CDS].DCODE, [OP Divn Sohna CDS].D_NAME, [OP Divn Sohna CDS].CHEQUENO, [OP Divn Sohna CDS].AMOUNT, [Bank Statement OBC].Date
FROM [OP Divn Sohna CDS] LEFT JOIN [Bank Statement OBC] ON [OP Divn Sohna CDS].CHEQUENO = [Bank Statement OBC].[Cheque No#]
WHERE ((([OP Divn Sohna CDS].Date) Between [Start Date] And [End Date]) AND ((([Bank Statement OBC].[Cheque No#]) Is Null) OR (([Bank Statement OBC].Date) NOT Between [Start Date] And [End Date])));
Pls resolve.
Thanks
Manoj
0
 
incercCommented:
In this case, please try select distinct , like this :

SELECT DISTINCT [OP Divn Sohna CDS].Date, [OP Divn Sohna CDS].DCODE, [OP Divn Sohna CDS].D_NAME, [OP Divn Sohna CDS].CHEQUENO, [OP Divn Sohna CDS].AMOUNT, [Bank Statement OBC].Date
FROM [OP Divn Sohna CDS] LEFT JOIN [Bank Statement OBC] ON [OP Divn Sohna CDS].CHEQUENO = [Bank Statement OBC].[Cheque No#]
WHERE ((([OP Divn Sohna CDS].Date) Between [Start Date] And [End Date]) AND ((([Bank Statement OBC].[Cheque No#]) Is Null) OR (([Bank Statement OBC].Date) NOT Between [Start Date] And [End Date])));
0
 
ManojtanwarAuthor Commented:
Dear
incerc:

it working fine sir, thanks, can u explain the this statement 'WHERE ((([OP Divn Sohna CDS].Date) Between [Start Date] And [End Date]) AND ((([Bank Statement OBC].[Cheque No#]) Is Null) OR (([Bank Statement OBC].Date) NOT Between [Start Date] And [End Date])));' how it works. I enter date one time and bank date not between that date. how it works pls explain.

thanks
Manoj
0
 
incercCommented:

Hi, I'm glad it works :)

Please see these links about LEFT JOIN (and other joins :) ) :

http://msdn.microsoft.com/en-us/library/bb208894%28office.12%29.aspx

http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html

Left outer joins include all of the records from the first (left) of two tables, even if there are no matching values for records in the second (right) table.

Basicaly, as an exemple, if you have T1 with fields PK (as primary key), Date :
pk1, date1
pk2, date2

T2 with fields T1_PK (as foreign key from T1), Date :
pk1, date1
pk1, date3

Select T1.PK, T1.Date, T2.T1_PK, T2.Date From T1 LEFT JOIN T2 ON T1.PK = T2.T1_PK will give you :

pk1, date1, pk1, date1
pk1, date1, pk1, date3
pk2, date2, null, null

Now, having the info from the 2 tables, to find a NON MATCH in T2 for a given date existing in T1, all you have to do is filter through the WHERE clause.
- T1.Date must be btw the selected dates
- T2.T1_PK must be null  (or I could say also "T2.Date Is Null" !), or T2.Date must not be between the selected dates  (null means that no data was in T2 for the given key in T1).

Therefore, adding the
WHERE T1.date = date1 AND (T2.Date <> date1 OR T2.T1_PK is null)
the query will return you :

pk1, date1, pk1, date3

In your case, you don't have a given date, but an interval, however, the idea remains the same (I used equality for simplicity purposes).

I hope this explanation helps!
0

Featured Post

Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now