ERROR: ORA-00923: FROM keyword not found where expected - sql

I tried to fetch data from a oracle sql table with the count of records. I tried like following,
SELECT *,
(COUNT(BRAND_ID) AS TOTAL)
FROM
(
SELECT BRAND_ID,
BRAND_CODE,
BRAND_TITLE
FROM BRAND
WHERE ACTIVE = '1'
ORDER BY BRAND_TITLE ASC
OFFSET 10 ROWS
FETCH NEXT 10 ROWS ONLY
) BRAND
LEFT JOIN
((
SELECT PRODUCT_ID,
PRODUCT_SKU_ID,
PRODUCT_WEB_ID,
PRODUCT_TITLE,
PRODUCT_SALES_PRICE,
PRODUCT_REGULAR_PRICE,
PRODUCT_RATING
FROM PRODUCT
WHERE
(
PRODUCT_TYPE='B'
OR PRODUCT_TYPE='R'
)
AND AVAILABILITY='1'
) PRDUCT ) ON BRAND.BRAND_CODE= PRDUCT.BRAND_CODE
When I'm executing this I got the following error,
ERROR: ORA-00923: FROM keyword not found where expected
How may I fix this.
Thanks in Advance!

I guess You should remove * from select statement in the first line. Try the below one.
SELECT (COUNT(BRAND_ID) AS TOTAL)
FROM
(
SELECT BRAND_ID,
BRAND_CODE,
BRAND_TITLE
FROM BRAND
WHERE ACTIVE = '1'
ORDER BY BRAND_TITLE ASC
OFFSET 10 ROWS
FETCH NEXT 10 ROWS ONLY
) BRAND
LEFT JOIN
((
SELECT PRODUCT_ID,
PRODUCT_SKU_ID,
PRODUCT_WEB_ID,
PRODUCT_TITLE,
PRODUCT_SALES_PRICE,
PRODUCT_REGULAR_PRICE,
PRODUCT_RATING
FROM PRODUCT
WHERE
(
PRODUCT_TYPE='B'
OR PRODUCT_TYPE='R'
)
AND AVAILABILITY='1'
) PRDUCT ) ON BRAND.BRAND_CODE= PRDUCT.BRAND_CODE

You are using a aggreagte function in the select statement . So you cannot simply call Select * for other columns.
First you should give an alias for the inside columns selected for easiness.
Then select that columns in the outside SELECT
Since one of the column in select is using agg function then a Group By should be done by other columns coming in Select.
Here for easiness i gave column name as c2,c3....rename as like u want.
If no alias is given u can specify the column as it is specified.
SELECT c2,c3,c4,c5,c6,c7,c8,c9,c10,
COUNT(BRAND_ID) AS TOTAL
FROM
(
SELECT BRAND_ID ,
BRAND_CODE AS c2,
BRAND_TITLE AS c3
FROM BRAND
WHERE ACTIVE = '1'
ORDER BY BRAND_TITLE ASC
OFFSET 10 ROWS
FETCH NEXT 10 ROWS ONLY
) BRAND
LEFT JOIN
((
SELECT PRODUCT_ID AS c4,
PRODUCT_SKU_ID AS c5,
PRODUCT_WEB_ID AS c6,
PRODUCT_TITLE AS c7,
PRODUCT_SALES_PRICE AS c8,
PRODUCT_REGULAR_PRICE AS c9,
PRODUCT_RATING AS c10
FROM PRODUCT
WHERE
(
PRODUCT_TYPE='B'
OR PRODUCT_TYPE='R'
)
AND AVAILABILITY='1'
) PRDUCT ) ON BRAND.BRAND_CODE= PRDUCT.BRAND_CODE
Group By c2,c3,c4,c5,c6,c7,c8,c9,c10

I don't have 12c, so can't test, but maybe this is what you're after?
SELECT *
FROM
(
SELECT BRAND_ID,
BRAND_CODE,
BRAND_TITLE
FROM (select b.*,
count(brand_id) over () total
from BRAND b
WHERE ACTIVE = '1'
ORDER BY BRAND_TITLE ASC
OFFSET 10 ROWS
FETCH NEXT 10 ROWS ONLY
) BRAND
LEFT JOIN
((
SELECT PRODUCT_ID,
PRODUCT_SKU_ID,
PRODUCT_WEB_ID,
PRODUCT_TITLE,
PRODUCT_SALES_PRICE,
PRODUCT_REGULAR_PRICE,
PRODUCT_RATING
FROM PRODUCT
WHERE
(
PRODUCT_TYPE='B'
OR PRODUCT_TYPE='R'
)
AND AVAILABILITY='1'
) PRDUCT ) ON BRAND.BRAND_CODE= PRDUCT.BRAND_CODE;
This uses an analytic query to get the count of all brand_ids over the whole table before you filter the rows. I'm not sure if you wanted the count per brand_id (count(*) over (partititon by brand_id) or perhaps the count of distinct brand_ids (count(distinct brand_id) over ()), though, so you'll have to play around with the count function to get the results you're after.

Related

BigQuery SQL: Sum of first N related items

I would like to know the sum of a value in the first n items in a related table. For example, I want to get the sum of a companies first 6 invoices (the invoices can be sorted by ID asc)
Current SQL:
SELECT invoices.company_id, SUM(invoices.amount)
FROM invoices
JOIN companies on invoices.company_id = companies.id
GROUP BY invoices.company_id
This seems simple but I can't wrap my head around it.
Consider also below approach
select company_id, (
select sum(amount)
from t.amounts amount
) as top_six_invoices_amount
from (
select invoices.company_id,
array_agg(invoices.amount order by invoices.invoice_id limit 6) amounts
from your_table invoices
group by invoices.company_id
) t
You can create order row numbers to the lines in a partition based on invoice id and filter to it, something like this:
with array_table as (
select 'a' field, * from unnest([3, 2, 1 ,4, 6, 3]) id
union all
select 'b' field, * from unnest([1, 2, 1, 7]) id
)
select field, sum(id) from (
select field, id, row_number() over (partition by a.field order by id desc) rownum
from array_table a
)
where rownum < 3
group by field
More examples for analytical examples here:
https://medium.com/#aliz_ai/analytic-functions-in-google-bigquery-part-1-basics-745d97958fe2
https://cloud.google.com/bigquery/docs/reference/standard-sql/analytic-function-concepts

Return second from the last oracle sql

SELECT * FROM
(
SELECT DISTINCT(TRUNC(receipt_dstamp))
FROM inventory
WHERE substr(location_id,1,3) = 'GI-'
ORDER BY 1 ASC
)
WHERE ROWNUM <= 5
Output:
Hi all, i've got this subeqery and in this case my oldest date is in row 1, i want to retrive only second from the last(from the top in this case) which is gonna be 01-SEP-21.
I was trying to play with ROWNUM and OVER but without any results, im getting blank output.
Thank you.
Full query:
SELECT TRUNC(receipt_dstamp) as old_putaway_date, COUNT(tag_id) as tag_old_putaway
FROM inventory
WHERE substr(location_id,1,3) = 'GI-'
AND TRUNC(receipt_dstamp) IN (
SELECT * FROM
(
SELECT DISTINCT(TRUNC(receipt_dstamp))
FROM inventory
WHERE substr(location_id,1,3) = 'GI-'
ORDER BY 1 ASC
)
WHERE ROWNUM = 1
)
GROUP BY TRUNC(receipt_dstamp);
You should be able to simplify the entire query to:
SELECT old_putaway_date,
COUNT(tag_id) as tag_old_putaway
FROM (
SELECT TRUNC(receipt_dstamp) as old_putaway_date,
tag_id,
DENSE_RANK() OVER (ORDER BY TRUNC(receipt_dstamp)) AS rnk
FROM inventory
WHERE substr(location_id,1,3) = 'GI-'
)
WHERE rnk = 3
GROUP BY
old_putaway_date;
You can use dense_rank() :
SELECT * FROM (
SELECT L.*,DENSE_RANK()
OVER (PARTITION BY L.TAG_OLD_PUTAWAY ORDER BY L.OLD_PUTAWAY_DATE DESC) RNK
FROM
(
SELECT TRUNC(receipt_dstamp) as old_putaway_date, COUNT(tag_id) as tag_old_putaway
FROM inventory
WHERE substr(location_id,1,3) = 'GI-'
AND TRUNC(receipt_dstamp) IN (
SELECT * FROM
(
SELECT DISTINCT(TRUNC(receipt_dstamp))
FROM inventory
WHERE substr(location_id,1,3) = 'GI-'
ORDER BY 1 ASC
)
WHERE ROWNUM = 1
)
GROUP BY TRUNC(receipt_dstamp)
) L
) WHERE RNK = 2
You are using an old Oracle syntax that is not standard compliant in the regard that it relies on a subquery result order. (Sub)query results are unordered data sets by definition, but Oracle lets this pass in order to make their ROWNUM work with it.
Oracle now supports the standard SQL FETCH clause, which you should use instead.
SELECT DISTINCT TRUNC(receipt_dstamp) AS receipt_date
FROM inventory
WHERE SUBSTR(location_id, 1, 3) = 'GI-'
ORDER BY receipt_date
OFFSET 2 ROWS
FETCH NEXT 1 ROW ONLY;
https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/SELECT.html#GUID-CFA006CA-6FF1-4972-821E-6996142A51C6

Select every second record then determine earliest date

I have table that looks like the following
I have to select every second record per PatientID that would give the following result (my last query returns this result)
I then have to select the record with the oldest date which would be the following (this is the end result I want)
What I have done so far: I have a CTE that gets all the data I need
WITH cte
AS
(
SELECT visit.PatientTreatmentVisitID, mat.PatientMatchID,pat.PatientID,visit.RegimenDate AS VisitDate,
ROW_NUMBER() OVER(PARTITION BY mat.PatientMatchID, pat.PatientID ORDER BY visit.VisitDate ASC) AS RowNumber
FROM tblPatient pat INNER JOIN tblPatientMatch mat ON mat.PatientID = pat.PatientID
LEFT JOIN tblPatientTreatmentVisit visit ON visit.PatientID = pat.PatientID
)
I then write a query against the CTE but so far I can only return the second row for each patientID
SELECT *
FROM
(
SELECT PatientTreatmentVisitID,PatientMatchID,PatientID, VisitDate, RowNumber FROM cte
) as X
WHERE RowNumber = 2
How do I return the record with the oldest date only? Is there perhaps a MIN() function that I could be including somewhere?
If I follow you correctly, you can just order your existing resultset and retain the top row only.
In standard SQL, you would write this using a FETCH clause:
SELECT *
FROM (
SELECT
visit.PatientTreatmentVisitID,
mat.PatientMatchID,
pat.PatientID,
visit.RegimenDate AS VisitDate,
ROW_NUMBER() OVER(PARTITION BY mat.PatientMatchID, pat.PatientID ORDER BY visit.VisitDate ASC) AS rn
FROM tblPatient pat
INNER JOIN tblPatientMatch mat ON mat.PatientID = pat.PatientID
LEFT JOIN tblPatientTreatmentVisit visit ON visit.PatientID = pat.PatientID
) t
WHERE rn = 2
ORDER BY VisitDate
OFFSET 0 ROWS FETCH FIRST 1 ROW ONLY
This syntax is supported in Postgres, Oracle, SQL Server (and possibly other databases).
If you need to get oldest date from all selected dates (every second row for each patient ID) then you can try window function Min:
SELECT * FROM
(
SELECT *, MIN(VisitDate) OVER (Order By VisitDate) MinDate
FROM
(
SELECT PatientTreatmentVisitID,PatientMatchID,PatientID, VisitDate,
RowNumber FROM cte
) as X
WHERE RowNumber = 2
) Y
WHERE VisitDate=MinDate
Or you can use SELECT TOP statement. The SELECT TOP clause allows you to limit the number of rows returned in a query result set:
SELECT TOP 1 PatientTreatmentVisitID,PatientMatchID,PatientID, VisitDate FROM
(
SELECT *
FROM
(
SELECT PatientTreatmentVisitID,PatientMatchID,PatientID, VisitDate,
RowNumber FROM cte
) as X
WHERE RowNumber = 2
) Y
ORDER BY VisitDate
For simplicity add order desc on date column and use TOP to get the first row only
SELECT TOP 1 *
FROM
(
SELECT PatientTreatmentVisitID,PatientMatchID,PatientID, VisitDate, RowNumber FROM cte
) as X
WHERE RowNumber = 2
order by VisitDate desc

Need to change LIMIT into something else

Is there a way to change "LIMIT 1" and get the same output? I have to get client's name, surname and a quantity of books that has the most books
SELECT stud.skaitytojas.name, stud.skaitytojas.surname,
COUNT (stud.skaitytojas.nr) AS quantity
FROM stud.egzempliorius , stud.skaitytojas
WHERE stud.egzempliorius.client = stud.skaitytojas.nr
GROUP BY stud.skaitytojas.nr
ORDER BY quantity DESC
LIMIT 1
Postgres supports the ANSI standard FETCH FIRST 1 ROW ONLY, so you can do:
SELECT s.name, s.surname, COUNT(s.nr) AS quantity
FROM stud.egzempliorius e JOIN
stud.skaitytojas s
ON e.client = s.nr
GROUP BY s.name, s.surname
ORDER BY quantity DESC
FETCH FIRST 1 ROW ONLY;
Also notice the use of table aliases and proper JOIN syntax. I also prefer to list the columns in the SELECT in the GROUP BY, although that is optional if s.nr is unique.
You can select the row with the highest quantity using row_number()
SELECT * FROM (
SELECT * , row_number() over (order by quantity desc) rn FROM (
SELECT stud.skaitytojas.name, stud.skaitytojas.surname, COUNT (stud.skaitytojas.nr) AS quantity
FROM stud.egzempliorius , stud.skaitytojas
WHERE stud.egzempliorius.client = stud.skaitytojas.nr
GROUP BY stud.skaitytojas.name, stud.skaitytojas.surname
) t
) t where rn = 1
If you want to include ties for the highest quantity, then use rank() instead.

Total Row Count in sql query---sql server 2008

My query is as follows
BEGIN
WITH MyCTE
AS (
SELECT T.MusicAlbumTitle
,D.musicTitle
,D.mVideoID
,D.musicFileName
,T.ReleaseDate AS ReleasedDate
,D.MusicLength
,D.musicSinger
,D.MusicVideoID
,D.ExternalLink
,D.CoverImg
,ROW_NUMBER() OVER (
PARTITION BY D.MusicVideoID ORDER BY D.mVideoID
) AS row_num
FROM dbo.Music_Video T
JOIN dbo.Music_Video_Details D ON T.MusicVideoID = D.MusicVideoID
WHERE T.PortalID = #PortalID
AND T.CultureCode = #CultureCode
AND T.ComingSoon <> 1
GROUP BY T.MusicAlbumTitle
,D.musicTitle
,D.mVideoID
,T.ReleaseDate
,D.musicFileName
,D.MusicLength
,D.musicSinger
,D.MusicVideoID
,D.ExternalLink
,D.CoverImg
)
SELECT a.mVideoID
,a.MusicVideoID
,a.musicFileName
,a.MusicAlbumTitle
,a.ReleasedDate
,a.row_num
,a.CoverImg
,a.ExternalLink
,a.musicTitle
,a.MusicLength
FROM MyCTE a
WHERE row_num = 1
ORDER BY MusicVideoID DESC
END
I need to achieve total row count from last select statement.
which mean total row count that is being selected.
or any idea that might be use in this condition
How can i do this ..
Please add COUNT(*) OVER() in your select, which returns total rows selected as a new column.
Ex:
SELECT
*,
COUNT(*) OVER() AS [Total_Rows]
FROM YourTable
Just to be clear, you need to add the count to the CTE, not the outer query. The outer select is returning only one row, so the count would always be one.
The CTE should start:
WITH MyCTE
AS (
SELECT T.MusicAlbumTitle
,D.musicTitle
,D.mVideoID
,D.musicFileName
,T.ReleaseDate AS ReleasedDate
,D.MusicLength
,D.musicSinger
,D.MusicVideoID
,D.ExternalLink
,D.CoverImg
,ROW_NUMBER() OVER (
PARTITION BY D.MusicVideoID ORDER BY D.mVideoID
) AS row_num,
COUNT(*) over () as total_count