I have a exercise model which has two attributes: title and points. The goal is getting a list of exercises until to reach an amount of points fixed previously. For example:
TABLE: exercises
title | points
==============
aaaaa | 3
bbbbb | 5
ccccc | 10
ddddd | 10
eeeee | 5
fffff | 3
#points <= 14
RESULT
aaaaa | 3
bbbbb | 5
eeeee | 5
or
aaaaa | 3
ccccc | 10
or
ccccc | 10
fffff | 3
... etc ...
Somthing like:
select_values("SELECT * FROM exercises WHERE SUM(points) < 14)
The following SQL should work in MySQL.
SET #psum := 0;
SELECT t1.* FROM (
SELECT m.*,
(#psum := #psum + m.points) AS cumulative_points
FROM (SELECT title, points from Exercises r ORDER BY RAND()) m
) t1
WHERE t1.cumulative_points <= 14;
However the ActiveRecord query would look a bit messy. E.g. something like:
random_exercises = Exercise.transaction do
Exercise.connection.execute("SET #psum = 0;")
# this is returned, because it's the last line in the block
Exercise.find_by_sql(%Q|
SELECT t1.* FROM (
SELECT m.*,
(#psum := #psum + m.points) AS cumulative_points
FROM (SELECT title, points from exercises r ORDER BY RAND()) m
) t1
WHERE t1.cumulative_points <= 14
|)
end
I think you need something like this:
Exercise.where(...).group(...).having('SUM(points) < ?', predefined_value)
Or, in your case, simply:
Exercise.having('SUM(points) < ?', predefined_value)
Related
How can I use indexed variable in a Firebird stored procedure? I mean, I have output parameters ODATE1, ODATE2, ODATE3, can I use as ':ODATE' || COUNTER to set the value in a loop?
I have 2 tables like this:
1. T_EMPLOYEE
---------------
| ID_E | NAME |
---------------
| 1 | Anne |
---------------
| 2 | Bob |
---------------
2. T_WORK
----------------------------
| ID_W | DATE_W | ID_E |
----------------------------
| 1 | 2021-01-01 | 1 |
----------------------------
| 2 | 2021-01-01 | 2 |
----------------------------
| 3 | 2021-01-02 | 1 |
----------------------------
| 4 | 2021-01-03 | 2 |
----------------------------
From that table I want to make a stored procedure to get this result:
DASHBOARD
-----------------------------------------------------------
| OID_E | ONAME | ODATE1 | ODATE2 | ODATE3 |
----------------------------------------------------------
| 1 | Anne | 1 | 1 | 0 |
-----------------------------------------------------------
| 2 | Bob | 1 | 0 | 1 |
-----------------------------------------------------------
I tried using EXECUTE STATEMENT like this in stored procedure:
DECLARE VARIABLE COUNTER INT;
BEGIN
FOR
SELECT ID_E, NAME FROM T_EMPLOYEE
INTO :OID_E, :ONAME
D0
BEGIN
COUNTER = 1;
WHILE (COUNTER<=3) DO
BEGIN
EXECUTE STATEMENT 'SELECT COUNT(*) FROM T_WORK WHERE DATE_W = ''2021-01-0' || COUNTER ||
''' AND ID_E = ' || :OID_E || ' INTO :ODATE' || COUNTER;
COUNTER = COUNTER + 1;
END
SUSPEND;
END
END /*procedure*/
The procedure can't be compiled.
Then I tried the simple one like this without COUNTER index replacement:
DECLARE VARIABLE COUNTER INT;
BEGIN
FOR
SELECT ID_E, NAME FROM T_EMPLOYEE
INTO :OID_E, :ONAME
D0
BEGIN
COUNTER = 1;
WHILE (COUNTER<=3) DO
BEGIN
EXECUTE STATEMENT 'SELECT COUNT(*) FROM T_WORK WHERE ID_E = :OID_E ' ||
' AND DATE_W =''2021-01-02'' INTO :ODATE2';
COUNTER = COUNTER + 1;
END
SUSPEND;
END
END /*procedure*/
The procedure can be compiled, but when I execute, it will raise this error:
SQL Error: Dynamic SQL Error SQL error code = #1 Token unknown - line #1, column #2 #1. Error Code: -104. Invalid token
Please give me insight. How to use EXECUTE STATEMENT to make a flexible looping to set indexed variable.
Or you have another solution for my needs.
Additional information: Firebird v2.5
You cannot dynamically reference PSQL variables (including parameters) like this. However, you don't need to jump through all these hoops to get the desired results.
You can use something like the following(which doesn't even need a procedure):
select e.ID_E as OID_E, e.NAME as ONAME,
count(case when w.DATE_W = date '2021-01-01' then 1 end) as ODATE1,
count(case when w.DATE_W = date '2021-01-02' then 1 end) as ODATE2,
count(case when w.DATE_W = date '2021-01-03' then 1 end) as ODATE3
from T_EMPLOYEE e
inner join T_WORK w
on w.ID_E = e.ID_E
group by e.ID_E, e.NAME
order by e.ID_E
Fiddle: https://dbfiddle.uk/?rdbms=firebird_3.0&fiddle=59066afc0fd7f6b5cb87eac99164e899
Given:
Lp | COL1 | COL 2 | COL 3
ROW 1 | X | | X
ROW 2 | | X | X
ROW 3 | X | X |
ROW 4 | | |
ROW 5 | 1 | 1.5 | 2
ROW 6 | 2 | 1 | 3
I would like to use SUMPRODUCT of Row 1 with Row 5 (and then Row 6) but only in the places where row has X (or rather where it is non empty).
Expected result for Row 1: 1 * 2 + 2 * 3 = 8 (because first and last column is not empty)
Expected result for Row 2: 1.5 * 1 + 2 * 3 = 7.5 (second and last col not empty)
Expected result for Row 3: 1 * 2 + 1.5 * 1 = 3.5 (first and second non empty)
Expected result for Row 4: 0
I appreciate your help.
Use:
=SUMPRODUCT(($B$6:$D$6)*($B$7:$D$7)*(B2:D2<>""))
You can achieve the same thing without SUMPRODUCT.
Create another three columns COL1',2',3', replace
every X with the corresponding product using IF condition.
For example at COL1',ROW1 you write a formula such as =IF(A1="X", A$5\*A$6, 0)
(here A1 is COL1,ROW1)
and drag it to fill COL1',2',3'.
Then you do SUM over COL1',2',3'.
I have a table with 2 columns that contain some rows with unique id pairs and some rows with pairs that are a mirrored duplicate of another row. I want to remove one of the duplicates.
id1 | id2
-----+-----
1 | 9
2 | 10
5 | 4
6 | 16
7 | 11
8 | 12
9 | 1
10 | 2
12 | 14
14 | 8
16 | 6
So 1 | 9 mirrors 9 | 1. I want to keep 1 | 9 but delete 9 | 1.
I've tried.
SELECT
id1,
id2
FROM
(
SELECT
id1, id2, ROW_NUMBER() OVER (PARTITION BY id1, id2 ORDER BY id1) AS occu
FROM
table
) t
WHERE
t.occu = 1;
But it has no effect.
I'm pretty new to this so any help you can give would be greatly appreciated.
====UPDATE====
I accepted the answer from #Mureinik and adapted it to work as a filter in a subquery:
SELECT
*
FROM
table
WHERE
id1 NOT IN (SELECT
id1
FROM
table a
WHERE
id1 > id2
AND
EXISTS (SELECT *
FROM table b
WHERE a.id1 = b.id2 AND a.id2 = b.id1));
You could arbitrarily decide to keep the rows where id1 < id2, and use an exists clause to find their counterparts:
DELETE FROM myable a
WHERE id1 > id2 AND
EXISTS (SELECT *
FROM mytable b
WHERE a.id1 = b.id2 AND a.id2 = b.id1)
Let's say I have a database with the schema:
A - String
B - String
C - Int
D - Int
And the database is
A | B | C | D
------------------
'F' | 'a' | 1 | 2
'F' | 'a' | 1 | 4
'F' | 'b' | 2 | 4
'Z' | 'a' | 3 | 7
'Z' | 'b' | 4 | 3
'Z' | 'a' | 6 | 5
And I want something along the lines of
F
a 2 6
b 2 4
Z
a 9 12
b 4 3
So essentially, group by A, then group by B, and SUM(C), SUM(D). How can I do this in ActiveRecord?
Here is an AR solution:
things = Thing.select('a, b, sum(c) as sum_c, sum(d) as sum_d').group(:a, :b)
things.each do |thing|
puts "#{thing.a} / #{thing.b} / #{thing.sum_c} / #{thing.sum_d}"
end
# F / a / 2 / 6
# F / b / 2 / 4
# Z / a / 9 / 12
# Z / b / 4 / 3
You can dynamically create properties in your AR classes (see as sum_c). Depending on what you do this might be easier/nicer with straight SQL.
I'm an intermediate excel user trying to solve an issue that feels a little over my head. Basically, I'm working with a spreadsheet which contains a number of orders associated with customer account #s and which have up to 5 metadata "tags" associated with them. I want to be use that customer account # to pull the 5 most commonly occurring metadata tags in order.
Here is a mock up of the first set of data
Account Number Order Number Metadata
5043 1 A B C D
4350 2 B D
4350 3 B C
5043 4 A D
5043 5 C D
1204 6 A B
5043 7 A D
1204 8 D B
4350 9 B D
5043 10 A C D
and the end result I'm trying to create
Account Number Most Common Tag 2nd 3rd 4th 5th
5043 A C B N/A
4350 B D C N/A N/A
1204 B A C N/A N/A
I was trying to work with the formula suggested here:
=ARRAYFORMULA(INDEX(A1:A7,MATCH(MAX(COUNTIF(A1:A7,A1:A7)),COUNTIF(A1:A7,A1:A7),0)))
But I don't know how to a) use the customer account # as a precondition for counting the text values within the range. b) how to circumvent the fact that the Match forumula only wants to work with a single column of data and c) how to read the 2nd, 3rd, 4th, and 5th most common values from this range.
The way I'm formatting this data isn't set in stone. I suspect the way I'm organizing this information is holding me back from simpler solutions, so any suggestions on re-thinking my organization would be just as helpful as insights on how to create a formula to do this.
Implementing this kind of frequency analysis using built-in functions is likely to be a frustrating exercise. Since you are working with Google Sheets, take advantage of the custom functions, written in JavaScript and placed into a script bound to the sheet (Tools > Script Editor).
The function I wrote for this purpose is below. Entering something like =tagfrequency(A2:G100) in the sheet will produce desired output:
+----------------+-----------------+-----+-----+-----+-----+
| Account Number | Most Common Tag | 2nd | 3rd | 4th | 5th |
| 5043 | D | A | C | B | N/A |
| 4350 | B | D | C | N/A | N/A |
| 1204 | B | A | D | N/A | N/A |
+----------------+-----------------+-----+-----+-----+-----+
Custom function
function tagFrequency(arr) {
var dict = {}; // the object in which to store tag counts
for (var i = 0; i < arr.length; i++) {
var acct = arr[i][0];
if (acct == '') {
continue; // ignore empty rows
}
if (!dict[acct]) {
dict[acct] = {}; // new account number
}
for (var j = 2; j < arr[i].length; j++) {
var tag = arr[i][j];
if (tag) {
if (!dict[acct][tag]) {
dict[acct][tag] = 0; // new tag
}
dict[acct][tag]++; // increment tag count
}
}
}
// end of recording, begin sorting and output
var output = [['Account Number', 'Most Common Tag', '2nd', '3rd', '4th', '5th']];
for (acct in dict) {
var tags = dict[acct];
var row = [acct].concat(Object.keys(tags).sort(function (a,b) {
return (tags[a] < tags[b] ? 1 : (tags[a] > tags[b] ? -1 : (a > b ? 1 : -1)));
})); // sorting by tag count, then tag name
while (row.length < 6) {
row.push('N/A'); // add N/A if needed
}
output.push(row); // add row to output
}
return output;
}
You also could get this report:
Account Number Tag count
1204 B 2
1204 A 1
1204 D 1
4350 B 3
4350 D 2
4350 C 1
5043 D 5
5043 A 4
5043 C 3
5043 B 1
with the formula:
=QUERY(
{TRANSPOSE(SPLIT(JOIN("",ArrayFormula(REPT(FILTER(A2:A,A2:A<>"")&",",5))),",")),
TRANSPOSE(SPLIT(ArrayFormula(CONCATENATE(FILTER(C2:G,A2:A<>"")&" ,")),",")),
TRANSPOSE(SPLIT(rept("1,",counta(A2:A)*5),","))
},
"select Col1, Col2, Count(Col3) where Col2 <>' ' group by Col1, Col2
order by Col1, Count(Col3) desc label Col1 'Account Number', Col2 'Tag'")
The formula will count the number of occurrences of any tag.