I would like to average the weights which can be seen in the picture.
That means the calculation I want to make is : (50+65+70+68+70)/5
Do you know how to do that? (to specifically select the weights ones for every person using the household number and person number)
Average weights per person.....
Your initial question misleads into thinking you want the average for all.
SELECT HouseHoldNumber, PersonNumber, AVG(Weight) as AVG_Weight
FROM YOUR_TABLE
GROUP BY
HouseHoldNumber, PersonNumber
Simply add a WHERE if you want top do it for a specific house & Person.
Seem you want to average the distinct weights per household:
SELECT AVG(Weight)
FROM
(
SELECT DISTINCT HouseHoldNumber, Weight -- maybe need to add TravelNumber or PersonNumber
FROM tab
) AS dt
Use "Select AVG(your_column) From your_table"
uh
select avg(weight) from table
This will give you the average per household, per person.
SELECT [Household Number], [Person Number], AVG(Weight)
FROM Your_Table
GROUP BY [Household Number], [Person Number]
Related
My dataset:
enter image description here
I want to write SQL statement to group by and 1) add Quantity; 2) calculate earliest date based on the group by. In Dax I wrote this:
Table 2 = GROUPBY(
'Table',[Region],"Total Quantity",sumx(CURRENTGROUP(),[Quantity]),"First Available",MinX(CURRENTGROUP(),[Availability])
)
What is the SQL equivalent? Thank you
If the RDBMS is MS-SQL then the equivalent is
select
Region,
SUM(Quantity) as [Total Quantity],
MIN(Availabilty) as [First Available]
from
#t1
group by
Region /*you need to add the grouping columns manually here*/
Nevermind, after trial and error, I solved this myself:
SELECT product, region, min(availability) as 'First Available', sum (quantity) as N from [table2] group by product, region
My source table contains sales information. Each row is a person and records every time they've shopped/where. I can therefore calculate the average transaction value per industry by the following:
select
industry,
COALESCE(AVG(CASE WHEN shopcode in (1,2,4) THEN dollar END), 0) AS avt
from sales
group by industry
But how can I adapt this to calculate the spend per distinct count of user i.e.: sum(dollar)/count(distinct person) so similar to above but instead of sum/count(*) sum/count(distinct person)... I need to use coalesce with this as well.
how can i adapt this to calculate the spend per distinct count of user i.e.: sum(dollar)/count(distinct person)
You can use:
select industry,
sum(dollar) / count(distinct person)
from sales
group by industry;
I'm not sure what the filtering on shop_code is for. It is in your query but not part of the question. If you want this for particular shops, I would suggest moving this to a where clause:
select industry,
sum(dollar) / count(distinct person)
from sales
where shop_code in (1, 2, 4)
group by industry;
Group by and Pivot operations give different counts.
I used Group by to get count of vehicles by City and used Pivot to get count of vehicles by Make.
SELECT MAKE, [AMB],[BNG],[CBE],[GBM],[KKE],[OMR],[PDR]
FROM
(
SELECT MAKE, BRANCH, COUNT(DISTINCT [VEH NO]) [VEHICLE COUNT]
FROM MAKE_MODEL_DESCRIPTION
GROUP BY MAKE, BRANCH
) X
PIVOT
(
SUM([VEHICLE COUNT]) FOR BRANCH IN ([AMB],[BNG],[CBE],[GBM],[KKE],
[OMR],[PDR])
) AS PVT
The total count I get for above Pivot query is 150.
select BRANCH, COUNT(distinct [VEH NO])
from MAKE_MODEL_DESCRIPTION
group by BRANCH
The total count I get for above GROUP BY query is 140.
Shouldn't both the same number given they are from same data source?
Can someone let me know where I am going wrong.
No, you should not expect the counts to be the same. The GROUP BY is counting distinct vehicles over all makes. The PIVOT is counting distinct vehicles only within a single branch and model.
In other words, the same vehicle might be in different branches.
If you include the make, then the numbers should be the same:
select MAKE, BRANCH, COUNT(distinct [VEH NO])
from MAKE_MODEL_DESCRIPTION
group by MAKE, BRANCH
I'm trying to figure out how to get the distinct count of something that's conditional and doesn't use group by. I've got a table that has columns as seen here:
Employeeid, Training_Course_name, CompletedDate
Some of the courses have the word Rope in them.
I want to take the number of completed courses per person with the word "Rope" in the title and divide it by the number of unique courses there are that have the word rope in the title. If there are 15 unique course names that have the word rope in the title, regardless of who they're assigned to, I want to come up with that number and have it divided into the number of completed rope courses per person.
You can use conditional aggregation:
select count(distinct case when Training_Course_name like '%rope%'
then Training_Course_name
end) as courses_with_rope
This will help you to solve your problem
Declare #UniqueCourses As TABLE(Course As VarChar(32))
Select #UniqueCourses = Training_Course_Name
From
(SELECT DISTINCT Training_Course_Name
FROM Employess
WHERE Training_Course_Name LIKE '%Rope%') A
SELECT
EmpId,
(SELECT COUNT(1) FROM Employees innerEmployees
WHERE innerEmployees.EmpId = outerEmployees.EmpId AND
innerEmployees.CompletedDate is not null
) AS Completed Courses
From Employees outerEmployees
You can get the course with the word rope in them with this query
SELECT Employeeid, Training_Course_name, CompletedDate
FROM Table_Name_You_Did_Not_Say
WHERE Training_Course_name LIKE '%rope%'
And a distinct count like this
SELECT Employeeid, Training_Course_name, CompletedDate,
count(distinct Training_Course_name) as distinct_names
FROM Table_Name_You_Did_Not_Say
WHERE Training_Course_name LIKE '%rope%'
Anything "by employee id" would require a group by -- so what exactly is your requirement?
I have the below example:
SELECT name, age, location, SUM(pay)
FROM employee
GROUP BY location
This as expected will give me an error:
ORA-00979: not a GROUP BY expression
How can I get around this? I need to group by one maybe two columns but need to return all columns even if they're not used in the GROUP BY clause, I've looked at sub-queries to get around it but have had no luck so far.
You can use analytic functions:
SELECT name
, age
, location
, pay
, SUM(pay) over (partition by location order by location ) total
FROM employee
So, you can return all rows even if they are not used in the grouping.
So you want to know the total pay by location, and you want to know the names and ages of employees at each location? How about:
SELECT e.NAME,
e.AGE,
e.LOCATION,
t.TOTAL_LOCATION_PAY
FROM EMPLOYEE e
INNER JOIN (SELECT LOCATION,
SUM(PAY) AS TOTAL_LOCATION_PAY
FROM EMPLOYEE
GROUP BY LOCATION) t
ON (t.LOCATION = e.LOCATION)
Share and enjoy.
(Group b[http://docs.oracle.com/javadb/10.6.2.1/ref/rrefsqlj32654.html] Must have an aggregate function in every column that is not in the group by clause. When you are grouping, means that you want one row per group. Distinct values of the columns in the clause appear in the final result set.
This is because oracle can't know which of the values for the column that you don't have in the group by to retrieve. Consider this:
A X
B X
Select col1, col2 from myTable group by col2; -- incorrect
Select min(col1), col2 from myTable group by col2; -- correct
Why is the first incorrect? Because oracle can't know whether to retrieve A or B for the X value you have to specify it. i.e. MIN, MAX, etc.
There is an alternative to this named analytic functions that allow you to work under windows of your result set.
Now if you want total employee pay by location, and every employee you may want this.
SELECT name, age, location, SUM(pay) OVER(PARTITION BY location)
FROM employee
I believe this is better than #Bob Jarvis query as you only query the table once. Please correct me if I'm wrong. He also has employees and employee. Typo?