473,387 Members | 1,549 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,387 software developers and data experts.

How to write query to extract date only for different customers on the same day.

15 Byte
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:

Expand|Select|Wrap|Line Numbers
  1. ShowDate    Customer    Property
  2. 7/21/2017    James    123 Main
  3. 7/18/2017    James    231 Park
  4. 7/18/2017    James    345 Forest
  5. 7/18/2017    James    678 Central
  6. 6/19/2017    Carmen    531 Charleston
  7. 6/19/2017    James    898 Broadway
  8. 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.
Mar 3 '18 #1
7 1126
gnawoncents
214 100+
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:
Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCT tbProperties.ShowDate, tbProperties.Customer
  2. FROM tbProperties;
  3.  
Second Query SQL:
Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCT qryProperties.ShowDate
  2. FROM qryProperties
  3. WHERE (((DCount("ShowDate","qryProperties","ShowDate = #" & [ShowDate] & "#"))>1));
  4.  
Disclaimer: This may not be the most elegant way to do this, so feel free to jump in if anyone sees a better one.
Mar 5 '18 #2
twinnyfo
3,653 Expert Mod 2GB
privateguy,

I'm not sure this is any more elegant, but it does accomplish the task in one Query instead of two:

Expand|Select|Wrap|Line Numbers
  1. SELECT tblProperties.ShowDate
  2. FROM tblProperties
  3. WHERE Not DCount("[Customer]","tblProperties","[ShowDate] = #" & [ShowDate] & "# AND [Customer] <> '" & [Customer] & "'") = 0
  4. GROUP BY tblProperties.ShowDate;
Hope this hepps!
Mar 5 '18 #3
privateguy
15 Byte
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.

Expand|Select|Wrap|Line Numbers
  1. Dim dbs As Database
  2. Dim rst1 As Recordset
  3. Dim rst2 As Recordset                                        
  4. Dim qry1 As String
  5. Dim qry2 As String
  6. Dim DatesFromQuery As String                                        
  7. Dim i As Integer
  8.  
  9. Set dbs = CurrentDb
  10. DatesFromQuery = ""
  11.  
  12.  
  13. qry1 = "Select Distinct ShowDates from qryShowings "
  14. Set rst1 = dbs.OpenRecordset(qry1)
  15.  
  16.     If rst1.RecordCount = 0 Then
  17.  
  18.         MsgBox "no dates available"
  19.     Else                                        
  20.         rst1.MoveLast
  21.         rst1.MoveFirst                                                    
  22.         DistinctDates = rst1.RecordCount 
  23.  
  24.         For i = 1 To rst1.RecordCount - 1
  25.  
  26.             qry2 = "Select Customer from qryShowings where ShowDate =" & "#" & rst1("ShowDate") & "#" & "group by Customer"
  27.  
  28.             Set rst2 = dbs.OpenRecordset(qry2)
  29.             rst2.MoveLast
  30.             rst2.MoveFirst
  31.  
  32.             If rst2.RecordCount > 1 Then
  33.  
  34.                 If DatesFromQuery = "" Then
  35.                     DatesFromQuery = rst1("ShowDate")
  36.                 Else
  37.                     DatesFromQuery = DatesFromQuery & "; " & rst1("ShowDate")
  38.                 End If
  39.  
  40.             End If
  41.  
  42.             rst2.Close
  43.             rst1.MoveNext
  44.  
  45.         Next i
  46.         MsgBox "Final:  " & DatesFromQuery
  47.  
  48.     End If
  49.  
  50.     'Put results in combobox
  51.     Me.ReportDates.RowSource = DatesFromQuery
Mar 5 '18 #4
twinnyfo
3,653 Expert Mod 2GB
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.
Mar 5 '18 #5
privateguy
15 Byte
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.
Mar 5 '18 #6
twinnyfo
3,653 Expert Mod 2GB
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:

Expand|Select|Wrap|Line Numbers
  1. This is my code with code tags
instead of this:

This is my code without code tags
Mar 5 '18 #7
Frinavale
9,735 Expert Mod 8TB
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:
Expand|Select|Wrap|Line Numbers
  1. SELECT ShowDate, Count(*) as NumCustomersShown FROM (SELECT DISTINCT ShowDate, Customer FROM @PropertyShowings)
  2. Group By ShowDate
  3. Having Count(*)>=2
Where my @PropertyShowings is a temporary table:
Expand|Select|Wrap|Line Numbers
  1. declare @PropertyShowings table(ShowDate date, Customer nvarchar(50), Property nvarchar(255));
  2. declare @DistinctPropertyShowings table(ShowDate date, Customer nvarchar(50));
  3. insert into @PropertyShowings Values('7/21/2017', 'James','123 Main Street');
  4. insert into @PropertyShowings Values('7/18/2017', 'James','231 Park Road');
  5. insert into @PropertyShowings Values('7/18/2017', 'James','345 Forest Street');
  6. insert into @PropertyShowings Values('7/18/2017', 'James','678 Central Ave');
  7. insert into @PropertyShowings Values('6/19/2017', 'Carmen','531 Charleston Ave');
  8. insert into @PropertyShowings Values('6/19/2017', 'James','898 Broadway Road');
  9. 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:
Expand|Select|Wrap|Line Numbers
  1. Select ShowDate, count(distinct Customer) as NumCustomersShown From @PropertyShowings 
  2. Group By ShowDate
  3. 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:
Mar 5 '18 #8

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

Similar topics

7
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 =...
2
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...
1
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
2
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
24
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...
1
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....
1
by: sudheerk | last post by:
How to split the query results into different rows
4
Sandboxer
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...
9
robin a
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...
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
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
Oralloy
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,...

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.