Do arithmatic inside database. Is this possible? - database

Is it possible to this dynamically Add(stage_1 + stage_2) and get the total saved into the column called total. I am using phpMyAdmin. And the stage columns are of type float.
Car stage_1 stage_2 total
1 30 50 80
2 28 51 79
3 31 51 82
Thanks in advance for any help.

Try this:
update cartable set total = stage_1 + stage_2
In fact, instead of storing the column total in the database, you could just create a view:
create view carview as
select Car, state_1, stage_2, stage_1 + stage_2 as total
from cartable

Related

Add Countif to Array Formula (Subtotal) in Excel

I am new to array formulae and have noticed that while SUBTOTAL includes many functions, it does not feature COUNTIF (only COUNT and COUNTA).
I'm trying to figure out how I can integrate a COUNTIF-like feature to my array formula.
I have a matrix, a small subset of which looks like:
A B C D E
48 53 46 64 66
48 66 89
40 38 42 49 44
37 33 35 39 41
Thanks to the help of #Tom Shape in this post, I (he) was able to average the sum of each row in the matrix provided it had complete data (so rows 2 and 4 in the example above would not be included).
Now I would like to count the number of rows with complete data (so rows 2 and 4 would be ignored) which include at least one value above a given threshold (say 45).
In the current example, the result would be 2, since row 1 has 5/5 values > 45, and row 3 has 1 value > 45. Row 5 has values < 45 and rows 2 and 3 have partially or fully missing data, respectively.
I have recently discovered the SUMPRODUCT function and think that perhaps SUMPRODUCT(--(A1:E1 >= 45 could be useful but I'm not sure how to integrate it within Tom Sharpe's elegant code, e.g.,
=AVERAGE(IF(SUBTOTAL(2,OFFSET(A1,ROW(A1:A5)-ROW(A1),0,1,COLUMNS(A1:E1)))=COLUMNS(A1:E1),SUBTOTAL(9,OFFSET(A1,ROW(A1:A5)-ROW(A1),0,1,COLUMNS(A1:E1))),""))
Remember, I am no longer looking for the average: I want to filter rows for whether they have full data, and if they do, I want to count rows with at least 1 entry > 45.
Try the following. Enter as array formula.
=COUNT(IF(SUBTOTAL(4,OFFSET(A1,ROW(A1:A5)-ROW(A1),0,1,COLUMNS(A1:E1)))>45,IF(SUBTOTAL(2,OFFSET(A1,ROW(A1:A5)-ROW(A1),0,1,COLUMNS(A1:E1)))=COLUMNS(A1:E1),SUBTOTAL(9,OFFSET(A1,ROW(A1:A5)-ROW(A1),0,1,COLUMNS(A1:E1))))))
Data

AVG giving a Count instead of Average

This is probably a silly mistake on my end but I can't quite figure it out on my on.
I'm trying to calculate average over a set of data pulled from a sub-query presented in the following way:
TotalPDMPs DefaultClinicID
13996 -1
134 23
432 29
123 26
39 27
13 21
40 24
46 30
1 25
Now the average for each 'DefaultClinicID' calculated for 'TotalPDMPs' is the same as the data above.
Here's my query for calculating the average:
select DefaultClinicID as ClinicID, AVG(TotalPDMPs)
from
(select count(p.PatientID) as TotalPDMPs, DefaultClinicID from PatientPrescriptionRegistry ppr, Patient p
where p.PatientID = ppr.PatientID
and p.NetworkID = 2
group by DefaultClinicID) p
group by DefaultClinicID
can someone tell me what I'm doing wrong here?
Thanks.
The group by column is the same so it gets a count in the inner query by DefaultClinicID and then it tries to take an average of the same DefaultClinicID.
Does that make sense? Any aggregation on that column while you group by the same thing will return the same thing. So for clinic 23 the average calculation would be: 134 / 1 = 134.
I think you just need to do the average in your inner query and you get what you want. Or maybe avg(distinct p.patientID) is what you are after?
In the inner sub-query you already grouped by DefaultClinicID,
So every unique DefaultClinicID has already only one row.
And the avg of x is x.

create relationship based on two columns

I have two tables like below
Table 01
Company Date Size
A 01/05/2000 30
A 01/06/2000 40
B 01/05/2000 80
B 01/06/2000 90
Table 02
Company Date sales
A 01/05/2000 30
A 01/06/2000 40
B 01/05/2000 80
B 01/06/2000 90
I want to create relationship between these two tables based on date and company.
How to create relationship between two tables?
Thanks in advance
For a Power Pivot / Data Model, you can only use one column in a relationship. You can concatenate columns in a new calculated column using the & operator, e.g.
= 'Table 01'[Company] & "|" & 'Table 01'[Date]
Assuming your Table 1 is column A, B, C in "Sheet1", and Table 2 the same, but in "Sheet2", you can just use a Index/Match (entered with CTRL+SHIFT+ENTER.
In your Table 1, go to D2 (the first non-header row in column D, next to the 30), and use this formula:
=Index(Sheet2!$C$2:$C$10,Match($A2&$B2,Sheet2!$A$2:$A$10&Sheet2!$B$2:$B$10,0))
I'm assuming your last row is 10, if not, just change that in all parts of the formula. This should leave you with:
Table 01
Company Date Size
A 01/05/2000 30 30
A 01/06/2000 40 40
B 01/05/2000 80 80
B 01/06/2000 90 90

SQL Server : WHERE Clause on a selected variable of a column

I have table of 10k records, and would like to filter the data on a criteria.
Basically the criteria is on two columns one with int and other with text.
Sample Data :
Label Value
A 24
A 18
A 15
A 35
A 27
A 37
B 18
B 29
B 18
B 16
B 16
I wanted to filter and display the data excluding the Value < 20 and Label = A.
Please do help me out in getting an answer for this issue.
Thanks in advance.
How about this simple query?
Select * From MyTable Where Value >= 20 And Label <> 'A'
I think you were nearly there. try this:
Select * From MyTable
Where [Label] = 'B' OR ([Label] = 'A' AND [Value] > 20)

SQLServer Calculate Average of Multiple Columns

I have generated a table using PIVOT and the ouput of columns are dynamic. One of the output is as given below:
user test1 test2 test3
--------------------------------
A1 10 20 30
A2 90 87 75
A3 78 12 34
The output of above table represents a list of users attending tests. The tests will be added dynamically, so the columns are dynamic in nature.
Now, I want to find out average marks of each user as well as average marks of each test.
I am able to calculate the average of each test, but got puzzled to find out the average of each user.
Is there a way to do this??
Please help.
Mahesh
You can add the marks for each user then divide by the number of columns:
SELECT
user,
(test1 + test2 + test3) / 3 AS average_mark
FROM users
Or to ignore NULL values:
SELECT
user,
(ISNULL(test1, 0) + ISNULL(test2, 0) + ISNULL(test3, 0)) / (
CASE WHEN test1 IS NULL THEN 0 ELSE 1 END +
CASE WHEN test2 IS NULL THEN 0 ELSE 1 END +
CASE WHEN test3 IS NULL THEN 0 ELSE 1 END
) AS average_mark
FROM users
Your table structure has two disadvantages:
Because your table structure is created dynamically you would also have to construct this query dynamically.
Because some students will not have taken all tests yo may have some NULL values.
You may want to consider changing your table structure to fix both of these problems. I would suggest that you use the following structure for your table:
user test mark
-------------------
A1 1 10
A2 1 90
A3 1 78
A1 2 20
A2 2 87
A3 2 12
A1 3 30
A2 3 75
A3 3 34
Then you can do this to get the average mark per user:
SELECT user, AVG(mark) AS average_mark
FROM users
GROUP BY user
And this to get the average mark per test:
SELECT test, AVG(mark) AS average_mark
FROM users
GROUP BY test
Can you do it on your data source before you pivot it?
The simple answer is to UNPIVOT the same way you just PIVOTed. But the best answer is to not do the PIVOT in the first place! Store the unpivoted data in a table first, then from that do your PIVOT and your average.

Resources