MVC 3 get days of month in table columns - asp.net-mvc-3

I am learning MVC 3, it is fun, but not always :).
I have one problem I can't solve.
I have this in my table in db
id | worker | working_hours | year | month | day | shift
1 worker1 5 2013 6 1 day
2 worker2 6 2013 6 2 day
2 worker3 7 2013 6 3 night
That works ok. But I would like to make it better :). But I have two problems.
I need table columns do be prepopulated with days of month and for each day I could see working hours
I need table to look something like this
| 1 | 2 | 3 | Total
day 5 6 11
night 7 7
That is just for 3 days in db, but I need table headers to be prepopulated with all days for month.
It seems easy, but I cant solve this one :). Can someone please give me a hand with this. I am doing all this in MVC 3 and using linq to sql connection to my db.

Maybe do something like this if this is REALLY what you want:
id | id(from table 1) | Shift | 1 | 2 | 3 | ect..... Total
1 1 day 5 6 11
2 1 night 7 7
Then in your primary table replace the Day column with the id from the above table and remove the Shift column. You could then add a unique key constraint to the above table so it won't allow a repeat if the id from table 1 and the shift are the same.
To be brutally honest however all this is just a bit confusing. Best bet would be to have two tables:
A:
id | worker
1 worker1
2 worker2
3 worker3
B:
id | workerid | working_hours | year | month | day | day
1 1 5 2013 6 1 true
2 1 6 2013 6 2 true
3 2 7 2013 6 3 false
It doesn't really matter that this table will have loads of entries and doesn't read well because you then write smart queries using LINQ to pull out the data that you need to display on an MVC webpage.

Related

FIFO inventory aging report using a single query in T-SQL

I've got an inventory transactions table :
Product
Date
Direction
Quantity
A
Date 1
IN
3
B
Date 2
IN
55.7
A
Date 3
OUT
1
B
Date 3
OUT
8
B
Date 3
IN
2
I can easily get the stock for any date with the following query :
SELECT Product,
SUM(CASE Direction WHEN 'IN' THEN Quantity ELSE -1 * Quantity END)
FROM Transactions
WHERE Date <= '#DateValue#'
GROUP BY Product;
Now my purpose is to get stocks aged like this using the FIFO principle :
Product
Total stock
0-30 days
31-60 days
61-90 days
91+ days
A
3
3
0
0
0
B
34.2
10
14.2
7
3
C
25
20
3
1
1
D
10
2
8
0
0
E
1
0
0
1
0
I am using SQL Server 2016 & SSMS 18.
The solution should be fast as it will be working against a table with 3,000,000+ rows.
A single query is preferred since it will be integrated into an ERP system.
I have yet to find a solution based on a single query after weeks of research. Any help is appreciated. Thanks in advance.

Reorder factored matrix columns in Power BI

I have a matrix visual in Power BI. The columns are departments and the rows years. The values are counts of people in each department each year. The departments obviously don't have a natural ordering, BUT I would like to reorder them using the total column count for each department in descending order.
For example, if Department C has 100 people total over the years (rows), and all the other departments have fewer, I want Department C to come first.
I have seen other solutions that add an index column, but this doesn't work very well for me because the "count of people" variable is what I want to index by and that doesn't already exist in my data. Rather it's a calculation based on individual people which each have a department and year.
If anyone can point me to an easy way of changing the column ordering/sorting that would be splendid!
| DeptA | DeptB | DeptC
------|-------|-------|-------
1900 | 2 | 5 | 10
2000 | 6 | 7 | 2
2010 | 10 | 1 | 12
2020 | 0 | 3 | 30
------|-------|-------|-------
Total | 18 | 16 | 54
Order: #2 #3 #1
I don't think there is a built-in way to do this like there is for sorting the rows (there should be though, so go vote for a similar idea here), but here's a possible workaround.
I will assume your source table is called Employees and looks something like this:
Department Year Value
A 1900 2
B 1900 5
C 1900 10
A 2000 6
B 2000 7
C 2000 2
A 2010 10
B 2010 1
C 2010 12
A 2020 0
B 2020 3
C 2020 30
First, create a new calculated table like this:
Depts = SUMMARIZE(Employees, Employees[Department], "Total", SUM(Employees[Value]))
This should give you a short table as follows:
Department Total
A 18
B 16
C 54
From this, you can easily rank the totals with a calculated column on this Depts table:
Rank = RANKX('Depts', 'Depts'[Total])
Make sure your new Depts table is related to the original Employees table on the Department column.
Under the Data tab, use Modeling > Sort by Column to sort Depts[Department] by Depts[Rank].
Finally, replace the Employees[Department] with Depts[Department] on your matrix visual and you should get the following:

calculate the time difference for same column in Spotfire

I am a beginner for Spotfire. I have a problem about the difference calculation for the some column value. A sample table could be like this:
id timestamp state
1 7/1/2016 12:00:01 AM 1
2 7/1/2016 12:00:03 AM 0
3 7/1/2016 12:00:04 AM 1
4 7/1/2016 12:00:06 AM 0
5 7/1/2016 12:00:09 AM 1
6 7/1/2016 12:00:10 AM 0
7 7/1/2016 12:00:12 AM 1
I want to calculate the time difference for the timestamp when the state is 1,
the final table I want to have is:
id timestamp state time_diffence
3 7/1/2016 12:00:04 AM 1 3
5 7/1/2016 12:00:09 AM 1 5
7 7/1/2016 12:00:12 AM 1 3
it seems that I should identify an expression for the calculation, but I have not idea for the calculation just for one parameter :(. somebody could help me ?
still one more small question: what if the timestamp column value is just number value, how can i calculate the difference, is there any related function like DateDiff() here? for example:
id times state
1 12 1
2 7 0
3 10 1
4 11 0
5 6 1
6 9 0
7 7 1
the result could be :
id times state diffence
3 10 1 -2
5 6 1 -4
7 7 1 1
after running the code: i have the error as below:
for the row if it has the same time stamp as the last previous row, the difference will keep same as before, but actually the difference for the rows which have same time stamp would be as 0
thanks for your help :)
Assuming your data is sorted in ascending order by [timestamp] before you import it, you can partition using the Previous function with Over where the [state]=1.
Insert a calculated column with this expression:
If([state]=1,DateDiff("ss",Min([timestamp]) OVER (Previous([timestamp])),[timestamp]))
You will see it populated in your table like the below:
Then if you ONLY want to see the rows that have the difference you mentioned, on your table you can...
Right Click > Properties > Data > Limit data using expression >
And insert the expression: [time_difference] > 1
This will result in this table:

SAS Sorting within group

I would like to try and sort this data by descending number of events and from latest date, grouped by ID
I have tried proc sql;
proc sql;
create table new as
select *
from old
group by ID
order by events desc, date desc;
quit;
The result I currently get is
ID Date Events
1 09/10/2015 3
1 27/06/2014 3
1 03/01/2014 3
2 09/11/2015 2
3 01/01/2015 2
2 16/10/2014 2
3 08/12/2013 2
4 08/10/2015 1
5 09/11/2014 1
6 02/02/2013 1
Although the dates and events are sorted descending. Those IDs with multiple events are no longer grouped.
Would it be possible to achieve the below in fewer steps?
ID Date Events
1 09/10/2015 3
1 27/06/2014 3
1 03/01/2014 3
3 01/01/2015 2
3 08/12/2013 2
2 09/11/2015 2
2 16/10/2014 2
4 08/10/2015 1
5 09/11/2014 1
6 02/02/2013 1
Thanks
It looks to me like you're trying to sort by descending event, then by either the earliest or latest date (I can't tell which one from your explanation), also descending, and then by id. In your proc sql query, you could try calculating the min or max of the Date variable, grouped by event and id, and then sort the result by descending event, the descending min/max of the date, and id.

How to do alternate column sorting on two columns?

I know how to sort by two columns. But I wanted to know how to sort by alternate columns in neo4j.
Node name = Product
value | version
1 | 2
4 | 1
2 | 1
4 | 1
2 | 1
3 | 2
There are 2 values of versions 1 and 2. And value can be anything. First it will give higher value of version 1 , then it will give higher value of version 2, then it will give second higher value of version 1 , then it will give second higher value of version 2, and so on.
value | version
4 | 1
3 | 2
4 | 1
1 | 2
2 | 1
1 | 1
I don't know this type of sorting logically done or not through cypher query. I havn't done this type of logic in mysql also. Anyone can give me such clue of ne4j cypher query.
Update :
Match (p:Product)
RETURN p.value as value, p.version as version
ORDER BY version ASC, value DESC
This query sorts by version first then sorts by value. I don't want it.
I want alternate sort.
unwind[1,2,3] AS value unwind[1,2] AS version
RETURN value, version
ORDER BY value DESC , version ASC
value version
3 1
3 2
2 1
2 2
1 1
1 2

Resources