get top n most viewed photos in a given period of time - sql

I want to display 3 most viewed photos for 7 days. Let's say that I have 4 images, A, B, C and D.
Number of total views:
A - 300
B - 305
C - 310
D - 400
Number of views in the last 7 days from now:
A - 100
B - 90
C - 95
D - 45
So the 3 most viewed photos for 7 days are A, B and C.
But after 10 minutes it changed! Let's say that someone visited photo D a lot of times, and the table looks like this:
A - 95 (yeah, it can decrease)
B - 92
C - 98
D - 105
Now, the 3 most viewed photos for 7 days are B, C and D.
The question is: how to structure the database? I only have information about a total number of views and the date when the photo was published.

Related

How toPick up item which value greater than a number every month, and create new column

I have a dataset as this below, want to pick up the name which applied more than 3 times every month.
Which is passion, otherwise is n-passion
Month Name Applied
4 a 3
4 b 2
4 c 4
5 a 3
5 b 4
5 c 2
6 a 5
6 b 7
6 c 0
enter image description here
Wanted output as below
Name Status
a passion
b n-passion
c c-passion
enter image description here
enter image description here
How to achive this?

Pandas - create new rows with constraints and forward fill them with existing data

I have two dataframes. We'll call them main and valid_dates
main:
name from amount days
A 7/31/18 200 1
B 7/31/18 300 1
C 7/30/18 200 1
D 7/27/18 100 3
......
G 7/17/18 50 1
H 7/13/18 150 4
valid_dates:
date
7/13/18
7/16/18
7/17/18
7/27/18
7/30/18
7/31/18
Here's where it gets complicated. I need to expand the rows where days > 1, but I can't use a non-valid date.
output:
name from amount days
A 7/31/18 200 1
B 7/31/18 300 1
C 7/30/18 200 1
D 7/27/18 100 3
......
G 7/17/18 50 1
H 7/16/18 150 1
H 7/13/18 150 3
alternate (and equally valid) output:
name from amount days rep_days rep_date
A 7/31/18 200 1 1 7/31/18
B 7/31/18 300 1 1 7/31/18
C 7/30/18 200 1 1 7/30/18
D 7/27/18 100 3 3 7/27/18
......
G 7/17/18 50 1 1 7/17/18
H 7/13/18 150 4 1 7/16/18
H 7/13/18 150 4 3 7/13/18
To clarify what happened:
-7/27 + 3 = 7/30. However, no date between 7/27 and 7/30 was in valid_dates, so that entry is left alone with 7/27 representing 3 days.
-7/13 + 4 = 7/17. The only date between 7/13 and 7/17 in valid_dates is 7/16. Therefore a 7/16 entry will be added, and it'll represent one day. 7/13 has to represent 3. Rest of the row data is duplicated.
-Going by the above example. If 7/15 AND 7/16 were in valid_dates, then a 7/15 and 7/16 entry would be added, each representing one day. 7/13 would represent 2. Rest of the row data is duplicated.
You can assume that where days > 1, from + days will never be greater than another entry in the from column.
I realize this may be confusing so let me know if you have any questions.

How to write sql query to generate a group no for each grouped record

following is scenario:
I have data in following format:
entryid , ac_no, db/cr, amt
-----------------------------------------------
1 10 D 5
1 11 C 5
2 01 D 8
2 11 C 8
3 12 D 10
3 13 C 10
4 14 D 5
4 16 C 5
5 14 D 2
5 17 C 2
6 14 D 3
6 18 C 3
I want data in following format:
So far i have acheived the first 3 columns by query
select wm_concat(entryid),ac_no,db_cr,Sum(amt) from t1 group by ac_no,db_cr
wm_Concat(entryid),ac_no, db/cr, Sum(amt), set_id
------------------------------------------------
1 10 D 5 S1
2 01 D 8 S1
1,2 11 C 13 S1
3 12 D 10 S2
3 13 C 10 S2
4,5,6 14 D 10 S3
4 16 C 5 S3
5 17 C 2 S3
6 18 C 3 S3
I want an additional column `set_id` that either shows this S1, S2.. or any number 1,2.. so that the debit & credit entries sets can be identified.
I am making sets of debit and credit entries based on their Ac_no values.
Any little help will be highly appreciated. Thanks
Create a new column say set and give a unique identifier to the particular set. So for example the first three records will have set id S1, next two will have S2 and so on.
To distinguish a transaction from a set you can use column db/cr along with newly added set column. You can identify that the 3rd row is a set since it's transaction type is 'C' whereas the transactions are of type 'D'.
Here I have assumed that your transactions are debit only, if not please provide more details in the question. Hope this helps.

Custom order in TSQL: page of N records with max X of a value

I'm trying to order a table such that every 'page' with N number of records (i.e page size) includes a maximum of X records of each field value.
Example table values:
Value
-----
a
a
c
a
b
b
a
c
b
a
c
c
c
d (and so on..)
(Value can be any text, in random order. For simplicity, I've used alphabets.)
If page size is 5 and max of each field value is 2, the results can be as listed below (or in a different random order), as long as every 5 consecutive records have a max of 2 records of each character:
Value
-----
a ┐
a |
c |── first page of size 5 with max 2 records of a value
b |
b ┘
a ┐
a |
c |── second page of size 5 with max 2 records of a value
b |
c ┘
a ┐
c |── last page of size 5 (or less) with max 2 records of a value
c |
d ┘
Any help is appreciated.

Excel VBA function to solve impossible round-robin tournament roster with venue constraint

I am really having difficulty generating a round-robin tournament roster with the following conditions:
10 Teams (Teams 1 - 10)
5 Fields (Field A - E)
9 Rounds (Round 1 - 9)
Each team must play every other team exactly once.
Only two teams can play on a field at any one time. (i.e. all 5 fields always in use)
No team is allowed to play on any particular field more than twice. <- This is the problem!
I have been trying on and off for many years to solve this problem on paper without success. So once and for all, I would like to generate a function in Excel VBA to test every combination to prove it is impossible.
I started creating a very messy piece of code that generates an array using nested if/while loops, but I can already see it's just not going to work.
Is there anyone out there with a juicy piece of code that can solve?
Edit: Thanks to Brian Camire's method below, I've been able to include further desirable constraints and still get a solution:
No team plays the same field twice in a row
A team should play on all the fields once before repeating
The solution is below. I should have asked years ago! Thanks again Brian - you are a genius!
Round 1 2 3 4 5 6 7 8 9
Field A 5v10 1v9 2v4 6v8 3v7 4v10 3v9 7v8 1v2
Field B 1v7 8v10 3v6 2v9 4v5 6v7 1v8 9v10 3v5
Field C 2v6 3v4 1v10 5v7 8v9 1v3 2v5 4v6 7v10
Field D 4v9 2v7 5v8 3v10 1v6 2v8 4v7 1v5 6v9
Field E 3v8 5v6 7v9 1v4 2v10 5v9 6v10 2v3 4v8
I think I've found at least one solution to the problem:
Round Field Team 1 Team 2
1 A 3 10
1 B 7 8
1 C 1 9
1 D 2 4
1 E 5 6
2 A 8 10
2 B 1 5
2 C 2 6
2 D 3 7
2 E 4 9
3 A 1 4
3 B 2 3
3 C 8 9
3 D 5 7
3 E 6 10
4 A 6 7
4 B 4 10
4 C 2 8
4 D 5 9
4 E 1 3
5 A 2 9
5 B 3 8
5 C 4 7
5 D 1 6
5 E 5 10
6 A 3 9
6 B 4 5
6 C 7 10
6 D 6 8
6 E 1 2
7 A 5 8
7 B 6 9
7 C 1 10
7 D 3 4
7 E 2 7
8 A 4 6
8 B 2 10
8 C 3 5
8 D 1 8
8 E 7 9
9 A 2 5
9 B 1 7
9 C 3 6
9 D 9 10
9 E 4 8
I found it using the OpenSolver add-in for Excel (as the problem was too large for the built-in Solver feature). The steps were something like this:
Set up a table with 2025 rows representing the possible matches -- that is, possible combinations of round, field, and pair of teams (with columns like the table above), plus one extra column that will be a binary (0 or 1) decision variable indicating if the match is to be selected.
Set up formulas to use the decision variables to calculate: a) the number matches at each field in each round, b) the number of matches between each pair of teams, c) the number of matches played by each team in each round, and, d) the number of matches played by each team at each field.
Set up a formula to use the decision variables to calculate the total number of matches.
Use OpenSolver to solve a model whose objective is to maximize the result of the formula from Step 3 by changing the decision variables from Step 1, subject to the constraints that the decision variables must be binary, the results of the formulas from Steps 2.a) through c) must equal 1, and the results of the formulas from Step 2.d) must be less than or equal to 2.
The details are as follows...
For Step 1, I set up my table so that columns A, B, C, and D represented the Round, Field, Team 1, and Team 2, respectively, and column E represented the decision variable. Row 1 contained the column headings, and rows 2 through 2026 each represented one possible match.
For Step 2.a), I set up a vertical list of rounds 1 through 9 in cells I2 through I10, a horizontal list of fields A through E in cells J1 through N1, and a series of formulas to calculate the number of matches in each field in each round in cells J2 through N10 by starting with =SUMIFS($E$2:$E$2026,$A$2:$A$2026,$I2,$B$2:$B$2026,J$1) in cell J2 and then copying and pasting.
For Step 2.b), I set up a vertical list of teams 1 through 9 in cells I13 through I21, a horizontal list of opposing teams 2 through 10 in cells J12 through R12, and a series of formulas to calculate the number of matches between each pair of teams in the "upper right triangular half" of cells J13 through R21 (including the diagonal) by starting with =SUMIFS($E$2:$E$2026,$C$2:$C$2026,$I13,$D$2:$D$2026,J$12) in cell J13 and then copying and pasting.
For Step 2.c), I set up a vertical list of teams 1 through 10 in cells I24 through I33, a horizontal list of rounds 1 through 9 in cells J23 through R23, and a series of formulas to calculate the number of matches played by each team in each round in cells J24 through R33 by starting with =SUMIFS($E$2:$E$2026,$C$2:$C$2026,$I24,$A$2:$A$2026,J$23)+SUMIFS($E$2:$E$2026,$D$2:$D$2026,$I24,$A$2:$A$2026,J$23) in cell J24 and then copying and pasting.
For Step 2.d), I set up a vertical list of teams 1 through 10 in cells I36 through I45, a horizontal list of fields A through B in cells J35 through N45, and series of formulas to calculate the number of matches played by each team at each field in cells J36 through N45 by starting with =SUMIFS($E$2:$E$2026,$C$2:$C$2026,$I36,$B$2:$B$2026,J$35)+SUMIFS($E$2:$E$2026,$D$2:$D$2026,$I36,$B$2:$B$2026,J$35) in cell J36 and then copying and pasting.
For Step 3, I set up a formula to calculate the total number of matches in cell G2 as =SUM($E$2:$E$2026).
For Step 4, in the OpenSolver Model dialog (available from Data, OpenSolver, Model) I set the Objective Cell to $G$2, the Variable Cells to $E$2:$E$2026, and added constraints as described above and detailed below (sorry that the constraints are not listed in the order that I described them):
Note that, for the constraints described in Step 2.b), I needed to add the constraints separately for each row, since OpenSolver raised an error message if the constraints included the blank cells in the "lower left triangular half".
After setting up the model, OpenSolver highlighted the objective, variable, and constraint cells as shown below:
I then solved the problem using OpenSolver (via Data, OpenSolver, Solve). The selected matches are the ones with a 1 in column E. You might get a different solution than I did, as there might be many feasible ones.
come on ... that's an easy one for manual solution ;-)
T1 T2 VE
1 2 A
1 3 A
1 4 B
1 5 B
1 6 C
1 7 C
1 8 D
1 9 D
1 10 E
2 3 A
2 4 B
2 5 B
2 6 C
2 7 C
2 8 D
2 9 D
2 10 E
3 4 C
3 5 C
3 6 D
3 7 D
3 8 E
3 9 E
3 10 B
4 5 C
4 6 D
4 7 D
4 8 E
4 9 E
4 10 A
5 6 E
5 7 E
5 8 A
5 9 A
5 10 D
6 7 E
6 8 A
6 9 A
6 10 B
7 8 B
7 9 B
7 10 A
8 9 B
8 10 C
9 10 C
As far as I have checked no team more then twice on the same venue. Please double check.
To divide it into rounds should be a easy one.
Edit: this time with only 5 venues :-)
Edit 2: now also with allocated rounds :-)
Edit 3: deleted the round allocation again because it was wrong.