I have 2 tables - Client, Jobs,
Client - clientID,clientName,clientCity
Bricklayers - jobNum, clientID, startDate
Now I need to query the database to list me all clients in 3 cities (Charlotte, Raleigh, Wilmington) and if they have a job on the Bricklayers table also list their jobNum(s) and startDate
I'm completely unsure what approach to take to this problem as I've never needed a query like this. I know I need a join and tried an inner join on the clientID in both columns but kept receiving syntax errors.
Try this
SELECT c.clientID, c.clientName, b.jobNum, b.startDate
FROM Client c
INNER JOIN Bricklayers b ON c.clientID = b.clientID
WHERE c.clientCity IN ('Charlotte','Raleigh','Wilmington');
Related
**
SELECT clientName, orderID FROM
Orders INNER JOIN clients ON orders.clientID = clients.clientID;
**
I can show every client and each of their orders. Help me correct my SQL syntax, so I am showing the number of orders for each client?
I also want my results to be ordered alphabetically by client name,
please write a new SQL syntax for me!
SELECT clientName, COUNT(*)
FROM Orders INNER JOIN clients ON orders.clientID = clients.clientID
GROUP BY clientName
ORDER BY clientName
You can use aggregation:
select c.clientName, count(*) no_orders
from clients c
inner join orders o on o.clientID = c.clientID
group by c.clientID, c.clientName
Notes:
table aliases make the query easier to read and write
adding the id of the client to the group by clause handles the possibility that different clients might have the same name
in general, you want to avoid camel case identifiers (ClientName) and use snake case instead (client_name): from database perspective, identifiers are case insensitive (unless quoted)
Can anyone tell me why I am getting this error?
SELECT C.ClientID, C.ClientCode, C.FullName, E.FullName AS EmployeeName
FROM ClientID C, EmployeeInfo E, tmntClientTreatmentInfo T, CliniciansClients CC
JOIN CliniciansClients
ON CliniciansClients.ClientID = ClientID.ClientID
JOIN ClientID
ON ClientID.ClientID = CliniciansClients.ClientID
JOIN EmployeeInfo
ON EmployeeInfo.EmployeeID = CliniciansClients.EmployeeID
JOIN tmntClientTreatmentInfo
ON tmntClientTreatmentInfo.ClientID = ClientID.ClientID
The multi-part identifier "ClientID.ClientID" could not be bound. // Line 6
Tony, there are lots of ways to do selects with joins and many different types of joins. But I would recommend you start by doing something basic, making sure it works, then if it's not performant or brings back duplicate data, start optimizing the joins or work on your normalization / denormalization. After a while, you'll get the hang of it. Until then, I quickly set up a postgres instance in Docker, created the tables you referenced, and this query should work:
SELECT C.ClientID, C.ClientCode, C.FullName, E.FullName AS EmployeeName
FROM ClientID C
JOIN CliniciansClients AS CC
ON CC.ClientID = C.ClientID
JOIN EmployeeInfo AS E
ON E.EmployeeID = CC.EmployeeID
JOIN tmntClientTreatmentInfo as leonardo
ON leonardo.ClientID = C.ClientID
I need to create an Oracle SQL query possibly using MINUS
Booking(BookID, MotelID, ClientID, Date)
Motel(MotelID, MotelName)
Client(ClientID, ClientName)
I can show the names of clients who have stayed at either motel (I think!!!)
SELECT DISTINCT ClientName
FROM (Client INNER JOIN Booking
ON Client.ClientID = Booking.ClientID)
INNER JOIN Motel
ON Booking.MotelID = Motel.MotelID
WHERE (MotelName = 'MotelOne' OR MotelName='MotelTwo');
But I now need to show the clients who have stayed at MotelOne but NOT MotelTwo.
Very new to this, and trying to get my head around it so any help will be gratefully accepted!
Oracle has a MINUS operator --> http://docs.oracle.com/cd/B19306_01/server.102/b14200/queries004.htm
It returns only unique rows returned by the first query but not by the second.
SELECT c.clientid, c.clientname
FROM booking b JOIN client c
ON b.clientid = c.clientid JOIN motel m
ON b.motelid = m.motelid
WHERE m.motelname = 'MotelOne'
MINUS
SELECT c.clientid, c.clientname
FROM booking b JOIN client c
ON b.clientid = c.clientid JOIN motel m
ON b.motelid = m.motelid
WHERE m.motelname = 'MotelTwo'
MINUS operator sorts rows and eliminates duplicates, so SELECT DISTINCT is not required.
I am using ColdFusion 8 to develop my company's website and would like to return a list of records (just the clientname field) from a table (dbo.clients) that has no match in a different table (dbo.fees) for the purpose of prompting the end-user to add a fee schedule for those companies. An example:
dbo.clients
CLIENT_ID CLIENT_NAME
1 Joe's Diner
2 Save-a-Lot
3 Family Meds
4 DiFazio's
dbo.fees
CID CLIENT_NAME FEE
1 Joe's Diner 25.000
2 Save-a-Lot 35.000
4 DiFazio's 30.000
What I desire is a resultset that, in the case of the above tables/data, would return only clientid/clientname 3/Family Meds because they do not have a fee listed/record in the table dbo.fees. My DB is MSSQL 2005. My query is:
SELECT clientid
FROM clients
INNER JOIN fees
ON clients.clientid <> fees.cid;
Which returns a Cartesian product of 50,000+ results. Using LEFT/RIGHT OUTER JOIN still gives me a Cartesian product and DISTINCT simply returns every record from dbo.clients regardless of whether or not they have a dbo.fees entry or not. What am I doing wrong?
p.s. Also of note: The admin before me apparently did not set up a PK/FK relationship between the clients/fees tables and so any query syntax that might be reliant on that may not work in this situation. It would probably have to work based solely on the values of the relevant fields.
You can use a LEFT JOIN with a WHERE clause that will return only those records that do not appear in the fees table:
select c.CLIENT_ID, c.CLIENT_NAME
from clients c
left join fees f
on c.CLIENT_ID = f.CLIENT_ID
where f.CLIENT_ID is null
If you need help learning JOIN syntax, here is a great reference:
A Visual Explanation of SQL Joins
This can also be written using a NOT EXISTS:
select *
from clients c
where not exists (select CLIENT_ID
from fees f
where c.CLIENT_ID = f.CLIENT_ID)
See SQL Fiddle Demo with both queries
Simplest, you could just use a NOT IN;
SELECT clientid FROM clients WHERE clientid NOT IN
(SELECT clientid FROM fees)
...or you can use a LEFT JOIN to do the same thing a bit more verbosely; f.clientid will be NULL if a fee does not exist for the client.
SELECT c.clientid
FROM clients c
LEFT JOIN fees f
ON c.clientid = f.clientid
WHERE f.clientid IS NULL
"activity" is a bit field. I need to set it to true if one of the rows with this client_id has value true
SELECT c.client_id, u.branch_id, a.account_id, activity
FROM Clients c INNER JOIN
accounts a ON c.id=a.client_id INNER JOIN uso u ON a.uso_id = u.uso_id,
(SELECT MAX(CONVERT(int,accounts.activity)) as activity, client_id
FROM accounts GROUP BY client_id) activ
WHERE activ.client_id = c.id
This query executes about 2 minutes. Please help me to optimize it.
Seems activity field is a BIT and you cannot do a MIN or MAX on it.
Instead of this, use TOP:
SELECT c.client_id, u.branch_id, a.account_id,
(
SELECT TOP 1 activity
FROM accounts ai
WHERE ai.client_id = c.id
ORDER BY
activity DESC
)
FROM clients c
JOIN accounts a
ON c.id = a.client_id
JOIN uso u
ON a.uso_id = u.uso_id
Create an index on accounts (client_id, activity) for this to work fast.
You may want to read this article:
Minimum and maximum on bit fields: SQL Server
Join is expensive. Instead of Join, use memcache and make separate requests.