getting the last occurence of a certain item in a sql request - sql

I am trying to write sql requests to my database in order to get the last occurence of a certain item, here's what I mean illustrated by an example:
pigc=# SELECT date FROM msr_history WHERE pos = 'DV' AND msr_id = 177;
date
----------------------------
2018-06-20 08:04:09.724103
2018-06-20 08:09:49.484921
(2 rows)
The first line of my example is my sql request and I am trying to get only the last date, in this example it would be this one: 2018-06-20 08:09:49.484921
Note that the number of dates can differ so I can't just manualy select the second date each time. Thank you for your help.

Use select max(column) to retrieve the highest value for that specific data type for all rows included in the where clause.
In this instance, no aggregation is required since no non-aggregated columns are being retrieved.
See https://www.postgresql.org/docs/9.5/functions-aggregate for a complete list of aggregate functions and specifically https://www.postgresqltutorial.com/postgresql-max-function

The fastest way to achieve it to use the LIMIT clause in your query. This will prevent using aggregate function in your query and will run fastest -
SELECT date
FROM msr_history
WHERE pos = 'DV'
AND msr_id = 177
ORDER BY date DESC
LIMIT 1;

Related

Query another table with results of an another query that include a csv column

Brief Summary:
I am currently trying to get a count of completed parts that fall within a specific time range, machine number, operation number, and matches the tool number.
For example:
SELECT Sequence, Serial, Operation,Machine,DateTime,value as Tool
FROM tbPartProfile
CROSS APPLY STRING_SPLIT(Tool_Used, ',')
ORDER BY DateTime desc
is running a query which pulls all the instances that a tool has been changed, I am splitting the CSV from Tool_Used column. I am doing this because there can be multiple changes during one operation.
Objective:
This is where the production count come into place. For example, record 1 has a to0l change of 36 on 12/12/2022. I will need to go back in to the table and get the amount of part completed that equals the OPERATION/MACHINE/TOOL and fall between the date range.
For example:
SELECT *
FROM tbPartProfile
WHERE Operation = 20 AND Machine = 1 AND Tool_Used LIKE '%36%'
ORDER BY DateTime desc
For example this query will give me the datetimes the tools LIKE 36 was changed. I will need to take this datetime and compare it previous query and get the sum of all parts that were ran in this TimeRange/Operation/Machine/Tool Used

Use SQL to ensure I have data for each day of a certain time period

I'm looking to only select one data point from each date in my report. I want to ensure each day is accounted for and has at least one row of information, as we had to do a few different things to move a large data file into our data warehouse (import one large Google Sheet for some data, use Python for daily pulls of some of the other data - want to make sure no date was left out), and this data goes from now through last summer. I could do a COUNT DISTINCT clause to just make sure the number of days between the first data point and yesterday (the latest data point), but I want to verify each day is accounted for. Should mention I am in BigQuery. Also, an example of the created_at style is: 2021-02-09 17:05:44.583 UTC
This is what I have so far:
SELECT FIRST(created_at)
FROM 'large_table'
ORDER BY created_at
**I know FIRST is probably not the best clause for this case, and it's currently acting to grab the very first data point in created_at, but just as a jumping-off point.
You can use aggregation:
select any_value(lt).*
from large_table lt
group by created_at
order by min(created_at);
Note: This assumes that created_at is a date -- or at least only has one value per date. You might need to convert it to a date:
select any_value(lt).*
from large_table lt
group by date(created_at)
order by min(created_at);
BigQuery equivalent of the query in your question
SELECT created_at
FROM 'large_table'
ORDER BY created_at
LIMIT 1

Select and manipulate SQL data, DISTINCT and SUM?

Im trying to make a small report for myself to see how my much time I get inputed in my system every day.
The goal is to have my SQL to sum up the name, Total time worked and Total NG product found for one specific day.
In this order:
1.) Sort out my data for a specific 'date'. I.E 2016-06-03
2.) Present a DISTINCT value for 'operators'
3.) SUM() all time registered at this 'date' and by this 'operator' under 'total_working_time_h'
4.) SUM() all no_of_defects registered at this 'date' and by this 'operator' under 'no_of_defects'
date, operator, total_working_time_h, no_of_defects
Currently I get the data I want by using the Query below. But now I need both the DISTINCT value of the operator and the SUM of the information. Can I use sub-queries for this or should it be done by a loop? Any other hints where I can learn more about how to solve this?
If i run the DISTINCT function I don't get the opportunity to sum my data the way I try.
SELECT date, operator, total_working_time_h, no_of_defects FROM {$table_work_hours} WHERE date = '2016-06-03' "
Without knowing the table structure or contents, the following query is only a good guess. The bits to notice and work with are sum() and GROUP BY. Actually syntax will vary a bit depending on what RDBMS you are using.
SELECT
date
,operator
,SUM(total_working_time_h) AS total_working_time_h
,SUM(no_of_defects) AS no_of_defects
FROM {$table_work_hours}
WHERE date = '2016-06-03'
GROUP BY
date
,operator
(Take out the WHERE clause or replace it with a range of dates to get results per operator per date.)
I'm not sure why you are trying to do DISTINCT. You want to know the data, no of hours, etc for a specific date.
do this....
Select Date, Operator, 'SumWorkHrs'=sum(total_working_time_h),
'SumDefects'=sum(no_ofDefects) from {$table_work_hours}
Where date='2016-06-03'
Try this:
SELECT SUM(total_working_time) as total_working_time,
SUM(no_of_defects) as no_of_defects ,
DISTINCT(operator) AS operator FROM {$table_work_hours} WHERE
date = '2016-06-03'

SQL Min function on Date

How does Min function work on dates ? If 2 records have the same date and time stamp , the min function returns 1. Does it pull records based on when it was put into the table ?
MIN is an aggregate function so it will return 1 record in your question's case. Since the two records have the same date and timestamp it doesn't matter which date and timestamp are returned (they're the same). Finally, the time the records were inserted is not considered.
MIN() returns the smallest of all selected values of a column. It seems to me that your statement may simply be asking if a minimum exists.
Please post your sql statement.
possibly this is what you need:
SELECT MIN (date) AS "Min Date"
FROM tablename;
Elliot already expained it.
Just a sidenode, if you are using MySQL: MySQL allows to aggregate on a certain column, while fetching other columns without aggregation. (SQL Server does NOT allow that!)
Example:
date | name
2015-03-06 | A
2015-03-06 | B
Using SELECT Min(date), name FROM table on MySQL will return various results.
Sometimes it will be
2015-03-06 | A
sometimes
2015-03-06 | B
Docu:
When using this feature, all rows in each group should have the same
values for the columns that are omitted from the GROUP BY part. The
server is free to return any value from the group, so the results are
indeterminate unless all values are the same.
SQL Server will throw an error, that no aggregation has been performed on column name. See also http://dev.mysql.com/doc/refman/5.0/en/group-by-handling.html
MySQL works this way, cause sometimes grouping on the second column is not really required, for example:
SELECT MAX(id), user_id FROM posts WHERE user_id = 6
(There could be NO other user_id than 6, so aggregation is not required in MySQL - However not paying attention on THIS will lead to wrong results as example one shows.)

Grouping result by date in mysql

This SQL statement
SELECT `ip`, `when` FROM `metrics` WHERE `vidID` = '1' GROUP BY DATE('when')
returns one result, even though multiple are present in the table with different dates. I've tried using DATE_FORMAT as well. Am I doing something stupid?
When is a timestamp column with full timestamp, including hours, minutes and seconds. I'm trying to just group by results by day/month/year.
Looks like you're grouping by the constant string 'when' instead of the field when.
Use this instead:
GROUP BY DATE(`when`)
Sounds like you want to count the IP addresses for a given date:
SELECT COUNT(`ip`) AddressCount, `when`
FROM `metrics`
WHERE `vidID` = '1'
GROUP BY DATE(`when`)
Usually, GROUP BY is used in conjunction with some aggregate function (like SUM, say) to compute the result set by one or more columns. What are you trying to accomplish here? Do you mean to sort, or just get a collapsed list of which IPs have records on which dates?