DavyGravy
DavyGravy

Reputation: 311

SQL Query to access database

I'm writing a database query that will show me where there is a space for parking. It is only used in July.

There is one table that shows all the spaces and whether they are rented that day. There is another table that has the spaces and their sizes. I want to be able to select those spaces that are available on all the days within the selected time period and have the correct size.

I am having a problem, though, selecting only the spaces available within the given time period. Here is the query so far but it does not contain anything concerning the space size as I want this part to work first.

  SELECT C.Plads, SUM[C.optaget] C.[ledlig] FROM
  (SELECT Plads FROM OptagetPladser AS A Inner JOIN Bådpladser as B ON  
  A.plads=B.Pladsnummer
  WHERE
  (A.dato>=" + Startdato + "and A.dato<="+Slutdato+") //checking the time period
  and (a.optaget = 0))                           //0 means the space is availible
  as C 
  GROUP BY C.Plads 
  HAVING SUM(C.optaget) >="+ diffResult+")";//diff result is the timespan

At the moment I'm getting the error

Syntax error (missing operator) in query expression 'SUM[C.optaget]'

Any ideas?

Upvotes: 0

Views: 412

Answers (1)

Sergei Danielian
Sergei Danielian

Reputation: 5015

First of all, you should rework your SQL query - it contains too many simple errors.

Here are a few.

  • Try adding a ',' and make some changes in query:

    SELECT C.Plads, SUM(C.optaget), C.ledlig FROM

  • Your subquery C doesn't have an optaget and ledlig fields too. To fix this add those fields right after sebquery's SELECT

  • Fix a syntax error here:

    (A.dato>=" + Startdato + "and A.dato<="+Slutdato+") which should be:

    (A.dato >= " + Startdato + " and A.dato <= "+Slutdato+")

  • Your last double quote is redundant as well as last ')'. Remove it:

    HAVING SUM(C.optaget) >= "+ diffResult+" ;//diff result is the timespan

Below is how your SQL query should look. Please, note: there are still missing fields optaget and ledlig in subquery C.

SELECT C.Plads, SUM(C.optaget), C.ledlig FROM
(
    SELECT Plads FROM OptagetPladser AS A 
    INNER JOIN Bådpladser as B 
    ON A.plads = B.Pladsnummer
    WHERE (A.dato >= " + Startdato + " AND A.dato <= " + Slutdato + ")
      AND (a.optaget = 0)
)
AS C 
GROUP BY C.Plads 
HAVING SUM(C.optaget) >= " + diffResult + ";

I believe, there could appear an architectural or performance issues, but without table data I can't say it for sure.

Upvotes: 2

Related Questions