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
Related
I am trying to get my values sorted ascending but although I get the results, it is not sorted properly as seen below:
"50","2016-12-1","2016-12-01 17:42:30","2016-12-01 17:42:30","0","0"
"50","2016-12-10","2016-12-10 07:31:43","2016-12-10 07:31:43","0","0"
"50","2016-12-11","2016-12-11 04:27:35","2016-12-11 04:27:35","0","0"
"50","2016-12-12","2016-12-12 07:52:18","2016-12-12 18:02:47","10","10"
"50","2016-12-13","2016-12-13 07:28:22","2016-12-13 18:18:31","10","50"
"50","2016-12-14","2016-12-14 07:32:34","2016-12-14 18:37:09","11","4"
"50","2016-12-15","2016-12-15 07:14:15","2016-12-15 07:14:15","0","0"
"50","2016-12-2","2016-12-02 07:23:33","2016-12-02 17:37:22","10","13"
"50","2016-12-3","2016-12-03 07:49:27","2016-12-03 17:45:01","9","55"
"50","2016-12-5","2016-12-05 07:40:22","2016-12-05 17:32:29","9","52"
"50","2016-12-6","2016-12-06 07:41:43","2016-12-06 17:42:00","10","0"
"50","2016-12-7","2016-12-07 07:20:33","2016-12-07 17:40:51","10","20"
"50","2016-12-8","2016-12-08 07:22:02","2016-12-08 20:56:37","13","34"
"50","2016-12-9","2016-12-09 07:35:06","2016-12-09 18:11:18","10","36"
The 2016-12-2 is below the 2016-12-15. This is how I get this values:
SELECT uid, scan_date as 'Date' , min(scan_time) as 'Time In', max(scan_time) as 'Time
CAST(((strftime('%s', max(scan_time)) - strftime('%s', min(scan_time))) % (60 * 60 * 24)) / (60 * 60) AS TEXT) as Hours,
CAST((((strftime('%s', max(scan_time)) - strftime('%s', min(scan_time))) % (60 * 60 * 24)) % (60 * 60)) / 60 AS TEXT) as Minutes
FROM tbl_scanTimes
GROUP BY uid, scan_date
ORDER BY uid asc, scan_date
This is how I insert data into the sqlite
var sCommand = new StringBuilder(#"REPLACE INTO tbl_scanTimes(branch, uid, scan_date, scan_time) VALUES ");
sCommand.Append(string.Join(",", (from DataRow row in values.Rows
let branch = _yard
let empId = row[0].ToString().Trim()
let scanTime = row[1].ToString().Trim()
let date = Convert.ToDateTime(scanTime)
let oDate = date.Year+"-"+date.Month+"-"+date.Day
select string.Format("('{0}','{1}','{2}','{3}')", branch, empId, oDate, scanTime)).ToArray()));
sCommand.Append(";");
Is there a way that I can put 0 in the day part of 2016-12-1 so that it will be 2016-12-01? Thank you.
Following CL's comment on supported date formats, I think that your data-inserting may be using your desktop dateformat settings, because you are passing oDate as just '{2}' in the format string.
Try using this format when you insert data:
string.Format("('{0}','{1}','{2}','{3}')",
branch, empId, oDate.ToString("yyyy-MM-dd HH:mm:ss.fff"), scanTime)
(yes, I know that it can be packed into {2} expr, I just want it to stand out).
Btw. watch out for the upper-case MM and HH. It's important.
Here's the full list
Whever it works or not, check carefully what's the actual query you are sending to the db. Place a breakpoint on that line and see what the Format() method produces.
I would like to group the following datetimes together using Linq in c# as follows:
Group 1:
2015-03-03 15:18:42.880
2015-03-03 15:18:42.897
Group 2:
2015-03-19 16:29:59.977
2015-03-19 16:29:59.983
2015-03-19 16:29:59.983
Group 3:
2015-03-26 11:27:29.813
2015-03-26 11:27:30.030
2015-03-26 11:27:30.030
Group 4:
2015-03-27 15:13:58.483
2015-03-27 15:13:58.483
2015-03-27 15:13:58.500
I'm having an issue with some of these groupings. Currently I'm just grouping the dates ignoring the milliseconds portion. What I would like to do it group dates which are within 1 second of each other without the milliseconds.
This is my query so far:
var query =
from trans in TransactionTable.AsEnumerable()
let dateWithoutMilliseconds = trans.Field<DateTime>("TranactionDate").AddMilliseconds(-trans.Field<DateTime>("TranactionDate").Millisecond)
group trans by dateWithoutMilliseconds into g
select new
{
TransDate = g.Key,
};
You can convert to Ticks (100 nano seconds), round up, and then back to DateTime. In case you want just to GroupBy just round up Ticks:
DateTime source = ...
...
// Up to nearest second
const int shift = 10000000;
DateTime result = new DateTime(
(source.Ticks / shift + (source.Ticks % shift >= (shift / 2) ? 1 : 0)) * shift);
// If you want just a key to group by
long key = (source.Ticks / shift + (source.Ticks % shift >= (shift / 2) ? 1 : 0)) * shift;
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
I'm building a query with LINQ to SQL in my C# project, but I have some problems with it...
What I want to do, is select the 4 lasts days that are like today (For example, a Friday), so if we're on Friday 28, I want to query for: Friday 21, 14, 7... The last four Fridays but NOT today.
This is easy, I've done that but here's the complex part, I want to not query the exceptions I set, for example End of month, which are from 28th to 1st day of each month, so let's say i want to query this (october, fridays):
Today is Friday 26, I want to query:
19, 12, 5 and September 28th (the fourth friday from now), but as I said, 28th is end of month, so i need to return September 21th which is the last friday and it is not end of month... I have the same issues with holidays, but I think if I can handle end of months, I can do with them...
i hope I've explained good for you to understand what I want... Here's my query, which is working but can't handle exceptions. (the field b.day is the Id for each days, 8 means end of month, and 7 holiday)
var values =
from b in dc.MyTable
where // This means end of month
b.day != 8
// This triggers to query last 4 days
&& b.date == Convert.ToDateTime(last.ToString("dd/MM/yyy")).AddDays(-28)
|| b.date == Convert.ToDateTime(last.ToString("dd/MM/yyy")).AddDays(-21)
|| b.date == Convert.ToDateTime(last.ToString("dd/MM/yyy")).AddDays(-14)
|| b.date == Convert.ToDateTime(last.ToString("dd/MM/yyy")).AddDays(-7)
orderby b.id descending
group b.valor by b.hora_id into hg
orderby hg.Key descending
select new
{
Key = hg.Key,
Max avg = System.Convert.ToInt32(hg.Average() + ((hg.Average() * intOkMas) / 100)),
Min avg = System.Convert.ToInt32(hg.Average() - ((hg.Average() * intOkMenos) / 100))
};
You should prepare the list of days you'd like retrieve before trying to query:
// Get the last four days excluding today on the same weekday
var days = Enumerable.Range(1, 4).Select(i => DateTime.Today.AddDays(i * -7));
Then remove any days you don't want:
// Remove those pesky end-of-month days
days = days.Where(d => d.Day < 28 && d.Day > 1);
When you're done preparing the list of days you want to retrieve, only then should you perform your query:
from b in dc.MyTable
where days.Contains(b.date) // Translated to SQL: date IN (...)
...
EDIT: As you mentioned in your comment, you want a total of four days even after any filtering you perform. So simply generate more days and take the first four:
var days = Enumerable.Range(1, int.MaxValue - 1)
.Select(i => DateTime.Today.AddDays(i * -7))
.Where(d => d.Day < 28 && d.Day > 1)
.Take(4);
Due to the way LINQ (and in general, enumerators) work, only four days plus any skipped days will be calculated.
Building on Allon Guralnek's answer, I'd modify it slightly:
First, build an infinite date generator:
public IEnumerable<DateTime> GetDaysLikeMe(DateTime currentDate)
{
DateTime temp = currentDate;
while(true)
{
temp = temp.AddDays(-7);
yield return temp;
}
}
Then you can use deferred execution to your advantage by limiting to only dates that meet your additional criteria:
GetDaysLikeMe(DateTime.Now).Where(dt => /* dt meets my criteria */).Take(4)
Then you can use this list that was generated to query in your LINQ to SQL like Allon Guralnek suggested above:
from b in dc.MyTable
where days.Contains(b.date) // Translated to SQL: date IN (...)
...
This has the benefit of you being able to specify additional predicates for what are acceptable dates and still get at least 4 dates back. Just be sure to put some bounds checking on the infinite date generator in case one of your predicates always returns false for whatever reason (which means the generator will never exit).
IE: while(temp > currentDate.AddYears(-1))
I would highly suggest writing your exception code (last friday of the month) after retrieving your rows, as this logic seems to be too complicated for a LINQ statement. Instead of retrieving the last 4 days, retrieve the last 5. Remove any that are the last Friday of each respective months. If you still have 5 rows, remove the last one.
Update
var values1 =
from b in dc.MyTable
where // This means end of month
b.day != 8
// This triggers to query last 4 days
&& b.date == Convert.ToDateTime(last.ToString("dd/MM/yyy")).AddDays(-28)
|| b.date == Convert.ToDateTime(last.ToString("dd/MM/yyy")).AddDays(-21)
|| b.date == Convert.ToDateTime(last.ToString("dd/MM/yyy")).AddDays(-14)
|| b.date == Convert.ToDateTime(last.ToString("dd/MM/yyy")).AddDays(-7)
orderby b.id descending
select b;
//Do stuff with values
var values2 = from b in values2
group b.valor by b.hora_id into hg
orderby hg.Key descending
select new
{
Key = hg.Key,
Max avg = System.Convert.ToInt32(hg.Average() + ((hg.Average() * intOkMas) / 100)),
Min avg = System.Convert.ToInt32(hg.Average() - ((hg.Average() * intOkMenos) / 100))
};
I have a textfield that users input the date using a modified jquery datepicker. The textfield has the Month and Year such as 'July 2011'
I need to run a query to search for results between July 1, 2011 and July 31, 2011. My date in the database is a smalldatetime, so I need my query to look like this:
select * from members where signupdate between 'july 1 2011' and 'july 31 2011'
How can I get the user inputted date of July 2011 converted to 'July 1 2011' and 'July 31 2011'?
EDIT
I'm only getting a 0 value from InvalidCount but I know I have one record in there as a test. Why isn't it being counted?
MY PROC:
SELECT
(SELECT COUNT(*) FROM dbo.Members m WHERE m.memberID = #pMemberID AND m.SignUpDate BETWEEN #pDate AND DATEADD(MONTH, 1, #pDate)-1) AS 'SignUpDate',
COALESCE(SUM(m.ValidCount), 0) AS ValidCount,
COALESCE(SUM(m.InvalidCount), 0) AS InvalidCount
FROM
dbo.Members m
INNER JOIN
dbo.MemberStats ms ON m.MemberID = ms.MemberID
WHERE
m.SignUpdate BETWEEN #pDate AND DATEADD(MONTH, 1, #pDate)-1
The exact syntax depends on the SQL Engine, but if you start with the 1st of the month, then add 1 month, and finally subtract 1 day; you get the end of the month.
(I'll assume MS SQL Server to match your C# tag)
SELECT
*
FROM
members
WHERE
signupdate BETWEEN #param AND DATEADD(MONTH, 1, #param) - 1
If the between isn't required, you can use DatePart.
Untested example:
where DATEPART(yyyy, SignupDate) = 2011 and DATEPART(m, SignupDate) = 7
convert varchar to appropiate format you want and then compare
check the list of formats. Hope this helps dude.
convert date
Firstly, you must convert the string representation to a valid DateTime struct object on the server. You can use var date = DateTime.ParseExact("july 1 2011", "MMMM dd yyyy", CultureInfo.CurrentUICulture) or DateTime.TryParse. Then you pass this into your SqlCommand as parameters. Never use strings when querying, especially when it comes from user input.