I have an sql statement which I cant get the structure right on, when I run what I currently have It says syntax wrong. I am looking the result to look like this:-
(source: churchcom.co.uk)
.
This is my query so far but I dont think I am on the right track at all.
SELECT Name, ValueofTaught, Amount
FROM Activities
WHERE (Department = #Department)
ORDER BY Name
GROUP BY BurnhamGrade
(SUM ValueofTaught AND Amount
WHERE Departmetn = #Department)
Structure of the activities Table is like this:-
(source: churchcom.co.uk)
.
It sort of sounds like you're looking to group by two columns. I'm assuming the Value of Taught column is what gets rolled up for hours:
SELECT Department, Name, SUM([Value of Taught]) Hours, SUM(Amount) Pay
FROM Activities
GROUP BY Department, Name
ORDER BY Department, Name
WITH ROLLUP
Related
I'm sure this is pretty straightforward but can't get my head round it at all.
In one of my DB tables, I have a column 'partitionDate'. This is populated every time a transaction is logged in to the DB table with the date 11-JAN-2023 for example. So we could have 100 transactions all with the partitionDate of 11-JAN-2023.
I've run a query to give me the total count for each distinct partitionDate
SELECT partitionDate, COUNT (DISTINCT partitionDate)
from tablename
I'm trying to get a grand total at the bottom that shows me all of the totals added up which I guess will be a SUM but I can't work it out!
Thanks
I'm trying to get a grand total at the bottom that shows me all of the totals added up which I guess will be a SUM but I can't work it out!
In T-SQL:
SELECT partitionDate
,COUNT (partitionDate)
from tablename
GROUP BY GROUPING SETS
(
(partitionDate)
,()
)
In MySQL should be something like this:
SELECT partitionDate, COUNT(partitionDate)
FROM tablename
GROUP BY partitionDate WITH ROLLUP;
I dont rly know how to explain my Problem, but i have a Query where i need to group by a column but on the other i need to get an avg of a column which is not grouped by.
My Code is like this:
Select SID,PID,Cost, AVG(COST)
from catalog
group by SID,PID
ORDER by SID
All Columns are in the same table.
What can i do to get the AVG(Cost) of PID?
My Question is related to an exam question which is the following: Find the SID's who charge more for some PID than the average cost of that PID.
The table has the columns SID, PID, COST. I cant upload pictures of the table because my account is new, so im sorry.
So my Problem was that i couldnt get the AVG of the PID, my next Problem because i already tried it with Partition is, that i dont know how the having clause has to look like. Do i need a sub-query for that?
You can use window functions to add an average to a row:
select SID, PID, Cost,
avg(cost) over (partition by pid) as avg_cost_for_pid
from catalog
order by sid;
I would like to iterate the same query while using different parameter values from a predefined list.
Say, I have a table with two columns. The first columns contains customer name. The second column contains customer spending.
###CUSTOMER; SPENDING###
customer1; 1000
customer2; 111
customer3; 100
customer1; 323
...
I know the complete list of customers: customerlist = {customer1, customer2, customer3}.
I would like to do something like:
Select sum(spending)
from mytable
where customer = #customerlist
The query should compute the sum of spending for each customer defined in the customer list. I have found some examples of sql procedures with stored parameters but not the case with one parameter of multiple values.
Thank you
P.S. This is just a hypothetical example to illustrate my question (I know it would be much more effective to use here a simple group by).
You can use nested query like this
SELECT CustomerList.CustomerName Cust, isnull((SELECT SUM(Spending) CustSpending
FROM Customer
WHERE Customer.CustomerName = CustomerList.CustomerName),0)
FROM CustomerList
This would normally be done using GROUP BY:
Select customer, sum(spending)
from mytable
group by customer;
GROUP BY is a very fundamental part of SQL. You should review your knowledge of SQL so you understand how to use it.
I am new to SQL so, I have a table that holds records for assigned jobs as for employee, holding hours required to complete the job.
An employee might have a lot records, regarding to different jobs that was assigned. So what I want is to select all the employees and sum their hours assigned in total..
I want an output to display each employee individually and the number of total hours assigned.
Something like: select username, Count(Hours) from mytable
group by username
The query above obviously is not giving the expected output.
Is there/what is the way to do that?
Any help is welcome!
Assuming username is unique, you should replace count() aggregate function with sum():
select username, sum(Hours)
from mytable
group by username
I am trying to pull information from two columns titled clientstate and clientrevenue in my table. I want clientstate to show up as the state, and have only distinct names in it, and under client revenue I want the average revenue per state, and that will only show up if there are at least two clients from that state. I am very new at this, so what I have is pretty iffy:
SELECT clientstate, clientrevenue
FROM client
GROUP BY clientrevenue
HAVING COUNT (*) >=2;
Where am I going wrong here?
SELECT clientstate AS [State]
, AVG(clientrevenue) AS [Average Revenue]
FROM client
GROUP BY clientstate
Grouping by ClientRevenue will try to group similar values and that doesn't have a logical sense.
First, in order to get distinct states, clientstate column needs to be used in the GROUP BY statement.
Thus, the code would be :
SELECT clientstate, AVG(clientrevenue)
FROM Source_Table
GROUP BY clientstate --this would get you distinct states
Now, considering the 2 clients per state, it's rather a condition than a HAVING statement. HAVING statement limits your query results according to the aggregate function you are using. For instance, in the code aforementioned, the aggregate function is AVG(clientrevenue). So, we can only use it in HAVING. we can not add count(*) unless it was used in SELECT.
So, you need to add it as a condition like
SELECT clientstate, AVG(clientrevenue)
FROM Source_Table A
WHERE (SELECT count(DISTINCT client_ID) FROM Source_Table B
WHERE A.clientstate = B.clientstate) >= 2 --Condition
GROUP BY clientstate --this would get you distinct states