mysql views - view

I am trying to create a view for a UNION of 2 select statements that I have created.
The UNION is working fine when executed individually
But the problem is only the 1st part of the UNION is getting executed when I am executing it as a view.
The query I am using is as below
SELECT DISTINCT
products.pid AS id,
products.pname AS name,
products.p_desc AS description,
products.p_uid AS userid,
products.p_loc AS location,
products.isaproduct AS whatisit
FROM products
UNION
SELECT DISTINCT
services.s_id AS id,
services.s_name AS name,
services.s_desc AS description,
services.s_uid AS userid,
services.s_location AS location,
services.isaservice AS whatisit
FROM services
The above works fine when i execute it separately. But when I use it as a view, it does not give me the results of the services part.
Could someone please help me with this?

Try this
SELECT DISTINCT
products.pid AS id,
products.pname AS name,
products.p_desc AS description,
products.p_uid AS userid,
products.p_loc AS location,
products.isaproduct AS whatisit
FROM products
UNION ALL
SELECT DISTINCT
services.s_id AS id,
services.s_name AS name,
services.s_desc AS description,
services.s_uid AS userid,
services.s_location AS location,
services.isaservice AS whatisit
FROM services
I'm not sure, I tried that with a table on my server and it worked.. I guess it will work for you. Hint the only difference here is that I used "union all" instead of just "union"

Related

Golang gorm union function for orm approach

I am trying to union two results with each other the sql-query I could use for would look like this:
SELECT id, created_at, updated_at, deleted_at, user_id, friend_id
FROM public.friendlists where user_id = $1
union all
SELECT id, created_at, updated_at, deleted_at, user_id, friend_id
FROM public.friendlists where friend_id = $1
I know I could just use the sql statement or just join the two results. I just want to find out a way because im curious how to handle this and to avoid hardcoded sql statements.
The following code is what I have so far:
db.Where(&models.Friendlist{UserId: userID}).
Or(&models.Friendlist{FriendId: userID}).
Order("created_at desc").
Find(&result)
How I could I do this with an ORM approach in gorm? Or is it even possible?
i don't think union is implemented in gorm.
but you can execute raw query with union and marshal result into structure required via this method - https://gorm.io/docs/sql_builder.html#Raw-SQL

Oracle select rows from a query which are not exist in another query

Let me explain the question.
I have two tables, which have 3 columns with same data tpyes. The 3 columns create a key/ID if you like, but the name of the columns are different in the tables.
Now I am creating queries with these 3 columns for both tables. I've managed to independently get these results
For example:
SELECT ID, FirstColumn, sum(SecondColumn)
FROM (SELECT ABC||DEF||GHI AS ID, FirstTable.*
FROM FirstTable
WHERE ThirdColumn = *1st condition*)
GROUP BY ID, FirstColumn
;
SELECT ID, SomeColumn, sum(AnotherColumn)
FROM (SELECT JKM||OPQ||RST AS ID, SecondTable.*
FROM SecondTable
WHERE AlsoSomeColumn = *2nd condition*)
GROUP BY ID, SomeColumn
;
So I make a very similar queries for two different tables. I know the results have a certain number of same rows with the ID attribute, the one I've just created in the queries. I need to check which rows in the result are not in the other query's result and vice versa.
Do I have to make temporary tables or views from the queries? Maybe join the two tables in a specific way and only run one query on them?
As a beginner I don't have any experience how to use results as an input for the next query. I'm interested what is the cleanest, most elegant way to do this.
No, you most probably don't need any "temporary" tables. WITH factoring clause would help.
Here's an example:
with
first_query as
(select id, first_column, ...
from (select ABC||DEF||GHI as id, ...)
),
second_query as
(select id, some_column, ...
from (select JKM||OPQ||RST as id, ...)
)
select id from first_query
minus
select id from second_query;
For another result you'd just switch the tables, e.g.
with ... <the same as above>
select id from second_query
minus
select id from first_query

Impala - Getting error with Multiple count of distinct values

I am using CDH-5.4.4 Cloudera Edition, I have a CSV file in HDFS location, My requirement is to perform Real time SQL queries on Hadoop Environement (OLTP).
So I decided to go with Impala, I have created MetaStore table to a CSV file, then execuing query in impala editor (within HUE application) .
When i am executing below query, i am getting error like
"AnalysisException: all DISTINCT aggregate functions need to have the
same set of parameters as count(DISTINCT City); deviating function:
count(DISTINCT Country)".
CSV File
OrderID,CustomerID,City,Country
Ord01,Cust01,Aachen,Germany
Ord02,Cust01,Albuquerque,USA
Ord03,Cust01,Aachen,Germany
Ord04,Cust02,Arhus,Denmark
Ord05,Cust02,Arhus,Denmark
Problamatic Query
Select CustomerID,Count(Distinct City),Count(Distinct Country) From CustomerOrders Group by CustomerID
Problem:
Unable to execute the Impala Query with More than one Distinct Values in an Query.. I have searched over internet they provide NDV() method as a workaround, But NDV method only returns approximate count of distinct values, I need Exact unique count for more than one fields.
Expectation:
What is the best way to do Exact unique count for more than one fields? Kindly modify the above query to work with Impala.
Note: This is not my original table, I have replicate for the forum question.
I've the same problem in Impala. Here is my workaround:
SELECT CustomerID
,sum(nr_of_cities)
,sum(nr_of_countries)
FROM (
SELECT CustomerID
,Count(DISTINCT City) AS nr_of_cities
,0 AS nr_of_countries
FROM CustomerOrders
GROUP BY CustomerID
UNION ALL
SELECT CustomerID
,0 AS nr_of_cities
,Count(DISTINCT Country) AS nr_of_countries
FROM CustomerOrders
GROUP BY CustomerID
) AS aa
GROUP BY CustomerID
I think this can be done cleaner (untested):
WITH
countries AS
(
SELECT CustomerID
,COUNT(DISTINCT City) AS nr_of_countries
FROM CustomerOrders
GROUP BY 1
)
,
cities AS
(
SELECT CustomerID
,COUNT(DISTINCT City) AS nr_of_cities
FROM CustomerOrders
GROUP BY 1
)
SELECT CustomerID
,nr_of_cities
,nr_of_countries
FROM cities INNER JOIN countries USING (CustomerID)

cascading Input Control sql query return error: "ORA-01427: single-row subquery returns more than one row"

looking for solution on my sql query error.I'm trying to create second cascading Input Control in JaspersoftServer. The first Input Control works fine, however when I try to create a second cascade IC it returns with the error. I have 3 tables (user, client, user_client), many to many, so 1 linked table (user_client) between them.The 1st Input Control (client) - works well, end user will select the client, the client can have many users, so cascade is the key. Also, as the output, I would like to get not the user_id, but user's firstname and the lastname as one column field. And here is where i'm stuck. I'm pretty sure it is simple syntaxis error, but spent a good couple of hours to figure out what is wrong with it. Is anyone can have a look at it please and indicate where is the problem in my query ?! So far I've done:
select distinct
u.user_id,(
SELECT CONCAT(first_name, surname) AS user_name from tbl_user ),
c.client_id
FROM tbl_user u
left join tbl_user_client uc
on uc.user_id = u.user_id
left join tbl_client c
on c.client_id = uc.client_id
where c.client_id = uc.client_id
order by c.client_id
Thank you in advance.
P.S. JasperServer + Oracle 11g
You're doing an uncorrelated subquery to get the first/last name from the user table. There is no relationship between that subquery:
SELECT CONCAT(first_name, surname) AS user_name from tbl_user
... and the user ID in the main query, so the subquery will attempt to return every first/last name for all users, for every row your joins find.
You don't need to do a subquery at all as you already have the tbl_user information available:
select u.user_id,
CONCAT(u.first_name, u.surname) AS user_name
c.client_id
FROM tbl_user u
left join tbl_user_client uc
on uc.user_id = u.user_id
left join tbl_client c
on c.client_id = uc.client_id
where c.client_id = uc.client_id
order by c.client_id
If you want to put a space between the first and last name you'll either need nested concat() calls, since that function only takes two arguments:
select u.user_id,
CONCAT(u.first_name, CONCAT(' ', u.surname)) AS user_name
...
... or perhaps more readably use the concatenation operator instead:
select u.user_id,
u.first_name ||' '|| u.surname AS user_name
...
If the first control has selected a client and this query is supposed to find the users related to that client, you're joining the tables the wrong way round, aren't you? And you aren't filtering on the selected client - but no idea how that's actually implemented in Jasper. Maybe you do want the entire list and will filter it on the Jasper side.

Not a Group By Expression, with own declared functions

Ok so I know when using aggregate functions such as MAX, MIN, AVG and so fort, in a select statement. You need to use the GROUP BY function for all the selected columns that DON'T use the aggregate functions.EX
SELECT name, MAX(age)
FROM person
GROUP BY name
but my issue is, when I use my own functions for certain columns and I use an Aggregate function within my select statement. EX
SELECT f_fullname(name, surname) as fullname, max(age)
FROM person
Should i add the whole function as a part of the group by clause?
GROUP BY f_fullname(name, surname)
because at this moment i get the ORA-00979 not a GROUP BY expression error.
Thanks for your help!
PS. the select statements are just for explanation purposes**
You can either have the whole function or the columns which are the parameters.
select f_fullname(name , surname) full_name, max (age)
from person
group by name, surname;
or
select f_fullname(name , surname) full_name, max (age)
from person
group by f_fullname(name , surname);
Here is a sqlfiddle demo

Resources