Catching errors in MS Access - ms-access

I am a beginner with MS Access and I am having some issues. I have created a query with 2 different dates (say x and y) and then a formula to calculate the days between these two dates - column C. I am now looking to only pull through results where the criteria of column c is greater than 30, however it is creating an error as some of the dates in x and y are missing at table level. Is there a way to catch this error before the query? For example, the Excel equivalent of iferror("-").

You can use DateDiff and filter for Not Null to ignore those records:
Select *
From YourTable
Where
DateDiff("d", [x], [y]) > 30
And
[x] Is Not Null And [y] Is Not Null
Should that still fail, use Nz:
Select *
From YourTable
Where
DateDiff("d", Nz([x], Date()), Nz([y], Date())) > 30
And
[x] Is Not Null And [y] Is Not Null

Related

I am trying to SELECT this 3 columns shown in one column using the IF statement or the concatinate

Here is the data I want to convert into SQL
Seetle_date(1st Column)
days_to_settle (2nd Column)
days_late (3rd Column)
Settle_Status (outcome = 1column)
1/8/2021
11
0
Settle on Time
11/5/2021
16
0
<15 Days Late
10/11/2020
31
1
<30 Days Late
I don't know what function need to do this;
CASE STAEMENT, GROUP BY,
ORDER BY
I Got this FORMULA in EXCEL to Understand what I want.
=IF(AND(B2-C2\<=15,B2-C2\>=0),"Settle on Time",IF(AND(B2-C2\<=30,B2-C2\>15),"\<15 Days Late","\<30 Days Late"))
I need to convert this in sql.
CASE
WHEN (B2 - C2 <= 15 AND B2 - C2 >= 0) THEN 'Settle on Time'
WHEN (B2 - C2 <= 30 AND B2 - C2 > 15) THEN '<15 Days Late'
ELSE '<30 Days Late'
END
This query uses the CASE statement to handle the conditional logic. The WHEN clauses define the conditions to be checked, and the expressions after the THEN keywords specify the values to be returned if the conditions are met. The ELSE clause provides a fallback value if none of the conditions are met.

Multiple queries in phpmyadmin - Distance using coordinates, Slope, Intercept, Angle, and few more

I having around 500 excel sheets in .csv format with data captured for my experiment having following columns in place.
Now I need to calculate the following parameters using this data. I have done these in excel, however doing this repeatedly for each excel so many times is difficult, so I want to write an SQL query in PhpmyAdmin will help some time.
Last charecter typed - need to capture last charecter from the column 'CharSq'
Slope (in column J) =(B3-B2)/(A3-A2)
Intercept (in column K) =B2-(A2*(J3))
Angle (in degrees) =MOD(DEGREES(ATAN2((A3-A2),(B3-B2))), 360) -
Index of Difficulty =LOG(((E1/7.1)+1),2)
Speed Value length (if speed value length >3, then mark as 1 or else 0) = =IF(LEN(D3) >= 3, "1","0")
Wrong Sequence (if I3=I2,then mark search time, else actual time) =IF(I3=I2,"Search Time","Actual Time")
Mark charecter into (1,2,3) = =IF(I2="A",1, IF(I2="B",2, IF(I2="C",3, 0)))
I have started with this SQL query
SELECT id, type, charSq, substr(charSq,-1,1) AS TypedChar, xCoordinate, yCoordinate, angle, distance, timestamp, speed FROM table 1 WHERE 1
Need help for the rest of the parameters. Thanks.
Note - I am going to run this in phpMyAdmin SQL
create table test.Table10 select mm.myid,mm.id,mm.type1 as GESTURE,MM.CHARSQ,MM.TYPE2 as TYPEDCHAR,MM.MYCHAR,MM.XCOR,MM.YCOR,MM.SLOPE,l4-(l2*(SLOPE)) as Intercept,
if (ANGLE1<0, (ANGLE1+360) , ANGLE1 ) as ANGLE0,MM.DISTANCE,MM.DW,MM.INDDIFF,MM.TIME1,MM.SPEED,MM.SPDFILT,MM.TIMETYPE from (select c11.*,((YCOR-l4)/(XCOR-l2)) as SLOPE,MOD(DEGREES (ATAN2((YCOR-l4),(XCOR-l2))), 360) as ANGLE1,(YCOR-l4)/(XCOR-l2) ATT,LOG2(((DW)+1)) as INDDIFF,
if(TYPE2=(LAG(TYPE2) OVER (
PARTITION BY MYID
ORDER BY ID)),"Search Time","Actual Time") as TIMETYPE,case when type2="A" then "1"
when type2="B" then 2
when type2="C" then 3
else 0
end as MYCHAR from (SELECT b.*,LEAD(XCOR) OVER (
PARTITION BY charsq) l1,LAG(XCOR) OVER (
PARTITION BY MYID
ORDER BY ID) l2,LEAD(YCOR) OVER (
PARTITION BY MYID) l3,LAG(YCOR) OVER (
PARTITION BY MYID
ORDER BY ID) l4,distance/7.1 as DW,IF(length(speed) >= 3, "1","0") as SPDFILT,RIGHT(charSq,1) as TYPE2 FROM test.table2 b) c11) mm

conditionally sum from one table based on information from another table

I am trying to write an sql query that outputs data for a python script. The python script will eventually push that data to a table so to make things smoother, I decided to cast the output as char.
The data that I have is organized by 15min periods. Data A and data B are stored on one table and have columns start_time (as a datetime), counts A, and counts B. The second table has start_time (as a datetime), and counts C.
What I need is sum for A, B, and C for each day. However, I want to sum conditionally where it only counts in the sum where the other two data counts are not NULL for that 15 min period. For example, if a "row" for a 15 min period has data for A and B but not C, it would not count in the sum. How do I implement this conditional?
example output:
date| SUM(A) | SUM(B) | SUM(C)
I can write without the conditional like this (new to sql):
SELECT
DATE('timezone conversion') AS date,
cast(SUM(p1.COUNT_DATA_A) as char)
AS A,
cast(SUM(p1.COUNT_DATA_B) as char)
AS B,
cast(SUM(p2.COUNT_DATA_C) as char)
AS C
FROM
table_data_A_B
AS p1
LEFT JOIN table_data_C
AS p2 ON p1.start_time = p2.start_time
WHERE
DATE('timezone conversion') >= '2018-03-27'
AND DATE('timezone conversion') < '2018-03-29'
GROUP BY DATE('timezone conversion')
ORDER BY DATE(p1.start_time) DESC
How would I implement the conditional in this query? I appreciate the help. I am a bit new to stackoverflow, coding and sql in general but I will try my best to be helpful.
Just test for this in the WHERE clause of the query.
WHERE DATE('timezone conversion') BETWEEN '2018-03-27' AND '2018-03-29'
AND p1.COUNT_DATA_A IS NOT NULL AND p1.COUNT_DATA_B IS NOT NULL AND p2.COUNT_DATA_C IS NOT NULL

Query to retrieve values form DB on multiple criteria

I Need to retrieve values from database to plot them in graph. For that I need to get values on criteria basis. Data matching different criteria has to be returned as different rows/ column to my query
(i.e)
I have a table called TABLEA which has a column TIME. I need to get the value based on time critreia as a result, count of rows which are matching TIME>1 and TIME<10 as a result, TIME>11 and TIME <20 as a result and so on. Is it possible to get the values in a single query. I use Mysql with JDBC.
I should plot all the counts in a graph
Thanks in advance.
select sum(case when `time` between 2 and 9 then 1 else 0 end) as count_1,
sum(case when `time` between 12 and 19 then 1 else 0 end) as count_2
from your_table
This can be done with CASE statements, but they can get kind of verbose. You may just want to rely on Boolean (true/false) logic:
SELECT
SUM(TIME BETWEEN 1 AND 10) as `1 to 10`,
SUM(TIME BETWEEN 11 and 20) as `11 to 20`,
SUM(TIME BETWEEN 21 and 30) as `21 to 30`
FROM
TABLEA
The phrase TIME BETWEEN 1 AND 10) will either returnTRUEorFALSEfor each record.TRUEbeing equivalent to1andFALSEbeing equivalent to0`, we then only need sum the results and give our new field a name.
I also made the assumption that you wanted records where 1 <= TIME <= 10 instead of 1 < TIME < 10 which you stated since, as stated, it would drop values where the TIME was 1,10,20, etc. If that was your intended result, then you can just adjust the TIME BETWEEN 1 AND 10 to be TIME BETWEEN 2 AND 9 instead.

Pivot with changing dates

I have a script with result set something like this:
-- #Test (temptable)
Branch_Name C_Date Percentage
Branch 1 20140107 90
Branch 1 20140108 82
Branch 2 20140107 85
Branch 2 20140108 86
I would like to pivot this data, however the C_Date is populated to get 7 days back:
WHERE (1 = 1) AND
(tTable.C_Date > CONVERT(VARCHAR(10),GETDATE() -7, 112)) AND
(tEnter.C_Date < CONVERT(VARCHAR(10),GETDATE() -1, 112)).
I've tried
Select * from #Test
pivot (avg (Percentage) for C_date in ([20140107],[20140108])) as PivotTable
and it gives me the data I want (see below),
Branch_Name 20140107 20140108
Branch 1 90 82
Branch 2 85 86
but how do I get the pivot to look at dates populated by the GETDATE? I've tried putting the GETDATE command in each [] but that obviously didn't work.
** Note, my example shows 2 days, but my query is for 7 days back, not including the day it's being run.
Any help appreciated - thank you!
Instead of trying to work with the dates, try working with "the number of days between C_Date and today".
So early on (in a subquery or CTE) you do DATEDIFF(day,C_Date,GETDATE()) as NumDays.
You can then filter your where as WHERE NumDays between 1 and 7 and your pivot as:
pivot (avg (Percentage) for NumDays in ([1],[2],[3],[4],[5],[6],[7])) as PivotTable
Now, that handles most of what you need. The one thing we can't do (in plain SQL) is to convert those column names back into dates - because any particular SQL query has to produce a result set with a fixed "shape" - the number, names and types of the columns are fixed.
But hopefully that's enough to do in SQL, and if you do need to convert back into date headings, that can be done with whatever is consuming this result set.