SQL Server : WHERE Clause on a selected variable of a column - sql-server

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)

Related

Read excel and populate 2 dimensional array in typescript

i would like to read a simple table in an excel sheet (x rows, y cols) and populate a 2 dimensional array in typescript. Something like below:
client_id orderid
1 : 12 3 45 78 97
2 : 67 89 12
3 : 7 90 23
Do you know a good excel library ? Thank you
regards,
Titi
You just need to specify the table name and then you can use getRange().getValues() to capture the values within the table as a 2 day array. Example:
function main(workbook: ExcelScript.Workbook) {
const theTableName:string = "table1"; // or whatever the name is
var theTable = workbook.getTable(theTableName);
var allValues = theTable.getRange().getValues();
// allVallues is now an 2-day array with values
}

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.

Do arithmatic inside database. Is this possible?

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

Get all employees count whose first name starts alphabetically

In TSQL how do i get the starting letter of the fname of the employee and number of employee with that letter. I got to do this on PUBS database
select ASCII(fname) 'ASCII CODE',SUBSTRING(fname,1,1) 'LETTER' from employee
Output
65 A
65 A
65 A
65 A
67 C
67 C
68 D
68 D
69 E
.. ..
Expecting output
10 A
20 B
30 C
.. ..
Since it involved a Grouping by first letter of fname i had included groupby fname but no change in output. What is the exact SQL i need to run ?
SELECT LEFT(fname,1), COUNT(1)
FROM employee
GROUP BY LEFT(fname,1)
Edit: Damn! Ninja'd - SO can be a bit slow to update sometimes lol
Just need to group by the two columns you selected ...
SELECT SUBSTRING(fname,1,1) 'LETTER', COUNT(*) cnt
FROM employee
GROUP BY (fname,1,1)

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