ms access: retrieving latest value - ms-access

I have a table Project.
It has the following column: Project ID, projectName, UpdateTime
The data in the table is as follows:
Project ID projectName UpdateTime
1 abc 12-2-2009 01:10:00
1 abc 12-2-2009 04:18:00
2 xyz 17-7-2009 08:45:00
2 xyz 17-7-2009 12:21:00
i want the result set to display the latest update project information based on the update time.
for the above example , it should display
Project ID projectName UpdateTime
2 xyz 17-7-2009 12:21:00
1 abc 12-2-2009 04:18:00

This ought to do the job (as edited to reflect the information #Remou pointed out that I failed to note in your original question):
SELECT [Project ID], ProjectName, Max(UpdateTime)
FROM Project
GROUP BY [Project ID], ProjectName
ORDER BY Max(UpdateTime) DESC
If that doesn't do it, either I've made a mistake, or there's other information not included in your question.

SELECT P.[Project ID]
, P.projectName
, Max(P.UpdateTime) AS [Latest Update Time]
FROM Project AS P
GROUP BY P.[Project ID]
, P.projectName
ORDER BY Max(P.UpdateTime) DESC;

I have an answer which uses the graphical interface of a query which may be easy for you to implement. It is presumed that all 3 fields are contained in a single table. Create a new query by clicking "CREATE" and thereafter "Query Design". Right click in the upper section of the graphical interface that opens and click on "Show Table" Select the table that contains these three fields and double click on each field, one at a time so that it appears in the lower section. If you have an auto-number field please ensure that you do not include it in the lower section since this will cause all the table records to display. Any other additional field included in the query could have the same undesired result. Next, on your ribbon click "DESIGN" and after that click on the "Totals" icon. Focus attention on your "UpdateTime" field in the lower section. Change "Group By" to "Max" by clicking into that field and selecting from the drop-down list. To not make ay changes to the other two fields. Save the query and open it in Datasheet view (Do this by clicking "HOME", "View", "Datasheet View"
You should get the results you required. Please let me know whether this has now worked for you.
Ps. Spelling errors in the project name will also give you inaccurate results. To reduce the likelihood of this possible problem have your users to select project names from a combo-box that that has a drop-down list of project names. It is also possible to devise a means whereby the Project name is auto entered when a user selects a Project ID from a drop-down list.

I am not sure why everyone seems to want to group the projects. The question simply seems to ask to display the results in descending order of UpdateTime
SELECT * FROM Project
ORDER BY UpdateTime DESC
I also notice however, that in his example, the results that he anticipates has the records grouped by Project ID and Project Name, in which case the answer provided by #David-W-Fenton must be sufficient.
In Conclusion, he should have asked his question more clearly.

Related

Drop down source issues for Apex Form

I have a table I created to use as the source for the of dropdowns of a form. The goal is to have only what possible options are available for the Department code based on who the owner is. I got the scrap owner to display correctly but unfortunately I have not been able for the department code to show up. No options are able to pop up. This is what my code looks like:
Select DEPT_CODE, DEPT_CODE as Value From Degrade_Robots
WHERE SO_NAME = :P7_SCRAP_OWNER
group by DEPT_CODE
order by DEPT_CODE
My table is set up like:
You aren't aggregating anything; don't use group by but distinct.
select distinct
dept_code as display_value,
dept_code as return_value
from degrade_robots
where so_name = :P7_SCRAP_OWNER
order by dept_code;
As you don't see anything in the list, I presume that's because you forgot to mention P7_SCRAP_OWNER item in Select List's LoV Parent Item(s) property.

How to make a table on Access using "Count" and Age Range?

I downloaded a record of all the people on the first fleet from this website. http://firstfleet.uow.edu.au/download.html
I downloaded the Excel and imported it into Access. In the document, certain people’s age is not known so they are simply put as -1.
I am trying to make this table but I am struggling.
I’ve tried doing it by creating a table but that was too hard so I decided to create a query instead. I am using “Crosstab” but failing to use it successfully.
When I do try to sub the -1 for unknown:
Messages like this pop up:
How do we add the table in but sub the -1 for unknown while keeping the layout the same? I am hard stuck at a place where I cannot even view how my query looks.
How do I even make the layout the same as the picture shown above?
UPDATE:
Calculate the age groups with Switch() function and use that calculated field for CROSSTAB RowColumn. I renamed the Age field in table from the original import name to shorten the expression.
TRANSFORM Count(Convicts.ID) AS CountOfID
SELECT Switch([Age]=-1,"Unknown",[Age]<15,"10-14",[Age]<20,"15-19",[Age]<25,"20-24",[Age]<30,"25-29",[Age]<35,"30-34",[Age]<40,"35-39",[Age]<45,"40-44",[Age]<50,"45-49",[Age]<60,"50-59",[Age]<100,"60-99") AS AgeGrp
FROM Convicts
GROUP BY Switch([Age]=-1,"Unknown",[Age]<15,"10-14",[Age]<20,"15-19",[Age]<25,"20-24",[Age]<30,"25-29",[Age]<35,"30-34",[Age]<40,"35-39",[Age]<45,"40-44",[Age]<50,"45-49",[Age]<60,"50-59",[Age]<100,"60-99")
PIVOT Convicts.Gender;
The total row is added by clicking the Sigma(Totals) icon on the ribbon when query is in Datasheet view.
A simpler calculation will generate more groups:
TRANSFORM Count(Convicts.ID) AS CountOfID
SELECT Partition([Age],0,100,5) AS AgeGrp
FROM Convicts
GROUP BY Partition([Age],0,100,5)
PIVOT Convicts.Gender;
But a little adjustment to the SELECT clause will get same output as the first:
SELECT IIf([Age]=-1,"Unknown",IIf([Age]<50,Partition([Age],0,50,5),IIf([Age]<60,"50:59","60:99"))) AS AgeGrp
Could add a field in table and use one of those expressions in UPDATE action SQL to add calculated AgeGrp, which would simplify the CROSSTAB and other queries needing that group identifier.

Searching a database in Report Builder 3.0

I am not sure if this is possible in Report Builder or not. I want to put a text box in a Report Builder report and then search the database for a specific person and display that in a subreport. Is this possible and, if so, how would you suggest I do it? I have tried researching it online without success.
This part will produce a parameter into which your user can type whatever they want
You need to set up your report to use parameters. You can set these parameters up to require user input either from manual entry or by picking from a pre-defined list.
Assuming you are returning your data using a SQL query, you can then reference these parameters in your dataset script. If for example you had a parameter called FirstName and another called Surname, and you only wanted to return values in your data set that matched both exactly, you would reference these parameters like so:
select PersonID
,FirstName
,Surname
,OtherDetails
from PersonTable
where FirstName = #FirstName
and Surname = #Surname
If you would rather have more of a 'search' type function, you can use the SQL like operator and wildcards, though bear in mind this will have a potentially very detrimental effect on your query performance:
select PersonID
,FirstName
,Surname
,OtherDetails
from PersonTable
where FirstName like '%'+#FirstName+'%'
and Surname like '%'+#Surname+'%'
This part shows you how to change that parameter so it provides a drop down menu. This part is optional.
If you want to provide a list of available options to select from, you can create a parameter that has a list of 'Available Values'. These can either be manually typed in by yourself - hard coding them in to the report design - or you can make them data driven by basing the parameter on a second dataset.
To get that list of people, you would want to return the ID of the person you are looking for as well as the details that are end-user friendly to be visible in the report:
-- select distinct so we have no duplicates
select distinct PersonID as Value -- Value is what is used to the query. This ideally will be a uniquye identifier
,FirstName
+ ' '
+ Surname as Label -- Label is what the user sees. This can be a bit more verbose and detailed
from PersonTable
order by Surname -- Specify how you want the options ordered
,FirstName
If you set this dataset as the source by selecting Get Values From A Query in the parameter options, you will see the drop down list appear when you run the report. Users can then select one, click Run Report and have their selection impact the data that is returned.

ssrs 2008 sort data based upon parameter value

In an existing ssrs 2008 report, I have added a few new columns to the existing report as requested by the user. In addition, the user wants to be able to sort the data on any column on the report by using a sort parameter value.
The data does not contain any summary values. The columns in the report include:
a. student number,
b. student name,
c. birth date,
d. age,
e. current grade level,
d. attendance code value.
The default value is to sort by student name alphabetically. Whatever field is to be sorted first, the student name will be the second.
I have tried to sort the data at the tablix level and the row group level and the logic has not worked yet. There is no row group setup right now.
Thus could you show me how to setup the sort by using the parameter value? Would you show how the ssrs report needs to look for me to accomplish this goal and/or point me to link(s) that will show me how to accomplish this goal?
On the row group's Sorting, set the Sort By to use the Sort parameter to determine which column to sort, like:
=IIF(Parameters!SORT.Value = "NUMBER", Fields!STUDENT_NUMBER.Value,
IIF(Parameters!SORT.Value = "NAME", Fields!STUDENT_NAME.Value,
IIF(Parameters!SORT.Value = "BIRTH_DATE", Fields!BIRTH_DATE.Value,
IIF(Parameters!SORT.Value = "AGE", Fields!AGE.Value,
IIF(Parameters!SORT.Value = "GRADE", Fields!GRADE.Value, Fields!STUDENT_ATTENDANCE_CODE.Value)))))
Set a second SORT to use the Student_Name field.
Did you try using interactive sort. That way users can sort on any column in the table. In my experience this has worked perfectly well.
Here is more info- (the first section is what i think you would need - Sorting Detail Rows for a Table with No Groups ) -
https://technet.microsoft.com/en-us/library/cc627509(v=sql.100).aspx
Let me know if that was helpful.
Select the tablix, right click and click on Tablix Properties
Go to the Sorting Tab. Click Add (below the Change Sorting Options)
Now select the order or click expression and write an expression to sort the columns based on parameters supplied.
Click Ok
In the order select A to Z ASC or Z to A for DESC
You need to select each column and select A to Z or Z to A
For example
In the expression,
IIF(Parameters!SORT.Value = "NUMBER", Fields!STUDENT_NUMBER.Value,"")
In the order column select A to Z or Z to A

How to simulate a Group Header in an Access Continuous Form?

I have a form (not a report) in Access displayed in Continuous View that has alot of repeat data. i.e. it looks like:
State Names
FL Abe
FL Bart
FL Charlie
GA Deleanor
GA Mary
This needs to be interactive (there's some command buttons on each row as well as in the form header), but there's alot of repetitive data. Is there a way to add a group header by state? i.e. make it look like:
State Names
FL
Abe
Bart
Charlie
GA
Deleanor
Mary
I found a number of problems with the solutions posted. Most obviously there are issues (as noted) if there are duplicate entries. But I found there was a deeper issue which is that you can't do this and still sort the data on yet another field (suppose you wanted to sort by the date by which each name was first registered in each state, and yet NOT rank them that way).
A better solution is found in the Using Code section of the Microsoft KB:
http://support.microsoft.com/kb/q101081
The code from that section retrieves the actual data from the Previous field, in whatever sort order you define.
Computationally this can be a bit intense, so it works only for small datasets, but I achieved good results by first testing the function:
=PrevRecVal([Forms]![myForm],"ID",[ID],"Names")=[Names])
then putting it inside the IIF function:
=IIf(PrevRecVal([Forms]![myForm],"ID",[ID],"Names")=[Names],"",[FullName])
Have you looked at subdatasheets? If you have a state table (or just query your existing data for the unique states), you could use that in a non-editable parent datasheet, and then the subdatasheet would display the people data, and you could make it editable.
I'd implement this with datasheet forms, not with the actual table or query datasheets. This gives you a lot more control over the subdatasheet linking and formatting, as well as making interacting with the datasheet events easier (it can be done with a table or query datasheet, but only through Screen.ActiveDatasheet.
The closest I can get is to add a field to your form's record source which numbers each row within a group. Then you could add a text box in the detail section of your form with this expression as its control source.
=IIf(rank = 1, [State], ""]
The text box would display the first occurrence of each state, and a zero length string otherwise.
I tested with this query for the record source.
SELECT
m1.state,
m1.names,
(
SELECT Count(names)
FROM MyTable AS m2
WHERE
m2.state=m1.state
AND m2.names<=m1.names
) AS rank
FROM MyTable AS m1
ORDER BY m1.state, m1.names;
And it worked, but is not editable. If you need to be able to edit, try a query which uses DCount to generate the rank.
SELECT
m1.state,
m1.names,
DCount("names", "MyTable",
"state ='" & state & "' AND " & "names <= '" & names & "'") AS rank
FROM MyTable AS m1
ORDER BY m1.state, m1.names;
Both those queries give this result set with your sample data:
state names rank
FL Abe 1
FL Bart 2
FL Charlie 3
GA Deleanor 1
GA Mary 2
Note, I assumed unique combinations of state and name.