So, simple question, I have combo box in my database and I want items that I have selected most frequently to appear first the next time that I add a record.
I would suggest adding a Long Integer field to the table consituting the row source for your combobox, and incrementing the value held by such field either on the AfterUpdate event of the combobox, or following the main operation being performed by your form.
Then, sort the items in the combobox by this new field in descending order.
I assume that your combo box selects a lookup value in for a property (PropertyID) that is saved in a table (MainTable).
You can get the number of times this property has been selected with
SELECT PropertyID, COUNT(*) AS SelectedTimes
FROM MainTable
GROUP BY PropertyID
Now get the sorted lookup table by using this query as sub-query:
SELECT L.PropertyID, L.Name
FROM
LookupTable L
( SELECT PropertyID, COUNT(*) AS SelectedTimes
FROM MainTable
GROUP BY PropertyID) X
ON L.PropertyID = X.PropertyID
ORDER BY X.SelectedTimes DESC, L.Name
I'm also sorting by name in case two entries have the same count.
As usage may change over time, you should record the time when an item was selected. Then you can weight the usage, so recent usage of an item have higher weight than those items used, say, a year ago.
Then you can run a query to list the usage having the most recently used items at top:
Select Item, Sum(1 / DateDiff("h", [SelectedTime], Now())) As Usage
From ItemUsage
Group By Item
Order By Sum(1 / DateDiff("h", [SelectedTime], Now())) Desc
Of course, this linear weighting may be too simple. You can apply any math to the usage like square or log.
Related
I'm new to tableau.
I have a data like this.
[department]
[employee]
And I want to get employee count under 30 age per department. like this..
[my goal]
I want to show all departments (if there is no employee)
To get result, I do these steps,
make relationship of dept - emp (one datasource)
make datasource filter (emp.age < 30)
make sheet and set row as dept_no, countd(emp_no)
But I could only get like this.
[my result]
How can I get my goal..!!??
Help me!!
It is simple hereafter. Use join (instead of relationship) on the side of dept table and Take dept_no column from dept_table instead of emp_table. To do this follow these steps
double click on first added table (say emp). A join window will open (thgis differs from relationship)
add dept table and use right join
right click age convert to dimension
right click again age and convert to continuous
create filter at most 29 on age and don't forget to check include null values in the table
If you want to add all values in view. create a calc field with calculation as
INT([Age] <30)
add sum of this field to view
edit the calculation to show all nulls as 0
ZN(INT([Age] <30))
I have the following sample from a table with students results with date for a school entry exam
First student passed exam - This is the most common record found for most students
Second student failed 1st time entry and passed second time based on the date
3rd student had a failed input entry and was corrected based on the Version
I need the results to like like the picture above, so we take into regard using the latest date and highest version!
My basic query thus far is
select studentid
,examdate --(Date)
,result -- (charvar)
from StudentEntryExam
How should I approach this issue?
demo:db<>fiddle
SELECT DISTINCT ON (studentid)
*
FROM mytable
ORDER BY studentid, examdate DESC, version DESC
DISTINCT ON returns the first record of an ordered group. In that case the groups are the studentids. You must find the correct order to set the required record first. So, you need to order by studentid, of course. Then you need the most recent examdate first, which can be achieved with DESC order. If there are two records on the same date, you need to order the highest version first as well using the DESC modifier, too.
I want sum of only unique values in SSRS report.Is there any logic to achieve this.
this is what something like this sum(distinct value)
Thanks
Supposed you have a column named value in your query, you could do the following:
Add an additional column to the query of the dataset:
ROW_NUMBER() OVER (PARTITION BY [value] ORDER BY [value]) AS valueNr
Then, if you already have created a Sum field in the table, change the expression of the textbox to
=Sum(Iif(Fields!valueNr.Value=1, Fields!value.Value, 0))
Repeat this for every "distinct sum" calculation.
Yes. You use groups. At the top click the insert menu, then table, then Table Wizard. Pick your dataset and hit next. Now drag the column for the different types of items you want a distinct sum of into the Row Groups section. Drag your count column into the Values section. This should automatically turn it into Sum(ColumnName). You can click the down arrow to change the aggregate type (if desired). Press next and next and finish. Viola. You have a distinct sum for each specified field.
I am trying to sort the retrieved data in the result grid in MySQL Workbench.
E.g. with the query
SELECT * FROM deepmining.prediction
WHERE (search_id = 39 OR search_id = 41) AND lead = 10 AND lag = 10 ;
I get in the result grid, when I sort on the column scale and timeshift (sorting using the result grid UI, not modifying the SQL query):
Shouldn't the first two rows be:
instead, just as if I had appended ORDER BY timeshift, scale; to the SQL query?
Or does sort MySQL Workbench sorting on multiple columns, despite what the UI seems to indicate by the presence of an arrow on the columns scale and timeshift?
The sort order depends on the order how you clicked the headers. The first click is used for the primary sort, the second for the sorting within the primary and so on. See the following images:
Sorted by first clicking on last_name then first_name.
Sorted by first clicking on first_name then last_name.
I have a report that shows items by goups.
Lets say he groups are:
In Inventory
In Process
When there is no data In Inventory, that row is not shown. any idea how to show a row with 0 inventory?
It sounds as though you are grouping on a field (such as stock item status), where there may be 0 rows returned for certain values of the field (such as In Inventory). The answer is to amend your query to right outer join to a lookup table holding all values of the grouping field, for example as follows:
select lu.status_value stock_item_status,
si.stock_item_status item_status,
si.stock_item_id,
coalesce(si.quantity,0) quantity
from stock_item si
right join stock_item_status lu
on si.stock_item_status = lu.status_value
This will now include a row returned for stock_item_status values with no corresponding stock_items, with null values for all of the stock_item fields.
If you were including a subtotal of stock_item.quantity values for each status, changing this to coalesce(...,0) should ensure that this null value is displayed as 0.
If I'm understanding you correctly, you've got a field {table.inventory} that holds the number of items in inventory that has a null value when the inventory is zero? When you group on this field you're not seeing the rows with null values in this field?
Crystal should still display those rows, just in a group with a null group name. To fix this you can go into the Group Expert -> Select the group in question -> Options -> Options tab -> Select 'customize group name field' -> and then specify a formula as a new group name field where you simply check to see
if isnull({table.inventory}) then 0 else {table.inventory}
Similarly, you can just create a formula that does this and group on that instead.