I've been trying to find an Access version of the subject and testing many concepts. All I want to do is what I thought was simple. Let's say I have a single column in a table. Values are like
California
Florida
California
California
New York
California
New York
let's leave it that simple. All I'm trying to do which seems easy in SQL Server or Oracle is show the distinct values and how many times they are referenced in the table. So output would be:
California 4
Florida 1
New York 2
On Access 2019 if it matters.
Standard SQL; would be the same in SQL-Server, Oracle, MsAccess (and almost all other SQL dialects); just use:
select State, count(*)
from YourTbl
group by State
Related
Here's my situation : I have a table that has large amounts of records, I need to pull out a number of these records for each name in the database, note that TOP will not work for my use case. My end user wants the report formatted in such a way that each user shows up only once, and up to 3 different dates are shown for the user.
Table format
AutoID
Enum
TNum
Date
Comments
1
25
18
2/2/22
2
25
18
1/2/21
Blah
3
18
18
1/2/21
4
18
18
1/2/20
5
25
17
1/2/22
6
25
17
1/2/20
Now the Enum and TNum fields are fk with other tables, I have created a join that pulls the correct information from the other tables. In the end my query provides this output
RecordID
Training
CompletedDate
FirstName
LastName
Location
2821
MaP
1/1/21
David
Simpson
123 Sesame St.
2822
1/2/22
Fuller
MaP
Dough
GHI
David
123 Sesame St.
2825
1/1/20
Simpson
The two "Blank fields" represent information that is pulled and may or may not be needed in some future report.
So to my question : How do I manage to get a report, with this query's pull to look like this:
Place
LastName
FirstName
Training
FirstCuttoff
Secondcutoff
ThirdCutoff
Comments
123 Sesame St.
David
Simpson
MaP
1/1/20
1/1/21
123 Sesame St.
John
Dough
MaP
1/1/22
I was originally planning on joining my query to itself using where clauses. But when I tried that it just added two extra columns of the same date. In addition it is possible that each record is not identical; locations may be different but since the report needs the most recent location and the name of the trainee. In addition, to add more complexity, there are a number of people in the company with effectively the same name as far as the database is concerned, so rejoining on the name is out. I did pull the Enum in my query, I can join on that if needed.
Is there an easier way to do this, or do I need to sort out a multiple self-joining query?
I have a project I am working on where I am going to have to do this. Some of the suggestions I received were to use a Pivot query. It wouldn't work in my case but it might for yours. Here is a good example
Pivot Columns
This question already has an answer here:
How to output table results by using cfoutput group by date?
(1 answer)
Closed 7 years ago.
I'm working on outputting values from data base in the table. My table has 5 columns: Date, FirstName, LastName, City, State. Here is example of my data base table:
DateMeeting FirstName LastName City State
2015-12-11 Mike Johns Dallas TX
2015-12-11 John Cook Dallas TX
2015-12-11 Nick Roberts Dallas TX
2015-12-11 Oliver Ryan New York NY
2015-12-11 Michael Best New York NY
2015-12-11 David Holmes New York NY
So I want to have output table that will display just one date for multiple records. I tried to use DISTINCT on the date and that works fine but if I include my WHERE clause for City and State my query breaks. Also I tried to use GROUP BY but same problem, I can get Date values only once as long as I do not include other columns. In this case I need all columns but my Date value only once. Here is my query that I use:
Select Distinct(DateMeeting),FirstName, LastName, City, State
From Customers
Where City = 'Dallas'
and State = 'TX'
This query does not work with all columns that I have in my select, only if I run DISTINCT(DateMeeting). I would like to output my values in the table to look like this:
Date First Name Last Name City State
Mike Johns Dallas TX
John Cook Dallas TX
Nick Roberts Dallas TX
2015-12-11 Oliver Ryan New York NY
Michael Best New York NY
David Holmes New York NY
If anyone knows how this can be done please let me know. Thank you.
Each layer in the technology stack has its strengths and weaknesses.
As for mysql, do not turn it into a report engine as described with blank date columns except for one per date somewhere in the middle of a date chunk as shown. Subsequent dates as ordered will get muddled and confused.
True, one could use slightly interesting mysql variables and dump it just on the first row of a chunk. But for what.
Play to mysql's strengths, return all the data. And have the front-end (coldfusion or whatever), deal with the reporting features you desire for the output.
HI Guys i have a question...
I have a table
Name Country Date
abc Singapore 1/1/1111
cde Korea 1/1/1111
fgh Korea 1/1/1111
ijk Singapore 1/1/1111
mno Singapore 1/1/1111
pqr Singapore 2/1/1111
stuv Korea 2/1/1111
wxy Korea 2/1/1111
z Korea 2/1/1111
I want it to come out like this
Country Date(1/1/1111 Date2 (2/1/1111
Singapore 3 1
Korea 2 3
Note. Date and Date 2 either one or both can be temporary columns
I tried
SELECT Country, Count(Country) AS Quantity , 'Date1' As Expr1
FROM Reg
WHERE Date= #6/16/2014#
GROUP BY Country;
But as for the part i tried casing But it dosnt seems to work...
SELECT Reg.Date, Reg.Country, Reg.Date1
CASE Date
WHEN (((Reg.Date)=#6/16/2014#)) THEN Reg.Date1 = #6/16/2014#
END
Please help !!!
MS ACCESS
I noticed you tagged your post with both "MySQL" and "SQL-Server", but then referenced MS Access in the text. Which database system are you, in fact, using? How you implement something like this very much depends on the DB system, as there is no common support for it in the SQL standard. That said:
If you are using MS Access, then you want a "crosstab" query (just Google it; plenty of how-to articles already out there)
If you are using SQL Server, then you want a "pivot" query (again, just Google)
If you are using MySQL, then AFAIK you have to roll your own using an approach something like the one outlined here: http://stratosprovatopoulos.com/web-development/mysql/pivot-a-table-in-mysql/
Good luck!
I have the following data in my database table, since I'm fairly new to MYSQL i'm having problems in querying it to give me the following output
City Subject
london english
toronto math
london math
london math
toronto english
toronto english
There can only be two subjects, english or math. Im trying to output the data this way, first the query should pick all the distinct items in the city column. Then tell me the count of each subject in that city.
output
city English Math
london 1 2
toronto 2 1
I tried grouping, but since I don't know mysql that well, I realized it just groups the subjects together and eats the cities while grouping.
try this:
SELECT city,
SUM(IF(subject='english',1,0)) AS English,
SUM(IF(subject='math',1,0)) AS Math
FROM foo
GROUP BY city;
I have a table structured like this (not the actual data or fields, but the same structure applies):
ID | City Field2 Field 3
-------------------------------
1 New York Ohio data
2 Cincinnati data data
3 Los Angeles data Ohio
4 Cleveland data data
Then there's a second table something like this (again, not the actual data)
City State
-------------------------------
Los Angeles California
New York New York
Cincinnati Ohio
Cleveland Ohio
Houston Texas
etc.
I have a php web page that allows users to search the database; when they do, it will automatically print (in my example) the corresponding state from the second table after the city.
However, if the user searches for "Ohio", it should return all four records, even though "Ohio" doesn't exist in the first table. It looks like I'd have to run multiple queries - find the search term in the first table, find it again in the second table, and then search for column 1 of the second table in the first table (and then join the results). Is there an easier way to do that? (I'm fairly new to MySQL.)
Have you tried using a UNION?
SELECT City FROM table1 WHERE Field2 = 'Ohio' OR Field3 = 'Ohio'
UNION ALL
SELECT City FROM table2 WHERE State = 'Ohio'
It would be helpful to see what your query looks like.