Sample Header Ad - 728x90

Database Administrators

Q&A for database professionals who wish to improve their database skills

Latest Questions

0 votes
1 answers
1890 views
Why can't I use a table alias in a subquery?
I have a MS Access database. When I try to run this query: SELECT * FROM ( SELECT * FROM tblClients AS a1 LEFT JOIN ( SELECT TOP 1 lVersion, lRequestID AS lVerReqId, lClientId FROM tblClientsVersionsHistory WHERE ID = ( SELECT MAX(ID) FROM tblClientsVersionsHistory WHERE lClientId = a1.Id ) ) AS a2...
I have a MS Access database. When I try to run this query: SELECT * FROM ( SELECT * FROM tblClients AS a1 LEFT JOIN ( SELECT TOP 1 lVersion, lRequestID AS lVerReqId, lClientId FROM tblClientsVersionsHistory WHERE ID = ( SELECT MAX(ID) FROM tblClientsVersionsHistory WHERE lClientId = a1.Id ) ) AS a2 ON a1.Id = a2.lClientId ) It suggest me to enter a1.Id, but I want to use it from (SELECT * FROM tblClients AS a1).
John (11 rep)
Dec 14, 2020, 10:27 AM • Last activity: Jan 2, 2023, 05:01 PM
2 votes
1 answers
295 views
(postgre)SQL how to disambiguate a name that could be a column name or a table alias
In PostgreSQL anyway you can pass a table (row) alias into a function and it will pass the entire record in. But when you have a table alias x and also a column name x, when you specify f(x) it will not know which one, throw error "ambiguous name x" or pass the wrong one. Obviously it is easy to dis...
In PostgreSQL anyway you can pass a table (row) alias into a function and it will pass the entire record in. But when you have a table alias x and also a column name x, when you specify f(x) it will not know which one, throw error "ambiguous name x" or pass the wrong one. Obviously it is easy to disambiguate for the column name: just precede with it's table alias: t.x. But what if I want to disambiguate for the table alias? Simple example: select f(x) from (select 1 x) x Obviously I can avoid having those clashes by renaming either the table alias or the column, but there should be a way to say "!x" or "table x" or whatever to force a reference to the table alias (or row variable).
Gunther Schadow (523 rep)
Jan 1, 2023, 10:53 AM • Last activity: Jan 1, 2023, 07:19 PM
1 votes
2 answers
646 views
Table Aliases: MySQL says table R1 does not exist when Joining the same table 3 times
I have a table `R` with the following fields stated below: weatherID, dateRecorded, Days, locationID, timeRecorded, rainAmount, rainNumberHours, temperature, windSpeed, windDirection NB: The `Days` field is an integer type that describes the number of days with auto-increment +1. The `weatherID` fie...
I have a table R with the following fields stated below: weatherID, dateRecorded, Days, locationID, timeRecorded, rainAmount, rainNumberHours, temperature, windSpeed, windDirection NB: The Days field is an integer type that describes the number of days with auto-increment +1. The weatherID field is a concatenation of two other fields in two different tables not shown here. This schema of the table is simplified, but it is relevant to what I want to investigate. We want to find out if there exist some weather patterns that follow other weather patterns and to find records that satisfy specific parameters and conditions. I use aliases in the query to get records for different field values Days. A model query will make it clear: I am joining the table R, multiple times in an alias style query so that I have alias R1 and alias R2 making it look like joining three tables together.
$sql = "
SELECT R.weatherID, R.Days, R.locationID, R.timeRecorded, R.windDirection, 
R1.weatherID, R1.Days, R1.locationID, R1.timeRecorded, R1.windDirection, 
R2.weatherID, R2.Days, R2.locationID, R2.timeRecorded, R2.windDirection 
FROM ((R 
INNER JOIN R1 ON R.Days= R1.Days) 
INNER JOIN R2 ON R.locationID= R2.locationID) 
WHERE ((R.Days)=[R1].[Days]-2) AND ((R1.Days)=[R2].[Days]-2);
"
This model gives me similar weather patterns that re-occurs or repeat within two (2) days (refers to the "2" in the WHERE clause). Now the question is how do we I want to arrange the results of the query in a grid-like manner in the following way: R.weatherID R.Days R.locationID R.windDirection R1.weatherID R1.Days R1.locationID R1.windDirection R2.weatherID R2.Days R2.locationID R2.windDirection And the next record is also in the same grid-like format. I have only table R in the database: that is I have weatherdb.R, but I don't have "weatherdb.R1" and "weatherdb.R2" in other words, I don't have tables R1 and R2 but am using them in aliases, which is not working. But, MySQL said: Documentation #1146 - Table 'weatherdb.R1' doesn't exist The sqlWeather1.php file is my first trial example below:
");
$first_row = true;
while ($row = mysqli_fetch_assoc($result)) {
    if ($first_row) {
        $first_row = false;
        // Output header row from keys.
        echo '';
        foreach($row as $key => $field) {
            echo '' . htmlspecialchars($key) . '';
        }
        echo '';
    }
    echo '';
    foreach($row as $key => $field) {
        echo '' . htmlspecialchars($field) . '';
    }
    echo '';
}
echo("");
?>
BenGik (13 rep)
Oct 7, 2022, 09:28 PM • Last activity: Oct 8, 2022, 07:51 AM
2 votes
1 answers
2454 views
Alias Names and Two Table Query Problems
I'm quite new to querying databases, I've only been doing it for about 2 weeks in the current job that I'm in and have never touched SQL prior to this. I'm working on trying to use alias names to pull two different sets of tables and display the fields in each table, all while ordering them by a spe...
I'm quite new to querying databases, I've only been doing it for about 2 weeks in the current job that I'm in and have never touched SQL prior to this. I'm working on trying to use alias names to pull two different sets of tables and display the fields in each table, all while ordering them by a specific field. Here is the code I'm currently trying to use when to do that: SELECT ars.facility_id, ars.service_code, ars.coding_system, ars.result_label_seq, ars.display_seq, ats.facility_id, ats.service_code, ats.coding_system, ats.display_name, ats.test_name FROM anc_test_codes AS ats, anc_results_seqs AS ars WHERE ats.service_code = ars.service_code AND ars.coding_system = ats.coding_system AND ars.facility_id = ats.facility_id AND ats.facility_id = 'C' AND ats.service_code = 'CBC' ORDER BY ats.display_name I'm sure my methods and style is totally hideous, so excuse this. The error I'm getting is a generic Oracle SQL error: SQL Command Not Properly Ended - ORA-00933 Can anyone tell me what I'm doing wrong? Any help on this would be greatly appreciated. Thanks!
SQLSavant (209 rep)
Apr 30, 2012, 03:28 AM • Last activity: Sep 27, 2020, 09:40 AM
0 votes
0 answers
18 views
Sql query is failing in Oracle DB
I run below query to fetch data from a `Oracle DB` - SELECT T1.Col1, T2.Col1 AS Tab2_Col1 FROM Tab1 AS T1 INNER JOIN Tab2 AS T2 ON Tab1.Col3 = Tab2.Col4 With that, I am getting below error - ORA-00933: SQL command not properly ended 00933. 00000 - "SQL command not properly ended" *Cause: *Action: Er...
I run below query to fetch data from a Oracle DB - SELECT T1.Col1, T2.Col1 AS Tab2_Col1 FROM Tab1 AS T1 INNER JOIN Tab2 AS T2 ON Tab1.Col3 = Tab2.Col4 With that, I am getting below error - ORA-00933: SQL command not properly ended 00933. 00000 - "SQL command not properly ended" *Cause: *Action: Error at Line: 2 Column: 32 Can someone help me to understand why my query is failing? Thanks for your time
Bogaso (101 rep)
Sep 27, 2020, 08:53 AM • Last activity: Sep 27, 2020, 09:39 AM
Showing page 1 of 5 total questions