Solved

SQL query assistance requested

Posted on 2014-09-03
15
116 Views
Last Modified: 2014-09-04
I have a table with the following fields:

ID (pk auto-generated)
PLACE_CODE
PLACE
TRAVELDATE
CITY
TRAVELTIME
QUALIFY

I am trying to create a query fo an MS Access DB that:
1) displays all fields,
2) only displays where CITY = 'Seattle'
3) sets TRAVELTIME = (TRAVELTIME/2) if QUALIFY = "YES"
4) GROUPS BY PLACECODE, PLACE, TRAVELDATE
5) only unique PLACECODE-PLACE-TRAVELDATE are displayed
0
Comment
Question by:Cam Raben
  • 7
  • 4
  • 3
  • +1
15 Comments
 
LVL 45

Assisted Solution

by:Vitor Montalvão
Vitor Montalvão earned 275 total points
ID: 40301174
SELECT ID, PLACECODE, PLACE, TRAVELDATE, CITY,  iif(QUALIFY = "YES", (TRAVELTIME/2) , TRAVELTIME),  QUALIFY
FROM YourTableName
WHERE CITY = 'Seattle'
ORDER BY PLACECODE, PLACE, TRAVELDATE 

Open in new window

0
 

Author Comment

by:Cam Raben
ID: 40301316
So how does this handle a TRAVELDATE with a time stamp on it?  For example, what if there were 2 TRAVELDATES = 01/22/2014, but with different time stamps?   I assume both would be displayed.  I really just want only one of those to appear, and it can be an average of the two.
0
 
LVL 45

Expert Comment

by:Vitor Montalvão
ID: 40301355
Ok. But then ID shouldn't be returned.
I don't know if this going to work because I don't have Access here:
SELECT PLACECODE, PLACE, TRAVELDATE, CITY,  iif(QUALIFY = "YES", (TRAVELTIME/2) , TRAVELTIME),  QUALIFY
FROM YourTableName
WHERE CITY = 'Seattle'
GROUP BY PLACECODE, PLACE, TRAVELDATE 
ORDER BY PLACECODE, PLACE, TRAVELDATE 

Open in new window

0
 

Author Comment

by:Cam Raben
ID: 40301409
It does not let me select all my fields, using the group by.  I was thinking here may be a special way to use the TRAVELDATE so that the timestamp is not in play.   ?
0
 

Author Comment

by:Cam Raben
ID: 40301609
Any way to define TRAVELDATE as MM/DD/YYYY only, without the timestamp?
0
 

Author Comment

by:Cam Raben
ID: 40301656
I keep working with this but it keeps giving me duplicate rows.  I need only unique PLACECODE-PLACE-TRAVELDATE to be displayed.   ?
0
 
LVL 31

Expert Comment

by:awking00
ID: 40301823
Can you post some sample data (just enough to demonstrate the issue) and what you expect as output?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:Cam Raben
ID: 40301903
The DB is way too large to send.  The issue is that the suggested code shown below does not return unique instances of [PLACECODE, PLACE, TRAVELDATE].  In other words, the query returns more than one row with [PLACECODE = 125, PLACE = "California", and TRAVELDATE = 01/22/2014], when it should return that combination only once.

SELECT ID, PLACECODE, PLACE, TRAVELDATE, CITY,  iif(QUALIFY = "YES", (TRAVELTIME/2) , TRAVELTIME),  QUALIFY
FROM YourTableName
WHERE CITY = 'Seattle'
ORDER BY PLACECODE, PLACE, TRAVELDATE
0
 
LVL 31

Expert Comment

by:awking00
ID: 40301920
No need to send entire DB. What are the results of the following query?
select top 25 id, placecode, place, traveldate, city, traveltime, qualify
from yourtable
where city = 'Seattle'
and placecode = 125
and place = 'Califormia'
and traveldate = 01/22/2014
0
 
LVL 45

Expert Comment

by:Vitor Montalvão
ID: 40302821
You can use the format function to return only date without time.
SELECT DISTINCT PLACECODE, PLACE, format(TRAVELDATE, "mm/dd/yyyy"), CITY,  iif(QUALIFY = "YES", (TRAVELTIME/2) , TRAVELTIME),  QUALIFY
FROM YourTableName
WHERE CITY = 'Seattle'
ORDER BY PLACECODE, PLACE, TRAVELDATE 

Open in new window

0
 
LVL 12

Assisted Solution

by:Ammar Gaffar
Ammar Gaffar earned 50 total points
ID: 40303197
I don't know which database engine you are using, but if you are using SQL Server then the bellow code must return only date part

SELECT CONVERT(VARCHAR(10), TRAVELDATE, 111) FROM TABLENAME

Open in new window


If you are using MS Access, Vitor Montalvão's solution should work, if not the write the format in caps letter

SELECT Format([TRAVELDATE],"DD/MM/YYYY") FROM TABLENAME

Open in new window


Good Luck
0
 
LVL 45

Expert Comment

by:Vitor Montalvão
ID: 40303210
Even this is also in SQL Server topic it's in the question:
I am trying to create a query fo an MS Access DB that:
0
 

Author Comment

by:Cam Raben
ID: 40304085
Vitor - An error message say that the ORDER BY clause conflicts with the DISTINCT command.   ??
0
 
LVL 31

Accepted Solution

by:
awking00 earned 175 total points
ID: 40304287
I suspect that you need to order by PLACECODE, PLACE, format(TRAVELDATE, "mm/dd/yyyy")
0
 

Author Closing Comment

by:Cam Raben
ID: 40304510
Thanks all!
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now