MySQL Query to remove zero columns - mysql

MySQL query to remove all the columns from a table having sum of amount equal to zero
I have a table like this
Date Col 1 Col 2 Col 3 Col 4 Col 5
1/1/15 0 0 1 0 0
1/2/15 1 0 0 0 0
1/3/15 1 0 1 1 0
1/4/15 1 0 1 1 0
I want to get the out put table having the output data as given below
Date col 1 col 3 col 4
1/1/15 1 1 1
1/2/15 0 0 1
1/3/15 0 1 0
1/4/15 1 1 1
want to remove the col2 & col5 which have the sum amount equal to zero.

Related

Bar chart from many variables where varx = in Stata

I have a bar chart question here. Given that for all the variables in the dataset 1 = yes and 0 = No. I would like to plot a bar graph with the percentages (where var=1) on the y-axis and the variables on the x axis. Thanks in advance.
Dataset
Water
Ice
Fire
Vapor
1
1
0
1
1
0
0
1
0
1
1
1
1
1
1
1
1
1
0
1
1
1
1
0
0
1
1
1
0
1
0
1
0
1
1
1
1
0
1
1
0
1
0
0
0
1
1
0
1
0
1
0
1
0
1
0
1
1
1
1
0
1
0
1
1
0
1
1
1
0
1
0
1
1
0
1
1
0
0
1
0
1
1
1
1
1
0
1
1
0
0
1
0
1
1
1
The percent of 1s in a (0, 1) variable is just the mean multiplied by 100. As you probably want to see the percent as text on the graph, one method is to clone the variables and multiply each by 100.
You could then use graph bar directly as it defaults to showing means. I don't like its default in this case and the code instead uses statplot, which must be installed before you can use it.
* Example generated by -dataex-. For more info, type help dataex
clear
input byte(water ice fire vapor)
1 1 0 1
1 0 0 1
0 1 1 1
1 1 1 1
1 1 0 1
1 1 1 0
0 1 1 1
0 1 0 1
0 1 1 1
1 0 1 1
0 1 0 0
0 1 1 0
1 0 1 0
1 0 1 0
1 1 1 1
0 1 0 1
1 0 1 1
1 0 1 0
1 1 0 1
1 0 0 1
0 1 1 1
1 1 0 1
1 0 0 1
0 1 1 1
end
quietly foreach v of var water-vapor {
clonevar `v'2 = `v'
label var `v'2 "`v'"
replace `v'2 = 100 * `v'
}
* ssc install statplot
statplot *2 , recast(bar) ytitle(%) blabel(bar, format(%2.1f))
Try
. ssc install mylabels
checking mylabels consistency and verifying not already installed...
all files already exist and are up to date.
. sysuse nlsw88, clear
(NLSW, 1988 extract)
. mylabels 0(10)70, myscale(#/100) local(labels)
0 "0" .1 "10" .2 "20" .3 "30" .4 "40" .5 "50" .6 "60" .7 "70"
. graph bar (mean) married collgrad south union, showyvars legend(off) nolabel bargap(20) ylabel(`labels')
. table, statistic(mean married collgrad south union)
------------------------------
Married | .6420303
College graduate | .2368655
Lives in the south | .4194123
Union worker | .2454739
------------------------------
This relies on mylabels, and implements the bar gap (which I also like).

Find the matching between multiple tables in database

I have four tables,in each one I have two columns ID and Available. I need a Select Statement that finds the set of ID's that corresponds to available =0.
Table A: ID : 1 2 3 4 5 6
Available :1 1 0 0 0 0
Table B : ID 1 2 3 4 5 6
Available 1 1 1 0 0 0
Table C : ID 1 2 3 4 5 6
Available 0 1 0 0 1 0
Table D : ID 1 2 3 4 5 6
Available 1 1 0 0 1 0
If I understand correctly, you can use intersect if you want all the available values to be 0:
select id from a where available = 0
intersect
select id from b where available = 0
intersect
select id from c where available = 0
intersect
select id from d where available = 0;
If you want any of them to be 0, then use union instead.

Find distinct id by many row manipulations

Data set looks like this:
ID Rank Case
1 1 1
1 2 0
1 3 0
2 1 0
2 2 1
2 3 0
3 1 1
3 2 0
3 3 0
I want to find all the IDs that has Rank=1 Case=0, Rank=2 Case=1, Rank=3 Case=0. In the above case, this would return ID2
select id
from your_table
group by id
having sum(rank=1 and `case`=0) > 0
and sum(rank=2 and `case`=1) > 0
and sum(rank=3 and `case`=0) > 0

Sort a table based on one condition and the list remaining rows accordingly

I had a table with following details
cID sID Name pID childrenCount
1 1 Site 1 5
2 1 Safty 2 4
3 1 Archit 3 3
4 1 Civil 1 0
5 1 Concs 1 0
6 1 Pavm 1 0
7 1 Paint 3 0
8 1 Alum 3 0
9 1 Doors 3 0
10 1 Highw 1 0
11 1 Road 1 0
12 1 Alarm 2 0
13 1 Safty 2 0
14 1 Fence 2 0
15 1 Beaco 2 0
What I want is to write a select query to order the above table first by their childrenCount values in descending order and the list the corresponding rows according as below
cID sID Name pid childrenCount
1 1 Site 1 5
4 1 Civil 1 0
5 1 Conc 1 0
6 1 Pavm 1 0
10 1 Highw 1 0
11 1 Road 1 0
2 1 Safty 2 4
12 1 Alarm 2 0
13 1 Safty 2 0
14 1 Fence 2 0
15 1 Beacon 2 0
3 1 A WRK 3 3
7 1 Paint 3 0
8 1 Alumin 3 0
9 1 Doors 3 0
Thanks In Advance
Try this...first order by pid then childrenCount desc.
SELECT * FROM TABLENAME order by pid, childrenCount desc
Try this:
SELECT * FROM tableA ORDER BY pid, childrenCount DESC, cid

counting the number of non null or non zero columns in a table

I have a table like this
ID Name Score_1 Score_2 Score_3
1 Abcd 4 5 5
2 Bdc 8 7 0
3 dcd 0 0 3
4 cdded 0 0 0
I need another column in the end which can count the number of non zero columns.Result should be like this
ID Name Score_1 Score_2 Score_3 Count
1 Abcd 4 5 5 3
2 Bdc 8 7 0 2
3 dcd 0 0 3 1
4 cdded 0 0 0 0
Thank you
select *,
if(score_1<>0,1,0)+if(score_2<>0,1,0)+if(score_3<>0,1,0) as `count`
from table
Use a select like this when you need instead of storing a calculated field.