put together two selects in one column in sql (apex) - sql

i have 3 tables: reservation (with foreign key showing_id_showing), showing (with column "date" and foreign key film_id_film) and film (with column "title").
i need to show date together with film title in one column.
separately it will look like this, but i don't know how to put it together.
(SELECT date FROM showing WHERE id_showing=showing_id_showing) "Date",
(SELECT title FROM film WHERE id_film=film_id_film) "Title",

To get the result set from multiple tables in SQL Server you can you SQL JOIN command. Below is similar example using INNER JOIN:
SELECT S.Date,T.Title FROM Showing S
Inner Join Title
on T.ID_Film = T.Film_ID_Film
Where S.ID_Showing=S.Showing_ID_Showing
The actual solution may vary as per your table schema definition.

SELECT film.title || ' # ' || showing.date AS title_and_date
FROM reservation
INNER JOIN showing
ON showing.id_showing = reservation.showing_id_showing
INNER JOIN film
ON film.id_film = reservation.film_id_film
WHERE reservation.id_reservation = :THE_RESERVATION_I_WANT_TO_SHOW
This is assuming you wanted to concatenate to get "one column".
Also assuming you wanted to show the one reservation. Otherwise just drop the last line with the WHERE

Related

SQL select with three tables

Hi guys I'm new with databases and I'm trying to make a query where I join 3 tables. I could make it and I want to clean up the result. I want to know how can I delete the column "pin" from users table and maybe some "ids" columns.
Select * from "wish-list"
Join products
On "wish-list".id = products.holiday_id
Join users
On "wish-list".user_id = users.id
Where "wish-list".id = 1
You need to specify which columns you really need in your output. At the moment you are using
SELECT * which outputs all columns of all joined tables.
Here is what it should look like:
SELECT holiday, products.description, users.pin FROM "wish-list"
JOIN products ON "wish-list".id = products.holiday_id
JOIN users ON "wish-list".user_id = users.id
WHERE "wish-list".id = 1
It's important that you reference all columns which are not your main entity (here wish-list) with tablename.column (products.description and not only description). It will work without referencing strictly but only if the column name is unique in your query.
Furthermore you can rename columns. This is useful for example if you want to get the id's of the product table and the wish-list table.
SELECT product.id AS product_id, id AS wishlist_id FROM "wish-list"
...
Hope that helps!

JOIN of 4 tables, how to restrict SELECT columns to one table only?

I am working on ABAP program - user input is to query column ANLAGE and output is to get all records from table EADZ (and only fields of EADZ) based on ANLAGE.
Statement and joins should work like this:
Input ANLAGE, find in table EASTL, gets LOGIKNR
Input LOGIKNR, find in table EGERR, gets EQUNR
Input EQUNR, find in table ETDZ, gets LOGIKZW
Input LOGIKZW, find in table EADZ, gets all records (this is the final output)
Here is the code I tried:
DATA: gt_cas_rezy TYPE STANDARD TABLE OF eadz,
lv_dummy_eanl LIKE eanl-anlage.
SELECT-OPTIONS: so_anl FOR lv_dummy_eanl NO INTERVALS NO-EXTENSION.
SELECT * FROM eadz
INNER JOIN etdz ON eadz~logikzw EQ etdz~logikzw
INNER JOIN egerr ON etdz~equnr EQ egerr~equnr
INNER JOIN eastl ON egerr~logiknr EQ eastl~logiknr
INTO CORRESPONDING FIELDS OF TABLE #gt_cas_rezy
WHERE eastl~anlage IN #so_anl.
I got the records from table EADZ except that the date fields are empty (even though, they are filled in database table). I am assuming there is a problem with JOINs since in statement like this I join all the fields of all 4 tables into one "record" and then to corresponding fields of internal table.
How to get the values of date fields?
You can find the answer in the documentation.
If a column name appears multiple times and no alternative column name was granted, the last column listed is assigned.
In your case, at least two tables share the same column name. Therefore the values from the last mentioned table are used in the join.
You can solve this by listing the columns explicitly (or eadz~* in your case), giving an alias if required.
SELECT EADZ~* FROM EADZ INNER JOIN ETDZ ON EADZ~LOGIKZW = ETDZ~LOGIKZW
INNER JOIN EGERR ON ETDZ~EQUNR = EGERR~EQUNR
INNER JOIN EASTL ON EGERR~LOGIKNR = EASTL~LOGIKNR
INTO CORRESPONDING FIELDS OF TABLE #gt_cas_rezy
WHERE EASTL~ANLAGE IN #SO_ANL.
If you require additional fields, you can add them explicily with e.g. EADZ~*, EASTL~A.

I want to use CONCAT and exclude and duplicate any duplicate entries

I'm trying to use the CONCAT expression, but also exclude any duplicate entries.
So I'm trying to update a report based on a single process held in our product. The problem is that whoever created the tables that the current report is pulling from is not from a single table. Currently I have found three tables that the report pulls from for one column.
SELECT concat(dbo.t_log_TaskBody.TaskDescription,' ', dbo.t_ezDocument.FileName) as Title
FROM dbo.t_logs_SigDocPrintedEmailed
LEFT JOIN dbo.t_log_Data ON t_logs_SigDocPrintedEmailed.t_ezDataPKid = dbo.t_log_Data.PKid
LEFT JOIN dbo.t_log_TaskBody ON dbo.t_logs_SigDocPrintedEmailed.t_ezSignDocumentQ_PKid = dbo.t_log_TaskBody.DocumentId
LEFT JOIN dbo.t_ezSignDocumentQ ON dbo.t_logs_SigDocPrintedEmailed.t_ezSignDocumentQ_PKid = dbo.t_ezSignDocumentQ.PKid
LEFT JOIN dbo.t_ezArcSigDocQLog ON dbo.t_logs_SigDocPrintedEmailed.t_ezSignDocumentQ_PKid = dbo.t_ezArcSigDocQLog.t_ezSignDocQPKID
LEFT JOIN dbo.t_ezDocument ON dbo.t_ezSignDocumentQ.t_ezDocument = dbo.t_ezDocument.PKID or dbo.t_ezArcSigDocQLog.t_ezDocument = EasyID.dbo.t_ezDocument.PKID
So now that I have one entry that happens to connect to two of the tables I'm pulling from to get a title of a document I end up with the title appearing twice in one box. Is there anyway I can use CONCAT to combine the two tables while keeping it to unique entries, or is there a better way of doing this.
I'll get Something along the lines of:
Title
null
Title Title
null
Title
Title
Based on your sample results, I think you want COALESCE(), not CONCAT():
concat(dbo.t_log_TaskBody.TaskDescription, dbo.t_ezDocument.FileName) as Title
You have no examples where results are actually concatenated.

Join SQL tables, but with additional info from joined table

Here is a basic illustration of the issue
I have two tables I want to join.
One table contains information about a product sale. (product_sales)
The other contains keys for the names of locations. (states_keys)
Both contain the same column 'state_key'.
The states_keys table has an additional column called 'state_names'.
I would like to join the table so that I can view the accurate state name that coordinates to each product sale.
So, I want to join where each table's state_key are = but display the additional column state_names from table states_keys.
SELECT product_sales.*, state_names
FROM product_sales
INNER JOIN states_keys
ON product_sales.state_key = states_keys.state_key
Try with something like this:
Select prod.*, state.state_names
from product_sales prod
inner join states_keys state on state.state_key = prod.state_key

Select average rating from another datatable

I have 3 data tables.
In the entries data table I have entries with ID (entryId as primary key).
I have another table called EntryUsersRatings in there are multiple entries that have entryId field and a rating value (from 1 to 5).
(ratings are stored multiple times for one entryId).
Columns: ratingId (primary key), entryId, rating (integer value).
In the third data table I have translations of entries in the first table (with entryId, languageId and title - translation).
What I would like to do is select all entries from first data table with their titles (by language ID).
On a top of that I want average rating of each entry (which can be stored multiple times) that is stored in EntryUsersRatings.
I have tried this:
SELECT entries.entryId, EntryTranslations.title, AVG(EntryUsersRatings.rating) AS AverageRating
FROM entries
LEFT OUTER JOIN
EntryTranslations ON entries.entryId = EntryTranslations.entryId AND EntryTranslations.languageId = 1
LEFT OUTER JOIN
EntryUsersRatings ON entries.entryId = EntryUsersRatings.entryId
WHERE entries.isDraft=0
GROUP BY title, entries.entryId
isDraft is just something that means that entries are not stored with all information needed (just incomplete data - irrelevant for our case here).
Any help would be greatly appreciated.
EDIT: my solution gives me null values for rating.
Edit1: this query is working perfectly OK, I was looking into wrong database.
We also came to another solution, which gives us the same result (I hope someone will find this useful):
SELECT entries.entryId, COALESCE(x.EntryUsersRatings, 0) as averageRating
FROM entries
LEFT JOIN(
SELECT rr.entryId, AVG(rating) AS entryRating
FROM EntryUsersRatings rr
GROUP BY rr.entryId) x ON x.entryId = entries.entryId
#CyberHawk: as you are using left outer join with entries, your result will give all records from left table and matching record with your join condition from right table. but for unmatching records it will give you a null value .
check out following link for the deta:
http://msdn.microsoft.com/en-us/library/ms187518(v=sql.105).aspx