Get all employees count whose first name starts alphabetically - sql-server

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)

Related

capture reoccurring seventh day in new column

I have the below table...
run_dt check_type curr_cnt
6/1/21 ALL 50
5/31/21 ALL 25
5/26/21 ALL 43
5/25/21 ALL 70
6/1/21 SUB 23
5/25/21 SUB 49
I would like to capture the value of what the check_type was seven days from the run_dt. What was the previous weekday value.
Something like...
run_dt check_type curr_cnt prev_nt
6/1/21 ALL 50 70
5/31/21 ALL 25
5/26/21 ALL 43
5/25/21 ALL 70
6/1/21 SUB 23 49
5/25/21 SUB 49
Can I use lead/lag or CTE?
What's the best option here, appreciate the feedback.
You could join the table to itself:
SELECT
a.run_dt,
a.check_type,
a.curr_cnt,
b.curr_cnt as prev_nt
from table a
left join table b on b.run_dt = dateadd(d,-7,a.run_dt)

when duplicate values found then

I want to have a query that selects all duplicate values in a column. If those value meet the conditions then I'd like the query to return only those values.
Class Student_ID Location
Biology 511 4A
Biology 512 15B
Biology 513 15B
English 514 6A
Biology 521 6A
Spanish 522 6A
Spanish 523 15B
Chemistry 524 4A
English 531 15B
Biology 532 4A
Chemistry 534 4A
Select all duplicate values in the class column and if among those values there is location in both 4A and 15B then assign 1.
CASE WHEN count(class) > 1 AND (Location = '4A' AND Location = '15B') THEN 1
ELSE 0 END
what is most important is how to select duplicate values as a group and then look at the condition (location must be 4A and 15B). So the query must first group the duplicated values from the class column and then see if within the group the values meet the condition of location. So for example we first group the class column we get 5x biology this is then seen as a group and then within this group if there exist one row with location 4A AND one row with location 15B then and only then assign value 1 for biology. Almost all the values in class column have duplicates.
Desired Output
Class Location
Biology 1
Chemistry 0
English 0
Spanish 0
As an alternative to Tim Schmelter's answer, you can also do this with a LEFT JOIN.
SELECT yt1.Class, IIF(COUNT(yt2.Class) > 0, 1, 0) AS IsMatch
FROM YourTable yt1
LEFT JOIN YourTable yt2 ON yt1.Location = '4A' AND yt2.Location = '15B' AND
yt2.Class = yt1.Class
GROUP BY yt1.Class

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.

SQL Server : GPS distance returning invalid floating point

I have a table in a SQL Server database that contains columns:
ID, Latitude, Longitude
I want to calculate the distance between each point and take all that are less, but when I apply the WHERE distance < 10 I'm getting an error
An invalid floating point operation occurred
I'm not sure what im doing wrong, because it's working perfectly without the WHERE clause.
My code:
SELECT
[t5].Name as Hotel1, [t6].Name as Hotel2, Distance
FROM
(SELECT
[t2].ID as GPS1, [T1].ID as GPS2,
(ACOS(Cos(PI()*[t1].Latitude/180.0) * Cos(PI() * [t1].Longitude/180.0) * Cos(PI()*[t2].Latitude/180.0) * Cos(PI() * [t2].Longitude/180.0) + Cos(PI()*[t1].Latitude/180.0) * Sin(PI() * [t1].Longitude/180.0) * Cos(PI()*[t2].Latitude/180.0) * Sin(PI() *[t2].Longitude/180.0) + Sin(PI()*[t1].Latitude/180.0) * Sin(PI() * [t2].[Latitude]/180.0)) * 6371) AS Distance
FROM
GPSLocations [t1]
JOIN
GPSLocations [t2] ON [t1].ID <> [t2].ID) [t4]
JOIN
Hotels [t5] ON [t5].FK_GPSLocationID = GPS1
JOIN
Hotels [t6] ON [t6].FK_GPSLocationID = GPS2
WHERE
distance < 10
Sample data:
GPSLocations table looks like this
Latitude Longitude ID
39,7531224 -105,0001446 847
55,10309 12,31064 581
55,10317 12,37527 684
55,10382 9,35923 740
55,1097 12,50471 636
55,11163 10,77026 358
55,11366 11,74766 668
Hotel table looks like this:
ID Name FK_GPSLocationID
64 Hotel Findus 59
65 Best Western CPH 60
66 Comwell Middelfart 61
67 Hotel Middelfart 62
68 Master demo 1 63
69 Sky hotel 1 64
70 Rene bigstart hotel 3 65
This is just sample data and there for the foreign keys do not match.
All the GPS locations can be downloaded here: http://noxiaz.dk/GPSLocations.txt
The joins with the table Hotels makes no difference on the error im getting

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)

Resources