views:

92

answers:

2

Hey Friends
Here is the table details and data

id data h1 h2 h3 h4 h5 h6 h7 h8
1 2010-10-10 1 1 1 1 1 1 1 1 
1 2010-10-11 1 0 1 0 1 0 0 1
1 2010-10-12 0 1 1 1 1 0 0 0

what i need to know is how can i create query for following

  1. find total no of zero in h1-h8 for id 1
  2. find total no of one in h1-h8 for id 1
  3. find total of data in h1-h8 for id1(total no of zero+total no of one)
  4. after getting 1,2,3 use the following answer in the equation (val(3)-val(1))/val(3)

i am not that much good in mysql, if you guys can help me then it will be a great help,thks in advance

+3  A: 

First of all, it seems like answer to 3. will be 8*number of rows, but...

answer 1:

SELECT SUM (8-(h1+h2+h3+h4+h5+h6+h7+h8)) FROM table WHERE id=YourID

answer 2:

SELECT SUM (h1+h2+h3+h4+h5+h6+h7+h8) FROM table WHERE id=YourID

answer 3:

SELECT SUM (8 as x) FROM table WHERE id=YourID

and answer 4:

figure it out yourself, you should have enough to start your journey now :)

Daniel Mošmondor
@daniel: for answer 1;did it show total no of zeros in h1-h8?because if new row added with all zero what will we do?
Alex Mathew
@alex: try it please and see if it works - I guess that it should. adding new rows and re-running the query will do the trick. can you explain what is there that is so confusing for you?
Daniel Mošmondor
@daniel:as you know the data in row may change, like in4th row all maybe zero,so the no of zero will be 16,right, we cannot predefine the zero,so thats the problemq
Alex Mathew
@alex: I still dont understand where is the problem. Can you try the queries and report if there is a difference in expected vs obtained results?
Daniel Mošmondor
@daniel:why you use 8 in answer one???
Alex Mathew
@alex: it seems that you have 8 data columns - so if all of them were '0's, you will have 8-0=8 -> proper number of 'zeroes'. first - do you agree that total number of '0's and '1's per row is always totaled at 8?
Daniel Mošmondor
@Daniel:the data will be saved only from h1-h8,and in first answer how can we say it will return the no of zeros,as we are not giving zero in where clause
Alex Mathew
@alex: I'll have to get 50 of your rep for the effort here :) you said that you want to filter your data by ID column, not by data columns. so no where is required on them.
Daniel Mošmondor
@daniel:what i said is, i need result for total no of zeros for id 1 from 50 rows,i just gave 3 rows for example,without checking whether the value of h1=0 or h2=0 orh3=0...how will we get the answer???
Alex Mathew
@alex: well... if you have 10 marbles, and some of them are white (0) and some of them are red (1) you don't have to WHERE them, you just have to add 1's for all the red marbles.
Daniel Mošmondor
k,i got it....what about the 3rd answer??? what is X?
Alex Mathew
X is a column alias.
Daniel Mošmondor
+2  A: 

Choosing the right data structure is very important. You can change you table definition to something like this:

id data        attribute value
1  2010-10-10  h1        1
1  2010-10-10  h2        1
...

Now you can use simple queries like:

1) SELECT COUNT(*) FROM table WHERE id = 1 AND value = 0
2) SELECT COUNT(*) FROM table WHERE id = 1 AND value = 1
3) SELECT COUNT(*) FROM table WHERE id = 1
Branimir
@Banimir:saving data in that format will lead more complex in the structure
Alex Mathew
You would need more rows... It's worth of thinking about.
Branimir
can you please explain me why?
Alex Mathew
Attribute-value structure, which I suggested, is more "robust". For example, if you add attribute h9 all queries will work without changes, in your sample you must add field to table and change all queries. Do you agree?
Branimir