I'm stuck on writing this piece of code.
the output that i need to get is
firstName lastname Name Price Date
--------------- --------------- ------------------------- -------- ----------
Bateman Michael Furniture DR 222.80 2013-05-01
Tara Roswell Clothes Ladies 24.25 2013-05-04
LeMay Mike Toys Child 12.00 2013-05-12
Create a query that will show who picked up unsold items during the month of May 2013. Include in your output the first and last name of the owner, along with the name of the item, max price and pick up date. Order your output by Pick up date. My output looked like the following:
and my output is
FirstName LastName Name Price Date
--------------- --------------- ------------------------- ------- ----------
my code is
SELECT P.FirstName
, P.LastName
, IT.Name
, I.MaxPrice AS Price
, IP.Date
FROM People P
JOIN CHARITY C ON P.PeopleID = C.ContactID
JOIN Donation D ON C.CHARITYID = D.CHARITYID
JOIN Item_Donation ID ON D.DonationID = ID.DonationID
JOIN IteM I ON ID.ItemID = I.ItemID
JOIN Item_Type IT ON I.ItemTypeID = IT.ItemTypeID
JOIN Item_PickUp IP ON I.ItemID = IP.ItemID
ORDER BY IP.Date
I see nothing wrong with your query, so I suspect a data issue. Here is how I would investigate this. Start with querying the "root" table:
SELECT * FROM People p
If the results are not what you expect, then you have found the issue in your data.
If the results are ok, then add the first join:
SELECT *
FROM People P
JOIN CHARITY C ON P.PeopleID = C.ContactID
Same thing, if the results are not what you expect, then you have a data issue in the CHARITY table. Query the CHARITY table by itself to see why the rows from that table are not joining to People:
SELECT * FROM CHARITY
If the results are what you expect, then add the next JOIN, and continue, one JOIN at a time, until you find the one that causes no rows to be returned. Examine the data in that table to see why it doesn't join to your query before the join.
Related
I have two tables as follows that the id values of the tblBranch table in the tblPost table are in the Cityid, catid, Desid columns
How can I join these two tables together to see the values of cityid, catid, Desid?
tblBranch tblPost
---------- -----------
id id
Name title
cityid
catid
Desid
When you are join tables, you are bringing/combine TWO TABLES data rows
and their columns into ONE BIG TABLE base on the MATCHING JOIN COLUMNS.
Here is example of inner join:
tblDepartment
--------------
id_Dept NameDept
1 Math
2 Physic
tblTeacher
----------
id_Teacher FullName id_Dept
1 Smith Adam 1
2 John Doe 1
3 Marry Doe 2
EXAMPLE 01: No rows filter
select TC.id_Teacher, TC.FullName, TC.id_Dept, TD.NameDept from tblTeacher TC inner join tblDepartment TD on TC.id_Dept = TD.id_Dept
Result ONE BIG TABLE OF JOINING TABLES
id_Teacher FullName id_Dept NameDept
1 Smith Adam 1 Math
2 John Doe 1 Math
3 Marry Doe 2 Physic
EXAMPLE 02: With rows filter
Result ONE BIG TABLE OF JOINING TABLES with SQL where clause to limit smaller set of data rows return
select TC.id_Teacher, TC.FullName, TC.id_Dept, TD.NameDept from tblTeacher TC inner join tblDepartment TD on TC.id_Dept = TD.id_Dept
where TC.id_Dept = 2
id_Teacher FullName id_Dept NameDept
3 Marry Doe 2 Physic
You can use the above example to see WHAT COLUMNS your table should join to return result with BIG TABLE
So if I understand you correctly, you want to take CityId, CatId and DesId from tblPost, and for each of them, you want to join to the tblBranch table and show the Name for that Id - correct?
In that case, you need a query like this:
SELECT
CityId,
CityName = brCity.Name,
CatId,
CatName = brCat.Name,
DesId,
DesName = brDes.Name
FROM
tblPost p
LEFT OUTER JOIN
tblBranch brCity ON p.CityId = brCity.Id
LEFT OUTER JOIN
tblBranch brCat ON p.CatId = brCat.Id
LEFT OUTER JOIN
tblBranch brDes ON p.DesId = brDes.Id
Basically, you need to have three joins - one for each of the Id in your tblPost table.
I chose to use LEFT OUTER JOIN to avoid eliminating a row completely, if one of the Id cannot be found/matched in the tblBranch table.
I'm having trouble working on this query. I have a table that store customer id and Purchase as follow :
CustomerID PurchaseDate
------------------------
1 09/10/2018
1 08/15/2018
2 09/05/2018
2 09/07/2018
3 09/02/2018
4 08/20/2018
I'm trying to create a monthly report where that shows customerID and purchasedate for the current month and also show the purchasedate from the previous if they purchase something.So something like this:
select CustomerId, PurchaseDate
From Orders
Where PurchaseDate between '09/01/18' and '09/30/18' --- can't be changed
or PurchaseDate between '08/01/18' and '08/31/18 --------- wouldn't work
because this will include list of customer that bought purchased and item for the previous month even if they didn't purchase anything for the current month. I could use temp tables to store customerID and purchasedate for each month and then join them together but I'm trying to do it without using temps. This is the result I'm trying to get.
CustomerID PurchaseDate
------------------------
1 09/10/2018
1 08/15/2018 ------ purchased previous month
2 09/05/2018
2 09/07/2018
3 09/02/2018
Any suggestion? I will gladly appreciated it . Thanks!
Here's one option using exists:
select *
from orders o
where exists (
select 1
from orders o2
where o.customerid = o2.customerid
and o2.PurchaseDate between '09/01/18' and '09/30/18'
) and o.PurchaseDate between '08/01/18' and '09/30/18'
Online Demo
your code should look like this:
select Orders.CustomerId, Orders.PurchaseDate, c.PurchaseDate as previous_month
From Orders, (select CustomerId, PurchaseDate from Orders where PurchaseDate
between '08/01/18' and '08/31/18' ) as c
Where Orders.PurchaseDate between '09/01/18' and '09/30/18'
and Orders.CustomerId = c.CustomerId
This is called a subquery and can be slow if you let it get out of hand.
I need to perform different queries with different arguments. These arguments are arranged in the table T2 below. Everyday these arguments change so the table T2 change and the result of the query I want to do will change. The query is simple but I dont know how to perform using the columns of T2 as arguments...
-----Table T2----
ID Country FilterExpression
----------- ------- ------ ----- -----
1 Argentina 'Filter01'
2 Brazil 'Filter02'
3 USA 'Filter03'
4 UK 'Filter04'
5 France 'Filter05'
6 Mexico 'Filter06'
...
100 Canada 'Filter100'
The query I need to perform:
SELECT Element,Value
FROM ArchTot
WHERE Country = [Column Country of T2]
AND FilterExpr = [Column FilterExpression of T2]
Once I have a Table of arguments of 100 rows, the result of my query must have the same 100 rows.
Somebody can help me to build this query?
Try to be more specific so I can understand what you need to do.. Why you need to retrieve 100 for 100? And when The table data changes what happens to The old data... You need to query new data or The data before it changes?
Everyday the table of arguments change so I have to perform the query to update the Result. Once I Have 100 arguments I'll have 100 results:
Result of query from Tables Archtot or Archtot2
Country FilterExpr Value
----------- ------- ------ ----- -----
Argentina 'Filter01' 100.82
Brazil 'Filter02' 102.87
USA 'Filter03' 82.7
UK 'Filter04' 106.8
France 'Filter05' 110.7
Mexico 'Filter06' 79.9
...
Canada 'Filter100' 102.04
I Tried the following, It works...
SELECT A.Element,A.Value
FROM ArchTot A, T2 B
WHERE A.Country = B.Country
AND A.FilterExpr = B.FilterExpression
The problem now is that I Cant perform a INNER JOIN Statement once I have inserted the Table T2 in the FROM clause, it gives timeout. For example:
SELECT * FROM
(
(SELECT A.Element As Element,A.Value As Value, A.Country As Country
FROM ArchTot A, T2 B
WHERE A.Country = B.Country
AND A.FilterExpr = B.FilterExpression)T1
INNER JOIN
(SELECT C.Element As Element,C.Value As Value, C.Country As Country
FROM ArchTot2 C, T2 D
WHERE C.Country = D.Country
AND C.FilterExpr = D.FilterExpression)T2
ON T1.Country=T2.Country
)
Is the query I Built OK? Any Ideas why I Cant Perform the INNER JOIN?
Hi i need to write a query that does multiple things, i made it so it can get the details of orders from within a certain time frame as well as for ages between 20 and 30, however i need to check if the orders product cost more then a set amount
however that data is in multiple tables
one table has the orderid the prodcode and quantity, while the other day has the prod information such as code and price, and im 3rd from another table
So i need to access the price of the product with the prodcode and quantity to do a cross table calculation and see if its above 100 and trying to do this with an and where command
so if i have 3 tables
Orderplaced table with oid odate custno paid
ordered table with oid itemid quant
items itemid itemname price
and i need to do a calcultion across those tabkes in my query
SELECT DISTINCT Orderplaced.OID, Orderplaced.odate, Orderplaced.custno, Orderplaced.paid
FROM Cust, Orderplaced, items, Ordered
WHERE Orderplaced.odate BETWEEN '01-JUL-14' AND '31-DEC-14'
AND Floor((sysdate-Cust.DOB) / 365.25) Between '20' AND '30'
AND Cust.SEX='M'
AND items.itemid=ordered.itemid
AND $sum(ordered.quan*item.PRICE) >100;
no matter what way i try to get the calculation to work it doesnt seem to work always returns the same result even on orders under 100 dollars
so any advice on this would be good as its for my studies but is troubling me a lot
I think this is what you want. (I not familiar with $sum, I've replaced it with SUM())
SELECT
Orderplaced.OID,
Orderplaced.odate,
Orderplaced.custno,
Orderplaced.paid,
sum(ordered.quan * item.PRICE)
FROM
Cust
JOIN Orderplaced ON Cust.CustNo = Orderplaced.custno
JOIN Ordered ON Ordered.Oid = Orderplaced.Oid
JOIN items ON items.itemid = ordered.itemid
WHERE
Orderplaced.odate BETWEEN date 2014-07-01 AND date 2014-12-31
AND Floor((sysdate-Cust.DOB) / 365.25) Between 20 AND 30
AND Cust.SEX = 'M'
GROUP BY
Orderplaced.OID,
Orderplaced.odate,
Orderplaced.custno,
Orderplaced.paid
HAVING
sum(ordered.quant * item.PRICE) > 100;
I think you want to try something like this...
SELECT DISTINCT Orderplaced.OID, Orderplaced.odate, Orderplaced.custno, Orderplaced.paid
FROM Cust
JOIN Orderplaced ON
Cust.<SOMEID> = OrderPlaces.<CustId>
AND Orderplaced.odate BETWEEN '01-JUL-14' AND '31-DEC-14'
WHERE Floor((sysdate-Cust.DOB) / 365.25) Between 20 AND 30
AND Cust.SEX='M'
AND (
SELECT SUM(Ordered.quan*Item.PRICE)
FROM Ordered
JOIN Item ON Item.ItemId = Ordered.ItemId
WHERE Ordered.<SomeId> = OrderPlaced.<SomeId>) > 100
Couple of pointers:
1. Floor returns a number... you are comparing it to a string
2. Typically, when referencing a table in a query, the table has to be joined on its primary keys, ie. In your query you're referencing Item and ordered, without joining any of those tables on any key columns.
Hope that helps
I have 3 tables:
Table Position:
KodePosition | NamePosition | UserLogin
========================================
0037 Master A winz\alfa
0038 Master B winz\beta
0043 Master C winz\carlie
Table UserBackup (PJS):
KodePosition | UserOrigin | UserChange | StartDate | EndDate
================================================================
0037 winz\alfa winz\carlie 10-10-2014 17-10-2014
Table History:
IdHistory | KodePosition | StartDate | EndDate | User | Comment
===============================================================================
19F5FCFC 0038 14-10-2014 14-10-2014 winz\beta i not agree...
19F5FCFC 0043 15-10-2014 15-10-2014 winz\carlie i agree...
I want to display data like this :
Name | Date | Position | Comment
===================================================
winz\beta 14-10-2014 Master B i not agree...
winz\carlie 15-10-2014 Master A i agree...
Description :
please note the data in Table UserBackup(PJS).
if StartDate in Table History between StartDate and EndDate in Table UserBackup(PJS) and also the same UserChange with user, and then get NamePosition from Table Position by KodePosition of Table UserBackup(PJS).
For now, I have a stored procedure like this, but doesn't display the data I need.
select
A.IdHistory, A.StartDate, B.NamePosition, B.UserLogin, A.Comment
from
History as A
left join
Position as B on A.KodePosition = B.KodePosition
Where
A.IdHistory = '19F5FCFC'
order by
A.StartDate asc
Please help me guys... Thanks...
If you want to ask three tables, you can do it like this (sorry for not using your data, but I dont understand the connections of them(or your explanations)).
SELECT a.Column1, a.Column2, b.Column1
FROM table1 AS a
LEFT JOIN table2 AS b
ON a.Column1=b.Column1
WHERE (
SELECT c.Column1
FROM table3 AS c
INNER JOIN table2 as b
ON b.Column1=c.Column1
WHERE c.Column2 LIKE 'MyTarget%');
You got to think in two SELECTs, dont try to put everything in one. Btw. most DBs dont support multi Joins.
Without more information about what you are trying to do, this is a little bit of a stab in the dark...but based on your description, I believe you are trying to first filter the rows in the UserBackup(PJS) table based on whether the corresponding History.KodePosition record falls within the StartDate and EndDate of the corresponding UserBackup(PJS) record. Then, based on the returned KodePositions, you want to retrieve the related records from the Position table. I'm not sure this is the complete picture of what you are looking for, but hopefully this gets you further along:
;WITH cteData
AS (
SELECT u.KodePosition, h.IDHistory, h.StartDate, h.Comment
FROM UserBackup(PJS) u
INNER JOIN History h ON h.User = u.UserChange AND (h.StartDate >= u.Startdate and h.StartDate <= u.EndDate)
)
SELECT c.IDHistory, c.StartDate, p.NamePostition, p.UserLogin, c.Comment
FROM Position p
INNER JOIN cteData c ON c.KodePosition = p.KodePosition