I have a table called vw_results which holds the result profile for a particular student:
idNum |courseUnit | marks | Gpp | grade| id | semseterID | sessionName
06/021| 2 | 47 | 8 | B+ | 1 | 1 | 2010/11
06/021| 3 | 56 | 7 | C | 1 | 1 | 2010/11
. | 4 | 34 | 5 | C | 1 | 1 | 2010/11
. | 5 | 34 | 0 | F | 1 | 1 | 2010/11
. | 2 | 89 | 10 | A | 1 | 2 | 2010/11
. | 3 | 45 | 4 | D | 1 | 2 | 2010/11
. | 4 | 56 | 10 | C | 1 | 2 | 2010/11
. | 2 | 67 | 12 | B+ | 2 | 1 | 2011/12
. | 1 | 70 | 15 | A | 2 | 1 | 2011/12
. | 2 | 80 | 10 | A | 2 | 2 | 2011/12
. | 3 | 90 | 5 | A | 2 | 2 | 2011/12
To calculate the GPA
"SELECT SUM( vwr.courseUnit ) cummUnit, SUM( vwr.GPP ) cummGPP, (
SUM( vwr.GPP ) / SUM( vwr.courseUnit ))cummGPA
FROM vw_result vwr
WHERE vwr.Grade NOT IN ('F') AND vwr.sessionID=".$_GET['sessionID']." AND vwr.semesterID=".$_GET['semesterID'].";"
if $_GET['sessionID']=1 and $_GET['semesterID']=2
then
|cummGPA|
|1.913 |
if $_GET['sessionID']=2 and $_GET['semesterID']=1
then
|cummGPA|
|2.730 |
if $_GET['sessionID']=2 and $_GET['semesterID']=2
then
|cummGPA|
|2.774 |
any suggestions on how to go about this?!
THANKS!
Uff, you should ask more clearly, no one will take time to understand you question. Its just that I have nothing to do..
If i understand what you want correctly then it will be:
select count(id) from vw_results group by (id, semseterID) having (id=1 and semseterID=1) or (id=1 and semseterID=2) or (id=2 and semseterID=1) or (id=2 and semseterID=2)
Hope it will work, never tried it.
Related
I am generating sequential reference numbers like complaint/1, complaint/2 when an insertion is made. I get what the previously generated value is (complaint/2) and then increment.
But when two users submit at the same time, I sometimes get same reference nos for both complaints.
How do I prevent this?
SELECT RIGHT(Date_format(from_financial_year_,'%Y'),2),
RIGHT(Date_format(to_financial_year_,'%Y'),2)
INTO from_financial_year_,
to_financial_year_;SELECT rec.receipt_ref_no
INTO last_receipt_ref_num_
FROM svk_apt_receipts rec
WHERE Replace(Substring_index(rec.receipt_ref_no, '/', 2),'REC/','') = Concat(from_financial_year_,to_financial_year_)
AND rec.customer_id = customer_id_
AND rec.association_id = association_id_
ORDER BY rec.receipt_id DESC limit 1;IF(last_receipt_ref_num_ IS NULL) then
SELECT 1
INTO max_ref_id_;
else
SELECT (replace(last_receipt_ref_num_, concat('REC/',from_financial_year_,to_financial_year_,'/'),'')+1)
INTO max_ref_id_;ENDIF;SELECT Concat('REC/',from_financial_year_,to_financial_year_,'/',max_ref_id_)
INTO receipt_ref_no_;INSERT INTO svk_apt_receipts
(
receipt_ref_no, paid, payable, is_paid, master_receipt_to_id, receipt_from_id, receipt_to_id, receipt_date,
receipt_mode, transaction_ref_no, customer_id, association_id, is_active, created_by, created_on, receipt_status_id, remarks
)
VALUES
( receipt_ref_no_, _total_amount, 0,1,3, receipt_from_id_, receipt_to_id_, Cast(Now()AS DATE), 3, _transaction_ref_no,
customer_id_, association_id_, 1, _created_by, Now(), 2, 'Paid through Payment Gateway'
);
As mentioned in comments, just store an autoincrementing id. All the other stuff can be handled by trivial queries and/or your presentation layer.
By way of example...
DROP TABLE IF EXISTS my_table;
CREATE TABLE my_table
(id SERIAL PRIMARY KEY
,user INT NOT NULL
);
INSERT INTO my_table (user) VALUES
(1),(1),(3),(3),(5),(2),(1),(8),(4),(5),(7),(5),(5),(4),(1),(2),(3),(6),(4),(6),(1),(5),(1),(8);
SELECT * FROM my_table;
+----+------+
| id | user |
+----+------+
| 1 | 1 |
| 2 | 1 |
| 3 | 3 |
| 4 | 3 |
| 5 | 5 |
| 6 | 2 |
| 7 | 1 |
| 8 | 8 |
| 9 | 4 |
| 10 | 5 |
| 11 | 7 |
| 12 | 5 |
| 13 | 5 |
| 14 | 4 |
| 15 | 1 |
| 16 | 2 |
| 17 | 3 |
| 18 | 6 |
| 19 | 4 |
| 20 | 6 |
| 21 | 1 |
| 22 | 5 |
| 23 | 1 |
| 24 | 8 |
+----+------+
24 rows in set (0.01 sec)
SELECT x.*
, COUNT(*) complaint
FROM my_table x
JOIN my_table y
ON y.user = x.user
AND y.id <= x.id
GROUP
BY x.id
ORDER
BY user
, id;
+----+------+-----------+
| id | user | complaint |
+----+------+-----------+
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 7 | 1 | 3 |
| 15 | 1 | 4 |
| 21 | 1 | 5 |
| 23 | 1 | 6 |
| 6 | 2 | 1 |
| 16 | 2 | 2 |
| 3 | 3 | 1 |
| 4 | 3 | 2 |
| 17 | 3 | 3 |
| 9 | 4 | 1 |
| 14 | 4 | 2 |
| 19 | 4 | 3 |
| 5 | 5 | 1 |
| 10 | 5 | 2 |
| 12 | 5 | 3 |
| 13 | 5 | 4 |
| 22 | 5 | 5 |
| 18 | 6 | 1 |
| 20 | 6 | 2 |
| 11 | 7 | 1 |
| 8 | 8 | 1 |
| 24 | 8 | 2 |
+----+------+-----------+
24 rows in set (0.00 sec)
i have 3 tables. 2 reference table and one table transactions.
Table puskesmas
mysql> select id,nama from puskesmas;
+----+----------------------+
| id | nama |
+----+----------------------+
| 1 | BAMBANGLIPURO |
| 2 | BANGUNTAPAN |
| 3 | BANTUL |
| 4 | DLINGO |
| 5 | IMOGIRI |
| 6 | JETIS |
| 7 | KASIHAN |
| 8 | KRETEK |
| 9 | PAJANGAN |
| 10 | PANDAK |
+----+----------------------+
Table poli
mysql> select * from poli;
+----+---------------+
| id | nama |
+----+---------------+
| 1 | BP |
| 2 | KIA |
| 3 | GIGI |
| 4 | JIWA |
+----+---------------+
Table loket
mysql> select pasien_id,poli_id,puskesmas_id from loket limit 10;
+-----------+---------+--------------+
| pasien_id | poli_id | puskesmas_id |
+-----------+---------+--------------+
| 1 | 1 | 1 |
| 5 | 2 | 2 |
| 1 | 1 | 3 |
| 9 | 3 | 4 |
| 1 | 3 | 5 |
| 5 | 2 | 1 |
| 1 | 1 | 3 |
| 8 | 1 | 2 |
| 10 | 3 | 6 |
| 5 | 2 | 5 |
+-----------+---------+--------------+
I want to show the total number (count) of visits to puskesmas table. then made field all_total
fter that I want to take patient visits where poli_id = 1 and used as a column bp. for other poly are the same as the
+----+----------------------+--------------+------+------+------+------+
| id | nama | All Total | BP | KIA | GIGI | JIWA |
+----+----------------------+--------------+------+------+------+------+
| 1 | BAMBANGLIPURO | 30 | 10 | 3 | 15 | 2 |
| 2 | BANGUNTAPAN | 35 | 20 | 4 | 11 | 0 |
| 3 | BANTUL | 15 | 10 | 0 | 5 | 0 |
| 4 | DLINGO | 12 | 10 | 1 | 1 | 0 |
| 5 | IMOGIRI | 10 | 5 | 2 | 1 | 2 |
| 6 | JETIS | 25 | 13 | 3 | 7 | 2 |
| 7 | KASIHAN | 20 | 10 | 10 | 0 | 0 |
| 8 | KRETEK | 23 | 20 | 1 | 1 | 1 |
| 9 | PAJANGAN | 10 | 5 | 1 | 3 | 1 |
| 10 | PANDAK | 0 | 0 | 0 | 0 | 0 |
+----+----------------------+--------------+------+------+------+------+
I've tried to make a query to display like this but it always fails to hold. Can anyone help to make such queries desired results
i have tried query like this :
select puskesmas_id,
sum(case when poli_id=1 then 1 else 0 end) bp,
sum(case when poli_id=5 then 0 else 1 end) gigi,
count(id) total
from loket
group by puskesmas_id
and result like this :
+--------------+------+------+-------+
| puskesmas_id | bp | gigi | total |
+--------------+------+------+-------+
| 1 | 97 | 126 | 143 |
| 6 | 74 | 185 | 210 |
| 8 | 131 | 179 | 190 |
| 7 | 90 | 92 | 98 |
| 2 | 33 | 39 | 54 |
| 4 | 158 | 248 | 263 |
| 3 | 66 | 68 | 72 |
+--------------+------+------+-------+
but puskesmas_id the value 0 does not perform data when querying
I already read Switch id numbers of two rows in MySql and Mysql: Swap data for different rows, yet I don't understand. Suppose a table colors that looks like this:
------------------------------------------
| id | box | pouch | color | value |
-----------------------------------------
| 1 | 1 | 1 | red | 30 |
| 2 | 1 | 1 | blue | 20 |
| 3 | 1 | 1 | green | 10 |
| 4 | 1 | 1 | yellow | 40 |
| 5 | 1 | 1 | purple | 20 |
| 6 | 1 | 1 | black | 50 |
| 7 | 1 | 2 | red | 30 |
| 8 | 1 | 2 | blue | 20 |
| 9 | 1 | 2 | green | 10 |
| 10 | 1 | 2 | yellow | 40 |
| 11 | 1 | 2 | purple | 20 |
| 12 | 1 | 2 | black | 50 |
| 13 | 2 | 1 | red | 35 |
| 14 | 2 | 1 | blue | 25 |
| 15 | 2 | 1 | green | 15 |
| 16 | 2 | 1 | yellow | 45 |
| 17 | 2 | 1 | purple | 25 |
| 18 | 2 | 1 | black | 55 |
| 19 | 2 | 2 | red | 35 |
| 20 | 2 | 2 | blue | 25 |
| 21 | 2 | 2 | green | 15 |
| 22 | 2 | 2 | yellow | 45 |
| 23 | 2 | 2 | purple | 25 |
| 24 | 2 | 2 | black | 55 |
------------------------------------------
How can I swap the "value" of a row with another, leaving the "id", "box", "pouch", and "color" intact?
Notes:
1. I need to swap the value of box='1' with box='2'
2. The number of rows are dynamic
Example:
SWAP ROWS OF VALUE WITH BOX='1' WITH ROWS OF VALUE WITH BOX='2'
Result:
------------------------------------------
| id | box | pouch | color | value |
-----------------------------------------
| 1 | 1 | 1 | red | 35 |
| 2 | 1 | 1 | blue | 25 |
| 3 | 1 | 1 | green | 15 |
| 4 | 1 | 1 | yellow | 45 |
| 5 | 1 | 1 | purple | 25 |
| 6 | 1 | 1 | black | 55 |
| 7 | 1 | 2 | red | 35 |
| 8 | 1 | 2 | blue | 25 |
| 9 | 1 | 2 | green | 15 |
| 10 | 1 | 2 | yellow | 45 |
| 11 | 1 | 2 | purple | 25 |
| 12 | 1 | 2 | black | 55 |
| 13 | 2 | 1 | red | 30 |
| 14 | 2 | 1 | blue | 20 |
| 15 | 2 | 1 | green | 10 |
| 16 | 2 | 1 | yellow | 40 |
| 17 | 2 | 1 | purple | 20 |
| 18 | 2 | 1 | black | 50 |
| 19 | 2 | 2 | red | 30 |
| 20 | 2 | 2 | blue | 20 |
| 21 | 2 | 2 | green | 10 |
| 22 | 2 | 2 | yellow | 40 |
| 23 | 2 | 2 | purple | 20 |
| 24 | 2 | 2 | black | 50 |
------------------------------------------
Anyone got some ideas? Thanks
Create a table that holds all values you want to swap.
create table tmp_t as select * from t;
Then update your table twice:
update t t1
inner join tmp_t t2 on t1.color = t2.color
and t1.pouch = t2.pouch
and t1.box = 1 and t2.box = 2
set t1.value = t2.value;
update t t1
inner join tmp_t t2 on t1.color = t2.color
and t1.pouch = t2.pouch
and t1.box = 2 and t2.box = 1
set t1.value = t2.value;
And that's it. See it working live in an sqlfiddle.
To do the swap, try resetting one of them to a temporary value. You need to do that, otherwise the second update will affect the first one.
UPDATE mytable SET box = -1 WHERE box = 1; -- Set box 1 to a temporary value
UPDATE mytable SET box = 1 WHERE box = 2; -- Set box 2 to box 1
UPDATE mytable SET box = 2 WHERE box = -1; -- Set box 1 to box 2
I've assumed it is not possible to have a negative box number. If -1 is permissible, use something else, such as null.
For extra safety, wrap the above in a transaction, so if anything goes wrong, you can rollback.
I have table from mysql like below:
id | name | grade | k1 | k2 | k3 | s1| s2 | s3| e1 | e1 | e3 |
1 | Aa | 5 | 1 | 0 | 5 | 3 | 2 | 1 | 0 | 6 | 1 |
2 | Bb | 1 | 1 | 3 | 5 | 3 | 5 | 3 | 4 | 6 | 1 |
3 | Cc | 2 | 1 | 4 | 2 | 2 | 2 | 4 | 0 | 6 | 1 |
4 | Dd | 4 | 1 | 3 | 5 | 3 | 3 | 1 | 0 | 6 | 1 |
5 | Ee | 3 | 1 | 5 | 2 | 1 | 0 | 5 | 0 | 6 | 1 |
6 | Ff | 2 | 1 | 3 | 1 | 3 | 4 | 2 | 0 | 6 | 1 |
7 | Gg | 5 | 1 | 1 | 5 | 5 | 2 | 1 | 0 | 6 | 1 |
Using FOR and FOREACH looping, i do able to show all row. But in final view/table i want to
SUM [k1,k2,k3], SUM[s1,s2,s3] and SUM[e1,e2,e3]
So it will place new colum as K, S and E in each row.
Here are my code:
for ($i = 0; $i < Evaluation::model()->count(); $i++) {
foreach (Yii::app()->db->createCommand()
->from('evaluation')
->queryAll() as $item) {
// Row-Column start here
// id | name | grade | K | S | E |
}
}
thanks.
I love the query builder. So here is one of the solution.
$result = Yii::app()->db->createCommand()->
select('id, name, grade, (k1+k2+k3) AS K, (s1+s2+s3) AS S, (e1+e2+e3) AS E')->
from(MyModel::model()->tableName())->
queryAll();
var_dump($result);
select k1+k2+k3, s1+s2+s3, e1+e2+e3 from t
and it is not yii question, it is about MySql
If you want to express it in Yii, you can
Yii:app()->createCommands()
->select('k1+k2+k3, s1+s2+s3, e1+e2+e3')
->from('t')
->queryAll();
I want to concatenate and count data of the same column, so I can concatenate but I can not count the repeated data.
Here's my table of data:
| ID | bills | class |
|-----|-------|-------|
| 1 | 0.5 | 2 |
| 2 | 1 | 1 |
| 3 | 0.5 | 2 |
| 5 | 1 | 3 |
| 6 | 0 | 2 |
| 7 | 0.5 | 1 |
| 8 | 1 | 2 |
| 9 | 1 | 3 |
| 10 | 0.5 | 1 |
| 11 | 0 | 2 |
| 12 | 1 | 1 |
| 13 | 0 | 3 |
| 14 | 1 | 2 |
| 15 | 0 | 1 |
| 16 | 0 | 1 |
| 17 | 0.5 | 3 |
| 18 | 0 | 3 |
| 13 | 0.5 | 3 |
Here's my sql query I'm using to concatenate data:
SELECT class AS lesson,
GROUP_CONCAT( bills ORDER BY bills ) AS bills
FROM tb_presence
GROUP BY class;
Here's my result below:
| class | bills |
|-------|------------------|
| 1 | 1,0.5,0.5,1,0,0 |
| 2 | 0.5,0,1,0,1 |
| 3 | 1,1,0,0.5,0,0.5 |
Now I would like to count the data that are equal, but continue with the same concatenation.
I want to "count" the data with the same values and display concatenated (column observation and only to help understanding)
| class | bills | observation |
|-------|-------|-----------------------------|
| 1 | 2,2,2 | (2=0+0) (2=0.5+0.5) (2=1+1) |
| 2 | 2,1,2 | (2=0+0) (1=0.5) (2=1+1) |
| 3 | 2,2,2 | (2=0+0) (2=0.5+0.5) (2=1+1) |
Is this really possible?
Here is a solution (thanks to #wchiquito for the sqlfiddle) See http://sqlfiddle.com/#!2/2d2c8/1
As you can see it cannot dynamically determine the bills' values and count them. But there is a count per bill value that you want.
SELECT class AS lesson,
GROUP_CONCAT( bills ORDER BY bills ) AS bills
,SUM(IF(bills=0,1,0)) AS Count0
,SUM(IF(bills=0.5,1,0)) AS Count05
,SUM(IF(bills=1,1,0)) AS Count1
,COUNT(*) AS totalRecords
,COUNT(*)
- SUM(IF(bills=0,1,0))
- SUM(IF(bills=0.5,1,0))
- SUM(IF(bills=1,1,0))
AS Missing
FROM tb_presence GROUP BY class;
I added an extra record to show how the 'missing' column could show if you were not taking all values into consideration.
Results
| LESSON | BILLS | COUNT0 | COUNT05 | COUNT1 | TOTALRECORDS | MISSING |
|--------|-----------------------------------------|--------|---------|--------|--------------|---------|
| 1 | 0.00,0.00,0.50,0.50,1.00,1.00,1.00,4.00 | 2 | 2 | 3 | 8 | 1 |
| 2 | 0.00,0.00,0.50,0.50,1.00,1.00 | 2 | 2 | 2 | 6 | 0 |
| 3 | 0.00,0.00,0.50,0.50,1.00,1.00 | 2 | 2 | 2 | 6 | 0 |