SQL to Rails Query [closed] - sql

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
Can anyone Convert my Sql query into Rails Query???
SELECT DISTINCT conversation_cbgs.* FROM "conversation_cbgs"
INNER JOIN "message_cbgs"
ON "message_cbgs"."conversation_cbg_id" = "conversation_cbgs"."id"
INNER JOIN "user_message_cbgs"
ON "user_message_cbgs"."message_cbg_id" = "message_cbgs"."id"
WHERE "user_message_cbgs"."user_id" = 1
ORDER BY conversations_cbgs.updated_at DESC

Assuming you have your models built correctly - which would be helpful for those answering your questions, by the way - you would have something like:
Conversation.select("DISTINCT conversation_cbgs.*")
.joins(:messages)
.joins(:user_messages)
.where("user_message_cbgs.user_id = ?", 1)
.order("conversations_cbgs.updated_at DESC")
There are other ways using scopes and merges, etc. but you haven't posted any models for us to show you.

You have to first establish association between tables.If association is there then show it in question.
Or
You can execute this query by Model.find_by_sql(sql_query)

ConversationCbg.find(:all, :conditions=>["user_message_cbgs.user_id=?",1], :joins=>"As conversation_cbgs inner join message_cbgs on message_cbgs.conversation_cbg_id=conversation_cbgs.id inner join user_message_cbgs on user_message_cbgs.message_cbg_id=message_cbgs.id" , :select=>"Distinct conversation_cbgs.*", :order=> "conversations_cbgs.updated_at desc")

Related

Whats wrong in this sql query? It throws error [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
SELECT *
FROM ItemTable
WHERE item_link not in 'http://foo.com', 'http://bar.com'
It throws error..
in requires parenthesis:
in (...)
You missed the () after IN
SELECT * FROM ItemTable
WHERE item_link not in ('value1', 'value2')

sql query returning results not in 'between' clause [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I have the following in a where clause of a select statement:
InvoiceDate BETWEEN CONVERT (DATETIME, '01.01.2011', 104) AND CONVERT(DATETIME, '01.08.2011', 104)
and I'm receiving results all the way back to 2008. What's wrong with my query?
Posting Jonathan's comment as an Answer so that this question can be answered and come off the unanswered list:
Do you have an or condition in your where clause that may be including the additional results?

Select Count(*) returning old value from before deleting all the entries in a data table [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
So I inserted some data on my table, ran a Select Count(*) query, and showed the result, it was 120, then I deleted the data of the table from sqldeveloper, ran my VB project again with the same query as before just to test if it worked ok, and to my surprise it returned the same value as before! Still 120, although the same exact Select Count query does return 0 from sqldeveloper, I already closed the project and opened it again, no change. What could be happening?
This is my query code:
sql.CommandText = "select count(*) from mytable"
sql.CommandType = CommandType.Text
sql.Connection = conexion.con
test = Convert.ToInt32(sql.ExecuteScalar().ToString())
TextBox1.Text = test.ToString()
test is an Integer variable, the reason I convert it to an Integer is because I want to evaluate the result and decide what to do based on the number returned by the query.
VB.NET 3.5 and Oracle10g XE
I'll answer this myself just in case anyone else encounters this issue in the future, indeed as the commenters have said, you have to COMMIT everything you do on SQLdeveloper before expecting to see any actual changes in the database, just as if it was an actual SQL Transaction.
Thanks to the people that commented on this.

sql stored procedures [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 11 years ago.
I have table called "product review" with columns productreviewid , productid ,shopperid, review , rating .
I want to create the stored procedure for productreviewfetch with input parameter productreview ID
Can Any one pls help me out ?
You're not very clear about what you're trying to do - but something like this might get you started:
CREATE PROCEDURE dbo.FetchReviews #ProductReviewID INT
AS
SELECT
ProductReviewID, ProductID, ShopperID, Review, Rating
FROM
dbo.ProductReview
WHERE
ProductReviewID = #ProductReviewID
Does that give you the expected results??
It will return the values for those five columns, if your ProductViewID exists, and all NULL values, if that ID is not in your table.
Microsoft SQL Server provides the stored procedure mechanism to simplify the database development process by grouping Transact-SQL statements into manageable blocks.
More here : SQL Stored Procedures

Error Returned from a Select/Inner Join Statement [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
When I try to run the following statement, an error message is returned:
Server: Msg 208, Level 16, State 1, Line 1
Invalid object name 'vendortofaultypeitemsmap'.
SQL Help indicates this message appears when "an object that does not exist is referenced". This table does exist and returns values if I run *select * from vendortofaulttypeitemsmap*. Can someone help me pinpoint what is wrong with the statement below that is causing the error message? Thanks in advance.
select
vendortofaulttypeitemsmap.vendorid,
vendortofaulttypeitemsmap.faultypeitemguid,
guid_faulttypeitems.faulttypeitemname,
vendortoworkactionmap.workactionitemguid,
guid_workactionitem.workactionitemname
from vendortofaultypeitemsmap
inner join guid_faulttypeitems on
vendortofaulttypeitemsmap.faultypeitemguid=
guid_faulttypeitems.faultypeitemguid
inner join guid_workactionitem on
vendortoworkactionmap.workactionitemguid=
guid_workactionitem.workactionitemguid
where vendortofaulttypeitemsmap.vendorid=45
You missed a 't' vendortofaultTypeitemsmap.
You have a typo. It is either vendortofaultypeitemsmap or vendortofaulttypeitemsmap.
Writing those table names out every time makes it easy to make typos. Use table aliases to simplify the whole thing, and change it to:
select v.VendorID, v.FaultTypeItemGUID, f.FaultTypeItemName,
v.WorkActionItemGUID, w.WorkActionItemName
from VendorToFaultTypeItemsMap v
inner join GUID_FaultTypeItems f on v.FaultTypeItemGUID = g.FaultTypeItemGUID
inner join GUID_WorkActionItem w on v.WorkActionItemGUID = w.WorkActionItemGUID
where v.VendorID = 45
With long names like that, using mixed case can also help you spot problems easier (assuming your database isn't set to case-sensitive).