Subquery: Help correcting syntax errors - subquery

I am recieving the following 2 errors when I try to run the below subquery:
Msg 156, Level 15, State 1, Line 11 - Incorrect syntax near the keyword 'SELECT'.
Msg 102, Level 15, State 1, Line 13 - Incorrect syntax near ')'.
SELECT
RIGHT(A.Name,LEN(Name)-5) AS ASSOCIATE
,B.WeekEnding
,COUNT(B.ID)AS Policies
,SUM (B.Amount) AS Premium
FROM
dbo.Users A
INNER JOIN (SELECT*,
SELECT DATEADD(DAY,(12-DATEPART(WEEKDAY,CreateDate)%7)-6,CreateDate)
WeekEnding
FROM dbo.Policy) B
ON A.User = B.Agent
WHERE
B.CreateDate >= CONVERT (DATE,GETDATE()))
GROUP BY A.Name, B.WeekEnding
ORDER BY Policies DESC, Premium DESC

You have an extra SELECT in the subquery. It should be:
(SELECT *,
DATEADD(DAY,(12-DATEPART(WEEKDAY,CreateDate)%7)-6,CreateDate) WeekEnding
FROM dbo.Policy)
and you have an extra parenthesis here:
WHERE
B.CreateDate >= CONVERT (DATE,GETDATE()))
^

Related

Node js,Sequelize findAndCountAll with offset and limit doesn't work when contains "include" and "where: array[]" options

I'm trying to fetch paginated messages from a database given the ids of different chats. It works if I do not provide limit and offset, but when I provide the limit and offset parameters, it stops working. I use mariadb sql.
Message.findAndCountAll({
where: {chat_id: ids},//ids=> array of ints
offset: limit * page,
limit: limit,
include: {
model: UnreadMessage, as: 'unreadMessages',
where: {participant_id: userId}
}
},
)
The error I see is this
"(conn=12896, no: 1064, SQLState: 42000) You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''20') AS `messages` INNER JOIN `unread_message` AS `unreadMessages` ON `messa...' at line 1\nsql: SELECT `messages`.*, `unreadMessages`.`message_id` AS `unreadMessages.message_id`, `unreadMessages`.`participant_id` AS `unreadMessages.participant_id` FROM (SELECT `messages`.`id`, `messages`.`chat_id`, `messages`.`content`, `messages`.`sender_id`, `messages`.`created_at` FROM `messages` AS `messages` WHERE `messages`.`chat_id` IN (3, 5) AND ( SELECT `message_id` FROM `unread_message` AS `unreadMessages` WHERE (`unreadMessages`.`participant_id` = 10 AND `unreadMessages`.`message_id` = `messages`.`id`) LIMIT 1 ) IS NOT NULL LIMIT 0, '20') AS `messages` INNER JOIN `unread_message` AS `unreadMessages` ON `messages`.`id` = `unreadMessages`.`message_id` AND `unreadMessages`.`participant_id` = 10; - parameters:[]"
My speculation was right all along when I first saw it. The error says it all.
...right syntax to use near ''20') AS `mess....
limit is string. Cast it using +limit.
If I'm right, you're passing it from the request directly without casting it to integer.

Invalid Column In SQL Query

select
university_cars_video_kroenke.dbo.car_customer.cus_first,
university_cars_video_kroenke.dbo.car_customer.cus_last,
(
select COUNT(university_cars_video_kroenke.dbo.car_customer.cus_id)
from university_cars_video_kroenke.dbo.car_purchases
where university_cars_video_kroenke.dbo.car_customer.cus_id = university_cars_video_kroenke.dbo.car_purchases.cus_id
)
from university_cars_video_kroenke.dbo.car_customer
(edited for clarity)
select
customer.cus_first,
customer.cus_last,
(select
COUNT(customer.cus_id)
from purchases
where customer.cus_id = purchases.cus_id )
from customer
My error message is
Msg 8120, Level 16, State 1, Line 4 Column
'university_cars_video_kroenke.dbo.car_customer.cus_first'
is invalid in the select list because it is not contained
in either an aggregate function or the GROUP BY clause
I just want a count of records the cus_id is the same in both tables.
I just want a count of records the cus_id is the same in both tables.
Something like the following should work.
SELECT
A.cus_id,
count(A.cus_id)
FROM
university_cars_video_kroenke.dbo.car_customer AS A,
university_cars_video_kroenke.dbo.car_purchases AS B
WHERE
A.cus_id = B.cus_id

Oracle INSTR equivalent in Spark SQL

I tried to replicate the oracle Instr function, but it seems to me that there are not all the arguments that exist in Oracle. I receive this error and I would like to include this transformation in a "plataforma" field in the table but I can't:
SELECT
SUBSTR(a.SOURCE, 0, INSTR(a.SOURCE, '-', 1, 2) - 1) AS plataforma,
COUNT(*) AS qtd
FROM db1.table AS as a
LEFT JOIN db1.table2 AS b ON a.ID=b.id
GROUP BY SUBSTR(a.SOURCE, 0, INSTR(a.SOURCE, '-', 1, 2) - 1)
ORDER BY qtd
The Apache Spark 2.0 database encountered an error while running this
query.
Error running query: org.apache.spark.sql.AnalysisException: Invalid number of arguments for function instr. Expected: 2; Found: 4;
line 8 pos 45
I made the transformation of the field that way but I don't know if it is the correct one:
How can I replicate the same Oracle function in Spark? I need to do just this:
Source:
apache-spark-sql
sql-server-dw
Result:
apache-spark
sql-server
What you're looking for is substring_index function :
substring_index('apache-spark-sql', '-', 2)
It returns the substring before 2 occurrences of -.
I suppose you want to get the substring before the last occurrence of -. So you can count the number of - in the input string and combine it with substring_index function like this:
substring_index(col, '-', size(split(col, '-')) - 1)
Where size(split(col, '-')) - 1 gives the number of occurences of -.

Spotfire : Syntax issue or Window functions not allowed?

I am using Data functions in Spotfire.
I have the sqldf package installed.
Here is the query:
#Package to run sqls
library(sqldf)
#Input data frame
op1 <- sqldf("SELECT Prod_parnt,prodct_grop,year,month,week,
count(distinct id) as prd_cnt,
Sum(Count(distinct id))
over (partition by modlty,prodct_grop order by year,month,week
rows between 12 preceding and current row) as cumu_prd_cnt,
avg(rate) as sal_rate
FROM ip1
group by Prod_parnt,prodct_grop,year,month,week")
The error I am facing:
"TIBCO Spotfire Statistics Services returned an error: 'Error: error in statement: near "(": syntax error'."
Now the point to note here is that when I remove the Window function statement i.e cumu_prd_cnt field; the code works fine.
Need your help.

Count null columns as zeros with oracle

I am running a query with Oracle:
SELECT
c.customer_number,
COUNT(DISTINCT o.ORDER_NUMBER),
COUNT(DISTINCT q.QUOTE_NUMBER)
FROM
Customer c
JOIN Orders o on c.customer_number = o.party_number
JOIN Quote q on c.customer_number = q.account_number
GROUP BY
c.customer_number
This works beautifully and I can get the customer and their order and quote counts.
However, not all customers have orders or quotes but I still want their data. When I use LEFT JOIN I get this error from Oracle:
ORA-24347: Warning of a NULL column in an aggregate function
Seemingly this error is caused by the eventual COUNT(NULL) for customers that are missing orders and/or quotes.
How can I get a COUNT of null values to come out to 0 in this query?
I can do COUNT(DISTINCT NVL(o.ORDER_NUMBER, 0)) but then the counts will come out to 1 if orders/quotes are missing which is no good. Using NVL(o.ORDER_NUMBER, NULL) has the same problem.
Try using inline views:
SELECT
c.customer_number,
o.order_count,
q.quote_count
FROM
customer c,
( SELECT
party_number,
COUNT(DISTINCT order_number) AS order_count
FROM
orders
GROUP BY
party_number
) o,
( SELECT
account_number,
COUNT(DISTINCT quote_number) AS quote_count
FROM
quote
GROUP BY
account_number
) q
WHERE 1=1
AND c.customer_number = o.party_number (+)
AND c.customer_number = q.account_number (+)
;
Sorry, but I'm not working with any databases right now to test this, or to test whatever the ANSI SQL version might be. Just going on memory.

Resources