sql functions and WHERE clause - sql

Is the function in the SELECT clause takes the WHERE clause in account?
For example:
SELECT COUNT(id)
FROM Person
WHERE age > 18
will print the count of all persons, or only persons that have an age older than 18 years?

This query will return the count of rows where age > 18

Where clause specifies a condition that tells which rows to retrieve from the table. Since the condition is age>18 it will return the rows whose age column has a value greater than 18. Ṭhe function uses only those rows. Read more about select statement and different parts in it here:
http://en.wikipedia.org/wiki/Select_%28SQL%29

Related

Count Nulls in a table and group by column name in SQL Server

Basically I have a table that I need to count the number of NULL values in each column and return the count for that each column along with that column's name. I can query each column individually but there are 191 columns in this table.
Any assistance would be greatly appreciated.
This gets the Null value for one column but I would have to run this 190 more times to get all the rows:
SELECT COUNT(*) AS TS_EQUIPMENTLOCATION
FROM dbo.USR_IT_PURCHASE_TRACKER
WHERE (TS_EQUIPMENTLOCATION IS NULL)
use case when with aggregate function sum()
SELECT sum(case when TS_EQUIPMENTLOCATION IS NULL then 1 else 0 end )
AS TS_EQUIPMENTLOCATION
FROM dbo.USR_IT_PURCHASE_TRACKER

Group BY Statement error to get unique records

I am new to SQL Server, used to work with MYSQL and trying to get the records from a table using Group By.
The table structure is given below:
SELECT S1.ID,S1.Template_ID,S1.Assigned_By,S1.Assignees,S1.Active FROM "Schedule" AS S1;
Output:
ID Template_ID Assigned_By Assignees Active
2 25 1 3 1
3 25 5 6 1
6 26 5 6 1
I need to get the values of all columns using the Group By statement below
SELECT Template_ID FROM "Schedule" WHERE "Assignees" IN(6, 3) GROUP BY "Template_ID";
Output:
Template_ID
25
26
I tried the following code to fetch the table using Group By, but it's fetching all the rows.
SELECT S1.ID,S1.Template_ID,S1.Assigned_By,S1.Assignees,S1.Active FROM "Schedule" AS S1 INNER JOIN(SELECT Template_ID FROM "Schedule" WHERE "Assignees" IN(6, 3) GROUP BY "Template_ID") AS S2 ON S2.Template_ID=S1.Template_ID
My Output Should be like,
ID Template_ID Assigned_By Assignees Active
2 25 1 3 1
6 26 5 6 1
I was wondering whether I can get ID of the column as well? I use the ID for editing the records in the web.
The query doesn't work as expected in MySQL either, except by accident.
Nonaggregated columns in MySQL aren't part of the SQL standard and not even allowed in MySQL 5.7 and later unless the default value of the ONLY_FULL_GROUP_BY mode is changed.
In earlier versions the result is non-deterministic.
The server is free to choose any value from each group, so unless they are the same, the values chosen are nondeterministic. Furthermore, the selection of values from each group cannot be influenced by adding an ORDER BY clause.
This means there's was no way to know what rows will be returned this query :
SELECT S1.ID,S1.Template_ID,S1.Assigned_By,S1.Assignees,S1.Active
FROM "Schedule" AS S1
GROUP BY Template_ID;
To get deterministic results you'd need a way to rank rows with the ranking functions introduced in MySQL 8, like ROW_NUMBER(). These are already available in SQL Server since SQL Server 2012 at least. The syntax is the same for both databases :
WITH ranked as AS
(
SELECT
ID,Template_ID,Assigned_By,Assignees Active,
ROW_NUMBER(PARTITION BY Template_ID Order BY ID)
FROM Scheduled
WHERE Assignees IN(6, 3)
)
SELECT ID,Template_ID,Assigned_By,Assignees Active
FROM ranked
Where RN=1
PARTITION BY Template_ID splits the result rows based on their Template_ID value into separate partitions. Within that partition, the rows are ordered based on the ORDER BY clause. Finally, ROW_NUMBER calculates a row number for each ordered partition row.

How to calculate ages in Oracle SQL

I have two databases in the same server(dbA and dbB). The both have a table called CUSTOMERS. I want to calculate the age of my customers and instert it on a column AGE, on the dbB.CUSTOMERS, based on the current date and the DateOfBirth column on the dbA.CUSTOMERS table.
To calculate the age I tried to use
SELECT floor(months_between(SYSDATE, (SELECT BIRTH_DATE FROM dbA.CUSTOMERS)) /12) from dual;
but this returns an ORA-01427:single-row subquery returns more than one row
I am guessing this is because the subquery returns every row of the BIRTH_DATE column. Is there someway to do this for all the rows and then insert the result to my dbB.CUSTOMERS table?
I am using OracleSQL
There is no need for the subquery here
select floor(months_between(sysdate,BIRTH_DATE)/12) as Age
from CUSTOMERS
Using the subquery returns all the rows for the table and cannot apply the functions against all of them. This method applies the function against each row, so it works

rows with max value

I'm wanting to retrieve all rows and columns which contain the MAX value in one column.
For example: 3 columns - ID (not unique), name, age
I'm wanting all rows and columns which contain the max value in age.
Thanks
SELECT * FROM table WHERE age = (SELECT MAX(age) FROM table)
Select * from table where value=max(value)
Maybe you need to exchange where with having

Multiple Distinct Values with SUM

i have a table which Data is like
userID name amount Date
1 mark 20 22-10
1 mark 30 22-10
2 kane 50 22-12
2 kane 60 22-12
3 mike 60 22-10
Date is Unique with combination of userID + Username + Date
but as its more then 100k records there maybe duplicate date records but wither other user ids and names not with
now i want Output like
userID name amount Date
1 mark 50 22-10
2 kane 110 22-12
3 mike 60 22-10
if i try group By with id,name,sum(amount),date it returns multiple rows and answer incorrect
i have tried various combination of distict and SUM etc etc but not succeeded
any Solution
Thanks
You typically use a GROUP BY to aggregate one or more fields by one or more other fields.
SELECT userID, Name, SUM(amount), MIN(date)
FROM YourTable
GROUP BY
userID, Name
From MSDN: Aggregate functions
Aggregate functions perform a calculation on a set of values and
return a single value. With the exception of COUNT, aggregate
functions ignore null values. Aggregate functions are often used with
the GROUP BY clause of the SELECT statement.
some typical aggregate functions are
SUM
MIN
AVG
...
From MSDN: GROUP BY
Groups a selected set of rows into a set of summary rows by the values
of one or more columns or expressions in SQL Server 2008 R2. One row
is returned for each group. Aggregate functions in the SELECT clause
list provide information about each group instead of
individual rows.
select
userId, name, sum(amount) as amount
from
table
group by
userId, name
You don't want DISTINCT here, but rather GROUP BY with the aggregate SUM()
SELECT
userID,
name,
SUM(amount) AS amount
FROM tbl
GROUP BY userID, name