I have a SQL query I want to convert it to a dql query in symfony 3.4,
this is the SQL query:
SELECT c.*
FROM chat c INNER JOIN (
SELECT idsender, MAX(created) created
FROM chat
WHERE idreceiver = 2 OR idsender = 2
GROUP BY idsender
) t ON t.idsender = c.idsender AND t.created = c.created
WHERE c.idreceiver = 2
This is my attempt:
public function findAllChatBox($idreceiver,$idsender)
{
return $this->getEntityManager()
->createQuery(
'SELECT c FROM offer Bundle:chat c WHERE c.idreceiver = :idreceiver AND idsender=:idsender'
)
->groupBy('c.idsender')
->join('t ON t.idsender = c.idsender AND t.created = c.created
WHERE c.idreceiver =: idreceiver')
->setParameter('idreceiver',$idreceiver)
->setParameter('idsender',$idsender)
->getResult();
}
But I had a syntax error, any suggestions please?
syntax error here 'join('t ON t.idsender = c.idsender AND t.created = c.created WHERE c.idreceiver =: idreceiver')'
Related
SELECT
Email_address, COUNT(Order_date)
FROM
(SELECT
cust.Email_address, COUNT(ol.Variant_name), ord.Order_date
FROM
DMW_Order_Line_v3 ol
JOIN
DMW_Order_v3 ord ON ol.Unique_transaction_identifier = ord.Unique_transaction_identifier
AND ol.Brand_country = ord.Brand_country
JOIN
DMW_Customer_v3 cust ON ord.Email_address = cust.Email_address
AND ord.Brand_country = cust.Brand_country
WHERE
ord.Brand_country = 'kiehls-emea_CZ'
AND cust.Address_country = 'CZ'
AND cust.Optin_email != 'False'
AND ol.Line_status = 'SHIPPED'
AND ol.Variant_name = 'Sample'
GROUP BY
cust.Email_address, ord.Order_date
HAVING
COUNT(ol.Variant_name) >= 4)
GROUP BY
Email_address
Please, forgive me that I'm posting the whole body of the code. But it might be helpful somehow, who knows. As you can see it is a query on subquery containing joins. I'm using Salesforce SQL. When I run the code, I get this error:
Error saving the query field. Incorrect syntax near the keyword 'GROUP'.
What am I doing wrong? Besides being a noob ;-)
You don't need the count column in the subquery:
SELECT Email_address, COUNT(*)
FROM (SELECT cust.Email_address, ord.Order_date
FROM DMW_Order_Line_v3 ol JOIN
DMW_Order_v3 ord
ON ol.Unique_transaction_identifier = ord.Unique_transaction_identifier AND
ol.Brand_country = ord.Brand_country JOIN
DMW_Customer_v3 cust
ON ord.Email_address = cust.Email_address AND
ord.Brand_country = cust.Brand_country
WHERE ord.Brand_country = 'kiehls-emea_CZ' AND
cust.Address_country = 'CZ' AND
cust.Optin_email <> 'False' AND
ol.Line_status = 'SHIPPED' AND
ol.Variant_name = 'Sample'
GROUP BY cust.Email_address, ord.Order_date
HAVING COUNT(ol.Variant_name) >= 4
) e
GROUP BY Email_Address
Query is below:
select
C.ngo_seq_no, B.member_name, B.member_pan_no, B.member_aadhar_no,
C.dept_name, C.source, C.fin_year, C.amt_sanctioned, C.comment,
R.member_designation_desc
from
member_designation R, ngo_member_detail B
inner join
ngo_source_fund C on B.ngo_seq_no = C.ngo_seq_no
where
mem_designation = member_designation_id::text
and C.ngo_seq_no = '15' ;
15;"Sanjay";"43537459874";"346555378895";"Nic";"2";"29922";"Nothing"
15;"Sanjay";"43537459874";"346555378895";"Nic";"2";"29922";"Nothing"
15;"Sanjay";"43537459874";"346555378895";"Nic";"2";"29922";"Nothing"
15;"Ajay ";"587592894832";"646738972456";"Nic";"2";"29922";"Nothing"
15;"Ajay ";"587592894832";"646738972456";"Nic";"2";"29922";"Nothing"
15;"Ajay ";"587592894832";"646738972456";"Nic";"2";"29922";"Nothing"
15;"Rahul";"4353745983";"346732856575";"Nic";"2";"29922";"Nothing"
15;"Rahul";"4353745983";"346732856575";"Nic";"2";"29922";"Nothing"
15;"Rahul";"4353745983";"346732856575";"Nic";"2";"29922";"Nothing"
I have the following query
SELECT
dtc.coupon_type_company_name,
COUNT(*) * dtc.coupon_type_company_coupon_amount AS 'Total_Coupon_To_Be_Used',
dtc.coupon_type_company_coupon_months_combinable
FROM
[dbo].[coupon_type_Company_User] dtcu
JOIN
coupon_type_Company dtc ON dtcu.coupon_type_Company_ID = dtc.id
JOIN
person p ON dtcu.userID = p.userID
WHERE
coupon_type_company_coupon_is_combinable = 1
OR coupon_type_company_has_coupon = 1
AND dtc.companyID = 1081
AND p.is_active = 1
GROUP BY
dtc.coupon_type_company_name,dtc.coupon_type_company_coupon_amount,
dtc.coupon_type_company_coupon_months_combinable
This returns the following:
What I want to have however is just one column and one row that should take the SUM of my middle column (count(*)*dtc.coupon_type_company_coupon_amount).
How could I achieve this and prevent doing this in my code backend (C#)
You can wrap your query like this:
SELECT SUM(Total_Coupon_To_Be_Used) AS the_sum
FROM (
your query
) s
Use a "Table Expression", as in:
select sum(Total_Coupon_To_Be_Used) from (
SELECT dtc.coupon_type_company_name,
count(*) * dtc.coupon_type_company_coupon_amount as 'Total_Coupon_To_Be_Used',
dtc.coupon_type_company_coupon_months_combinable
FROM [dbo].[coupon_type_Company_User] dtcu
JOIN coupon_type_Company dtc ON dtcu.coupon_type_Company_ID = dtc.id
JOIN person p ON dtcu.userID = p.userID
WHERE coupon_type_company_coupon_is_combinable = 1
or coupon_type_company_has_coupon = 1
and dtc.companyID = 1081
AND p.is_active = 1
GROUP BY
dtc.coupon_type_company_name,dtc.coupon_type_company_coupon_amount,
dtc.coupon_type_company_coupon_months_combinable
) x
Hi i'm new to SQL and LINQ and i need to convert this sql statement to LINQ
select r.*,
(Select name From org_table where org_ID= r.org_ID )org
(Select name From Depart_table where Depart_ID= r.Depart_ID)depart
from reserve_table r
where name like '
try it this way:
var result = from r in reserve_table
where r.name.Contains("something")
select new {
org = (from o in org_table where o.Org_ID = r.Org_ID select o.name)
depart = (from d in Depart_table where d.Depart_ID = r.Depart_ID select d.name)
// add the rest
}
SELECT *
FROM jobs
WHERE (SELECT DISTINCT jobs.*
FROM jobs, job_requests
WHERE (jobs.user_id = 1) OR
(job_requests.user_id = 1 AND job_requests.job_id = jobs.id)
)
This sql gives me:
Mysql::Error: Operand should contain 1 column(s).
If I execute the select from the where clause it works
SELECT DISTINCT jobs.* FROM jobs, job_requests
WHERE (jobs.user_id = 1) OR
(job_requests.user_id = 1 AND job_requests.job_id = jobs.id)
Could somebody explain me why? This query is generated by rails activerecord so the main select is needed.
The ror code:
has_many :my_jobs, :class_name=>"Job", :finder_sql =>
'SELECT DISTINCT jobs.* ' +
'FROM jobs, job_requests ' +
'WHERE (jobs.user_id = #{id}) OR ' +
'(job_requests.user_id = #{id} AND job_requests.job_id = jobs.id AND job_requests.request_status IN ("requested", "confirmed") )'
I am submitting my answer based on the additional information provided. The query below should address your requirement.
Job.all(:conditions=> [ "
jobs.user_id = ? OR
EXISTS (
SELECT *
FROM job_requests AS B
WHERE B.job_id = jobs.id AND B.user_id = ?
)", user_id, user_id ]
)
If you want an efficient version of the same query then you should go with UNIONs.
sql = "
SELECT *
FROM jobs A WHERE A.user_id = ?
UNION
SELECT *
FROM jobs A, job_requests B
WHERE A.id = B.job_id AND B.user_id = ?
"
Job.find_by_sql(Job.send(:sanitize_sql_array, [sql, user_id, user_id]))
The first query can be converted to a named_scope.
class Job < ActiveRecord::Base
named_scope :for_user, lambda { |user_id| { :conditions=> [ "
jobs.user_id = ? OR
EXISTS (
SELECT *
FROM job_requests AS B
WHERE B.job_id = jobs.id AND B.user_id = ?
)", user_id, user_id ] }
}
end
Now you can use the named_scope as follows:
Jobs.for_user(current_user)
SQL query: Documentation
SELECT (
'95.86.122.224', NOW( ) , '95.86.122.224', '95.86.122.224'
)
FROM orders
LEFT JOIN visitors ON visitors.ip = '95.86.122.224'
WHERE visitors.ip IS NULL
LIMIT 0 , 30
MySQL said: Documentation
#1241 - Operand should contain 1 column(s)
If I have understood you correctly you need to restructure as
SELECT * FROM jobs
WHERE EXISTS
(SELECT DISTINCT jobs.*
FROM jobs, job_requests
WHERE (jobs.user_id = 1)
OR (job_requests.user_id = 1 AND job_requests.job_id = jobs.id))
I'm guessing you only want to select from jobs where the inner query is true, the above will do this for you.
Following code will work, if you want to get the jobs with job requests for a given user:
Job.all( :joins => :jobs_requests,
:conditions => ["job_requests.user_id = ?", user_id])