materialized view using WITH statement - sql

i created a materialized view but i have a mistake i do not understand to resolve it
RA-00937: not a single-group group function
00937. 00000 - "not a single-group group function
on line
SELECT x.*,SUM(x.quantities) as Tquantities
can you help me to resolve it
CREATE MATERIALIZED VIEW TestView AS
With x AS(
SELECT Numclient as CLIENT,
Numcommand as COMMAND,
count(gender) as quantities
FROM customer,
Command
WHERE Numclient = Numcommand
AND gender =2
GROUP BY Numclient,
Numcommand
),
x1 AS (
SELECT x.*,SUM(x.quantities) as Tquantities
FROM x
)
SELECT x.*,ROUND(x.quantities*100/x1.Tquantities) as Percent
FROM x1, x;

In order to eliminate error remove x.*, in your original subquery x1.
Your select statement can be simplified, like here:
select Numclient CLIENT, Numcommand COMMAND, count(gender) quantities,
round(100*count(gender)/sum(count(gender)) over()) percent
from customer
join Command on Numclient = Numcommand and gender = 2
group by Numclient, Numcommand
SQLFiddle
It's little unclear why are you displaying column COMMAND, when it's equal to CLIENT?
I suspect that maybe this is mistake in where condition or this column is superfluous.

Since when is this valid in Oracle? This is not MySQL.
SELECT x.*,SUM(x.quantities) as Tquantities FROM x
In order to this to work, you have to GROUP BY the columns in x.

Related

How to run a subquery in hive

I have this query that I am trying to run in HIVE:
select transaction_date, count(total_distinct) from (
SELECT transaction_date, concat(subid,'**', itemid) as total_distinct
FROM TBL_1
group by transaction_date, subid,itemid
) group by transaction_date
What I am trying to do it get the distinct combination of subid and itemid, but I need the total count per day. When I run the query above, I get this error:
Error while compiling statement: FAILED: ParseException line 6:2 cannot recognize input near 'group' 'by' 'TRANSACTION_DATE' in subquery source
The query looks correct to me though. Has anyone encountered this error?
Hive requires subqueries to be aliased, so you need to specify a name for it:
select transaction_date, count(total_distinct) from (
SELECT transaction_date, concat(subid,'**', itemid) as total_distinct
FROM TBL_1
group by transaction_date, subid,itemid
) dummy -- << note here
group by transaction_date
True, the error message is far from helpful.

How to PARTITION BY to show the same value for all rows?

I have a listing of all consumer purchases where some consumers make many purchases over the time frame in scope. I'd like to populate a column with the location of each consumer's first purchase but I'm getting this error:
Error in SQL statement: ParseException:
mismatched input '(' expecting <EOF>(line 2, pos 25)
== SQL ==
SELECT consumer_id
,location OVER(partition BY table.consumer_id) AS first_purchase_site
---------------------^^^
FROM table
For clarity, here is my query:
SELECT consumer_id
,location OVER(partition BY table.consumer_id) AS first_purchase_site
FROM table
WHERE consumer_purchase_order_sequence = 1
I'd like to populate a column with the location of each consumer's first purchase
Are you looking for first_value()?
SELECT consumer_id,
FIRST_VALUE(location) OVER (partition BY table.consumer_id) AS first_purchase_site
FROM table;
Your window function is, errr, missing the function.
You need window function FIRST_VALUE():
SELECT DISTINCT consumer_id,
FIRST_VALUE(location) OVER(PARTITION BY consumer_id ORDER BY consumer_purchase_order_sequence) AS first_purchase_site
FROM table
Change consumer_purchase_order_sequence with the column that orders the purchases.
Its hard to do it with window calculations .You can do it with joins,
SELECT
table.consumer_id,
table.location,
a.first_purchase_site
FROM table LEFT JOIN
(SELECT consumer_id,location AS first_purchase_site FROM table WHERE
consumer_purchase_order_sequence = 1) a ON a.consumer_id=table.consumer_id

Can a HIVE SELECT combine GROUP BY and ORDER BY?

I'm doing some relatively simple queries in Hive and cannot seem to combine GROUP BY and ORDER BY in a single statement. I have no problem doing a select into a temporary table of the GROUP BY query and then doing a select on that table with an ORDER BY, but I can't combine them together.
For example, I have a table a and can execute this query:
SELECT place,count(*),sum(weight) from a group by place;
And I can execute this query:
create temporary table result (place string,count int,sumweight int);
insert overwrite table result
select place,count(*),sum(weight) from a group by place;
select * from result order by place;
But if I try this query:
SELECT place,count(*),sum(weight) from a group by place order by place;
I get this error:
Error: Error while compiling statement: FAILED: ParseException line 1:45 mismatched input '' expecting \' near '_c0' in character string literal (state=42000,code=40000)
Try using group by as a sub-query and order by as an outer query as show below:
SELECT
place,
cnt,
sum_
FROM (
SELECT
place,
count(*) as cnt,
sum(weight) as sum_
FROM a
GROUP BY place
) a
ORDER BY place;
use sort by like this:
SELECT place,count(*),sum(weight) from a group by place sort by place;

PostgreSQL: column does not exist when searched in WHERE clause

So, I have this little SQL query:
SELECT
COUNT( distinct (customerid)) AS cs, prod_id
FROM
(orderlines JOIN orders ON (orderlines.orderid=orders.orderid)) AS table_1
WHERE table_1.cs= 1
GROUP BY table_1.prod_id
ORDER BY cs ASC
What this is supposed to do, is count the distinct customerid's and return a table containing only the entries where there was only distinct customerid.
When I execute this I get the following error:
ERROR: column table_1.cs does not exist
LINE 6: WHERE table_1.cs= 1
^
*********Error**********
ERROR: column table_1.cs does not exist
SQL state: 42703
Character: 156
It claims that the column cs does not exist when I have clearly defined it here:
SELECT
COUNT( distinct (customerid)) AS cs, prod_id
You can't reference in a WHERE clause an alias defined in the Selection list. You can either use an HAVING clause or use a sub select ..
With a sub select you can arrange something like this :
SELECT * FROM (
SELECT
COUNT( distinct (customerid)) AS cs, prod_id
FROM
(orderlines JOIN orders ON (orderlines.orderid=orders.orderid))
GROUP BY table_1.prod_id) AS table_1
WHERE cs= 1
ORDER BY cs ASC

How to combine select max and count?

I've got this:
select ordernr
from users
having count(ordernr) =
( select max(count(ordernr))
from users where ordernr = ordernr
group by ordernr )
group by ordernr
to get the most used order-number (ordernr) from all users.
How to get it into ABAP SAP System? I've tried this:
select SINGLE ordernr
from ZDEVXXX_PROJECT3 INTO ordernrU
having count( * ) =
( select max( count( * ) )
from ZDEVXXX_PROJECT3
where ordernr = ordernr
group by ordernr )
But I get this error:
"Unknown columnname COUNT("
How to combine max and count in ABAP? The SQL Query above is working in Oracle for me.
Thanks!
You need to have COUNT(*) in the result set if you want to use it in the HAVING clause. See http://help.sap.com/abapdocu_751/en/ABENWHERE_LOGEXP_ALL_ANY_SOME.htm for an example.
Since release 6.1 you can use aggregates in HAVING clause.
But your answer is "No way". Aggregates must be only in form aggr( {[distinct] column | *} )
So you must to
select count( * )
into table itab
from ZDEVXXX_PROJECT3
where ordernr = ordernr
group by ordernr
Then to find maximum of counts programmaticaly. And only then to use it in HAVING condition.
Or you can use ABAP Open SQL. It gives you the access to SQL of your particular DB and you can execute your mentioned above query.