PostgreSQL - ERROR: column does not exist SQL state: 42703 - sql

I am trying to do a cohort analysis and compare average number of rentals based on the renter's first rental year(= the year where a renter rented first time). Basically, I am asking the question: are we retaining renters whose first year renting was 2013 than renters whose first year was 2015?
Here is my code:
SELECT renter_id,
Min(Date_part('year', created_at)) AS first_rental_year,
( Count(trip_finish) ) AS number_of_trips
FROM bookings
WHERE state IN ( 'approved', 'aboard', 'ashore', 'concluded', 'disputed' )
AND first_rental_year = 2013
GROUP BY 1
ORDER BY 1;
The error message I get is:
ERROR: column "first_rental_year" does not exist
LINE 6: ... 'aboard', 'ashore', 'concluded', 'disputed') AND first_rent...
^
********** Error **********
ERROR: column "first_rental_year" does not exist
SQL state: 42703
Character: 208
Any help is much appreciated.

SELECT renter_id,
Count(trip_finish) AS number_of_trips
FROM (
SELECT renter_id,
trip_finish,
Min(Date_part('year', created_at)) AS first_rental_year
FROM bookings
WHERE state IN ( 'approved', 'aboard', 'ashore', 'concluded', 'disputed' )
) T
WHERE first_rental_year = 2013
GROUP BY renter_id
ORDER BY renter_id ;

ERROR:
SQL Error [42703]: ERROR: column XYZ does not exist
Check you have double quotes around Column Fields:
BAD:
update public."AppTime" t Set "CustomTask"= 'XYZ' where t.SharedAppId = 12890;
GOOD:
With double quotes around "SharedAppId"
update public."AppTime" t Set "CustomTask"= 'XYZ' where t."SharedAppId" = 12890;
If you created the table without quotes, you should not use quotes when querying it, and vice versa. This is explained in the manual: "If you want to write portable applications you are advised to always quote a particular name or never quote it"

Related

Unable to join two oracle tables

i have 2 tables.
First table SEC_SEAL_LOG with columns:
DATA_ADD,
DATA_AREA,
SEAL_NUMBER,
DATA_SEALING,
DATA_UNPLUG,
SORRUPTED.
SEC_WRITING_OFF_SEALS
second table with columns:
DATA, SEAL.
I want to put these 2 tables together, but I cannot understand where I have the error, I will be grateful for your help.
select DATA_ADD,
DATA_AREA,
SEAL_NUMBER,
DATA_SEALING,
DATA_UNPLUG,
СORRUPTED,
Data
from SEC_SEAL_LOG,SEC_WRITING_OFF_SEALS
where (data_area = (select data_area
from SEC_USERS_LIST
where login = LOWER(:APP_USER)
and SEAL_NUMBER = SEAL
)
or 20 >= (select u.role
from SEC_users_list u
where u.login = lower(:APP_USER)
)
)
and СORRUPTED = 'Так'
and SEAL_NUMBER = SEAL
ORDER BY data_add DESC
I amd getting this error
ORA-20999: Failed to parse SQL query! ORA-06550: line 7, column 4: ORA-00918: column ambiguously defined
The error Column Ambiguously Defined occurs when a column name is present in more than one table, and you have failed to specify which table.
You are doing that in this line: and SEAL_NUMBER = SEAL (which you have twice).
From which table to you want to compare that SEAL value?
Write it as SEC_SEAL_LOG.SEAL or SEC_WRITING_OFF_SEALS.SEAL or whatever table name you are trying to compare this value from, and it will get rid of the Column Ambiguously Defined error.

IBM Informix-SQL syntax error, basic query from Microsoft BIDS to Cisco UCCX database

I'm running the below query against an IBM Informix database and getting an ERROR 42000: A syntax error has occurred. The FROM and WHERE clauses run fine in other queries, so I'm looking at the SELECT and GROUP BY portions. Any ideas what's wrong with the syntax?
SELECT COUNT(DISTINCT "informix".agentconnectiondetail.sessionid) AS calls_abandoned,
DAY("informix".agentconnectiondetail.startdatetime) AS Expr2
FROM "informix".agentconnectiondetail, "informix".contactqueuedetail, "informix".contactservicequeue
WHERE "informix".agentconnectiondetail.sessionid = "informix".contactqueuedetail.sessionid AND
"informix".contactqueuedetail.targetid = "informix".contactservicequeue.recordid AND "informix".contactqueuedetail.disposition = 1 AND
"informix".agentconnectiondetail.startdatetime BETWEEN '2016-10-1 00:00:00' AND CURRENT
GROUP BY DAY("informix".agentconnectiondetail.startdatetime)
The goal btw is to find the total number of unique calls (calls_abandoned) that occur on each day of the month (1-31).
Replace the
GROUP BY DAY("informix".agentconnectiondetail.startdatetime)
by
GROUP BY 2

SQL not properly ended?

I know i don't have to use SQL but i have other code between declare begin and end . I just extracted this part and everything works except this . I get the following error:
Error report:
ORA-06550: line 5, column 5:
PL/SQL: ORA-00933: SQL command not properly ended
ORA-06550: line 3, column 5:
PL/SQL: SQL Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
This is the code:
DECLARE
BEGIN
UPDATE octombrie
SET nr_imprumut = I.nr
from (select book_date bd,count(book_date) nr
from rental
where to_char(book_date,'mm') like '10'
group by book_date) I
where data = I.bd;
END;
/
I don't get it what did i do wrong ?
EDIT: book_date will give me a day from the month of october. In that day multiple books are rented, so i find out how many books i rented by counting the number of times the same date apears (the rented books are in the rental table). I then take this data and Update October table(i put the number of books aka 'nr' where the date in the october table matches the date in which the books where rented);
DECLARE
BEGIN
UPDATE octombrie o
SET o.nr_imprumut =
(select count(r.book_date)
from rental r
where to_char(r.book_date,'mm') like '10' and o.data = r.book_date)
WHERE exists (select 1 from rental r
where to_char(r.book_date,'mm') like '10' and o.data = r.book_date);
END;
Use
where exists (select 1 from rental r
where to_char(r.book_date,'mm') like '10' and o.data = r.book_date)
if you want to update only those rows for which you found something in rental (otherwise ALL rows will be updated in octombrie)
Another variant (updates all October 2014 data in octombrie; for each data calculates the number of rental ON this data)
UPDATE octombrie o
SET o.nr_imprumut =
(select count(r.book_date)
from rental r
where r.book_date between trunc(o.data) and trunc(o.data) + 1 - 1/24/60/60)
WHERE o.data between to_date('2014-10-01','yyyy-mm-dd') and to_date('2014-10-31 23:59:59','yyyy-mm-dd hh24:mi:ss');
I guess you really wanted to something like that:
DECLARE
BEGIN
UPDATE octombrie o
SET o.nr_imprumut =
(SELECT count(1)
FROM rental r
WHERE r.book_date between o.data and o.data+1-1/86400)
WHERE data between to_date('2014-10-01','yyyy-mm-dd') and to_date('2014-10-31','yyyy-mm-dd');
END;
Note that the WHERE-clause of the UPDATE-statement specifies a year, I don't think you want to updated your records for all days in any october. Limiting the date like that (with a BETWEEN operator, but without a TO_CHAR) makes it possible to use an index on octombrie.data, something I hope you have.

Invalid Column Name in SQL Server Management Studio

When I execute this query in SQL Server Management Studio, this error appears:
'Msg 207, Level 16, State 1, Line 1
Invalid column name 'ACCOUNT_NO'.'
This is the code for the query:
DECLARE #largeaccnumber INT = ACCOUNT_NO
DECLARE #smallaccnumber INT
SET #smallaccnumber = (SELECT LEFT(#largeaccnumber, 6))
SELECT DNADRX.CODE,
DNADDR.NAME,
DNADDR.TYPE,
DNADDR.MAIL_NAME,
ADDRESS_LINE1,
ADDRESS_LINE2,
ADDRESS_LINE3,
TOWN_CITY,
COUNTY_STATE,
COUNTY_STATE_CODE,
COUNTRY,
POST_ZIP,
LAST_STAT_DATE,
ACCOUNT_NO
FROM DNADRX,
DNADDR,
BACCNT
WHERE DNADDR.CODE = DNADRX.ADDRESS_CODE
AND DNADDR.CODE = #smallaccnumber
ORDER BY DNADRX.CODE
I want the query to display the data from the columns of the different tables (the columns are listed in the SELECT bit of the query) from 3 different tables (DNADRX, DNADDR, BACCNT), and the factor linking all 3 tables together is the 6 digit code (ACCOUNT_NO in the BACCNT table, ADDRESS_CODE in the DNADRX table and CODE in the DNADDR table). Originally, ACCOUNT_NO from table BACCNT was 8 digits long, but I reduced it to the first 6 digits using SELECT LEFT and assigned this 6 digit value to the variable #smallaccnumber.
Whenever I try to execute the query, it keeps telling me that 'ACCOUNT_NO' is an invalid code name. I have checked the spelling, refreshed using IntelliSense and tried 'BACCNT.ACCOUNT_NO' instead of just 'ACCOUNT_NO' on the first line of the query but it still won't work (instead it says that the multi-part identifier could not be bound when I try 'BACCNT.ACCOUNT_NO').
I am really new to SQL coding so sorry if the answer to my problem is really simple.
Thank you for your assistance :)
You can try something like this.
This assumes you know the 6 character code. This query will only find results IF there is a record matching in EVERY table. If one table doesn't find a matching record this query will return NOTHING. If you want to find a row even if a recrod is missing from a table, replace the "INNER JOIN" with "LEFT OUTER JOIN"
SELECT Dnadrx.Code,
Dnaddr.Name,
Dnaddr.Type,
Dnaddr.Mail_Name,
Address_Line1,
Address_Line2,
Address_Line3,
Town_City,
County_State,
County_State_Code,
Country,
Post_Zip,
Last_Stat_Date,
Account_No
FROM Dnaddr
INNER JOIN BACCNT ON DNAADDR.CODE = BACCNT.ACCOUNT_NO
INNER JOIN Dnadrx ON Dnaaddr.Code=Dnaadrx.Address_Code
WHERE Dnaddr.Code='YOUR 6 CHARACTER CODE GOES HERE'
ORDER BY Dnadrx.Code;

Create view query not working

This the problem in my book that I am trying to solve..I need to create this report..
A list of the programs on all channels for a specific day showing the channel number, supplier, package, program name, rating code, and show time. This will be similar to a program guide, only not package specific. This is a date-driven report, therefore it should only display programs for a single date specified.
I tried this so far..
CREATE VIEW PROG_LINEUP AS
SELECT DISTINCT
PC.PROGTIME AS `SHOWTIME`,
P.PROGNAME AS `PROGRAM TITLE`,
C.CHID AS `CHANNEL #`,
SU.SUPNAME AS `SUPPLIER`,
R.RATING AS `RATING`
FROM
PROG_CHAN PC,
CHANNELS C,
SUPPLIERS SU,
PROGRAM P,
CHANNEL_PACKAGE CP,
RATING R
WHERE
PC.SHOWDATE = '18-DEC-10'
AND P.PROGID = PC.PROGID
AND CP.CHID = PC.CHID
AND R.RATINGID = P.RATINGID
AND C.CHID = PC.CHID
AND SU.SUPID = P.SUPID
ORDER BY PC.CHID;
But it's giving this error when the table Prog_chan exists! I checked.. What is wrong?
Please tell me if any table script is required. Please help...
WHERE PC.SHOWDATE = '18-DEC-10' AND
*
ERROR at line 13:
ORA-00903: invalid table name
I cant figure out what is wrong since Prog_chan table exists and has values too in it..
QL> desc prog_chan;
Name Null? Type
----------------------------------------- -------- ----------------------------
CHANID NOT NULL NUMBER(5)
PROGID NOT NULL NUMBER(5)
SHOWDATE NOT NULL DATE
STARTTIME NOT NULL DATE
#Jeff -
I removed that comma but error is this now...
CHANNEL_PACKAGE CP, * ERROR at line 11: ORA-00942: table or view does not exist
You have an erroneous extra comma before the WHERE clause.
RATING R,
WHERE PC.SHOWDATE = '18-DEC-10' AND