Get last hours records in select statement - sql

I have a table of machines in which I will display in the first row the actual scrap, in the second row the scrap from last 1 hour, in the third row the scrap from last 4 hours and in the last row the scrap from last 8 hours.
I thought to create in SSRS 4 Datasets for each hours needed. But I think there is a easier way to do it in one Dataset.
Can I do it in the select statement for each hour per one expression?

From what I can gather from your question, your select statement needs to be filtered to the last 8 hours. You can then add a new column for which "Type" it is - last hour, last four hours, last 8 hours. Then group by this in a tablix in SSRS.

Related

How to subtract specific fields in a row from 2 dates in SQL?

I have a table where the data updates every hour and I need to subtract the number of quantities as of today and what was 60 days back. How can that be done in SQL?

SQL date time Query

Need help to get the data of particular format
We have a table which have a data which of production now we need to select the data of each day with particular time period which is differentiate between three shift A,B,C.
In our table we have a datetime column which capture's each seconds data now that data we need in shiftwise like 6am to 2pm is of A shift production count and 2pm to 10pm of shift B and 10pm to 6 am of shift C.
here i am getting the data for single day where i have written the below query which is working good.
select distinct(count(PRD_SERIAL_NUMBER)),(select convert(date,getdate())) as date,'B' as shift_name
from table_name
where status=02
and LAST_UPDATED_DATE
between (SELECT FORMAT(GETDATE(),'yyyy-MM-dd 14:01:00.000')) and
(SELECT FORMAT(GETDATE()-26,'yyyy-MM-dd 22:01:00.000'))
refer below output image 1
Here i am getting the count for single day and for upcoming days i have solution but now the question arise is i have a past 4 Month data which i need to get in datewise and shiftwise count and for the column prd_serial_number have duplicate entries so it should be in distinct.
please refer below image 2 for required output format

Given starting Year and Month as separate columns, how to tell if a specific year and month are within next 6 months?

I have a table A with two columns named Year and Month. I need to join it with another table B also with Year and Month columns. The condition I need to impose is that the month in B is within next 6 months of the month in A. For example, if A.Year=2014 and A.Month=09, then B.Year=2015 and B.Month=01 would be selected because it is within the next 6 months.
I've searched on SO but have not been able to find a solution. This thread
gave me a hint of using Year*100+Month calculations. But I am not sure how to add 6 months to such a calculation easily (guess I could use modulo). Does anyone have a good clean solution to this?
Instead of Year*100+Month simply use Year*12+Month.
WHERE B.Year*12+B.Month BETWEEN A.Year*12+A.Month AND A.Year*12+A.Month +6

extracting common data of current months and last two monnth sql

I have a table with more than 20000 rows, In one of column i have month from jan 2014 to Dec 2014, and in another column i have a loan number. Most of the loan Numbers are reapeting every months,now i need to get only the loan Number which are apperead in all three monthy consecutively. For eg if i am getting data for current months i also wanted get data which are common in two months before the current months. The database that i m using is Access DB. Any adivice will be more than a help, Thanks in Advance.
SELECT Loans.LoanID, Sum(IIf([period]=[month],1,0)) AS CM, Sum(IIf([period]=[month]-1,1,0)) AS [m-1], Sum(IIf([period]=[month]-2,1,0)) AS [m-2]
FROM Loans
GROUP BY Loans.LoanID
HAVING (((Sum(IIf([period]=[month],1,0)))>1) AND ((Sum(IIf([period]=[month]-1,1,0)))>1) AND ((Sum(IIf([period]=[month]-2,1,0)))>1));
I used month as an integer, and didn't make any adjustment for months 1 and 2 to loop back and look at prior year - you should be able to modify this based on the actual format you are using for the month.

SQL Aggregate Subtotal

DATE|Sales
1|2
1|3
2|2
2|4
2|2
3|3
4|4
4|5
5|3
5|3
6|3
7|2
7|2
I have a table which is in this format. The first column is the date and the second one is the sales in dollars.
The table is 2 million rows.
So from the above table we can get that
after the first day we gained 5$
after the second day we gained 13$
after the third day we gained 16$
after the fourth day we gained 25$
after the fifth day we gained 31$
after the sixth day we gained 34$
after the seventh day we gained 38 $
What i want is to create a function to calculate the sales from one specific day (this date would be the input parameter to the function) to the most recent date to the table.
For example i would like to calculate the sales after the 5th 6th and 7th of the month.
Imagine that my table is two million rows and the dates instead of 3 (like in the previous case e.g.5th 6th and 7th) they might me thousands. Which is the most efficient way of doing that ?
Well, it sounds like you're basically going to be doing a table scan no matter what, it's just you need to start part-way down. I'm guessing the timestamp is indexed? Try doing WHERE date > START_DATE, and then you just do a sum(sales) as usual. Not sure that there's a more efficient way.