I'm using the below code to calculate tenure/customer age using datediff
select user_id, datediff('2014-09-30', to_date(date_joined)) as tenure_days
from users_table
group by user_id
Error
Error: Invalid column reference date_joined
Any help here would be highly appreciated!
There is no need for grouping here. Try bellow:
select distinct user_id, datediff('2014-09-30', to_date(date_joined)) as tenure_days
from users_table
Related
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
I needed to combine the two table product_price and trade_channels, when I use inner join,
the ID of product_price is remove.
Here is my code
DB::table('product_price')->where('product_id',$id)->where('action','customer_price')
->join('customers','product_price.action_id','=','customers.id')
->get();
A good practise is to select a column that you actually want to use no need of all columns.
Suppose in this case you require all column of product_price table and only customer id from customer_price table then you can do something like this:
DB::table('product_price')
->select(['product_price.*','customer_price.id AS customer_id'])
->where('product_price.product_id',$id)
->where('product_price.action','customer_price')
->join('customers','product_price.action_id','=','customers.id')
->get();
You can select any column but it's good to take alias of join table column in this case it is customer_price so it's not getting confusion if both table has same name column.
Good Luck
try
DB::table('product_price')
->select('*','product_price.id AS product_price_id')
->where('product_id',$id)
->where('action','customer_price')
->join('customers','product_price.action_id','=','customers.id')
->get();
the product_price id would be replace with customers id, so just print out the product_price id with other name.
hope it is help
I have a created a table (movies) in Hive as below(id,name,year,rating,views)
1,The Nightmare Before Christmas,1993,3.9,4568
2,The Mummy,1932,3.5,4388
3,Orphans of the Storm,1921,3.2,9062
4,The Object of Beauty,1991,2.8,6150
5,Night Tide,1963,2.8,5126
6,One Magic Christmas,1985,3.8,5333
7,Muriel's Wedding,1994,3.5,6323
8,Mother's Boys,1994,3.4,5733
9,Nosferatu: Original Version,1929,3.5,5651
10,Nick of Time,1995,3.4,5333
I want to write a hive query to get the name of the movie with highest views.
select name,max(views) from movies;
but it gives me an error
FAILED: Error in semantic analysis: Line 1:7 Expression not in GROUP BY key name
but doing a group by with name gives me the complete list (which is expected).
What changes should I make to my query?
It is very possible that there is a simpler way to do this.
select name
from(
select max(views) as views
, name
, row_number() over (order by max(views) desc) as row_num
from movies
group by name
) m
where row_num = 1
After little bit of digging, I found out that the answer is not so straightforward as we do in SQL. Below query gives the expected result.
select a.name,a.views from movies a left semi join(select max(views) views from movies)b on (a.views=b.views);
Can someone see where I am going wrong in the below query? I am getting the error message that the GROUP BY column doesn't exist, but it clearly does as I see that column name in the output when I don't use the GROUP BY.
SELECT
(SELECT customer_address.post_code FROM customer_address WHERE customer_address.address_type = 0 AND customer_address.customer_no = orders.customer_no) postcode, SUM(orders.order_no) orders
FROM
orders, customer_address
WHERE
orders.delivery_date = '27-MAY-15'
GROUP BY
postcode;
The answer is: You cannot use an alias name in GROUP BY.
So:
GROUP BY (SELECT customer_address.post_code ...);
Or:
select postcode, sum(order_no)
from
(
SELECT
(SELECT customer_address.post_code FROM customer_address WHERE customer_address.address_type = 0 AND customer_address.customer_no = orders.customer_no) postcode,
orders.order_no
FROM orders, customer_address
WHERE orders.delivery_date = '27-MAY-15'
)
GROUP BY postcode;
EDIT:
However, your query seems wrong. Why do you cross-join orders and customer_address? By mistake I guess. Use explicit joins (INNER JOIN customer_address ON ...), when using joins to avoid such errors. But here I guess you'd just have to remove , customer_address.
Then why do you add order numbers? That doesn't seem to make sense.
This query work when I try it in SQLite:
Transaction.where(:paid => true).select("created_at, SUM(amount) amount").group("DATE(created_at)").order('created_at')
But when I run it with postgreSQL it doesn'y work.
Heres the error message:
ActiveRecord::StatementInvalid: PGError: ERROR: column "transactions.created_at" must appear in the GROUP BY clause or be used in an aggregate function : SELECT created_at, SUM(amount) as amount FROM "transactions" WHERE ("transactions"."paid" = 't') GROUP BY DATE(created_at) ORDER BY created_at
Anyone who can help me?
Thanks in advance
You have to either use DATE(created_at) in the select clause, or use created_at in the group by clause.
You're selecting created_at, sum(amount), ordering by created_at, but are grouping by date(created_at). The latter will disallow the use of anything but the grouped by fields and aggregates except in the join/where clause.
To fix, either group by created_at, or select and order by date(created_at) instead of created_at.