Line-shift report, Line-day report - c#

I'm generating a line-shift report:
In my application, I'm providing a dropdownlist for selecting shift and line and they will select a date from calender
I have 3 shifts
shift1 starts at 7am and ends at 3pm
shift2 starts at 3pm and ends at 11pm
shift3 starts at 11pm and ends at 3am
I have a table called datalogging where login information will be stored as shown below:
Name Shiftname ID operatorname Date plantname line Machine
Pradeepa Shift2(11-7) 3 Operator 3 2011-05-28 Plant 3 Line5 mc10
Ashwini Shift1(7-3) 1 Operator 1 2011-05-29 Plant 3 Line6 mc12
Deepika Shift2(11-7) 2 Operator 3 2011-05-29 Plant 5 Line9 mc18
Ashwini Shift1(7-3) 1 Operator 1 2011-05-24 Plant 1 Line1 mc1
Deepika Shift2(3-11) 2 Operator 2 2011-05-24 Plant 2 Line3 mc5
Ashwini Shift2(3-11) 1 Operator 2 2011-05-25 Plant 2 Line3 mc5
and so on..
I have a parameter table like temperature,pressure,ph,speed,co2 etc
Temperature table contains following data and this table will contains all the reading from 7am to till 3am
Temperature Time Date
27 13:13:54.000 2011-05-25
27.3 13:14:04.000 2011-05-25
27.6 13:14:14.000 2011-05-25
27.9 13:14:24.000 2011-05-25
28.2 13:14:34.000 2011-05-25
28.5 13:14:44.000 2011-05-25
27 16:13:29.000 2011-05-26
27 16:13:31.000 2011-05-26
and so on..
The user will select a line from dropdownlist and shift and he will select a date from th calender
If the user select shift2,line3 and date 25/05/2011 what are the readings are there between 3pm to 11pm should be displayed in my report
My report should look like:
Machine Shiftname Date Time Temperature
mc5 Shift2 25/05/2011 13:13:54.000 27
mc5 Shift2 25/05/2011 13:14:04.000 27.3
mc5 Shift2 25/05/2011 13:14:14.000 27.6
I'm also doing line-day report
where shiftname should change according to time for eg if time changes to 23:00:00 shiftname should change to shift3 in my report
if the user select particular shift and date for eg if the user selects shift1,line1 and date my report should contain all reading between 7am to 3pm
can any one help me on this.

You could get your report with following query
SELECT d.Machine
, CASE WHEN t.time BETWEEN '19:00:00.000' AND '23:59:59.999' THEN 'Shift1'
WHEN t.time BETWEEN '00:00:00.000' AND '02:59:59.999' THEN 'Shift1'
WHEN t.time BETWEEN '03:00:00.000' AND '10:59:59.999' THEN 'Shift2'
WHEN t.time BETWEEN '11:00:00.000' AND '18:59:59.999' THEN 'Shift3'
END
, t.Date
, t.Time
, t.Temperature
FROM Datalogging d
INNER JOIN Temperature t ON t.Date = d.Date
WHERE d.Shifname = 'Shift2(3-11)'
AND d.Line = 'Line3'
AND t.Date = '25/05/2011'
but if we can assume that each machine would have temperature readings each day, it is clear that there's a relationship missing between your Temperature and Datalogging table.

Related

Group by an array of objects and select based on multiple conditions

I have an array of objects that may contain duplicate records based on some ID.
I am trying to group the objects by ID and from each group pick the record where either status is 3 or with the earliest date (status takes precedence).
ID Status Last LastModified
1 1 Smith 02/06/2023 04:00 AM
1 2 Smith 02/06/2023 02:00 AM
2 1 Jones 02/01/2023 11:24 AM
3 1 Jack 02/05/2023 02:00 AM
3 3 Jack 02/06/2023 06:00 AM
What I would want from a query is to get the following:
ID Status Last LastModified
1 2 Smith 02/06/2023 02:00 AM
2 1 Jones 02/01/2023 11:24 AM
3 3 Jack 02/06/2023 06:00 AM
I get the records based on date but not sure how to stick the status portion in there:
var filtered = from d in tests.AsEnumerable()
group d by d.ID into g
select g.OrderBy(p => p.DateTime).FirstOrDefault();
This returns:
ID Status Last LastModified
1 2 Smith 02/06/2023 02:00 AM
2 1 Jones 02/01/2023 11:24 AM
3 1 Jack 02/05/2023 02:00 AM
How about:
var filtered =
from d in tests.AsEnumerable()
group d by d.ID into g
select g.OrderByDescending(p => p.Status == 3).ThenBy(p => p.DateTime).FirstOrDefault();

How To Find The Sum By Sum Of Alias Column Value?

Here i am trying to calculate the SUM of amount paid for Tution by particaular student in particular fiscal year and also for every month of that fiscal year.
For example in below line PaidTutionFee is calculated by SUM(Tution) AS PaidTutionFee. Now what i want is to sum PaidTutionFee column,
StudentId FYID Month PaidTutionFee
7 16 3 2855.00
7 16 4 2855.00
For this i have sum the Tution i.e SUM(Tution) AS PaidTutionFeeand group by StudentId, FYId, Month which will give the sum of Tution for every months for that fiscal year and finally to total Tution paid for entire fiscal year i have sum the alias of SUM(Tution) AS PaidTutionFee i.e SUM(PaidTutionFee) AS TotalTutionFeeInFiscalYear.
Now the problem is that i throws an error saying
Msg 207, Level 16, State 1, Line 11
Invalid column name 'PaidTutionFee'.
Below is my sql server query
SELECT StudentId,FYId, Month, SUM(Tution) AS PaidTutionFee,
SUM(PaidTutionFee) AS TotalTutionFeeInFiscalYear
FROM [dbo].[FeePaymentDetails]
WHERE FYId = 16 AND StudentId = 7
GROUP BY StudentId, FYId, Month
You can't aggregate an aggregate in the same SELECT statement. BUT... that's not really what you are after here. You want a sum of tuition when grouped by StudentID, FYid, Month AND a sum of tuition when grouped by StudentID, FYid (If I'm reading this correctly). For this you can use Window Functions:
SELECT StudentId,FYId, Month, SUM(Tution) AS PaidTutionFee
Sum(Tution) OVER (PARTITION BY SutdentID, FyID) as TotalTutionFeeInFiscalYear
FROM [dbo].[FeePaymentDetails]
WHERE FYId = 16 AND StudentId = 7
GROUP BY StudentId, FYId, Month;
Now your result set's granularity is at the StudentId, FYId, Month level, but you have one extra column that has it's values repeated for every distinct StudentId, FYId and it's value represents the sum for that higher level of granularity that you are after.
The much under appreciated rollup and grouping sets facilities of sqlserver can do this one quite easily:
SELECT StudentId,FYId, Month, SUM(Tution) AS PaidTutionFee
Sum(Tution) OVER (PARTITION BY SutdentID, FyID) as TotalTutionFeeInFiscalYear
FROM [dbo].[FeePaymentDetails]
/*WHERE FYId = 16 AND StudentId = 7*/
GROUP BY ROLLUP(StudentId, FYId, Month)
I've picked ROLLUP because i think it's easiest to understand
Sqlserver will do your grouping as you ask, down to the month level so you'll get a row for every month. Then thanks to the addition of ROLLUP you'll get a row with a NULL month and that is the sum of everything for the student/year. You'll also get a row with null month and null year that is the sum for the student, and a row of all nulls that is the grand total for all students. The effect is thus that the grouping/summing "rolls up" from most precise (student/year/month) to least precise (one sum of everything) in the order of right-to-left depending on the order of columns in the group by
I commented out your where clause to better demo the rollup effect - if you put it back in you'll get some totals lines that are no different to what you already know (because there is only one student and one year, so the total for all students will be the same as the total for the one student etc)
GROUPING SETS let you fine tune the subtotal/grandtotal effect better
If you made your group by like this:
GROUP BY GROUPING SETS((StudentId,FyId,Month),(StudentId,FyId))
You'll just get the month details (thanks to the first grouping set) and the lines where month is null (th total for the year) thanks to the second grouping set
You can use over Partition By to calculate running totals and grand totals without need of group by:
SELECT StudentId,FYId, Month, SUM(Tuition) over (partition by StudentId, FyId) AS PaidTutionFee,
SUM(Tution) over (partition by StudentId, Year(FyId)) AS TotalTutionFeeInFiscalYear
FROM [dbo].[FeePaymentDetails]
WHERE FYId = 16 AND StudentId = 7
Order BY StudentId, FYId, Month

How to get data from the previous month result in SQL Server

I have 2 tables ms and fr.
ms contains columns pid,month, and totalsales, while fr contains pid, month, and forecast.
In both tables, each pid has 12 record based on month.
I actually want to calculate the demand where the formula is (0.3 * totalsales) + (0.7 * previous month result of forecast). The previous month result of forecast is like when the user choose pid 2 and month 2, then it will take the forecast result from month 1 as its calculation data.
I tried it already but the desired result is not returned:
select
((0.3 * totalsalesamount) + (0.7 * forecastdemand)) as demand
from
Monthlysales, forecastreorder
where
Monthlysales.P_ID = forecastreorder.Productid
and Monthlysales.P_ID = 1
and forecastreorder.Month = 2
When I execute the code above the result is based on their each forecast result. For example, when I choose pid 1, month 2, then it will take the forecast result from month 2 also. meanwhile i want it to take the forecast result from month 1 as its calculation data.
Try this to get previous month in sql
SELECT Convert(datetime, DateAdd(month, -1, Convert(date, GetDate())));
Using a similar table but with a year field, using ISNULL for no found data from previous moth, presume the previous is the month sales.
SELECT
0.3 * ms.totalsalesamount + 0.7 * ISNULL(fr.forecastdemand, ms.totalsalesamount) as demand
FROM
ms
LEFT OUTER JOIN
fr ON ms.pid = fr.pid
AND fr.month = CASE WHEN ms.month > 1 THEN ms.month - 1 ELSE 12 END
AND fr.year = CASE WHEN ms.month > 1 THEN ms.year ELSE ms.year - 1 END

Analytical TSQL

I need to produce some SQL that will show me the trend (up or down tick) in some transacitons.
Consider this table with a PlayerId and a Score
PlayerId, Score, Date
1,10,3/13
1,11,3/14
1,12,3/15
If I pull data from 3/15 I have a score of 12 with an upward trend compared to the historical data.
I did something similar in Oracle 8i about 10 years ago using some of the analytical functions like rank, however it was 10 years ago....
The results would look similar to
PlayerId, Score, Date, Trend
1,12,3/15,UP
How can I do something similar with sql azure?
This SQL:
with data as (
select * from ( values
(1,11,cast('2013/03/12' as smalldatetime)),
(1,15,cast('2013/03/13' as smalldatetime)),
(1,11,cast('2013/03/14' as smalldatetime)),
(1,12,cast('2013/03/15' as smalldatetime))
) data(PlayerId,Score,[Date])
)
select
this.*,
Prev = isnull(prev.Score,0),
tick = case when this.Score > isnull(prev.Score,0) then 'Up' else 'Down' end
from data this
left join data prev
on prev.PlayerId = this.PlayerId
and prev.[Date] = this.[Date] - 1
returns this output:
PlayerId Score Date Prev tick
----------- ----------- ----------------------- ----------- ----
1 11 2013-03-12 00:00:00 0 Up
1 15 2013-03-13 00:00:00 11 Up
1 11 2013-03-14 00:00:00 15 Down
1 12 2013-03-15 00:00:00 11 Up

c# Refunding between date ranges

Basically I have an invoice lines for a below account as below:
BillID AccountID BilledFrom BillTo Days Price
38 3456 10/10/2012 10/11/2012 30 86p
39 3456 11/11/2012 11/12/2012 30 87p
40 3456 12/12/2012 30/12/2012 18 81p
The user would like to refund the customer for a partial date Range, therefore the user will need to enter a date from and date to date:
Date From: 18/10/2012 DateTo: 14/12/2012
This will cause credit lines to refund the client working out the days and price for each line. I need to return each line and show the cut off points. As you can see the ranges entered span across the 3 invoice lines.
The result needed is:
BillID AccountID BilledFrom BillTo RangeStart RangeEnd Days Price
38 3456 10/10/2012 10/11/2012 18/10/2012 10/11/2012 22 86p
39 3456 11/11/2012 11/12/2012 11/11/2012 11/12/2012 30 87p
40 3456 12/12/2012 30/12/2012 11/11/2012 11/12/2012 2 81p
The results basically will bring back the initial applicable invoice line but work out how this range fits and calculates the days against the bill from and bill to from the date range entered.
I need a function either in SQL to provide this result. Any Help would be appreciated. Thank you in advance.
try this:
declare #DateFrom date='10/18/2012'
declare #DateTo date='12/14/2012'
select T.BillID,T.AccountID,T.BilledFrom,T.BillTo,
case when BilledFrom<#DateFrom then #DateFrom else BilledFrom end [RangeStart],
case when BillTo<#DateTo then BillTo else #DateTo end [RangeEnd],DATEDIFF(D,case when BilledFrom<#DateFrom then #DateFrom else BilledFrom end ,case when BillTo<#DateTo then BillTo else #DateTo end ) [Days],Price
from t_account T
SQL Fiddle Demo
A pure T-SQL answer would be the following (assuming a table named InvoiceLine with the columns as specified in your text):
declare #from datetime
declare #to datetime
set #from = '2012-10-18'
set #to = '2012-12-14'
select
BillID, AccountID, BilledFrom, BillTo,
case when datediff(d, #from, BilledFrom)>0 then BilledFrom else #from end [RangeStart],
case when datediff(d, BillTo, #to)>0 then BillTo else #to end [RangeEnd],
[days]
+case when datediff(d, #from, BilledFrom)>0 then 0 else datediff(d, #from, BilledFrom) end
+case when datediff(d, BillTo, #to)>0 then 0 else datediff(d, BillTo, #to) end [Days],
Price
From InvoiceLine
Note that I am making some assumptions here:
The last line in your example output is wrong - the range is 12/12/2012 to 14/12/2012 (the number of days in your example is correct, however (2).
#from must be less than #to for this code to work - but you should check that anyway.
You want this to be done in T-SQL. It's not very neat, and C# is much better at this (min, max methods and date manipulation).

Categories

Resources