I need to display a history all our Products we've sold by day, week, month and year. This data will be sent to google charts API to display a line graph of the results.
So if i have a table called Products and it looks like:-
Products
ProductID INT
DateCreated DATETIMEOFFSET
and the user asks to see the history for ProductID 1. How could i retrieve this?
eg output.
Graph 1 (Dates vs Sale Count)
Monday 1st Dec: 0
Tuesday 2nd Dec: 3
Wed 3rd Dec: 1
Graph 2 (Weeks vs Sale Count)
Week 49 2008: 21
Week 50 2008: 11
Week 51 2008: 45
Week 52 2008: 0
Graph3 (Months vs Sale Count)
Dec 08: 102
Jan 09: 23
I'm not sure if the 'by day' can be done ... or any of it.
cheers :)
Update 1 : got part of it working...
After spending a bit of time, i got the first one working... but still need help on the other two AND making it all part of one query...
from p in Products
where p.ProductId == 69
group p.DateCreated by p.DateCreated.Date into grouping
orderby grouping.Key
select new { Date = grouping.Key, Count = grouping.Count() }
var data = from p in ctx.Products
where p.ProductID == *productId*
group p by p.DateCreated.DayOfWeek into groupedProducts
select new { DayOfWeek = groupedProducts.Key, Count = groupedProcuts.Count() };
Without testing I think that may do it for you
To do it per-year something like this:
var data = from p in ctx.Products
where p.ProductID == *productId*
group p by n.CreateDate.Year into gn
select new {
Count = from a in gn
group a by a.CreateDate.DayOfYear into aa
select new { Count = aa.Count(), Key = new DateTime(gn.Key , 1, 1).AddDays(aa.Key) }
};
(Sorry about the variable names :P)
I don't know how to do it with a LINQ expression, but you could write a stored procedure using the PIVOT operator. See Using PIVOT and UNPIVOT.
Use DateTime.Date, DateTime.Month and DateTime.Year. However there is a bug in LINQ-to-SQL that sometimes translates those to invalid SQL. As a workaround you can group by SqlMethods.DateDiffDay, SqlMethods.DateDiffMonth and SqlMethods.DateDiffYear from say '1980-01-01'.
The week part is not so easy. Start of the week depends on Sql Server settings so it's not reliable. Alternatively you can use SqlMethods.DateDiffDay from some date that starts the week in your case and divide day difference by 7. If you need the week number in a given year you should calculate day difference from the most recent week start day that is less or equal to start of the year.
Related
I'm running a points system for companies where every employee that works for that company is worth some points.
Every month the points for the companies are calculated.
This works so far, however In the 9th month of this year I would like to give double points for each acquired employee in that month.
I don't know how to do that.
I have this query now:
SELECT company, (employees *2) as "Points"
FROM data
WHERE month = '10'
GROUP BY company
But as you can see I give 2 points for each employee that works for that company in that month.
But for month 9 I want to give double points and add them to current points in current month(10)
I have this SQLfiddle as example: http://sqlfiddle.com/#!9/2cb812/7
Expected result:
company Points
__________________
company 1 26 + (extra points from month 9)
company 2 32 + (extra points from month 9)
company 3 44 + (extra points from month 9)
So it's all about the August/September delta 2018. If you run the query for any month before September 2018 (June 2018, May 2012, whatever), you just want to get the current month's points. If you run the query for any month after August 2018 (December 2018, March 2022, ...) you want the 2018 bonus points added.
Group by company and use conditional aggregation (an aggregation function on a condition) in order to calculate this.
We must look at the requested month (e.g. 10/2018) and August 2018 and September 2018.
SET #yearmonth = '201810';
SELECT
company,
SUM(
CASE WHEN yearmonth = #yearmonth THEN employees * 2 ELSE 0 END +
CASE WHEN #yearmonth >= '201809' AND yearmonth = '201809' THEN employees * 4 ELSE 0 END -
CASE WHEN #yearmonth >= '201809' AND yearmonth = '201808' THEN employees * 4 ELSE 0 END
) AS points
FROM data
WHERE yearmonth in ('201808', '201809', #yearmonth)
GROUP BY company
ORDER BY company;
The WHERE clause is superfluous, as the months are checked inside the SUM function, but it may speed up the query.
Rextester demo: https://rextester.com/ELOWTL44361
I've looked upon multiple threads but can't seem to find a desirable answer to my question. I am creating a system with a scheduler in it and I need multiple chains in order for the query to return an answer. So here is the scenario. A user needs to register and upon registration, the user is presented with a date.
I have users table where users(obviously) are listed. One column here has the date.
There is also a date table where the dates are stored. Each date can only occupy 30 persons.
The date table also has the availability column. If the date is available, it is labeled 1. If the date has expired (the current date is higher than this date), it is labeled 0.
for example, i have dates Jan 1, Jan 2 and Jan 3 and the current date is Jan 2. Obviously, Jan 1 should be expired. That wouldn't be included in the list so I will set the availability to 0 (yes, manually). There is only Jan 2 and Jan 3. I also need to find if Jan 2 has accommodated 30 people. Else, I need to put him to Jan 3. I got a bunch of parts of the codes that I don't know how to chain.
Expected Output :
query1 (Jan 2 and Jan 3 should be the result)
SELECT * FROM rooms WHERE availability = 0
RoomID Room Date Room Availability
1 Jan 1 1
2 Jan 2 0
3 Jan 3 0
query2 - (count people assigned in specific rooms)
SELECT COUNT(RoomAssigned) FROM users
Users RoomAssigned
Jack 2
Eddie 2
query3 - (system should be able to locate the first room that is available)
if (query2 results<30)
put new user to rooms from result in query1
If ever the chaining I was looking for would possibly be not advisable, I am open for different suggestions. Thanks. :)
Your question seems to be very confused about what tables you have and what's in them, and I don't see how your sample queries can produce the output you showed. But it seems like this query will do what you want:
SELECT *
FROM rooms
WHERE availability = 1
AND roomID NOT IN (
SELECT roomAssigned
FROM users
WHERE availability = 1
GROUP BY roomAssigned
HAVING COUNT(*) >= 30)
ORDER BY roomDate
LIMIT 1
The subquery finds all the rooms that are filled, and then we exclude those from the main query. Then we sort the remaining rooms by date, and select the first one with LIMIT 1.
I think you're going to want something like this:
SELECT r.[RoomID],
r.[Room Date]
FROM rooms r
LEFT JOIN users u
ON r.[RoomID] = u.[RoomAssigned]
WHERE r.[Room Date] >= CURDATE()
GROUP BY r.[RoomId], r.[Room Date]
ORDER BY r.[Room Date], r.[Room Id]
HAVING COUNT(u.[RoomAssigned]) < 30
LIMIT 1
I haven't tested it, so it may require some tweaking. It's like #Barmar's answer, only using a join instead of a nested select. I also checked the availability based on the current date, not the availability column, which I don't think should be stored in the database, because it can be determined based on the Room Date.
Given the sample data in the screenshot below, would it be possible in mysql to return a sum of values from monthly_amount only where the values are before this month. I used a join to pull this data. The 5 left columns are from one table, and the rest are from another.
The issue I'm running into is, lets say its April of 2015, I can't just do a sum WHERE goal_year <= 2015 AND month_id_FK <= 4, or else I'll get only those 4 months from both years, when in that scenario, I really want all the months from 2014, plus the 4 months from 2015.
I could handle this in PHP, but I wanted to first see if there would be a way to do this in mysql?
try
WHERE Goal_Year*100+month_id_FK <= 201504
alternatively:
WHERE
GOAL_YEAR < 2015 OR
(GOAL_YEAR = 2015 and month_id_FK <= 4)
select sum(monthly_amount) from table where goaldate<(SELECT CURDATE())
this is not the actual query for your table..but if you do like this you will get the answer
you need the sum of monthly amount where the date is before current-date means today.
then you can just compare the currentdate with goal date
I am setting up a code to pull all employees hired within the last 2 years who got a certain rating. I have been looking into the YEAR(NOW()) function but I am having a difficult time setting it up. I need to use the NOW function because I need it to pull the data from the time the user access the query. The ratings are completed every following feburary (i.e 2013 ratings will be completed in February of 2014) so it needs to read something like
YEAR(NOW()-12) but it
This way if I were to run it today it would go back and pull the ratings for 2012 and 2011 since 2013 have not yet been completed.
My whole code looks like:
SELECT dbo_v_TMS_QPR_01_Score.TMS_ID, dbo_v_TMS_QPR_01_Score.QPR_Year, dbo_v_TMS_QPR_01_Score.Final_QPR_Score
FROM O867IA_VJOBHST INNER JOIN dbo_v_TMS_QPR_01_Score ON O867IA_VJOBHST.SYS_EMP_ID_NR = dbo_v_TMS_QPR_01_Score.GEMSID
WHERE (((dbo_v_TMS_QPR_01_Score.Final_QPR_Score)>="1.25") AND ((O867IA_VJOBHST.EMP_ACN_TYP_CD)="HIR") AND ((O867IA_VJOBHST.REC_EFF_STT_DT)=Year(Now()-12)))
GROUP BY dbo_v_TMS_QPR_01_Score.TMS_ID, dbo_v_TMS_QPR_01_Score.QPR_Year, dbo_v_TMS_QPR_01_Score.Final_QPR_Score;
But I keep getting the error: INCONSISTENT DATATYPES: EXPECTED DATE GOT NUMBER (#932)
What you have does not work. It subtracts 12 days off the current date/time and then converts it to the year. Thus, it returns 2013.
Use the dataadd() function. The following is a blank query in the query designer.
I am asking for today's date minus 12 months. See output below.
I would THINK you want something like:
If Month(Now()) > 3 then 'If it's after Feb, we want the last 2 years
LastDayOfPrevYear = DateAdd("d", -1, DateSerial(Year(Now()), 1, 1))
Else 'If it's before March, we want the 2 years prior to last year
LastDayOfPrevYear = DateAdd("d", -1, DateSerial(Year(Now())-1, 1, 1))
End If
SELECT dbo_v_TMS_QPR_01_Score.TMS_ID, dbo_v_TMS_QPR_01_Score.QPR_Year, dbo_v_TMS_QPR_01_Score.Final_QPR_Score
FROM O867IA_VJOBHST INNER JOIN dbo_v_TMS_QPR_01_Score ON O867IA_VJOBHST.SYS_EMP_ID_NR = dbo_v_TMS_QPR_01_Score.GEMSID
WHERE (((dbo_v_TMS_QPR_01_Score.Final_QPR_Score)>="1.25") AND ((O867IA_VJOBHST.EMP_ACN_TYP_CD)="HIR") AND ((O867IA_VJOBHST.REC_EFF_STT_DT)>=DateAdd("m", -24, LastDayOfPrevYear)))
GROUP BY dbo_v_TMS_QPR_01_Score.TMS_ID, dbo_v_TMS_QPR_01_Score.QPR_Year, dbo_v_TMS_QPR_01_Score.Final_QPR_Score;
This will give you a "rolling" 24 month timespan from the last date of the previous year.
This may need a little tweaking, but it should be, at the very least, extremely close.
I'm not a db expert. I'm just working on a project where we need to show page views on daily basis between two dates selected by a user from a calendar. I have the following
SQL query that brings brings total number of page views like
SELECT DATE_FORMAT(created_at,'%d %b %Y') as this_date, count(*) as Views
from promotion_insights WHERE f_id = '2' AND p_id = '12'
AND created_at BETWEEN '2012-08-15' AND '2012-08-19'
GROUP BY this_date
Result comes like
----------------------
this_date View
---------------------
15 Aug 2012 3
16 Aug 2012 2
----------------------
I have also a calendar table and a store procedure.calendar table has one column of dates named (datefield). I have already generated dates by calling a stored procedure so don't worry about that. Now what I want, is to make a right join on date basis to above table having (this_date and view columns) with calendar table to show all_dates between selected by the user and put 0 for the dates having 0 views.
count(*) is also making trouble by returing 1 instead 0
Expected output by the join I want is like this:
----------------------
this_date View
---------------------
15 Aug 2012 3
16 Aug 2012 2
17 Aug 2012 0
18 Aug 2012 0
19 Aug 2012 0
----------------------
Any help would be highly highly appreciated.
You can readily do this with a calendar table -- a good idea in most databases.
SELECT DATE_FORMAT(c.date,'%d %b %Y') as this_date, count(*) as Views
from calendar c left outer join
promotion_insights pi
on c.date between '2012-08-15' AND '2012-08-19' and
c.date = pi.created_at
WHERE f_id = '2' AND p_id = '12'
GROUP BY c.date
Note: this assumes that created_at is stored as a date, not a date time. Having a time component could throw off the comparisons.
If you want to do this in SQL you need a row generator to generated a row for each day in your date range, and then join your query to the generated rows to get results for each day in you date range (even if there is no data present for some days in the range).
If you are using Oracle you can use ALL_OBJECTS or DUAL to generate rows. As you are using DATE_FORMAT in your query you seem to use MySQL. A question on how to make a row generator in MySQL was posted before. It states:
Hate to say this, but MySQL is the only RDBMS of the big four that doesn't have this feature.