Can I select a set of averages in SQL - sql-server-2008-r2

Can I select a set of averages based on fields in SQL?
Something like:
SELECT
AVG(Salary WHERE department = 1) as AvgDept1,
AVG(Salary WHERE title = 1) as AvgTitle1,
AVG(TimeOnJob WHERE title = 1 and department = 1) as AvgTime1
FROM
Employees
I understand this is similar to AVG with a GROUP BY but I can't wrap my head around how to get the three values from this.

Those look like three separate queries,
if you want them bundled into one select you can do something like:
SELECT
(SELECT AVG(Salary) WHERE department = 1 FROM Employees) AS AvgDept1,
(SELECT AVG(Salary) WHERE title = 1 FROM Employees) AS AvgTitle1
(select AVG(TimeOnJob) WHERE title = 1 and department = 1 FROM Employees) AS AvgTime1

Related

Postgres SQL Query sum + count in one query

I need help to get an result.
perscarcountoffset
person
0
1
0
1
I need a Total from a count of the lines where is person 1 + sum of the cell perscarcountoffset
select SUM((select sum(perscarcountoffset) from table where person = 1) + (select count(*) from table where person = 1)) from table where person = 1;
Thanks for any idea.
Try to create a query in postgresql. This works but it gives me 4 as result. But it must be only 2.
This returns 4 because you're actually computing
(select sum(perscarcountoffset) from table where person = 1) + (select count(*) from table where person = 1)
for each row in table (where person = 1), then summing that. So you're getting 2+2.
This is because anything outside of the aggregation method (i.e. the outer SUM() here) is per-row, and the inner sub-selects returns 2 for both rows.
The query you want doesn't need to be this complicated, this should do:
SELECT SUM(perscarcountoffset) + COUNT(*)
FROM table
WHERE person = 1;

T-SQL select distinct based on highest values from another column

I have a table that contains number of tryouts, customerID, status of that one tryout and some other columns with various data.
Of course a single customerID can have multiple number of tryouts ( in the real table first tryout is number 1, second one number 2 etc.).
Ex.
Customer ID = 1, tryout = 1
Customer ID = 1, tryout = 2
Customer ID = 1, tryout = 3
Customer ID = 2, tryout = 1
Customer ID = 3, tryout = 1
Customer ID = 3, tryout = 2
And I want to have all distinct customerIDs but for each one only the row, that contains the highest tryout number for each customer in one table with data from all the other columns as well.
Ex.
tryouts, customerID, status, data1, data2
How can I achieve that ?
If you only want the customer ID and tryout value then you can try the following:
SELECT customerID, MAX(tryout) AS max_tryout
FROM yourTable
GROUP BY customerID
If you want the entire record, then one option would be to use ROW_NUMBER():
SELECT t.customerID, t.tryout, t.status, t.data1, t.data2
FROM
(
SELECT *, ROW_NUMBER() OVER (PARTITION BY customerID ORDER BY tryout DESC) rn
) t
WHERE t.rn = 1
Try
SELECT
CustomerID,
MAX(tryout) AS [Max tryout]
FROM
TheTable
GROUP BY
CustomerID
This should give you what you want

Query SQL on Orientdb with append and substring

I need to retrieve the country from a number having a table with international_prefix and local_prefix like image.
This is a test that I've made but unfortunately won't work.
SELECT *
FROM core_phone_prefix
WHERE (("+393925559000").asString()).left(international_prefix.append(local_prefix).length()) = international_prefix.append(local_prefix)
Try these:
With this one I compare only the prefix
select distinct(country) as country from(select international_prefix, "+393925559000" as number, country, local_prefix from core_phone_prefix) where international_prefix=number.subString(0,international_prefix.length())
Instead, with this one I compare the international_prefix + the local_prefix with the international_prefix and the local_prefix of the number
select country as country from(select international_prefix, "+393925559000" as number, country, local_prefix from core_phone_prefix) where international_prefix.append(local_prefix)=number.subString(0,international_prefix.append(local_prefix).length())
Hope it helps,
Let me know
Can you try with this query
select country from(
select number.left(international_prefix.append(local_prefix).length()) as a, international_prefix.append(local_prefix) as b, country from (
select international_prefix, "+393925559000" as number, country, local_prefix from core_phone_prefix
)
) where a = b
UPDATED
You can insert two index, one on the field international_prefix (NOTUNIQUE_HASH_INDEX) and one on the field local_prefix (NOTUNIQUE_HASH_INDEX )
You can use this query
select expand($a) from (select "+393925559000" as number from core_phone_prefix limit 1)
let $a=(select from core_phone_prefix WHERE international_prefix = $parent.current.number.left(international_prefix.length())
and local_prefix = $parent.current.number.subString(international_prefix.length(),international_prefix.append(local_prefix).length())) limit -1
Let me know

Group by multiple columns in PostgreSQL

I have two queries:
SELECT city, count(id) as num_of_applicants
FROM(
select distinct(students.id), city
FROM STUDENTS INNER JOIN APPLICATIONS ON STUDENTS.ID = APPLICATIONS.STUDENT_ID
WHERE APPLICATIONS.COLLEGE_ID = '28'
) AS derivedTable
GROUP BY city;
SELECT city, count(id) as num_of_accepted_applicants
FROM
(select applications.id, city FROM
STUDENTS INNER JOIN APPLICATIONS ON STUDENTS.ID = APPLICATIONS.STUDENT_ID
WHERE status = 'Accepted' and college_id = '28') as tbl
GROUP BY city
one give the number of applicants for each college and one give the number of accepted applicants in each college, but I want to get a result in on query (instead of) where the result is something like:
city | number_of_applicants | number_of_accepted_applicants
You can simplify (fyi: I didn't understand why you used the derived tables, you could have just put the COUNT and GROUP BY on the inner queries) and combine the queries as this:
SELECT city
, COUNT(*) AS num_of_applicants
, SUM( CASE
WHEN status = 'Accepted' THEN 1
ELSE 0
END
) AS num_of_accepted_applicants
FROM STUDENTS
JOIN APPLICATIONS
ON STUDENTS.ID = APPLICATIONS.STUDENT_ID
WHERE college_id='28'
GROUP BY city;
Another way is to continue with the technique of derived tables. Make each of your queries a derived table and JOIN on the city - but that would not perform as well.

Updating with Nested Select Statements

I have a table that holds 3 fields of data: Acct#, YMCode, and EmployeeID. The YMCode is an Int that is formatted 201308, 201307, etc. For each Acct#, I need to select the EmployeedID used for the YMCode 201308 and then update all of the other YMCodes for the Acct# to the EmployeedID used in 201308.
so for each customer account in the table...
Update MyTable
Set EmployeeID = EmployeeID used in YMCode 201308
Having a hard time with it.
Put it in a transaction and look at the results before committing, but I think this is what you want:
UPDATE b
SET EmployeeID = a.EmployeeID
FROM MyTable a
INNER JOIN MyTable b
ON a.[Acct#] = b.[Acct#]
where a.YMCode =
(SELECT MAX(YMCode) from MyTable)
To get max YMCode, just add select statement at the end.