I want to add a column only flat numbers without decimal - sql-server

My column data will be like this
1,
2,
1.02,
2.23,
5
I want to add only 1,2,5 and remaining should be print as it is
How can i add that.?
SUM(case when col not like '%,%' then col else 0 end )
I tried with this but remaining value it is making 0
I am not good at explaining i hope u will understand.

Perhap you need the following?
SUM(case when col % 1 = 0 then col else 0 end)

Related

How can we add a vector to a single column of a matrix?

I want to add a vector to just a single column of a matrix.
For example:
a = zeros(5,5);
b = ones(5,1);
I want to add such b only to the second column of a such that the resultant a is
a= [ 0 1 0 0 0;
0 1 0 0 0;
0 1 0 0 0;
0 1 0 0 0;
0 1 0 0 0;]
How can I do this? I have tried doing a+b but it adds one to all the columns.
a(:,2) = a(:,2)+b does this. Specifically, you index all rows, :, of the second column, 2, of a, and add the vector b to that. Read this post for details on various indexing methods.
rahnema1 mentioned that Python-like syntax of adding to or subtracting from an argument does not require that argument to be repeated. You can thus do:
a:(,2) += b

How to have an incremntal column along with fixed one in matlab

I want to have an array like this:
second column with values from 1 to 1000.
first column with zero value for first 1000 ones, one for second 1000 values,... till 1000.
[ 0 1
0 2
...
0 1000
1 1
1 2
...
1 1000
1000 1
1000 2
...
1000 1000]
What I've tried would not increment the values of first column.
z = ones (10000, 2);
for i=1:1000
z(:,2)= (1:1:1000);
end
You can do it in a smart way :), with no loop.
Try:
firstColumn = repmat(0:1000,1000,1);
firstColumn = firstColumn(:); % Convert to a single column vector
secondColumn = repmat([1:1000]', 1001, 1);
M = [firstColumn secondColumn];
Read repmat and column, : if you are struggling to understand the code above.

Group and sum with SQL Server

I am a begginer using this kind of query in SQL Server and I have a big table similar as I'm showing, the trouble that I have is that I don't know how to group and sum at the same time, I need to sum the SUP with every SIG with the code
CODE SIG SUP
R501004002 BCO 30
R501004002 BCO 31.27
R501004008 BCO 34.09
R501004008 BCO 35.94
R501004003 MET 42
R501004020 RIP 42.5
R501004039 BCO 47.44
R501004020 RIP 68.56
The result that I want to show is something like this
CODE BCO MET RIP
R501004002 61.27 0 0
R501004003 0 42 0
R501004008 70.03 0 0
R501004020 0 0 111.06
R501004039 47.44 0 0
I group the code
Assuming only 3 sigs...
SELECT code
, sum(case when sig = 'BCO' then sup else 0 end) as BCO
, sum(case when sig = 'MET' then sup else 0 end) as MET
, sum(case when sig = 'RIP' then sup else 0 end) as RIP
FROM tableName
GROUP BY code
otherwise use a dynamic pivot as exampled here

Find integer value using bitwise logic in SQL

I am migrating old data into a new database structure and I need to find out the total int of 3 BIT fields in SQL.
So for example,
(Col A = 1, Col B = 2, Col C = 4)
Col A, Col B, COL C
0 0 0 = 0
1 0 0 = 1
0 1 0 = 2
0 0 1 = 4
1 1 1 = 7
I have tried in SQL
SELECT Col A & 1, Col B & 2
But not entirely sure if that logic will work..
Thanks in advance
Just multiply the columns by their respective powers of 2. Assuming that ColA is the least significant bit:
SELECT ColA + ColB * 2 + ColC * 4 FROM MyTable;
SqlFiddle

Matlab counting elements in array

Hey guys I just have a quick question regarding counting elements in an array.
the array is something like this
B = [1 0 1 0 0 -1; 1 1 1 0 -1 -1; 0 1 -1 0 0 1]
From this array i want to create an array structure, called column counts and another row counts. I really do want to crate an array structure, even if it is a less efficient process.
basically i want to go through the array and total for each column, row the total amount of times these values occur. For instance for the first row, i want the following output.
Row Counts
-1 0 1
1 3 2
thanks in advance
You can use the hist function to do this.
fprintf('Row counts\n');
disp([-1 0 1])
fprintf('\n')
for row = 1:3
disp(hist(m(i,:),3));
end
yields
Row counts
-1 0 1
1 3 2
2 1 3
1 3 2
I don't fully understand your question, but if you want to count the occurrences of an element in a Matlab array you can do something like:
% Find value 3 in array A
A =[ 1 4 5 3 3 1 2 4 2 3 ];
count = sum( A == 3 )
When comparing A==3 Matlab will fill an array with 0 and 1, meaning the second one that the element in the given position in A has the element you were looking for. So you can count the occurrences by accumulating the values in the array A==3
Edit: you can access the different dimensions like that:
A = [ 1 2 3 4; 1 2 3 4; 1 2 3 4 ]; % 3rows x 4columns matrix
count1 = sum( A(:,1) == 2 ); % count occurrences in the first column
count2 = sum( A(:,3) == 2 ); % ' ' third column
count3 = sum( A(2,:) == 2 ); % ' ' second row
You always access given rows or columns like that.

Resources