Select lname, fname, title as "Book Title"
from books join line
on books.bookid = book_author.bookid
join author
on author.authorid = book_author.authorid;
It keeps saying the table or view does not exist but when I do select * from books; it does show me the table.
Select author.lname, author.fname, books.title as "Book Title"
from books join book_author --line
on books.bookid = book_author.bookid
join author
on author.authorid = book_author.authorid;
line has been commented. it should be book_author instead. Also included the table-names before the column names in the select clause
Related
I wrote a below query class, which is throwing a missing attribute error which I didn't put in the select method anywhere. Any idea from where this error might be coming? Why project_id is being searched in the to_json method.
class WorkitemPriceResearchPresenter
def initialize(company)
#company = company
end
def query
#workitems ||= \
#company
.workitems
.joins(
:workitem_category,
:workitem_status,
{ project: [:shipyard, {vessel: :vessel_category}] },
)
.select <<-EOS
workitems.id as id,
projects.sequential_id as sequential_id,
vessels.name as vessel_name,
vessel_categories.code as vessel_category,
shipyards.name as shipyard,
workitems.item_code as item_code,
workitems.description as description,
workitems.unit as unit,
workitems.price_cents as price_cents,
workitems.currency as currency,
workitems.quantity as quantity,
workitem_statuses.name as status,
workitems.discount as discount,
DATE_PART('year', projects.scheduled_from::date) as scheduled_from_year,
projects.scheduled_from as scheduled_from,
DATE_PART('year', projects.scheduled_to::date) as scheduled_to_year,
projects.scheduled_to as scheduled_to,
workitem_categories.name as Category
EOS
end
def as_json
query.to_json
end
end
Error is:
pry(main)> WorkitemPriceResearchPresenter.new(company).query.as_json
Workitem Load (33.7ms) SELECT workitems.id as id,
projects.sequential_id as sequential_id,
vessels.name as vessel_name,
vessel_categories.code as vessel_category,
shipyards.name as shipyard,
workitems.item_code as item_code,
workitems.description as description,
workitems.unit as unit,
workitems.price_cents as price_cents,
workitems.currency as currency,
workitems.quantity as quantity,
workitem_statuses.name as status,
workitems.discount as discount,
DATE_PART('year', projects.scheduled_from::date) as scheduled_from_year,
projects.scheduled_from as scheduled_from,
DATE_PART('year', projects.scheduled_to::date) as scheduled_to_year,
projects.scheduled_to as scheduled_to,
workitem_categories.name as Category
FROM "workitems" INNER JOIN "workitem_categories" ON "workitem_categories"."id" = "workitems"."workitem_category_id" INNER JOIN "workitem_statuses" ON "workitem_statuses"."id" = "workitems"."workitem_status_id" INNER JOIN "projects" ON "projects"."id" = "workitems"."project_id" AND "projects"."deleted_at" IS NULL INNER JOIN "shipyards" ON "shipyards"."id" = "projects"."shipyard_id" AND "shipyards"."deleted_at" IS NULL INNER JOIN "vessels" ON "vessels"."id" = "projects"."vessel_id" AND "vessels"."deleted_at" IS NULL INNER JOIN "vessel_categories" ON "vessel_categories"."id" = "vessels"."vessel_category_id" AND "vessel_categories"."deleted_at" IS NULL WHERE "workitems"."deleted_at" IS NULL AND "workitems"."company_id" = $1 [["company_id", "c61e4368-1a60-464b-8002-9da31bf301e5"]]
ActiveModel::MissingAttributeError: missing attribute: project_id
from /Users/ar/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/activerecord-4.2.11.3/lib/active_record/attribute_methods/read.rb:93:in `block in _read_attribute'
Let me know if I need to provide any more information.
Since you are joining workitems with project, your workitems is supposed to have a project_id attribute in order to join the tables. But it looks like you do not have such column, so you are getting this error.
i have two queries:
the first one query is:
SELECT sale.saleid,
sale.totalpaid,
item.itemname AS item,
stock.saleprice AS Price,
invoice.qty,
sale.discount,
invoice.saleprice AS [invoice saleprice],
cetegory.catname AS [Cateogory],
cetegory.subcat AS [Sub Catgry],
vehicle.vehicle_name AS [Vehicle],
vehicle.vehicle_model AS [Model],
item.model_number AS [Part No.],
sale.date,
stock.size,
sale.customerid
FROM invoice
JOIN item
ON invoice.itemid = item.itemid
JOIN sale
ON invoice.saleid = sale.saleid
JOIN stock
ON item.itemid = stock.itemid
JOIN cetegory
ON item.catid = cetegory.catid
JOIN vehicle
ON item.vehicleid = vehicle.vehicleid
WHERE sale.saleid = 5
and the second query is:
SELECT customer.customername,
customer.customercontact,
customer.customeraddress,
account.account_type
FROM account
JOIN customer
ON customer.customerid = account.customerid
Now i want to combine these two queries by customer id because i have "custome id" in sale table
You could use a CTE and then join both:
WITH CUST
AS
(SELECT customer.customername,
customer.customercontact,
customer.customeraddress,
account.account_type ,
customer.customerid
FROM account
JOIN customer
ON customer.customerid = account.customerid
),
SALES
AS
(
SELECT sale.saleid,
sale.totalpaid,
item.itemname AS item,
stock.saleprice AS Price,
invoice.qty,
sale.discount,
invoice.saleprice AS [invoice saleprice],
cetegory.catname AS [Cateogory],
cetegory.subcat AS [Sub Catgry],
vehicle.vehicle_name AS [Vehicle],
vehicle.vehicle_model AS [Model],
item.model_number AS [Part No.],
sale.date,
stock.size,
sale.customerid
FROM invoice
JOIN item
ON invoice.itemid = item.itemid
JOIN sale
ON invoice.saleid = sale.saleid
JOIN stock
ON item.itemid = stock.itemid
JOIN cetegory
ON item.catid = cetegory.catid
JOIN vehicle
ON item.vehicleid = vehicle.vehicleid
WHERE sale.saleid = 5
)
SELECT * FROM CUST
LEFT JOIN SALES
ON CUST.customerid = SALES.customerid
I have three models: company, event, event_space
company has many events
event belongs to event space
now I want to get all events from a company where the event_space has virtual attribute set to true
c = Comapny.first
c.events.joins(:event_space).where("event_space.virtual = true")
I'm doing something wrong because I have
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: event_space.virtual: SELECT "events".* FROM "events" INNER JOIN "event_spaces" ON "event_spaces"."id" = "events"."event_space_id" WHERE "events"."company_id" = 2 AND (event_space.virtual = true)
You can modify your where clause as follows to get it right:
c.events.joins(:event_space).where(event_spaces: {virtual: true})
I have а little question, write through Activerecord Query Interface
It's actually:
Gp.select("date('gps'.'created_at') as date,('users'.'name') as name, SUM('gps'.'sum_issue') as sum_issue").joins('LEFT JOIN users ON users.id = gps.user_id').where("users.ab_id = :abs_id AND users.id != 20", {:abs_id => current_user.ab_id}).group("users.name")
Result of query must be user name, sum,and date. If i do this query directly from SQLlite it's work, But
Active Record Query Interface give me
[#<Gp sum_issue: 289000>, #<Gp sum_issue: 364130>, #<Gp sum_issue: 620000>]
How i can get a name,date,sum_issue and show it in my helper.
like this:
{
created_at: datet,
sum_issue: sum_issue,
name: name
}
Try
Gp.select("date('gps'.'created_at') as date,('users'.'name') as name, SUM('gps'.'sum_issue') as sum_issue").joins('LEFT JOIN users ON users.id = gps.user_id').where("users.ab_id = :abs_id AND users.id != 20", {:abs_id => current_user.ab_id}).group("users.name").first.name
I have the following tables: users(id), articles(id), favorites(article_id, user_id). I need to list articles added to favorites by current user with a flag indicating if they were also favorited by other users. SQL is quite simple:
select articles.id, count(f2.article_id)
from articles a
inner join favorites f1 on f1.article_id = a.id
left join favorites f2 on f1.article_id = f2.article_id and not f1.user_id = f2.user_id
where f1.user_id = 1
group by a.id
Is there a way to do it using Rails query generator?
something like this should get you on your way (assuming you have an Article and Favorites model/table):
t = Article.joins("INNER JOIN #{Favorite.table_name} AS f1 on f1.article_id = #{Article.table_name}.id")
t = t.joins("LEFT JOIN #{Favorite.table_name} AS f2 on f1.article_id = f2.article_id AND NOT f1.user_id = f2.user_id")
t = t.where("f1.user_id = ?", 1)
t = t.group("#{Article.table_name}.id")
t.select("#{Article.table_name}.id, COUNT(f2.article_id)")