I want to select the dates where two or more different customers were shown properties on the same day. I want to exclude the dates where only a singular customer was shown properties on that day. For example: - ShowDate Customer Property
-
7/21/2017 James 123 Main
-
7/18/2017 James 231 Park
-
7/18/2017 James 345 Forest
-
7/18/2017 James 678 Central
-
6/19/2017 Carmen 531 Charleston
-
6/19/2017 James 898 Broadway
-
6/19/2017 James 543 Appleton
Only the date 6/19/2017 should be returned because properties where shown to both Carmen and James on the same day. Any help would certainly be appreciated.
7 1126
Welcome to Bytes, privateguy!
Your answer will depend on how you are trying to do this (VBA or Query/SQL). Regardless, you will have to do a two-step process that first finds all unique records, combining ShowDate and Customer. Next, based on those results, count how many are for any given day (anything over 1 means that at least two different people where shown properties on that day). Below is some SQL you can plug into two queries, if you want the results displayed via Query. Otherwise, just modify them to VBA and you can use the same idea to get results in a recordset, etc.
First Query (qryProperties) SQL: -
SELECT DISTINCT tbProperties.ShowDate, tbProperties.Customer
-
FROM tbProperties;
-
Second Query SQL: -
SELECT DISTINCT qryProperties.ShowDate
-
FROM qryProperties
-
WHERE (((DCount("ShowDate","qryProperties","ShowDate = #" & [ShowDate] & "#"))>1));
-
Disclaimer: This may not be the most elegant way to do this, so feel free to jump in if anyone sees a better one.
privateguy,
I'm not sure this is any more elegant, but it does accomplish the task in one Query instead of two: - SELECT tblProperties.ShowDate
-
FROM tblProperties
-
WHERE Not DCount("[Customer]","tblProperties","[ShowDate] = #" & [ShowDate] & "# AND [Customer] <> '" & [Customer] & "'") = 0
-
GROUP BY tblProperties.ShowDate;
Hope this hepps!
Thank you both for your replies. I am using VB. I had the same logic in mind but did not know of an easy way to write the query. In both of your examples it seems that I would have to know the date to insert it into the code. My workaround was to loop through the distinctive show dates and then test each individual date to see if the customer count was greater than one. If so, then I placed the date in a a combo box. Below is the code that I used. If you know of a simpler way, please advise. Again, thank you for taking out the time to respond. - Dim dbs As Database
-
Dim rst1 As Recordset
-
Dim rst2 As Recordset
-
Dim qry1 As String
-
Dim qry2 As String
-
Dim DatesFromQuery As String
-
Dim i As Integer
-
-
Set dbs = CurrentDb
-
DatesFromQuery = ""
-
-
-
qry1 = "Select Distinct ShowDates from qryShowings "
-
Set rst1 = dbs.OpenRecordset(qry1)
-
-
If rst1.RecordCount = 0 Then
-
-
MsgBox "no dates available"
-
Else
-
rst1.MoveLast
-
rst1.MoveFirst
-
DistinctDates = rst1.RecordCount
-
-
For i = 1 To rst1.RecordCount - 1
-
-
qry2 = "Select Customer from qryShowings where ShowDate =" & "#" & rst1("ShowDate") & "#" & "group by Customer"
-
-
Set rst2 = dbs.OpenRecordset(qry2)
-
rst2.MoveLast
-
rst2.MoveFirst
-
-
If rst2.RecordCount > 1 Then
-
-
If DatesFromQuery = "" Then
-
DatesFromQuery = rst1("ShowDate")
-
Else
-
DatesFromQuery = DatesFromQuery & "; " & rst1("ShowDate")
-
End If
-
-
End If
-
-
rst2.Close
-
rst1.MoveNext
-
-
Next i
-
MsgBox "Final: " & DatesFromQuery
-
-
End If
-
-
'Put results in combobox
-
Me.ReportDates.RowSource = DatesFromQuery
privateguy,
First, please use Code Tags when posting any code.
Second, if you look closely at both gnawoncents and my replies, it is not necessary to know the date. We are using the field name [ShowDate] (which you have provided as a Field Name) as either a field criteria or within our grouping criteria.
I believe both examples would work.
Twinnyfo,
This is my first time in seeking assistance on a forum. To be honest, I don't know what you mean by using Code Tags when pasting code. I will try the examples provided. Thank you again.
Code tags are codes you place around your code so that it formats properly. These are found in a button on the text entry display "[CODE/]. These tags allow your code to look like this: - This is my code with code tags
instead of this:
This is my code without code tags
I may be a bit off here since I don't usually work with Access but you should be able to Group By your ShowDate and select distinct ShowDate/Customer(s) having a count that is greater than or equal to 2.
Something like: -
SELECT ShowDate, Count(*) as NumCustomersShown FROM (SELECT DISTINCT ShowDate, Customer FROM @PropertyShowings)
-
Group By ShowDate
-
Having Count(*)>=2
Where my @PropertyShowings is a temporary table: -
declare @PropertyShowings table(ShowDate date, Customer nvarchar(50), Property nvarchar(255));
-
declare @DistinctPropertyShowings table(ShowDate date, Customer nvarchar(50));
-
insert into @PropertyShowings Values('7/21/2017', 'James','123 Main Street');
-
insert into @PropertyShowings Values('7/18/2017', 'James','231 Park Road');
-
insert into @PropertyShowings Values('7/18/2017', 'James','345 Forest Street');
-
insert into @PropertyShowings Values('7/18/2017', 'James','678 Central Ave');
-
insert into @PropertyShowings Values('6/19/2017', 'Carmen','531 Charleston Ave');
-
insert into @PropertyShowings Values('6/19/2017', 'James','898 Broadway Road');
-
insert into @PropertyShowings Values('6/19/2017', 'James','543 Appleton Lane');
If you were to do this in TSQL (MS Sql Server) you could have: -
Select ShowDate, count(distinct Customer) as NumCustomersShown From @PropertyShowings
-
Group By ShowDate
-
Having Count(distinct Customer)>=2;
But apparently Access doesn't support Count(distinct ...) so you have to select the distinct values first and then do a count on what was selected.
For more information see: Sign in to post your reply or Sign up for a free account.
Similar topics
by: vnl |
last post by:
I'm trying to run a SQL query but can't find any records when trying to
select a certain date. Here's the sql:
SELECT field 1, field2, date_and_time,
FROM table1
WHERE date_and_time =...
|
by: jcabc |
last post by:
The row counts I am getting from SQL Enterprise Manager and Query
Analyzer are different. When I double click a table in Enterprise
Manager it gives me a row count of say 1000. However, when I...
|
by: Pooja Raisingani via AccessMonster.com |
last post by:
Hello,
Can anyone please tell me
How to use a select query to extract date from a Datetime field in Access 2000??
Thanks
--
Message posted via http://www.accessmonster.com
|
by: Cyber Clone via DotNetMonster.com |
last post by:
Hello All,
i want to know how to date different in csharp.
i can datediff in vb6.
is there any simple command for date different (CSharp)?
thanks
Cyber Clone
|
by: clare at snyder.on.ca |
last post by:
I have a SQL query I need to design to select name and email addresses
for policies that are due and not renewed in a given time period. The
problem is, the database keeps the information for every...
|
by: sumathikcs |
last post by:
I am using perl scripts to receive the documents. For this i need to verify the received time. So, i need to extract a field with date/time from oracle db. the data type of the field format is date....
|
by: sudheerk |
last post by:
How to split the query results into different rows
|
by: Sandboxer |
last post by:
I want to be able to program Access to provide for me, by individual day, what my contract obligations are to my customers. Will Access recognize all the individual days in between a date range...
|
by: robin a |
last post by:
I have a query in which I need to get the number of periods between and including the start date and end date for a project to use in a calculation. There are 13 periods in a year. I can't use exact...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |