Here is my code
SELECT flightid,flightdate,numseats,seatnumber,maxcapacity;
FROM flight,flightbooking,seatbooking;
I get and error saying:
"ERROR: syntax error at or near "FROM"
LINE 2: FROM flight,flightbooking,seatbooking;"
^
These are my tables
LeadCustomer (CustomerID, FirstName, Surname, BillingAddress, email)
Passenger(PassengerID, FirstName, Surname, PassportNo, Nationality, DoB)
Flight (FlightID, FlightDate, Origin, Destination, MaxCapacity, PricePerSeat)
FlightBooking (BookingID, CustomerID, FlightID, NumSeats, Status, BookingTime, TotalCost)
SeatBooking(BookingID, PassengerID, SeatNumber)
This is what i am trying to achieve
"Check the availability of seats on all flights by showing the flight ID number, flight date along with the number of booked seats, number of available seats and maximum capacity."
The software i am using is PG Admin 4.
Thanks.
Remove the semicolon at the end of the SELECT line, that should fix it.
Try:
SELECT flightid, flightdate, numseats, seatnumber, maxcapacity
FROM flight, flightbooking, seatbooking;
Of course, I'm not sure this query will be much better. There are no JOIN conditions on these tables or WHERE clauses to filter results.
Related
I'm using MS Access for the following task (due to office restrictions). I'm quite new to SQL.
I have the following table:
I want to select all stores grouped by street, zip and place. But i only want to group them, if the SquareSum (after Group by) is < 1000. Rue de gare 2 should be grouped, while Bahnhofstrasse 23 should be seperate lines.
So far as i know MS Access doesn't allow a case statement. So my query looks like this:
SELECT
Street,
ZIP,
Place,
Sum(Square) AS SumSquare,
FROM Table1
SWITCH (SumSquare > 1000, GROUP BY (Street, ZIP, Place))
I also tried:
GROUP BY
SWITCH (SumSquare > 1000, (Street, ZIP, Place))
But it keeps telling me i have a syntax error. Could someone please help me?
In Access, I would do this with several queries.
This would be easier to do if you had an id on the rows (such as an autonumber).
First query identifies the streets that should be summed.
query: SumTheseStreets
SELECT
Street,
ZIP,
Place,
Sum(Square) AS SumSquare
FROM Table1
GROUP BY Street, ZIP, Place
HAVING sum(Square) < 1000
Note the HAVING which is a bit like a WHERE clause that's applied outside of the GROUP BY or SUM
Second query identifies the other rows (notes on this one below):
query: StreetsNotSummed
SELECT
Street,
ZIP,
Place,
Square AS SumSquare
FROM Table1
LEFT JOIN SumTheseStreets ON Table1.Street = SumTheseStreets.Street AND Table1.ZIP = SUmTheseStreets.ZIP AND Table1.Place = SumTheseStreets.Place
WHERE SumTheseStreets.Street IS NULL;
A couple of notes:
I've called the field SumSquare because I want it to be the same name as the SumSquare field in the first query
It uses the first query as one of the input "tables"
This uses a LEFT JOIN which means "give me all of the rows in the first table (table1) and if any rows in the second table (SumTheseStreets) match, put those in as well.
but then it filters out the rows that DO match.
So this query only lists the streets that you want NOT summed.
So now you need a third query.
This simply includes all of the rows in both of those queries.
I'm not too sure on the Access syntax on this one, but there's a union query wizard if this isn't right.
Query: TheAnswerRequired
SELECT
Street,
ZIP,
Place,
SumSquare
FROM SumTheseStreets
UNION
SELECT
Street,
ZIP,
Place,
SumSquare
FROM StreetsNotSummed
(it might need to be UNION ALL)
Good luck.
You can use UNION ALL:
SELECT ts.*
FROM (SELECT Street, Zip, Place, SUM(Square) as SumSquare
FROM Table1
GROUP BY Street, Zip, Place
) as ts
WHERE ts.SumSquare < 1000
UNION ALL
SELECT t1.*
FROM Table1 as t1 INNER JOIN
(SELECT Street, Zip, Place, SUM(Square) as SumSquare
FROM Table1
GROUP BY Street, Zip, Place
) as ts
ON t1.Street = ts.Street AND t1.Zip = ts.Zip and t1.Place = ts.Place
WHERE ts.SumSquare >= 1000
I have the following tables
AdmittedPatients(pid, workerid, admitted, discharged)
Patients(pid, firstname, lastname, admitted, discharged)
DiagnosticHistory(diagnosisID, workerid, pid, timeofdiagnosis)
Diagnosis(diagnosisID, description)
Here is an SQL Fiddle: http://sqlfiddle.com/#!15/e7403
Things to note:
AdmittedPatients is a history of all admissions/discharges of patients at the hospital.
Patients contain all patients who have records at the hospital. Patients also lists who are currently staying at the hospital (i.e. discharged is NULL).
DiagnosticHistory contains all diagnosis made.
Diagnosis has the description of the diagnosis made
Here is my task: list patients who were admitted to the hospital within 30 days of their last discharge date. For each patient list their patient identification number, name, diagnosis, and admitting doctor.
This is what I've cooked up so far:
select pid, firstname, lastname, admittedpatients.workerid, patients.admitted, admittedpatients.discharged
from patients
join admittedpatients using (pid)
group by pid, firstname, lastname, patients.admitted, admittedpatients.workerid, admittedpatients.discharged
having patients.admitted <= admittedpatients.discharged;
This returns pid's from 0, 1, and 4 when it should 0, 1, 2, and 4.
Not sure why out need group by or having here... no aggregate...
SELECT A.pid, firstname, lastname, A.workerid, P.admitted, A.discharged
FROM patients P
INNER JOIN admittedpatients A
on P.pID = A.pID
WHERE date_add(a.discharged, interval 30 day)>=p.admitted
and p.admitted >=a.discharged
updated fiddle: http://sqlfiddle.com/#!2/dc33c/30/0
Didn't get into returning all your needed fields but as this gets the desired result set I imagine it's just a series of joins from here...
Updated to postgresql:
SELECT A.pid, firstname, lastname, A.workerid, P.admitted, A.discharged
FROM patients P
INNER JOIN admittedpatients A
on P.pID = A.pID
WHERE a.discharged+ interval '30 day' >=p.admitted
and p.admitted >=a.discharged
http://sqlfiddle.com/#!15/e7403/1/0
I didn't see any diagnostic info in the fiddle, so I didn't return any.
select pid
,p.lastname,p.firstname
,ad.lastname,ad.firstname
from AdmittedPatients as a
join AdmittedPatients as d using (pid)
join Patients as p using (pid)
join AdminDoctors as ad on ad.workerid=a.workerid
where d.discharged between a.admitted-30 and a.admitted
You have a rather basic WHERE clause error here:
Admitted cannot be both before discharged AND after discharged+30
Also you have an extra semicolon before your whole query is ended, probably throwing out the last line altogether.
I think you're looking for admitted=discharged
am try to connect 3 tables but am getting error At most one record can be returned by this subquery
My code is
SELECT InvoiceNumber,
Terms(SELECT PaymentTerms
FROM PSD_customerPaymentTerms
WHERE PSD_customerPaymentTerms.PTId = NewInvoice_1.Terms
) AS Terms,
InvoiceDate,
OurQuote,
SalesPerson(SELECT FirstName
FROM Employee
WHERE Employee.EmployeeId = NewInvoice_1.SalesPerson
) AS SalesPerson,
CustomerName(SELECT CustomerName
FROM Customer
WHERE Customer.CustomerId = NewInvoice_1.CustomerName
) AS CustomerName,
OrderNumber,
GrandTotal,
(SELECT SUM(PaymentAmount)
FROM Payment_Receipt
WHERE Payment_Receipt.InvoiceNumber=NewInvoice_1.InvoiceNumber
) AS AmountPaid,
GrandTotal-IIf(AmountPaid Is Null,0,AmountPaid) AS AmountDue,
(SELECT InvoiceStatus
FROM Payment_Receipt
WHERE Payment_Receipt.InvoiceNumber=NewInvoice_1.InvoiceNumber
) AS Status -- Error getting after adding this line.
FROM NewInvoice_1;
Payment_Receipt Table contain Id, Invoice No, Customer name, Total Paid, Balance Amount, Payment Date, Payment Amount, Payment Type,Payment Remarks, InvoiceStatus.
This is my table
How to get InvoiceStatus from this table ??
One general way to solve this problem is to force the subquery to return one row by using max() on the column:
select max(someColumn)
from someTable
where ...
In case your data has multiple rows for the where clause.
While this approach will get your query working, it may not give the results you want. More likely the where clause needs work. That said, it can be very useful when diagnosing the problem, especially if you aren't sure which subquery is causing the problem you can remove the change one subquery at a time.
I am an oversea student, so I am not familiar with "Credit System" but I have a database question which is related to it. I just could not understand it well.
Here it the question:
Write a query:
The billing officer would like to know which customers are currently over their credit limit.
The schema of database is:
Sales_Rep (SLSRep_Number [pk], Last, First, Street, City, State, Post_Code,
Total_Commission, Commission_Rate)
Customer (Customer_Number [pk], Last, First, Street, City, State, Post_Code,
Balance, Credit_Limit, SLSRep_Number [fk])
Orders (Order_Number [pk], Order_Date, Customer_Number [fk])
Part (Part_Number [pk], Part_Description, Units_on_Hand, Item_Class, Warehouse_Number, Unit_Price)
Order_Line (Order_Number, [pk1] Part_Number [pk2], Number_Ordered, Quoted_Price)
Any idea?
Is that just :
Select customer_number,last,first,balance,credit_limit
from customer
where balance > credit_limit;
or might be:
select * from
(select mytable.customer_number,sum(mytable.number_ordered*mytable.quoted_price) as customer_cost from
(select customer.customer_number,order_line.number_ordered,order_line.quoted_price
from customer,orders,order_line
where customer.customer_number = orders.customer_number
and orders.order_number = order_line.order_number) mytable
group by mytable.customer_number) mytable2,customer
where customer.credit_limit < mytable2.customer_cost
and customer.customer_number = mytable2.customer_number;
first query is right, It will give the customer who has balance beyond the credit limit.
Given :
InsuranceCompanies (cid, name, phone, address)
Doctors (did, name, specialty, address, phone, age, cid)
Patients (pid, name, address, phone, age, gender, cid)
Visits (vid, did, pid, date, description)
Where:
cid - Insurance Company code
did - doctor code
pid - patient code
vid - code of visit
And a TASK : Find doctors (did, name) with number of visits (during this year) less than average number of visits to all doctors during this year.
My attempt is:
SELECT D.did, D. name
FROM Doctor D,Visit V
WHERE V.did = D.did and D.did = CV.did and CV.visits <
(SELECT AVG ( CV.visits)
FROM (SELECT V1.did AS did,COUNT(V1.vid) AS visits
FROM Visit V1
WHERE V1.date LIKE '%2012'
GROUP BY V1.did) AS CV)
A BIG THANKS TO Bridge Who shared the most beautifull and user freindly SQL commands visualator ever!
Databse Exemple : http://sqlfiddle.com/#!2/e85c7/3
Solution using views:
CREATE VIEW ThisYear AS
SELECT v.pid,v.vid,v.did
FROM Visits v
WHERE v.date LIKE '%2012';
CREATE VIEW DoctorsVisitCount AS
SELECT v.did, COUNT(v.vid) as c
FROM ThisYear v
GROUP BY v.did;
SELECT DISTINCT d.did,d.dname,dvc.c
FROM Doctors d,DoctorsVisitCount dvc
WHERE dvc.c < (SELECT AVG(dvc.c)
FROM DoctorsVisitCount dvc);