How to fix "Inner Join Group-By" question [duplicate] - sql

This question already has answers here:
ORA-00979 not a group by expression
(10 answers)
Closed 3 years ago.
Trying to run a query to pull back no duplicate lines. Currently when running this script I get an error back from Oracle saying
"ORA-00979: not a GROUP BY expression"
Tried removing different select rows.
SELECT QUEUE_NAME, ITEMS_IN_QUEUE, CREATION_USR_ID
FROM IN_WF_QUEUE B
INNER JOIN IN_WF_ITEM A
ON A.QUEUE_ID = B.QUEUE_ID
AND B.QUEUE_NAME LIKE '__________ %'
AND (A.QUEUE_START_TIME < (select sysdate from DUAL)-1000)
GROUP BY B.QUEUE_ID;
Thank you in advance for any help on this issue.

If you want to remove duplicates, why don't you use DISTINCT? That's more natural than using GROUP BY without any aggregates.
SELECT DISTINCT
queue_name, items_in_queue, creation_usr_id
FROM in_wf_queue b
INNER JOIN in_wf_item A
ON A.queue_id = b.queue_id
AND b.queue_name LIKE '__________ %'
AND A.queue_start_time < sysdate - 1000;
Also, there's no need to select SYSDATE from dual; it is a function that can be used standalone, as in my example. Note that it returns both date and time, so - maybe you'd want to remove time component by truncating it, i.e. use trunc(sysdate). Subtracting 1000 from it means "1000 days ago"; just saying, to avoid possible confusion.

Related

Count over previously defined alias [duplicate]

This question already has answers here:
Why can't I use column aliases in the next SELECT expression?
(4 answers)
Closed 3 months ago.
I'm trying to avoid writing twice the EXTRACT condition. There is any way to do that? Count over the previous alias apparently is not valid.
SELECT EXTRACT(DECADE FROM to_date(released_year::text, 'yyyy')) AS decade,
count(decade) AS total_by_decade
FROM album ...
Basically you can avoit it completely, but you can make a CTE
WITH
CTE as(
SELECT EXTRACT(DECADE FROM to_date(released_year::text, 'yyyy')) AS decade
FROM album ...)
SELECT decade, count(decade) AS total_by_decade FROM CTe GROUP BY decade

How to substruct dates in oracle db? [duplicate]

This question already has answers here:
Oracle date difference to get number of years
(5 answers)
Closed 2 years ago.
I want to substruct 2 dates(current date and hairdressing_date) to get the result from table to represent data during last 2 years.
I have following SELECT statement:
SELECT count(c_id)
INTO counter
FROM RESERVATIONS r
WHERE r.customer_id = 1
AND (Sysdate - r.hairdressing_date) / 365 < 2;
It is custom, but I am not sure about '/ 365' part of code.
How to get the needed data correctly?
Could you write the correct implementation of this line?
Please use MONTHS_BETWEEN() function like this:
SELECT MONTHS_BETWEEN(TRUNC(SYSDATE), TO_DATE('22.01.2019', 'DD.MM.YYYY'))/12
FROM DUAL;
and revert to this one considering your case:
SELECT count(c_id)
INTO counter
FROM RESERVATIONS r
WHERE r.customer_id = 1
AND MONTHS_BETWEEN(TRUNC(SYSDATE), hairdressing_date)/12 < 2;

Binding parameter in SELECT query for PostgreSQL [duplicate]

This question already has answers here:
How to declare a variable in a PostgreSQL query
(15 answers)
Closed 3 years ago.
select (CURRENT_TIMESTAMP - '60 days'::interval);
This shows me a current time stamp with an interval of 60 days getting subtracted.
SELECT VALUE
FROM schema_name.some_parameter
WHERE some_parameter.NAME LIKE 'some_reference_name'
I want to add the above query in SELECT so that I don't need to add the hardcoded data for 60 days. I want to get it through the parameter variable.
Basically i need to use nested queries where the second query gets me the 60 days value i.e. hardcoded in first query.
Is there a possible solution for my problem?
Maybe something like this?
with v(val)
as
(
VALUES( CURRENT_TIMESTAMP - '60 days'::interval)
)
SELECT v.val from
schema_name.some_parameter cross join v
WHERE some_parameter.NAME LIKE 'some_reference_name'

Selecting records based on other records using separate date/time fields [duplicate]

This question already has answers here:
TSQL to combine a date field and a time field
(4 answers)
Closed 9 years ago.
I have a database that stores date and time in separate fields. I need to select all records that occurred within + and - 90 minutes of the date and time each of these happened.
I am able to get everything in the format I need to pull it off
SELECT UFV1.USR_DATE
,UFV1.USR_TIME
,LEFT(CAST(DATEADD(minute, -90, UFV1.USR_TIME) AS TIME),8) AS MIN_TIME
,LEFT(CAST(DATEADD(minute, +90, UFV1.USR_TIME) AS TIME),8)AS MAX_TIME
FROM USR_Y_FACILITY_VISIT UFV1
WHERE UFV1.MASTER_CUSTOMER_ID = '2'
ORDER BY UFV1.USR_DATE, UFV1.USR_TIME
Where I am stuck is I need to build a query that takes this info (basically the min/max from each line) then selects all the info in the same table based off that. Thank you for your help I am totally stumped as to where to go next.
Add the key fields for the table to your query as given, along with a corresponding GROUP BY clause. Then query your data table joind to that query as a sun-query on this format:
SELECT *
FROM T
JOIN ( SELECT * ... ) U
ON U.key = T.key
AND T.dateTime BETWEENU.MinDateTime AND U.MaxDateTime
If I correctly understood a question then, this request is necessary for you
SELECT *
FROM USR_Y_FACILITY_VISIT UFV1
WHERE EXISTS (
SELECT 1
FROM USR_Y_FACILITY_VISIT UFV2
WHERE UFV2.MASTER_CUSTOMER_ID = '2'
AND UFV1.USR_DATE = UFV2.USR_DATE
AND UFV1.USR_TIME BETWEEN CAST(DATEADD(minute, -90, UFV2.USR_TIME) AS time)
AND CAST(DATEADD(minute, 90, UFV2.USR_TIME) AS time)
)

Calculating difference of dates In Postgresql

I'm trying to find out the time between certain fields in my tables. However cause I'm using Postgresql :(( I can't use the DATEDIFF function. I can't find any clear guides/ tutorials on the net which shows how to do a similar thing in Postgres so I need help doing the same thing but in Postgres
I'm assuming this query would work if I was using a RDBMS that supported the DATEDIFF function so basically my question is how can I change this so it works using features provided by Postgresql?
SELECT Question.ID,
Question.Status, COUNT (qUpdate.ID) AS NumberofUpdates,
DATEDIFF (Question.LoggedTime,MIN(qUpdate.UpdateTime)) AS TimeBeforeFirstUpdate,
DATEDIFF(Question.LoggedTime, MAX(qUpdate.UpdateTime)) AS TimeBeforeLastUpdate
FROM qUpdate
LEFT JOIN Question ON qUpdate.qID=Question.ID
WHERE Question.Status = 'closed' AND qUpdate.Update NOT NULL
GROUP BY Question.Status, Question.ID, Question.LoggedTime;
If you need more info or any clarification I'll responsd ASAP.
You don't need a "datediff" function.
Just subtract the two dates:
Question.LoggedTime - MIN(qUpdate.UpdateTime)
In case you don't know, but all that is documented online:
http://www.postgresql.org/docs/current/static/functions-datetime.html
You can use the age(<date1>, <date2>) function (instead of DATEDIFF).
This should work -
SELECT Question.ID,
Question.Status, COUNT (qUpdate.ID) AS NumberofUpdates,
age(Question.LoggedTime,MIN(qUpdate.UpdateTime)) AS TimeBeforeFirstUpdate,
age(Question.LoggedTime, MAX(qUpdate.UpdateTime)) AS TimeBeforeLastUpdate
FROM qUpdate
LEFT JOIN Question ON qUpdate.qID=Question.ID
WHERE Question.Status = 'closed' AND qUpdate.Update NOT NULL
GROUP BY Question.Status, Question.ID, Question.LoggedTime;
Note, if psql gives you this error - ERROR: date/time field value out of range, then you would need to choose an appropriate datestyle.
SELECT extract(year from age('2014-01-23', '1985-08-27'));
-- returns 28 years, my current age.
this gives you the time diff in seconds:
select extract(epoch from to_date::timestamp - from_date::timestamp)