SQL Query - error #1064 'having count(*) > 1 LIMIT 0, 30' - sql

This is the query I'm trying create using SQL on phpMyAdmin:
SELECT DISTINCT Client_Order.orderID, Client.Title, Client.First_Name, Set_Product.setName, Client_Order.orderDATE, count( * )
FROM Client_Order
INNER JOIN Set_Product ON Set_Product.setID = Set_Product.setID
INNER JOIN Client ON Client.clientID = Client.clientID
GROUP BY Client_Order.orderID
ORDER BY Client.First_Name ASC
having count(*) > 1
But I'm getting an error saying:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'having count(*) > 1 LIMIT 0, 30' at line 7
How can i display my query because without the count I am just getting multiple duplicates.

Related

How to write DELETE Statement with Inner Query in PostgreSQL?

The below query works fine in Oracle DB.
The same query fails in Postgres DB, but the inner query works fine in Postgres DB.
DELETE FROM (
SELECT ROW_NUMBER() OVER (PARTITION BY A.RESORT, A.RESV_NAME_ID ORDER BY ACTION_ID DESC) RNK
FROM STAGE_DETAILS A ) B
WHERE B.RNK>1;
I am getting syntax error for this.
Error :-
[Code: 0, SQL State: 42601] ERROR: syntax error at or near "(" Position: 13 [Script position: 3109 - 3110]
How to fix this ?
Thanks.
use following syntax
DELETE FROM STAGE_DETAILS A
USING (SELECT ACTION_ID,RESV_NAME_ID,ROW_NUMBER() OVER (PARTITION BY RESORT, RESV_NAME_ID ORDER BY ACTION_ID DESC) RNK
FROM STAGE_DETAILS) B
WHERE B.RNK>1 AND B.ACTION_ID = A.ACTION_ID AND B.RESV_NAME_ID = A.RESV_NAME_ID;
You cannot delete from a subquery in Postgres. This should do what you want:
DELETE FROM STAGE_DETAILS SD
WHERE ACTION_ID < (SELECT MAX(SD2.ACTION_ID)
FROM STAGE_DETAILS SD2
WHERE SD2.RESORT = SD.RESORT AND
SD2.RESV_NAME_ID = SD.RESV_NAME_ID
);

ERROR: syntax error at or near "from" when using count and finding percentage

with result as
(
select ((count(case when fsr.position <= 3 and fse.has_clicked = 'yes' THEN search_id END)
/
count(*) from fb_search_results)::float) * 100
from fb_search_results fsr JOIN fb_search_events fse on fsr.result_id = fse.search_id
)select * from result
ERROR: syntax error at or near "from"
I have no idea why the error is coming, can someone help?

not able to do a UNION in Oracle SQL

i want to do a union of a query but getting error.
SELECT
NB.NETBANKID,
NB.BANKNAME,
NBMAP.SORTORDER,
NB.BANKCODE
FROM VTSMNETBNK NB
INNER JOIN CGCMN.VTMESTRNETBNKMAP NBMAP
ON NBMAP.NETBANKID =NB.NETBANKID
WHERE NBMAP.STOREID=133 AND
NBMAP.EFFDATE <= SYSDATE
AND NBMAP.STATUS ='A'
AND NB.STATUS = 'A'
ORDER BY NBMAP.SORTORDER
FETCH NEXT 6 ROWS ONLY
ORA-00933: SQL command not properly ended
00933. 00000 - "SQL command not properly ended"
*Cause:
*Action:
Error at Line: 15 Column: 1
The FETCH clause is only available as of Oracle 12c. In Oracle 11g you can work with ROW_NUMBER instead:
select netbankid, bankname, sortorder, bankcode
from
(
select
nb.netbankid,
nb.bankname,
nbmap.sortorder,
nb.bankcode,
row_number() over (order by nbmap.sortorder) as rn
from vtsmnetbnk nb
inner join cgcmn.vtmestrnetbnkmap nbmap on nbmap.netbankid = nb.netbankid
where nbmap.storeid = 133
and nbmap.effdate <= sysdate
and nbmap.status = 'A'
and nb.status = 'A'
)
where rn <= 6
order by rn;

Hive - select rows within 1 year of earliest date

I am trying to select all rows in a table that are within 1 year of the earliest date in the table. I'm using the following code:
select *
from baskets a
where activitydate < (select date_add((select min(activitydate) mindate_a from baskets), 365) date_b from baskets)
limit 10;
but get the following error message:
Error while compiling statement: FAILED: ParseException line 1:55 cannot recognize input near 'select' 'date_add' '(' in expression specification
Total execution time: 00:00:00.338
Any suggestions?
EDIT:
With this code:
select *
from baskets a
where activitydate < (select date_add(min(activitydate), 365) from baskets)
limit 10;
I'm getting this error:
Error while compiling statement: FAILED: ParseException line 1:55 cannot recognize input near 'select' 'date_add' '(' in expression specification
I'd be tempted to use window functions:
select b.*
from (select b.*, min(activity_date) as min_ad
from baskets b
) b
where activity_date < add_months(min_ad, 12);
If you really want your syntax to work, try reducing the number of selects:
where activitydate < (select date_add(min(activitydate), 365) from baskets)
Use JOINs instead of select in Sub-query. I don't think Hive supports select in where clause with < condition. Only IN and EXISTS could be used as of Hive 0.13.
: Language Manual SubQueries
SELECT a.*
FROM baskets a
JOIN (SELECT DATE_ADD(MIN(b.activitydate), 365) maxdate
FROM baskets) b
ON a.activitydate < b.maxdate
LIMIT 10;

Why does this SQL query that works in MS Access not work in SQL Server?

The following query works in MS Access, but it does not work in MS SQL Server:
SELECT
tblSession.PatientID as PID,
max(tblSession.SessionAttend) -
min(tblSession.SessionAttend) + 1 as NumSA,
max(tblSession.SessionSched) -
min(tblSession.SessionSched) + 1 as NumSS FROM
(
SELECT top 100 percent
tblSession.PatientID,
tblSession.SessionNumber,
tblSession.SessionDate,
tblSession.SessionAttend,
tblSession.SessionSched FROM
tblPatient INNER JOIN tblSession ON
tblPatient.PatientID = tblSession.PatientID) WHERE
(tblSession.SessionDate >= '12/8/2010') AND
(tblSession.SessionDate <= '5/18/2011') AND
(tblSession.Status = '2') ORDER BY
tblSession.PatientID, tblSession.SessionNumber
) GROUP BY tblSession.PatientID
In SQL Server, it gives the error "Incorrect syntax near the keyword 'GROUP'." When I hover over the GROUP keyword, the tooltip displays "Incorrect syntax near 'GROUP'. Expecting AS, ID, or QUOTED_ID." I don't understand. Can anyone tell me how to make this query work?
The derived table in () needs an alias, and its column references in the SELECT list updated accordingly:
SELECT top 100 percent
ALIASNAME.PatientID as PID,
max(ALIASNAME.SessionAttend) -
min(ALIASNAME.SessionAttend) + 1 as NumSA,
max(ALIASNAME.SessionSched) -
min(ALIASNAME.SessionSched) + 1 as NumSS FROM
(
SELECT top 100 percent
tblSession.PatientID,
...
...
...
...
tblSession.PatientID, tblSession.SessionNumber
-- This derived table needs an alias
) ALIASNAME
GROUP BY ALIASNAME.PatientID