I'm trying to create a view for my report for
the Staff Sales.
My Staff Fields are
Staff_ID - PRIMARY KEY
Staff_Name
My Sale Fields are
Sale_Date
Payment_ID#
Staff_ID#
Sale_ID - PRIMARY KEY
I'm trying to calculate how much sales each member of staff has made, How would I go about this? I have tried this but as you can see I'm not very good at views in sql.
SELECT tblSale.Staff_ID,
SUM(tblSale.Sale_ID*tblSale.Staff_ID) AS
SalesPerStaff FROM tblSale
INNER JOIN tblStaff ON tblSale.Sale_ID = tblStaff.Staff_ID
GROUP BY tblSale.Sale_ID
A view is just the same as a standard piece of T-SQL, it's just stored in the database ready to be called when you need it
CREATE VIEW MyViewName
AS
SELECT
st.Staff_ID
,st.Staff_Name
,COUNT(sa.Sale_ID) Sales
FROM Staff st
LEFT JOIN Sale sa
ON st.Staff_ID = sa.Staff_ID
GROUP BY
st.Staff_ID
,st.Staff_Name
That way, you can just call MyViewName and it will give you results. I've used a LEFT JOIN in case you have staff with no sales.
If your Sales table has a value column, you can change COUNT(Sale_ID) to SUM(Sales_Value) or whatever the field is called.
Just read your reply to main question. If your Sales table has something like 'Item_ID' then just add this after st.Staff_Name in the select list (remember to add to the end of the GROUP BY too).
Related
I am new to sql and have a question about joining 2 tables. Why is there a . in between customers.custnum in this example. What is its significance and what does it do?
Ex.
Select
customers.custnum, state, qty
From
customers
Inner join
sales On customers.custnum = sales.custnum
The . is to specify a column of a table.
Let's use your customer table; we could do:
SELECT c.custnum, c.state, c.qty FROM customers as c INNER JOIN
sales as s ON c.custnum = s.custnum
You don't really need the . unless two tables have columns with the same name.
In the below query, there are two tables being referred. One is CUSTOMERS another is STATE. Since both has same column CUSTNUM, we need a way to tell the database which CUSTNUM are we referring to. Same as there may be many Bob's, if so their last name is used for disambiguation.
I would consider the below style as more clearer. That's opinionated.
Select
cust.custnum, cust.state, s.qty
From
customers cust -- use alias for meaningful referencing, you may be self-joining, during that time you can use cust1, cust2 as aliases.
Inner join
sales as s On cust.custnum = s.custnum
Think of it as a way to categorize the hierarchical nature of the database. Within a DB, there are tables, and within tables there are columns. It's just a way of keeping track, especially if you are working with multiple tables that may have the same column name.
For example, a table called Sales and a table called Customers might both have a column called Date. You may be writing a query where you only want the date from the Sales table, so you would specify that by writing:
Select *
From Sales
inner join Customers on Sales.ID = Customers.ID
where Sales.Date = '1/1/2019'
Staff Table
Payment Table
Here I can't upload the availability table. It consists of Availability_ID,Staff_ID and Shift_ID columns.
I want to Create a view which shows all details of all staff and their availability details. The view should include the last name and first name of the staff member, the full name of their mentor (if any), their hourly salary and the days and times they are available for work? How can I create?
Try this query, you will get all the columns of your three tables.
SELECT * FROM Staff Inner Join Payment ON Staff.Payment_Id = Payment.Payment_Id Innjer Join Availability on Availability.Staff_Id = Staff.StaffId
I am busy with my SQL Final exam preparation
My question is this, I have to create a View to display products bought Today and I must display the product name and price and customer name
my code is
USE pre_prac
GO
CREATE VIEW vw_Today
AS
SELECT Customer.custName, Product.prodName, Product.prodPrice, Invoice.invDate
FROM Customer
JOIN Product
ON Customer.custName = Product.prodIden
JOIN Invoice
ON Product.prodName = Invoice.invDate
WHERE Invoice.invDate = GETDATE()
GO
Is my code wrong or what am I missing
I think you need proper join with the corresponding table column(i mean relation with customer table to others table). it would be easy to submit write code if you provide your table schema. so far from my understanding below code may be your helpful
USE pre_prac
GO
CREATE VIEW vw_Today
AS
SELECT Customer.custName, Product.prodName, Product.prodPrice, Invoice.invDate
FROM Customer
JOIN Product
ON Customer.prodIden = Product.prodIden
JOIN Invoice
ON Product.invoiceId = Invoice.invoiceId
WHERE Invoice.invDate = GETDATE()
GO
The problem is with the JOIN How can Customer.custName = Product.prodIden and Product.prodName = Invoice.invDate
This is the error, you should have a relation based on an Integer column, for example Table Product should contain a FK from table Customer called CustomerId , etc!
I'm not sure if what I want to do is possible but if it is possible, it's probably a really easy solution that I just can't figure out. Once things get to a certain complexity though, my head starts spinning. Please forgive my ignorance.
I have a database running in MS Access 2007 for a school which has a plethora of tables joined to each other. I'm trying to create a query in which I get information from several tables. I'm looking up sales and payment information for different customers, pulling info from several different linked tables. Each sale is broken down into one of 4 categories, Course Fee, Registration Fee, Book Fee and Others. Because each customer will have multiple purchases, each one is a separate entry in the Sales table. The payment information is also in its own table.
My SQL currently looks like this:
SELECT StudentContracts.CustomerID, (Customers.CFirstName & " " & Customers.CLastName) AS Name, Customers.Nationality, Courses.CourseTitle, (StudentContracts.ClassesBought + StudentContracts.GiftClasses) AS Weeks, StudentContracts.StartDate, Sales.SaleAmount, SaleType.SaleType, Sales.DueDate, Payments.PaymentAmount
FROM (
(
(Customers INNER JOIN StudentContracts ON Customers.CustomerID = StudentContracts.CustomerID)
INNER JOIN Payments ON Customers.CustomerID = Payments.CustomerID)
INNER JOIN
(SaleType INNER JOIN Sales ON SaleType.SalesForID = Sales.SalesForID)
ON Customers.CustomerID = Sales.CustomerID)
INNER JOIN
(
(Courses INNER JOIN Classes ON Courses.CourseID = Classes.CourseID)
INNER JOIN StudentsClasses ON Classes.ClassID = StudentsClasses.ClassID)
ON Customers.CustomerID = StudentsClasses.CustomerID;
This works and brings up the information I need. However, I am getting one record for each sale as in:
CustomerID Name ... SaleAmount SaleType PaymentAmount
1 Bob $600 Course $1000
1 Bob $300 RgnFee $1000
1 Bob $100 Book $1000
What I need is one line for each customer but each sale type in it's own column in the row with the sale amount listed in its value field. As so:
CustomerID Name ... Course RgnFee Book Others PaymentAmount
1 Bob $600 $300 $100 $1000
Can anyone help and possibly explain what I should/need to be doing?
Thanks in advance!
You can create a cross tab from the query you have already created. Add the query to the Query Design Grid, choose Crosstab from query types, and select a Row or rows, Column and Value.
Say:
TRANSFORM Sum(t.SaleAmount) AS SumOfSaleAmount
SELECT t.ID, t.Name, Sum(t.SaleAmount) AS Total
FROM TableQuery t
GROUP BY t.ID, t.Name
PIVOT t.SaleType
If you want a certain order, you can edit the property sheet to include column headings, or you can add an In statement to the SQL. Note that if you add column headings, a column will be included for each column, whether or not data is available, and more importantly, a column will not be included that has data, if it is not listed.
TRANSFORM Sum(t.SaleAmount) AS SumOfSaleAmount
SELECT t.ID, t.Name, Sum(t.SaleAmount) AS Total
FROM TableQuery t
GROUP BY t.ID, t.Name
PIVOT t.SaleType In ("Course","RgnFee","Book","Others");
i have a complex query to be written but cannot figure it out
here are my tables
Sales --one row for each sale made in the system
SaleProducts --one row for each line in the invoice (similar to OrderDetails in NW)
Deals --a list of possible deals/offers that a sale may be entitled to
DealProducts --a list of quantities of products that must be purchased in order to get a deal
now im trying to make a query which will tell me for each sale which deals he may get
the relevant fields are:
Sales: SaleID (PK)
SaleProducts: SaleID (FK), ProductID (FK)
Deals: DealID (PK)
DealProducts: DealID(FK), ProductID(FK), Mandatories (int) for required qty
i believe that i should be able to use some sort of cross join or outer join, but it aint working
here is one sample (of about 30 things i tried)
SELECT DealProducts.DealID, DealProducts.ProductID, DealProducts.Mandatories,
viwSaleProductCount.SaleID, viwSaleProductCount.ProductCount
FROM DealProducts
LEFT OUTER JOIN viwSaleProductCount
ON DealProducts.ProductID = viwSaleProductCount.ProductID
GROUP BY DealProducts.DealID, DealProducts.ProductID, DealProducts.Mandatories,
viwSaleProductCount.SaleID, viwSaleProductCount.ProductCount
The problem is that it doesn't show any product deals that are not fulfilled (probably because of the ProductID join). i need that also sales that don't have the requirements show up, then I can filter out any SaleID that exists in this query where AmountBought < Mandatories etc
Thank you for your help
I'm not sure how well I follow your question (where does viwSaleProductCount fit in?) but it sounds like you will want an outer join to a subquery that returns a list of deals along with their associated products. I think it would go something like this:
Select *
From Sales s Inner Join SaleProducts sp on s.SaleID = sp.SaleID
Left Join (
Select *
From Deals d Inner Join DealProducts dp on d.DealID = dp.DealId
) as sub on sp.ProductID = sub.ProductID
You may need to add logic to ensure that deals don't appear twice, and of course replace * with the specific column names you'd need in all cases.
edit: if you don't actually need any information from the sale or deal tables, something like this could be used:
Select sp.SaleID, sp.ProductID, sp.ProductCount, dp.DealID, dp.Mandatories
From SaleProducts sp
Left Join DealProducts as dp on sp.ProductID = dp.ProductID
If you need to do grouping/aggregation on this result you will need to be careful to ensure that deals aren't counted multiple times for a given sale (Count Distinct may be appropriate, depending on your grouping). Because it is a Left Join, you don't need to worry about excluding sales that don't have a match in DealProducts.