Access 2013 Query Count - ms-access

I have to do a query that shows training completion dates for all employees. The end result would be the number of employees that attended company trainings each quarter.
I have setup the query to have the user enter a start and end date for it (example 1/1/17 - 3/31/17) which works and a total row which also works. The issue I am having is I only want the count to include the records that fall within that range.
EXAMPLE
Employee Name Training1 Training2 Training3 Training4
Noname 07/06/17 1/12/17 2/15/17 6/6/17
COUNT 1 1 1 1
I know the query above is right because 2 pieces of the record fit the puzzle however, 2 pieces do not. How can I make my query work to only count what fits in the given date range? So COUNT should be 0, 1, 1, 0?

Related

How to place 3 continuous row in to the 3 stacked row in single access report

I have multiple employee's salary data in tabular format for different months as displayed in the 3rd and 2nd image. (Presently extracted for a single employee in the following images example). There are 35 salary fields/columns for each employee and salary heads are different for different type of job profile. 3rd and 2nd images represent salary data table for all job profile employee. Because the table has many columns so I posted only two part of a single table in third and second images. There are also other fields which have 0 values or more than 0 as per the employee's job profile. I created a query to extract quarterly data for a single employee from the table as per the third and second images.
The first image is the result, which I want to arrange as the stacked format. Presently my report showing only single month data in the this report. There is report load event code to show only heads which have values more than 0 as following:
Dim C As Control
For Each C In Me.Report
If TypeOf C Is TextBox Then
If IsNull(C) Or C = "" Or C = 0 Or C = "0" Then
C.Visible = False
C.Height = 0
Else
C.Visible = True
End If
End If
I want to display the result as per the report images as follows. Is it possible to place 3 stacked column? thanks for the help and any suggestions.
Example images:
You can implement the following SQL query to a salary per employee per quarter considering that your table contains only one-year salaries
SELECT
EmployeeName
,SUM(IIF(Month([DateColumn]) BETWEEN 1 AND 3, Salary, 0)) AS Salary_Q1
,SUM(IIF(Month([DateColumn]) BETWEEN 4 AND 6, Salary, 0)) AS Salary_Q2
,SUM(IIF(Month([DateColumn]) BETWEEN 7 AND 9, Salary, 0)) AS Salary_Q3
,SUM(IIF(Month([DateColumn]) BETWEEN 10 AND 12, Salary, 0)) AS Salary_Q4
FROM TblSalary
GROUP BY EmployeeName

Applying row over row conditional logic in aggregate functions in SQL

I have two tables stored in a MySQL database. The first contains pricing information about SKUs I want to sell tomorrow. The second contains historical sales and visitation data for SKUs. What I want to do is calculate the conversion rate (orders divided by visits) for each SKU at the price specified in the tomorrow's pricing table based on prior sales data at tomorrow's specified price, conditioned that the SKU at that price has at least 100 visits OR 50 visits and 1 order, and if not then I want it to add the visits and orders from the next highest price point.
I've created a table containing example data in an SQL fiddle
http://www.sqlfiddle.com/#!9/bf4273
Using the example data in the sqlfiddle, what I want to return then would be
ABC1, 100.0, 100, 0
ABC2, 75.0, 24, 0
ABC3, 35.0, 1312, 57
ABC4, 190, 55, 1
ABC5, 250, 72, 1
ABC6, 250.0, 80, 1
If you examine the data in the sql fiddle, you can see that ABC1 doesn't meet the count requirements at the specified $100 price point, so it rolls up the visits from the next higher price point, in that case 110, but keeps the reported price at 100.
ABC2 doesn't have enough visits at the specified price, but there aren't any visits at any other price point higher than the specified one, so it just returns the 24 and 0 visits.
ABC3 meets the requirements, so it just returns the specified value
ABC4 meets the > 50 visits and > 1 sale, so it's returned
ABC5 doesn't have enough visits and orders at the specified price, so it sums up the visits and orders to include the next highest price point. This logic is re-applied again if rolling up to the next highest price point still doesn't meet the requirements for 100 visits or 50 visits and 1 order.
ABC6 is similar to ABC5 except that now the orders at the specified price point for ABC6 for tomorrow is zero, but we still take the orders count from the next highest price point too (e.g. we sum up visits and orders from the next highest price point until we meet the necessary visits and/or orders requirements).
At the moment I've applied this logic inside Python, but it's a bit cumbersome and if possible I'd like to have it all in SQL. Can anyone help? Even telling me it's not possible to do this in MySQL would be valued.
Thanks,
Brad

SQL Report Builder for Survey Data

I have a large dataset with People's names and their Rating from 1 to 5.
Then I made a query that summarizes this data for PersonA:
Rating Count
------- ------
1 4
2 6
3 1
4 0
5 2
I just need to know how to show this on my report.
I have made a cell for each rating and need to put in an expression that says "If Rating=1, show count for rating 1".
I tried using =IIf(Fields!Rating.Value = 1, Fields!Count.Value, 0) but this didn't work.
I'm not sure why you would need an expression like that, based on your description of the dataset it sounds like you already have two columns of data for rating and count, so you could use a tablix (table), with columns:
Rating Count
which would list all the rating values and associated count values, similar to the example result in your question.

Reporting Services How to get the Count() value from IIF

I have this table with a column named Open_Time which contain a datetime value. I would like to have another column named Total Ticket In June with the total count of ticket in June, so I've inserted the expression like below:
=Count((IIF(DatePart("m",Fields!Open_Time.Value,0,0) = "6",1,0)))
but there seems to be an error. To make myself clear, the table should look like this:
Assigned Name Ticket ID Open_Time Total Ticket in June
Ivan 001 3/28/2014 2
002 6/24/2014
003 6/11/2014
I would like to get value "2", which is the total number of ticket in June. Any idea? :)
You need to switch to a Sum aggregate instead of Count (based on your IIF, Count will count every row instead of giving you a total number of occurences):
=Sum(IIF(DatePart("m",Fields!Open_Time.Value,0,0) = 6, 1, 0))
From your table it's hard to tell your row groupings, but keep in mind that you'll only get the sum you expect if that expression is on a totals row (i.e. if you use that expression on the detail row it will simply list 1 or 0 for each date).
Try this:
=Count((IIF(DatePart(dateinterval.Month,Fields!Open_Time.Value,0,0) = 6,1,0)))

MySQL - The most occuring for the specific day?

I'm stuck on this problem.
Basically I need to find out for each department how to figure out which days had the most sales made in them. The results display the department number and the date of the day and a department number can appear several times in the results if there were several days that have equally made the most sales.
This is what I have so far:
SELECT departmentNo, sDate FROM Department
HAVING MAX(sDate)
ORDER BY departmentNo, sDate;
I tried using the max function to find which dates occurred most. But it only returns one row of values. To clarify more, the dates that has the most sales should appear with the corresponding column called departmentNo. Also, if two dates for department A has equal amount of most sales then department A would appear twice with both dates showing too.
NOTE: only dates with the most sales should appear and the departmentNo.
I've started mySQL for few weeks now but still struggling to grasp the likes of subqueries and store functions. But i'll learn from experiences. Thank you in advance.
UPDATED:
Results I should get:
DepartmentNo Column 1: 1 | Date Column 2: 15/08/2000
DepartmentNo Column 1: 2 | Date Column 2: 01/10/2012
DepartmentNo Column 1: 3 | Date Column 2: 01/06/1999
DepartmentNo Column 1: 4 | Date Column 2: 08/03/2002
DepartmentNo Column 1: nth | Date Column 2: nth date
These are the data:
INSERT INTO Department VALUES ('1','tv','2012-05-20','13:20:01','19:40:23','2');
INSERT INTO Department VALUES ('2','radio','2012-07-22','09:32:23','14:18:51','4');
INSERT INTO Department VALUES ('3','tv','2012-09-14','15:15:43','23:45:38','3');
INSERT INTO Department VALUES ('2','tv','2012-06-18','06:20:29','09:57:37','1');
INSERT INTO Department VALUES ('1','radio','2012-06-18','11:34:07','15:41:09','2');
INSERT INTO Department VALUES ('2','batteries','2012-06-18','16:20:01','23:40:23','3');
INSERT INTO Department VALUES ('2','remote','2012-06-18','13:20:41','19:40:23','4');
INSERT INTO Department VALUES ('1','computer','2012-06-18','13:20:54','19:40:23','4');
INSERT INTO Department VALUES ('2','dishwasher','2011-06-18','13:20:23','19:40:23','4');
INSERT INTO Department VALUES ('3','lawnmower','2011-06-18','13:20:57','20:40:23','4');
INSERT INTO Department VALUES ('3','lawnmower','2011-06-18','11:20:57','20:40:23','4');
INSERT INTO Department VALUES ('1','mobile','2012-05-18','13:20:31','19:40:23','4');
INSERT INTO Department VALUES ('1','mouse','2012-05-18','13:20:34','19:40:23','4');
INSERT INTO Department VALUES ('1','radio','2012-05-18','13:20:12','19:40:23','4');
INSERT INTO Department VALUES ('2','lawnmowerphones','2012-05-18','13:20:54','19:40:23','4');
INSERT INTO Department VALUES ('2','tv','2012-05-12','06:20:29','09:57:37','1');
INSERT INTO Department VALUES ('2','radio','2011-05-23','11:34:07','15:41:09','2');
INSERT INTO Department VALUES ('1','batteries','2011-05-21','16:20:01','23:40:23','3');
INSERT INTO Department VALUES ('2','remote','2011-05-01','13:20:41','19:40:23','4');
INSERT INTO Department VALUES ('3','mobile','2011-05-09','13:20:31','19:40:23','4');
For department1 the date 2012-05-18 would appear because that date occurred the most. And for every department, it should only show the one with the most sales, and if same amount of sales appears on the same date then both will appear, e.g. Department 1 will appear twice with both the dates of max sales.
I've tested the following query based on the table and two columns you've provided along with sample data. So, let me describe it for you. The inner-most "PREQUERY" is doing a count by department and date. The results of this will be pre-ordered by Department first, THEN the highest count in DESCENDING ORDER (so highest sales count is listed FIRST), it doesn't matter what date the count happened.
Next, by utilizing MySQL #variables, I'm pre-declaring two to be used in the query. #variables are like inline programming with MySQL. They can be declared once and then changed as applied to each record being processed. So, I'm defaulting to a bogus department value and a zero sales count.
Now, I'm grabbing the results of the PreQuery (Dept, #Sales and Date), but now, adding a test. If it is the FIRST ENTRY for a given department, use that record's "NumberOfSales" and put into the #maxSales variable and store as a final column name "MaxSaleCnt". The next column name uses the #lastDept and is set to whatever the current record's Department # is. So it can be compared to the next record.
If the next record is the same department, then it just keeps whatever the #maxSales value was from the previous, thus keeping the same first count(*) result for ALL entries on each respective department.
Now, the closure. I've added a HAVING clause (not a WHERE as that restricts what records get tested, but HAVING processes AFTER the records are part of the PROCESSED set. So now, it would have all 5 columns. I am saying ONLY KEEP those records where the final NumberOfSales for the record MATCHES the MaxSaleCnt for the department. If one, two or more dates, no problem it returns them all per respective department.
So, one department could have 5 dates with 10 sales each, and another department has 2 dates with only 3 sales each, and another with only 1 date with 6 sales.
select
Final.DepartmentNo,
Final.NumberOfSales,
Final.sDate
from
(select
PreQuery.DepartmentNo,
PreQuery.NumberOfSales,
PreQuery.sDate,
#maxSales := if( PreQuery.DepartmentNo = #lastDept, #maxSales, PreQuery.NumberOfSales ) MaxSaleCnt,
#lastDept := PreQuery.DepartmentNo
from
( select
D.DepartmentNo,
D.sDate,
count(*) as NumberOfSales
from
Department D
group by
D.DepartmentNo,
D.sDate
order by
D.DepartmentNo,
NumberOfSales DESC ) PreQuery,
( select #lastDept := '~',
#maxSales := 0 ) sqlvars
having
NumberOfSales = MaxSaleCnt ) Final
To clarify the "#" and "~" per you final comment. The "#" indicates a local variable to the program (or in this case and in-line sql variable) that can be used in the query. The '~' is nothing more than a simple string that probability would never exist that of any of your departments, so when it is compared to the first qualified record, does an IF( '~' = YourFirstDepartmentNumber, then use this answer, otherwise use this answer).
Now, how do the above work. Lets say the following is the results of your data returned by the inner-most query, grouped and ordered by the most sales at the top going down... SLIGHTLY altered from your data, lets just assume the following to simulate multiple dates on Dept 2 that have the same sales quantity...
Row# DeptNo Sales Date # Sales
1 1 2012-05-18 3
2 1 2012-06-18 2
3 1 2012-05-20 1
4 2 2012-06-18 4
5 2 2011-05-23 4
6 2 2012-05-18 2
7 2 2012-05-12 1
8 3 2011-06-18 2
9 3 2012-09-14 1
Keep track of the actual rows. The innermost query that finishes as alias "PreQuery" returns all the rows in the order you see here. Then, that is joined (implied) with the declarations of the # sqlvariables (special to MySQL, other sql engines dont do this) and starts their values with the lastDept = '~' and the maxSales = 0 (via assignment with #someVariable := result of this side ).
Now, think of the above being handled as a
DO WHILE WE HAVE RECORDS LEFT
Get the department #, Number of Sales and sDate from the record.
IF the PreQuery Record's Department # = whatever is in the #lastDept
set MaxSales = whatever is ALREADY established as max sales for this dept
This basically keeps the MaxSales the same value for ALL in the same Dept #
ELSE
set MaxSales = the # of sales since this is a new department number and is the highest count
END IF
NOW, set #lastDept = the department you just processed to it
can be compared when you get to the next record.
Skip to the next record to be processed and go back to the start of this loop
END DO WHILE LOOP
Now, the reason you need to have the #MaxSales and THEN the #LastDept as returned columns is they must be computed for each record to be used to compare to the NEXT record. This technique can be used for MANY application purposes. If you click on my name, look at my tags and click on the MySQL tag, it will show you the many MySQL answers I've responded to. Many of them do utilize # sqlvariables. In addition, there are many other people who are very good at working queries, so dont just look in one place. As for any question, if you find a good answer that you find helpful, even if you didn't post the question, clicking on an up-arrow next to the answer helps others indicate what really helped them understand and get resolution to questions -- again, even if its not your question. Good luck on your MySQL growth.
I think this can be achieved with a single query, but my experiences for similar functionality have involved either WITH (as defined in SQL'99) using either Oracle or MSSQL.
The best (only?) way to approach a problem like this is to break in into smaller components. (I don't think your provided statement provides all columns, so I'm going to have to make a few assumptions.)
First, how many sales were made for each day for each group:
SELECT department, COUNT(1) AS dept_count, sale_date
FROM orders
GROUP BY department, sale_date
Next, what's the most sales for each department
SELECT tmp.department, MAX(tmp.dept_count)
FROM (
SELECT department, COUNT(1) AS dept_count
FROM orders
GROUP BY department
) AS tmp
GROUP BY tmp.department
Finally, putting the two together:
SELECT a.department, a.dept_count, b.sale_date
FROM (
SELECT tmp.department, MAX(tmp.dept_count) AS max_dept_count
FROM (
SELECT department, COUNT(1) AS dept_count
FROM orders
GROUP BY department
) AS tmp
GROUP BY tmp.department
) AS a
JOIN (
SELECT department, COUNT(1) AS dept_count, sale_date
FROM orders
GROUP BY department, sale_date
) AS b
ON a.department = b.department
AND a.max_dept_count = b.dept_count