is there a possibility of fetch array while count the results
i'm trying to create an attendance monitoring which count the no. of presents, absents, total no. of meetings per sched. ID and Student ID
here is my database
SCHED_ID |STUDENT_ID |DATE | A_STAT
1234567 |2014-000003 |08/01/14 |Absent
123456 |2014-000003 |08/04/2014 |Present
1234567 |2014-000003 |08/10/2014 |Present
123456 |2014-000003 |08/10/2014 |Present
the output supposed to be like this
Subject Tot Num| Num of Days Present | Num of Days Absent
dasdasdasd 3 2 1
testing123 3 2 1
ASDASD 1 0 1
but its always been like this
Subject Tot Num | Num of Days Present | Num of Days Absent
dasdasdasd 3 1 1
testing123 3 1 1
ASDASD 3 1 1
$query = $this->db->query("SELECT * FROM (tbl_schedule c, tbl_subject d, tbl_attendance e) where e.SCHED_ID = c.SCHED_ID AND d.SUBJECT_CODE = c.SUBJECT_CODE AND (e.STUDENT_ID = '$si') GROUP BY e.SCHED_ID")->result_array();
$query1 = $this->db->query("SELECT * FROM (tbl_schedule c, tbl_subject d, tbl_attendance e) where e.SCHED_ID = c.SCHED_ID AND d.SUBJECT_CODE = c.SUBJECT_CODE AND e.A_STAT='Present' AND (e.STUDENT_ID = '$si') GROUP BY e.SCHED_ID AND e.A_STAT")->num_rows();
$query2 = $this->db->query("SELECT * FROM (tbl_schedule c, tbl_subject d, tbl_attendance e) where e.SCHED_ID = c.SCHED_ID AND d.SUBJECT_CODE = c.SUBJECT_CODE AND e.A_STAT='Absent' AND (e.STUDENT_ID = '$si') GROUP BY e.SCHED_ID AND e.A_STAT")->num_rows();
$query3 = $this->db->query("SELECT * FROM (tbl_schedule c, tbl_subject d, tbl_attendance e) where e.SCHED_ID = c.SCHED_ID AND d.SUBJECT_CODE = c.SUBJECT_CODE AND (e.STUDENT_ID = '$si') GROUP BY e.SCHED_ID ")->num_rows();
for($i=0;$i<sizeof($query);$i++){
$data[$i][0]=$query1; // present
$data[$i][1]=$query2; //absent
$data[$i][2]=$query3; //total
$data[$i][3]=$query[$i]['SUBJECT _DESC']; // subj
}
return $data;
Related
I'm trying to replace an element in 2D list
hand instead of one element at one specified index all element are changing
this is the list
`private val printS: MutableList<MutableList<Char>> = mutableListOf(mutableListOf())`
This is how I literate them
// adding S to the list
for (i in 1..seat) printS[0].add('S')
// now we have list of S char in the printS list
for (i in 1..row) {
printS.add(printS[0])
}
now try to change list 5 element 5
printS[5][5] = 'B'
this is the result
1 2 3 4 5 6 7
1 S S S S S B S
2 S S S S S B S
3 S S S S S B S
4 S S S S S B S
5 S S S S S B S
6 S S S S S B S
7 S S S S S B S
all the lists have changed not just one
I need to just change one Char so the result should be
1 2 3 4 5 6 7
1 S S S S S S S
2 S S S S S S S
3 S S S S S S S
4 S S S S S S S
5 S S S S S S S
6 S S S S S B S
7 S S S S S S S
Edit (based on OP's clarification in the first comment below):
val rows = 7
val cols = 7
val result = MutableList(rows) { MutableList(cols) { 'S' } }
result[5][5] = 'B'
Still valid:
Your code does not work because the inner loop is not nested due to you not having put parentheses on the outer loop:
for (i in 1..seat) printS[0].add('S')
for (i in 1..row) {
printS.add(printS[0])
}
This means – properly formatted - nothing else than:
for (i in 1..seat) {
printS[0].add('S')
}
for (i in 1..row) {
printS.add(printS[0])
}
Obsolete:
This should work:
val rows = 7
val cols = 7
val result: MutableList<MutableList<Char>> = mutableListOf()
for (row in 0 until rows) {
result.add(mutableListOf())
for (col in 0 until rows) {
result[row].add(if (row = 5 && col == 5) 'B' else 'S')
}
}
result.forEach(::println)
Output:
[S, S, S, S, S, S, S]
[S, S, S, S, S, S, S]
[S, S, S, S, S, S, S]
[S, S, S, S, S, S, S]
[S, S, S, S, S, S, S]
[S, S, S, S, S, B, S]
[S, S, S, S, S, S, S]
But a shorter way to create this matrix would be:
val result = List(rows) { row ->
List(cols) { col ->
if (row == 5 && col == 5) 'B' else 'S'
}
}
I have a table like the following in Google BigQuery. I am trying to get all possible unique combination(all subsets except the null subset) of the Item column partitioned on Group.
Group Item
1 A
1 B
1 C
2 X
2 Y
2 Z
I am looking for an output like the following:
Group Item
1 A
1 B
1 C
1 A,B
1 B,C
1 A,C
1 A,B,C
2 X
2 Y
2 Z
2 X,Y
2 Y,Z
2 X,Z
2 X,Y,Z
I have tried to use this accepted answer to incorporate Group to no avail:
How to get combination of value from single column?
Consider below approach
CREATE TEMP FUNCTION generate_combinations(a ARRAY<STRING>)
RETURNS ARRAY<STRING>
LANGUAGE js AS '''
var combine = function(a) {
var fn = function(n, src, got, all) {
if (n == 0) {
if (got.length > 0) {
all[all.length] = got;
} return;
}
for (var j = 0; j < src.length; j++) {
fn(n - 1, src.slice(j + 1), got.concat([src[j]]), all);
} return;
}
var all = []; for (var i = 1; i < a.length; i++) {
fn(i, a, [], all);
}
all.push(a);
return all;
}
return combine(a)
''';
with your_table as (
select 1 as _Group,'A' as Item union all
select 1, 'B' union all
select 1, 'C' union all
select 2, 'X' union all
select 2, 'Y' union all
select 2, 'Z'
)
select _group, item
from (
select _group, generate_combinations(array_agg(item)) items
from your_table
group by _group
), unnest(items) item
with output
Try this
with _data as
(
select 1 as _Group,'A' as Item union all
select 1 as _Group,'B' as Item union all
select 1 as _Group,'C' as Item union all
select 2 as _Group,'X' as Item union all
select 2 as _Group,'Y' as Item union all
select 2 as _Group,'Z' as Item
)
select distinct _Group ,Item from
(
select _Group,
Item
from _data
union all
select _Group,
string_agg(Item ,',') over(partition by _Group order by Item ) as item
from _data
union all
select a._Group ,
concat(a.item,',',b.item)
from _data a left join _data b on a._group = b._group and a.Item < b.Item
)
where item is not null
order by _group
I have a data set with these columns:-
FMID,County,WIC,WICcash
Here is a sample of data:-
1002267,Douglas,Y,N
21005876,Douglas,Y,N
1001666,Douglas,N,Y
I have grouped the data based on County and have filtered the data based on County = 'Douglas'. Here is the output:
(Douglas,{(1002267,Douglas,Y,N),(21005876,Douglas,Y,N),(1001666,Douglas,N,Y)})
Now if the WIC and WICcash columns have value as Y then I want to take the combine count of the values from both the columns.
Here, combining WIC and WICcash columns I have 3 Y values, so my output will be
Douglas 3
How can I achieve this?
Below is the code that I have written till now
load_data = LOAD 'PigPrograms/Markets/DATA_GOV_US_Farmers_Market_DataSet.csv' USING PigStorage(',') as (FMID:long,County:chararray, WIC:chararray, WICcash:chararray);
group_markets_by_county = GROUP load_data BY County;
filter_county = FILTER group_markets_by_county BY group == 'Douglas';
DUMP filter_county;
For looking inside a bag, you can use a nested-foreach.
A = LOAD 'input3.txt' AS (FMID:long,County:chararray, WIC:chararray, WICcash:chararray);
B = GROUP A by County;
describe B; /* B: {group: chararray,A: {(FMID: long,County: chararray,WIC: chararray,WICcash: chararray)}} */
C = FOREACH B {
FILTER_WIC_Y = FILTER A by WIC == 'Y';
COUNT_WIC_Y = COUNT(FILTER_WIC_Y);
FILTER_WICcash_Y = FILTER A by WICcash == 'Y';
COUNT_WICcash_Y = COUNT(FILTER_WICcash_Y);
GENERATE group, COUNT_WIC_Y + COUNT_WICcash_Y as count;
}
dump C;
Or, you can replace 'Y'&'N' into 1&0 and add them up.
A = LOAD 'input3.txt' AS (FMID:long,County:chararray, WIC:chararray, WICcash:chararray);
B = FOREACH A GENERATE FMID, County, (WIC == 'Y' ? 1 : 0 ) as wic, (WICcash == 'Y' ? 1 : 0 ) as wiccash;
C = GROUP B by County;
D = FOREACH C GENERATE group, SUM(B.wic) + SUM(B.wiccash) as count;
dump D;
Imagine I have a SQL table grades which has amongst other fields, the name of the student and the result of the grade:
| student | grade |
|----------|:---------:|
| Harry | Good |
| Ron | Good |
| Harry | Average |
| Harry | Fail |
| Hermione | Excellent |
| Hermione | Excellent |
| Ron | Average |
| ..... | .... |
If I wanted to select all the students with at least two 'Excellent' and zero 'Fail' grades one could do:
select student
from grades
group by student
having
sum(case when grade = 'Excellent' then 1 else 0 end) >= 2 and
sum(case when grade = 'Fail' then 1 else 0 end)
How could I translate such a query into Slick?
On the documentation the 'Having' clause they give seems simpler.
gradesTables
.groupBy(._student)
.map{ case(student, group) => (student, ???)}
.filter(???)
.list
On a related note, why do I get an error with the following:
gradesTables
.groupBy(._student)
.map{ case(student, group) => (student, group.filter(_.grade == "Fail").length)}
.list
The error is:
slick.SlickTreeException: Cannot convert node to SQL Comprehension
The following code in Slick will generate the SQL you need:
val query: Query[(Rep[String], Rep[Option[Int]], Rep[Option[Int]]), (String, Option[Int], Option[Int]), Seq] =
grades.groupBy( _.student ).map{ case (student, group) =>
val groupList = group.map(_.grade)
val gradeExcel = groupList.map( grade =>
Case.If(grade === "Excellent").Then(1).Else(0) ).sum
val gradeFail = groupList.map( grade =>
Case.If(grade === "Fail").Then(1).Else(0) ).sum
(student, gradeExcel, gradeFail)
}.
filter( g => g._2 >= 2 && g._3 === 0 )
// ...
println("Generated SQL:\n" + query.result.statements)
// Generated SQL:
// List(
// select "STUDENT", sum((case when ("GRADE" = 'Excellent') then 1 else 0 end)),
// sum((case when ("GRADE" = 'Fail') then 1 else 0 end)) from "GRADES" group by "STUDENT"
// having (sum((case when ("GRADE" = 'Excellent') then 1 else 0 end)) >= 2) and
// (sum((case when ("GRADE" = 'Fail') then 1 else 0 end)) = 0)
// )
db.run(query.result.map(println))
// Vector((Hermione,Some(2),Some(0)))
I have table store my answer and comment value. I need to get if it is null or answer value then combine. below query is not working. It's not getting any value from table.
SELECT DISTINCT
b.QM_ID,
b.QM_QCM_ID,
b.QM_Question,
b.QM_Type,
b.QM_Parent_Id,
null,
null
FROM question_master b
INNER JOIN Assessment_master d
ON (( d.AM_QM_ID = b.QM_Parent_Id
OR d.AM_QM_ID = b.QM_ID)
AND d.AM_HNM_ID = %d
AND d.AM_HM_ID = %d
AND d.AM_ASM_Local_Id = %#)
WHERE b.QM_Parent_Id != 0
AND b.QM_Status = 'A'
AND b.QM_QCM_ID = %#
AND b.QM_QRM_Id = %#
UNION
SELECT DISTINCT
b.QM_ID,
b.QM_QCM_ID,
b.QM_Question,
b.QM_Type,
b.QM_Parent_Id,
d.AM_Answer,
d.AM_Comments
FROM question_master b
INNER JOIN Assessment_master d
ON (( d.AM_QM_ID = b.QM_Parent_Id
OR d.AM_QM_ID = b.QM_ID)
AND d.AM_HNM_ID = %d
AND d.AM_HM_ID = %d
AND d.AM_ASM_Local_Id = %#)
WHERE b.QM_Parent_Id != 0
AND b.QM_Status = 'A'
AND b.QM_QCM_ID = %#
AND b.QM_QRM_Id = %#
Question_Master:
QM_ID QM_QRM_ID QM_LCM_ID QM_QCM_ID QM_Question QM_Parent_Id
432 5 19 1 question_parent 0
433 5 19 1 question_child 432
434 5 19 1 question_child1 432
Assessment_Master:
AM_ID AM_UM_ID AM_ASM_Local_Id AM_QM_ID AM_Answer AM_Comments AM_HNM_ID
1 8 1 433 NULL testing 1
If i answer the child question 433 and i'm getting only parent with 433 child. I'm not getting 434 question.
a.QM_ID a.QM_QCM_ID a.QM_Question a.QM_Parent_Id c.AM_Answer c.AM_Comments
432 1 question_parent 0 Null NULL
433 1 question_child 432 null value