How to sum a column from a query created table - sql

I created a SELECT query, after I execute it, it shows me a new table as I wanted (you can see it in the screenshot).
The problem is that I'm trying to add a new line to the query which supposes to sum() the column "Price" in the table that I got from the query
SELECT SUM(PRICE) AS totalPay
FROM Meals
You can see the tables here, and also the queries that worked at that, didn't.
I want it to be like that:
Like this

Just add the SUM(price) as new field in your first SELECT.
SELECT SUM(Meals.Price) as 'TotalPay', tableN01.waiterID
FROM tableN01 INNER JOIN Meals etc.

Related

How can I get multiplication between 2 different table columns with group by clause?

I tried this whole day but didn't got the result. Please go through the images on the link below
Here is my table 1 structure
and here is my table 2 structure
I want to multiply product_uom_qty from 1st table with cost in 2nd table and then group them by product_id which is there is both tables.
This is my query.
select sum(sale_order_line.product_uom_qty) * product_price_history.cost
from sale_order_line, product_price_history
group by product_id;
And result I am getting is
ERROR: column reference "product_id" is ambiguous. LINE 1: ...m
sale_order_line, product_price_history group by product_id...
please tell me where I am making mistake.
Presumably, you want something like this:
select product_id, sum(sol.product_uom_qty * pph.cost)
from sale_order_line sol join
product_price_history pph
using (product_id)
group by product_id;
That is, you need to join the tables on some condition.
Without sample data and desired results, it is unclear what you really want. But this at least is a sensible query that removes the syntax errors.

De-Dupe Query in MS Access

I'm using Access to create a report. I have several duplicate records in my Avail_Amt field. I used the Query Wizard to de-dup records from a table, like this.
In (SELECT [DEALLOCALBAL] FROM [TBL_DATA] As Tmp GROUP BY [DEALLOCALBAL] HAVING Count(*)=1 )
What I really want to do, though, is de-dupe the records in my Query, not in my Table. I think it should look like the script below, but it's not working.
In (SELECT [DEALLOCALBAL] FROM [dbo_LIMIT_HIST.Avail_Amt] As Tmp GROUP BY [DEALLOCALBAL] HAVING Count(*)=1 )
dbo_LIMIT_HIST is the Table in SQL Server and Avail_Amt is the Field that I am trying to de-dupe. Any idea what could be wrong here?
My data set looks like this:
Basically, for each Contact ID I could have multiple DEALLOCALBAL amounts. I want to capture each unique amount for each Contact ID, but I don't want dupes.
Thanks!!

New report from two tables with reportviewer doesnt have true result

I create a new view in SQL Server 2008 from two tables that have relation on a field. I want to create a report and do grouping on that common field.
For example:
table1: student(ID,first-name,last-name,phone,address,...)
table2: courses(ID,fk_ID,Course,....)
Now I want to have report that shows all data from both tables with grouping on ID from student table, that must show courses information separated for every student.
my query is:
SELECT TOP (100) PERCENT
dbo.tbl_student.ID,
dbo.tbl_student.firstname, dbo.tbl_student.lastname,
dbo.tbl_courses.Coursename,
dbo.tbl_Courses.CourseDate, dbo.tbl_courses.coursetype,
FROM
dbo.tbl_student LEFT OUTER JOIN
dbo.tbl_courses ON dbo.tbl_student.ID = dbo.tbl_courses.fk_id
ORDER BY
dbo.tbl_student.firstname DESC
But when I create a new report from this view, it shows just one record for every group. I spent 2 hours to solve the problem but I did not succeed.
please help me to create report from two or more tables.
Now it shows one record duplicates for several times for every group
Have you tried a query like this:
SELECT s.[ID], s.[first-name], s.[last-name], s.[phone], ...
c.[ID], c.[Course], ...
FROM student s
LEFT OUTER JOIN
courses c ON s.[ID] = c.[fk_ID]

SUM of a field within one table, GROUP BY a field in a second table

I found how to use SUM and GROUP BY within one table. What I try to do is to sum one field (Marks.m_GPA) in one table depending on a field in a second table (Curriculum.[c_Subject Group]).
I have one table with all the results of the students named Marks and another table named Curriculum. Both tables have the subject code in common (c_Code and m_Code), the Curriculum table has got one field [c_Subject Group]. I want to have the sum of all m_GPA values for each [c_Subject Group] and the results GROUP BY [c_Subject Group].
All fields are strings (text).
It's an ADO SQL VB6 application where I use the MS Jet engine of Access. I usually try to run a query using Access to test the query beforehand, ACCESS seems to accept my syntax, but the result is an empty table.
Here is the query:
SELECT
Curriculum.[c_Subject Group],
Sum([m_GPA]) AS Total_GPA
FROM Curriculum
INNER JOIN Marks ON
Curriculum.c_Code = Marks.m_Code
WHERE Curriculum.[c_Subject Group]= "1"
GROUP BY Curriculum.[c_Subject Group];
Even if I try Sum(cdbl(Marks.[m_GPA])), the result is an empty table.
Try this one.
SELECT
Curriculum.[c_Subject Group],
Marks.Total_GPA
FROM Curriculum
LEFT JOIN (
SELECT
m_Code,
Sum([m_GPA]) AS Total_GPA
FROM Marks
GROUP BY m_Code
)Marks
ON Curriculum.c_Code = Marks.m_Code
WHERE Curriculum.[c_Subject Group]= '1'

SQL query: HAVING, GROUP BY

I have two tables. One with a list of shops and their ID's (shop_id)
and one with a list of employees with the ID (shop_id) of the shop they work at.
I have to print out each employee with a certain position form a certain shop.
My query is normally correct but I seem to get an error like tblEmployees.
Normally my query would look something like.
SELECT tblEmployees.Name, tblEmployees.Surname, tblShops.shop_id
FROM tblEmployees, tblShops
GROUP BY tblEmployees.shop_id
HAVING tblEmployees.shop_id = tblShops.shop_id;
Normally I get an error saying something like:
tblEmployees.Name is not part of an aggregate function.
What I want to know is if it would solve my problem if I put every column that gives me this error under the GROUP BY statement. Or is there another way of fixing this error without it affecting the result I need to get from this query.
Drop the GROUP BY and HAVING clauses. You aren't aggregating here. You want to be joining your tables.
SELECT tblEmployees.Name, Surname, tblShops.shop_id
FROM tblEmployees JOIN tblShops
ON tblEmployees.shop_id=tblShops.shop_id