Building a query in Access to work with time - ms-access

This is my second post and it is about the same subject, I start to work with Access a little time, so I need your help to build one query that have some variables.
The scenarios
I need to do a query to show how many time one person remained inside the office, but the different of my first post is I need to see the details of operations.
The main list will be create one item when a person entry in the office and create other item when the person out the office, but exist some variables
1 – I have more one building
2 – Each building have a lot of turnstiles, and I need to define only the turnstile of main hall
3 – Some times we have a system problems, and the system not create the item of entry / out
Follow an example of list
ID Full Name Date Time Status Turnstile Office
1 Andre Silva 02/25/2013 09:00 AM In 03 SP
2 Andre Silva 02/25/2013 09:10 AM In 05 SP
3 Andre Silva 02/25/2013 12:00 PM Out 06 SP
4 Andre Silva 02/25/2013 12:10 PM Out 02 SP
5 Andre Silva 02/25/2013 14:00 PM In 02 SP
6 Andre Silva 02/25/2013 14:10 PM In 05 SP
7 Andre Silva 02/25/2013 19:00 PM Out 06 SP
8 Andre Silva 02/25/2013 19:10 PM Out 02 SP
9 Andre Silva 02/26/2013 14:00 PM In 03 BH
10 Andre Silva 02/26/2013 14:10 PM In 05 BH
11 Andre Silva 02/26/2013 23:55 PM Out 06 BH
12 Andre Silva 02/27/2013 01:10 AM Out 03 BH
13 Andre Silva 02/28/2013 09:00 AM In 03 RJ
14 Andre Silva 02/28/2013 09:10 AM In 05 RJ
15 Andre Silva 02/28/2013 12:00 PM Out 05 RJ
16 Andre Silva 02/29/2013 11:00 AM In 05 SP
17 Andre Silva 02/29/2013 17:10 PM Out 06 SP
18 Andre Silva 02/29/2013 17:20 PM Out 03 SP
Based in the main list I need create a query with this format
Full Name Date Turnstile Time Date Turnstile Time Office Total Time
Andre Silva 02/25/2013 03 09:00 AM 02/25/2013 02 12:10 PM SP 03:10
Andre Silva 02/25/2013 02 14:00 PM 02/25/2013 02 19:10 PM SP 05:10
Andre Silva 02/26/2013 03 14:00 PM 02/27/2013 03 01:00 AM BH 11:00
Andre Silva 02/28/2013 03 09:10 AM RJ Not found a records of out
Andre Silva 02/29/2013 03 17:20 PM SP Not found a records of in
I need see the interval between the in/out of turnstile 02/03 in the same office, and when one item of entry/out not found the answer should be 0.
I tried to use the standard queries of Access, like a crosstab or tried to create an expression with IFF, but no have success.
Thanks a lot
André

it is not answer but i have no time and this query may help you
SELECT
t1.FullName,
t1.Office,
t1.DateTime,
t1.status,
sum(IIf(isnull(t2.status),0,IIf(t2.status="In",1,-1))) AS deep_before,
sum(IIf(isnull(t2.status),0,IIf(t2.status="In",1,-1)))
+IIf(t1.status="In",1,-1) AS deep_after
FROM Table1 AS t1
LEFT JOIN Table1 AS t2 ON (t1.FullName=t2.FullName)
AND (t1.Office=t2.Office) AND (t1.Datetime>t2.DateTime)
GROUP BY t1.FullName, t1.Office, t1.DateTime, t1.status;
tomorrow may be i see it again

Related

Is it possible to retrieve Location wise data of product in mysql?

id
product
location
year
month
order_share
1
DAP
Portland
2022
Jan
12.55
2
DAP
Portland
2022
Feb
22
3
DAP
Portland
2022
Mar
15
4
DAP
Portland
2022
Apr
522
5
DAP
Portland
2022
May
55
----------------------------------------------------

SQL sum and show row with non-existent sum values

i have 2 tables : dt_user and dt_invoice.
**dt_members :**
id firstname
3 Salim
5 Sara
8 Julie
**dt_invoice**
user_id amount_ht period month year
3 4950 04 2018 04 2018
3 7200 10 2018 10 2018
8 11000 10 2018 10 2018
8 5500 11 2018 11 2018
3 6750 11 2018 11 2018
3 8700 12 2018 12 2018
3 8800 01 2019 01 2019
8 7500 01 2019 01 2019
3 4950 02 2019 02 2019
3 7550 03 2019 03 2019
I want to create a query joining the two table, but i want to show each user_id for PERIOD that there is in table dt_invoice.
**Expected results :**
user_id amount_ht period month year
3 4950 04 2018 04 2018
5 0 04 2018 04 2018 //non-existent record in dt_invoice
8 0 04 2018 04 2018 //non-existent record in dt_invoice
3 7200 10 2018 10 2018
5 0 10 2018 10 2018 //non-existent record in dt_invoice
8 11000 10 2018 10 2018
8 5500 11 2018 11 2018
5 0 11 2018 11 2018 //etc ...
3 6750 11 2018 11 2018
3 8700 12 2018 12 2018
5 0 12 2018 12 2018
8 0 12 2018 12 2018
3 8800 01 2019 01 2019
5 0 01 2019 01 2019
8 7500 01 2019 01 2019
3 4950 02 2019 02 2019
5 0 02 2019 02 2018
8 0 02 2019 02 2018
3 7550 03 2019 03 2019
5 0 03 2019 03 2018
8 0 03 2019 03 2018
Thanks in advance for your help, i'm totally stuck ..
SQL datas available here : https://rextester.com/live/LBSEY76360
also in sqlfiddle : http://sqlfiddle.com/#!9/728af3/1
Use a cross join to generate the rows and left join to bring in the values:
select m.user_id, p.period, p.month, p.year,
coalesce(t.amount_ht, 0) as amount_ht
from dt_members m cross join
(select distinct period, month, year from dt_invoice) p left join
dt_invoice t
on t.user_id = m.id and t.period = p.period;
Maybe this would help.
SELECT user_id, amount_ht, period, month, year
FROM dt_invoice
LEFT JOIN dt_members ON user_id = id

MySql Daily Report

I am trying to get some help fixing my complex query. I am explaining below my situation, thanks.
I have the following two tables:
ACTIVITY TABLE:
ID USER_ID CARD_ID CLOCK
1 123 04675545 4/3/2013 1:07:06 PM
2 123 04675545 4/3/2013 2:08:06 PM
3 124 04675550 4/3/2013 2:07:06 PM
4 124 04675550 4/3/2013 4:07:06 PM
5 124 04675550 4/4/2013 10:07:06 AM
6 124 04675550 4/4/2013 2:00:00 PM
7 124 04675550 4/5/2013 4:07:06 PM
8 124 04675550 4/7/2013 8:00:00 AM
9 124 04675550 4/7/2013 5:00:00 PM
PRICE TABLE:
ID FROMTIME TOTIME PRICEPERHOUR
1 08:00:00 19:59:59 50.00
2 20:00:00 07:59:59 75.00
And the following query:
select a.user_id, date(a.clock), ABS(TIME_TO_SEC(TIMEDIFF(a.clock, b.clock))/3600)*c.PRICEPERHOUR as total from
(Select if((#rn:=#rn+1)%2=0,#rn,#rn-1) As rId, act.* from act
join (select #rn:=-1)a
order by user_Id, clock) a
inner join
(Select if((#rn1:=#rn1+1)%2=0,#rn1,#rn1-1) As rId, act.* from act
join
(select #rn1:=-1)b
order by user_Id, clock) b
ON a.rid=b.rid AND a.id <> b.id
inner join
price c
on
TIME_TO_SEC(a.clock) between TIME_TO_SEC(c.FROMTIME)
AND
TIME_TO_SEC(c.TOTIME)
group by a.user_id, date(a.clock)
And I am getting the following result:
USER_ID DATE(A.CLOCK) TOTAL
123 April, 03 2013 00:00:00+0000 50.8333
124 April, 03 2013 00:00:00+0000 100
124 April, 04 2013 00:00:00+0000 194.0833
124 April, 05 2013 00:00:00+0000 1,994.0833
124 April, 07 2013 00:00:00+0000 1,994.0833
However, I am trying to get this result instead:
USER_ID DATE(A.CLOCK) TOTAL
123 April, 03 2013 00:00:00+0000 50.8333
124 April, 03 2013 00:00:00+0000 100
124 April, 04 2013 00:00:00+0000 194.0833
124 April, 05 2013 00:00:00+0000 50
124 April, 07 2013 00:00:00+0000 450
This is part of a clock system. Each time the user check-in, one entry gets recorded on the database. A correct user behavior will be that it has always a pair record recorded. For example user_id 123 clocks at 1:07:06pm and clocks again at 2:08:06pm. However, in some situations, the user may do it just once (unpaired record on the database) and therefore it should only be charged that particular hour from the record. As an example, user 124 on day 4/5/2013.
I am trying all weekend to get this query working :(. Once I get the correct result, I will add a condition to get only one user_id also, (e.g. where user_id=124).
I think even if you manage to do this there are are some potential design pitfalls:
Can people clock in for more than 1 period per day? If so then 2 records for example
10am and 2pm could total 2hours or 4hours.
What happens if people clock in at 11pm and again at 2am?
From a quick glance I don't think your sql takes into account time periods that span across the 2 different pay rates? You should definitely include this scenario in your test data.
If I was going to implement this I would probably move the logic into code, and simplify the price table by only having one time column like:
TIME, PRICE
00:00, 75.00
08:00, 50.00
20:00, 75.00
Also if a user only has one card you may not need to have card_id and user_id in the activity table.

MySQL - Compare total this week against same week last year

How would I get a sum of sales totals for the current week against the same week last year?
There are two possible scenarios related to how the dates are stored, as below:
Scenario 1
**Sales**
Date Sales
-----------------------
2012-08-10 11040.00
2012-08-09 11500.00
2012-08-08 14060.00
2012-08-07 93000.00
2012-08-06 11200.00
...
2011-08-10 11040.00
2011-08-09 11500.00
2011-08-08 14060.00
2011-08-07 93000.00
2011-08-06 11200.00
Scenario 2
**Sales**
year month day Sales
---------------------------------------------
2012 08 10 11040.00
2012 08 09 11500.00
2012 08 08 14060.00
2012 08 07 23000.00
2012 08 06 11200.00
...
2011 08 10 13040.00
2011 08 09 11500.00
2011 08 08 12060.00
2011 08 07 33000.00
2011 08 06 11250.00
For your first scenario, join against the same table on the WEEKOFYEAR() and one added to last year's YEAR():
SELECT
YEARWEEK(thisyear.Date) AS `YearWeek`
SUM(lastyear.Sales) AS LastYearSales
SUM(thisyear.Sales) AS ThisYearSales
FROM
Sales thisyear
LEFT JOIN Sales lastyear ON
WEEKOFYEAR(thisyear.Date) = WEEKOFYEAR(lastyear.Date)
AND YEAR(thisyear.Date) = (YEAR(lastyear.Date) + 1)
GROUP BY `YearWeek`
The second scenario requires building a date out of the 3 separate values. I think this will work:
SELECT
YEARWEEK(CONCAT_WS('-', thisyear.year, thisyear.month, thisyear.day)) AS `YearWeek`,
SUM(lastyear.Sales) AS LastYearSales,
SUM(thisyear.Sales) AS ThisYearSales
FROM
Sales thisyear
LEFT JOIN Sales lastyear ON
WEEKOFYEAR(CONCAT_WS('-', thisyear.year, thisyear.month, thisyear.day)) = WEEKOFYEAR(CONCAT_WS('-', lastyear.year + 1, lastyear.month, lastyear.day))
GROUP BY `YearWeek`

MySQL: I want to sum my employees cost and group it by employee and by day

I currently have a table where I record all my employee daily expenses
John Smith 01 JAN 2010 200$
John Smith 01 JAN 2010 50$
John Smith 01 JAN 2010 10$
Lady Gaga 01 JAN 2010 50$
Lady Gaga 01 JAN 2010 20$
John Smith 02 JAN 2010 10$
What I would like to display a table that contains a daily report of all bills:
01 JAN 2010
John Smith: 260$
Lady Gaga: 70$
02 JAN 2010
John Smith: 10$
I really hope my request is understandable and you will be able to help me.
Thank a lot for any advice!
I would use a standard group-by:
select date, name, sum(amount) as total
from mytable
group by 1,2
order by 1,2;
use SUM with GROUP BY , do something like
SELECT date,user,SUM(cost)
FROM mytable
GROUP BY date,user
ORDER BY date