SQL to show min,max during timerange where name =(x) - mysql

Im trying to establish a basic report function.
The tables contain basic columns like this
ID, timestamp, value, key, Query, JSON
The only unique identifier is in the key colum
Theres half a million rows
I'd like it to report back this basically
SELECT min(value) as begin, max(value) as end FROM `key`WHERE NAME='Key1'
TIMERANGE='LastMonth'
Could someone please assist with the syntax of this or the concepts im missing
Thankyou and much appreciated

Try this
SELECT min(value) as begin, max(value) as end FROM `key`
WHERE NAME='Key1' and
timestamp>=date_add(current_date(),interval -1 month) and timestamp<=current_date()

SELECT min(value) as begin, max(value) as end FROM `key`
WHERE ID='Key1'
AND timestamp >= DATEADD(month, DATEDIFF(month, -1, getdate()) - 2, 0)
AND timestamp <= DATEADD(ss, -1, DATEADD(month, DATEDIFF(month, 0, getdate()), 0))
OR
SELECT ID, min(value) as begin, max(value) as end FROM `key`
WHERE timestamp >= DATEADD(month, DATEDIFF(month, -1, getdate()) - 2, 0)
AND timestamp <= DATEADD(ss, -1, DATEADD(month, DATEDIFF(month, 0, getdate()), 0))
GROUP BY ID

Related

Getting the month to show date name rather than number when pulling each month in the year

I am pulling data that lists each month of the year but the month column states the first day of each year, how can I make this show the full month name?
Here is some of what I have. I read similar questions and tried to add in what it stated everywhere but I couldn't get it work, help?
SELECT a.MonthDate
,Sum(a.ScrapPcs) / (Sum(a.GrossPcs)*1.00) As ScrapPct
,0.05 As ScrapTarget
,sum (a.GrossPcs) As 'Total Parts Cast'
,sum (a.ScrapPcs) as 'Total Parts Scrap'
,CASE
WHEN Sum(a.ScrapPcs) / (Sum(a.GrossPcs)*1.00) <= 0.05 THEN 1
WHEN Sum(a.ScrapPcs) / (Sum(a.GrossPcs)*1.00) >= 0.9*.05 THEN -1
ELSE 0
END
AS Status
FROM
(
SELECT DATEADD(month, DATEDIFF(month, 0, b.CreationProdDate), 0) As MonthDate
,Count(b.BOOKING_ID) As ScrapPcs
, 0 As GrossPcs
FROM dbo.CPC_BOOKING b
WHERE b.CreationProdDate Between DATEADD(yy, DATEDIFF(yy, 0, GetDate()), 0) AND DateAdd("d", -1, GetDate())
and CPC_BookingTyp_ID in (2,8)
and ManufacturingPlant in (90002604,90017699, 90017705)
GROUP BY DATEADD(month, DATEDIFF(month, 0, b.CreationProdDate), 0)
UNION ALL
SELECT DATEADD(month, DATEDIFF(month, 0, n.ProductionDate), 0) As MonthDate
,0 As ScrapPcs
,Sum(n.Quantity) As GrossPcs
FROM [NORIS].[dbo].[MDL_Data_Ultimate] n
WHERE n.ProductionDate Between DATEADD(yy, DATEDIFF(yy, 0, GetDate()), 0) AND DateAdd("d", -1, GetDate())
and n.ManufacturingPlantGroup = 1
GROUP BY DATEADD(month, DATEDIFF(month, 0, n.ProductionDate), 0)
) a
group by a.MonthDate
Thanks in advance, anything helps
For MySQL:
https://www.w3resource.com/mysql/date-and-time-functions/mysql-monthname-function.php
Use the MONTHNAME function as per the example on that webpage.
SELECT MONTHNAME(a.MonthDate)
For MS SQL Server:
If you want the month name use this in the derived table:
SELECT datename(month,DATEADD(month, DATEDIFF(month, 0, b.CreationProdDate), 0)) As MonthDate
That is, you want to apply the datename(month, date)function to the first column of both unioned queries.

How to find data from a previous month in sql starting from day 1 in the SELECT in sql?

SELECT
team, team_name, COUNT(*) AS c
FROM
[helpdesk].[dbo].[inpc] , helpdesk.dbo.teams
WHERE
team = teams.id
AND st IN (4, 5)
AND CONVERT(VARCHAR(10), dd_1, 102) > DATEADD(month, -1, GETDATE())
GROUP BY
team, team_name
Your question is really a painful SQL Server date olympics question. We can phrase the requirement of the previous month as a date on or after the first of the previous month and strictly before the first of the current month.
SELECT
team,
team_name,
COUNT(*) as c
FROM [helpdesk].[dbo].[inpc], helpdesk.dbo.teams
WHERE
team = teams.id AND
st IN (4,5) AND
dd_1 >= DATEADD(month, DATEDIFF(month, 0, DATEADD(month, -2, GETDATE())), 0) AND
dd_1 < DATEADD(month, DATEDIFF(month, 0, DATEADD(month, -1, GETDATE())), 0)
GROUP BY
team,
team_name;
Also, as others have pointed out, you should use explicit join syntax. I'm not sure your current join even makes sense, as it appears to be a cross join.

TSQL First of last month - date only

This is using SQL2008 R2.
I am trying to get the first of the last month in these two formats:
'06/01/2013'
'2013-06-01'
I found this code, but it includes the time:
SELECT DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-1, 0)
My stored procedures want only the date.
I tried this to trim off the time, but it didn't work.
SELECT RIGHT(DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-1, 0),8)
What you want appears to be different character style outputs which are described here..
SELECT CONVERT(CHAR(10), DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-1, 0), 111)
SELECT CONVERT(CHAR(10), DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-1, 0), 120)
Your question essentially boils down to how to convert a datetime to a date. Like this:
CONVERT(date, someDate)
So, combine that with the code you already have to get the date you want:
SELECT CONVERT(date, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-1, 0))
try this, it has no string conversions ...
declare #dt DateTime = getDate()
Select DateAdd(mm,-1, DateAdd(day, 1-day(#dt),
datediff(day, 0, #dt)))
or ...
declare #dt DateTime = getDate()
Select DateAdd(mm,-1, DateAdd(day, 0,
datediff(day, day(#dt)-1, #dt)))
or, even simpler,....
declare #dt DateTime = getDate()
select dateadd(mm, datediff(mm, 1, #dt)-1, 0)
or, making it allow for arbitrary months back...
declare #dt DateTime = getDate()
declare #monthsBack tinyInt = 3
select dateadd(mm, datediff(mm, 1, #dt) - #monthsBack, 0)
Try this:
convert(varchar, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-1, 0), 101)
Here is another fun way... I am typing from my nexus, please forgive this
Select cast(cast(getdate() as int) as datetime),getdate()

Get MAX minutes from query in SQL Server

I have a query that gets all the records for today. With the query below I get the difference from the time a ticket was issued till the current time. This gets the total waiting time.
I want to get the MAXWaitTime from my query result.
SELECT
DATEDIFF(minute, Issued, GETDATE()) AS MaxWaitTime
FROM
tblData
WHERE
(DATEADD(day, DATEDIFF(day, 0, Issued), 0) = DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0))
This works fine I get the minutes back but for several records. I only want the highest.
My result:
32
50
25
I want the result to be the highest only:
50
You need to use the max function to get the highest value. Check this link
SELECT Max(DATEDIFF(minute,Issued,GETDATE()))AS MaxWaitTime
from tblData
WHERE
(DATEADD(day, DATEDIFF(day, 0, Issued), 0) = DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0))
Try Following Query :
SELECT max(DATEDIFF(minute,Issued,GETDATE())) AS MaxWaitTime
from tblData
WHERE (DATEADD(day, DATEDIFF(day, 0, Issued), 0) = DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0))

Neglecting the records from SQL Query

I have two date columns; given_schedule and delivery_schedule. To query them, I use the following:
SELECT *
FROM table2
WHERE CONVERT(date, given_schedule) >= CONVERT(date, DATEADD(dd, -3, GETDATE()))
This SELECT returns all the rows within the last 3 days. What I need to do, though, is remove all of the returned rows which have today's date in the delivery_schedule.
How should I update my query to do this? Essentially, I want to remove anything returned by the following query:
SELECT * FROM table2
WHERE CONVERT(date, delivery_schedule) = CONVERT(date, GETDATE())
SELECT * FROM table2
WHERE CONVERT(date, given_schedule) >=CONVERT(date, DATEADD(dd, -3, GETDATE()))
AND CONVERT(date, GETDATE()) <> CONVERT(date, delivery_schedule)
You mean something like this?
SELECT *
FROM table2
WHERE CONVERT(date, given_schedule) != GETDATE()
AND CONVERT(date, given_schedule)>=CONVERT(date, DATEADD(dd, -3, GETDATE()))
It's not clear what this question has to do with C# though, since this is SQL code