Rails: mysql error unknown column in sub query - mysql

I am implementing a search filter in my web app. using sub-queries like this:
tool = Tool.select('*, (select ROUND(AVG(ratings.rating)) from ratings where tool_id = tools.id AND rating_type = 2) as ratings,
3956 * 2 * ASIN(SQRT(POWER(SIN(('+"#{params[:latitude]}"+' - abs(tools.latitude)) * pi()/180 / 2), 2) + COS('+"#{params[:latitude]}"+' * pi()/180 ) * COS(abs(tools.latitude) * pi()/180) * POWER(SIN(('+"#{params[:longtude]}"+' - abs(tools.longitude)) * pi()/180 / 2), 2) )) as distance').where('user_id != ? AND pause_status =?', user_id, 0).order('distance asc')
# =>delivery type only if delivery type is 1
if params[:search].present? && !params[:search].nil?
tool = tool.where('title LIKE ? OR description LIKE ?', "%#{params[:search]}%","%#{params[:search]}%")
end
# =>Category search
if params[:category_id].present? && !params[:category_id].nil?
tool = tool.where('category_id =?', params[:category_id])
end
# =>price range
if params[:max_price].present? && params[:min_price].present? && !params[:max_price].nil? && !params[:min_price].nil?
tool = tool.where('price >= ? AND price <= ?', params[:min_price].to_f, params[:max_price].to_f)
end
# => filter availability
if params[:availability].present? && !params[:availability].nil?
if params[:availability].to_i == 2
tool = tool.where('available_type =?', 2) #=> weekend
elsif params[:availability].to_i == 1
tool = tool.where('available_type =?', 1) # => weekdays
end
end
if params[:rating].present? && !params[:rating].nil?
tool = tool.having('ratings > 5')
end
if params[:delivery_type].present? && !params[:delivery_type].nil?
if params[:delivery_type].to_i == 0
tool = tool.where('delivery_type = ?', 0)
end
end
if tool.empty?
return []
else
tool_array = []
tool.each do |t|
tool_hash = {}
tool_hash['id'] = t.id
tool_hash['title'] = t.title
tool_hash['latitude'] = t.latitude
tool_hash['longitude'] = t.longitude
tool_hash['attachment'] = Image.get_single_attachment(t.id)
tool_array.push(tool_hash)
end
return tool_array
end
when I pass rating parameter it print the query like this:
SELECT COUNT(*) FROM `tools` WHERE (user_id != 3 AND pause_status =0) HAVING (ratings > 5)"
and without rating parameter:
SELECT *, (select ROUND(AVG(ratings.rating)) from ratings where tool_id = tools.id AND rating_type = 2) as ratings, 3956 * 2 * ASIN(SQRT(POWER(SIN((30.657797735213 - abs(tools.latitude)) * pi()/180 / 2), 2) + COS(30.657797735213 * pi()/180 ) * COS(abs(tools.latitude) * pi()/180) * POWER(SIN((76.7327738833397 - abs(tools.longitude)) * pi()/180 / 2), 2) )) as distance FROM `tools` WHERE (user_id != 3 AND pause_status =0) ORDER BY distance asc"
and I a error like this in my having clause:
"error": "Mysql2::Error: Unknown column 'ratings' in 'having clause': SELECT COUNT(*) FROM `tools` WHERE (user_id != 3 AND pause_status =0) HAVING (ratings > 5)",
"code": 301
and if I comment the each loop it works.
Please tell where I am doing wrong.

having should work with group by

You can't use alias name as ratings in the following line,
select ROUND(AVG(ratings.rating)) from ratings where tool_id = tools.id AND rating_type = 2) as ratings
Give a different name as MYSQL confusing ratings as column.

Related

SQLSERVER Running Balance

Actually i have problem on my query to get the running balance , i have debit and credit transaction i need one column to showing the cumulative running balance this is the code i used :
Select * From (
Select D.AccNo, H.[Date], A.AccountName, H.TrxNo,
(Case When ((D.Remark = '') or (D.Remark is Null)) Then H.Trxnote Else D.Remark End) As TrxDetailDescA,
(D.Debit * 1) AS DebitValue, (D.Credit * 1) AS CreditValue,SUM(COALESCE(D.Debit, 0) - COALESCE(D.Credit, 0)) AS Balance
From TblHeadTrans H, TblTransDetails D, TblAccount A
Where H.Guid = D.[LineNo]
And D.AccNo = A.AccountNo
And H.[Date] >= '01-01-2022' And H.[Date] <= '10-07-2022' And D.AccNo >= '1003'
group by AccNo,H.[Date],A.AccountName,H.TrxNo,D.Remark,h.Trxnote,d.Debit,d.Credit
Union All
Select D.AccNo, Null As TrxDate, A.AccountName, Null As TrxNo,
'Opening Balance' As TrxDetailDesc,
Case When (Sum(D.Debit * 1) - Sum(D.Credit *1)) < 0 then 0
Else (Sum(D.Debit * 1) - Sum(D.Credit * 1)) End As DebitValue,
Case When (Sum(D.Credit * 1) - Sum(D.Debit * 1)) < 0 then 0
Else (Sum(D.Credit * 1) - Sum(D.Debit * 1)) End As CreditValue
, SUM(COALESCE(d.Debit, 0) - COALESCE(d.credit, 0)) AS Balance
From TblHeadTrans H, TblTransDetails D, TblAccount A
Where H.guid = D.[LineNo] And D.AccNo = A.AccountNo
And d.[Date] < '01-01-2022' And D.accno = '1003'
Group By D.AccNo, A.AccountName,H.Date,H.TrxNo
) ReportData
WHERE 1=1
Order By AccNo, [Date], TrxNo
and the result showing as the picture:
the result

How to sort before using STUFF in SSRS

I have 2 different values I'm trying to STUFF here. It is Quantity + Price. For example: 1-$0.36; 100-$0.29; 25-$0.31. How can I have it sort by Quantity before being stuffed? (1,25,100 instead of 1,100,25) I did come across this link Sort data before concatenating using STUFF FOR XML, but it dealt with 1 value and I'm dealing with 2 values
SELECT STUFF(
(SELECT DISTINCT TOP (5)
'; ' + (CAST(FLOOR(CASE WHEN PCFBD.Quantity IS NOT NULL THEN PCFBD.Quantity ELSE 1 END) AS VARCHAR) + '-$' + CAST(REPLACE(REPLACE(RTRIM(REPLACE(
CASE WHEN PCF.PriceMethod = 0 THEN ROUND(I.CdCost / (100 - PCF.FormulaPercent) * 100, 2)
WHEN PCFBH.PriceFormula = 2 AND PCFBD.FormulaPercent IS NULL THEN ROUND(I.CdCost / (100 - PCF.FormulaPercent) * 100, 2)
WHEN PCFBH.PriceFormula = 2 AND PCFBD.FormulaPercent IS NOT NULL THEN ROUND(I.CdCost / (100 - PCFBD.FormulaPercent) * 100, 2)
WHEN PCFBH.PriceFormula = 1 THEN ROUND((I.ListPrice * (100 - PCFBD.FormulaPercent)) * .01,2)
ELSE NULL END, '000' ,'')), ' ','0') + '', '. ', '') AS VARCHAR))
FROM Item AS I
INNER JOIN PriceContractFamily AS PCF ON I.FamilyId = PCF.FamilyId
AND I.ItemStatus IN (0, 5)
INNER JOIN StockItem SI ON I.ItemId = SI.ItemId
AND SI.WarehouseId = '502E5876-C26B-4E11-8B88-AFE0C34ECF0D'
LEFT OUTER JOIN PriceContractFamilyBracketHeader AS PCFBH ON PCF.PriceContractFamilyId = PCFBH.PriceContractFamilyId
LEFT OUTER JOIN PriceContractFamilyBracketDetail AS PCFBD ON PCFBH.BracketHeaderId = PCFBD.BracketHeaderId
WHERE I.ListPrice = #ListPrice
AND LEFT(I.ItemNumber, 6) = #ItemNumber
AND PCF.PriceContractId = #PriceContractId
FOR XML PATH('')),1, 2, '') AS QtyPrice
You should be able to add an ORDER BY before the FOR XML PATH statement.

mysql matching multiple and's and or's

I am trying to get rows from a table where there are matches on multiple other tables.
This is the query I am running.
SELECT qt.*, DATEDIFF(CURRENT_DATE, STR_TO_DATE(up.DOB, '%m-%d-%Y')) / 365 AS Age
FROM UserProfile AS up, Game AS g, QuestionTable AS qt
WHERE NOT EXISTS(SELECT * FROM Options WHERE UserID = 75 AND QID = qt.QID AND DateTime >= CURDATE())
AND g.Active = 1 AND g.QID = qt.QID
AND (((g.Gender = up.Gender OR g.Gender = 'B') AND ((g.City = up.City AND g.Zip = up.Zip AND g.Country = up.Country) OR g.Home = 0) AND ((Age BETWEEN g.Maximum AND g.Minimum) OR g.Age = 0) AND ((SQRT( POW( 69.1 * ( g.Latitude - -93.5746359 ) , 2 ) + POW( 69.1 * ( 44.9737707 - g.Longitude ) * COS( g.Latitude / 57.3 ) , 2 ) ) > g.Distance) OR g.Geo = 0)) OR g.Special = 0)
GROUP BY qt.QID
I have ran this expression through C# and it returns true, yet it is only matching on the 'g.Special = 0' part through MySql.
Any help on this would be much appreciated!
Resolved the issue by fixing some mistakes I made and changed the query to this,
SELECT *
FROM QuestionTable AS qt
WHERE NOT EXISTS
(SELECT * FROM Options WHERE UserID = 75 AND QID = qt.QID AND DateTime >= CURDATE())
AND EXISTS(SELECT g.* FROM UserProfile AS up, Game AS g WHERE g.Active = 1 AND g.QID = qt.QID AND(g.Special = 0 OR(
(g.Gender = up.Gender OR g.Gender = 'B') AND
((g.City = up.City AND g.zip = up.Zip AND g.Country = up.Country) OR g.Home = 0) AND
((SQRT( POW( 69.1 * ( g.Latitude - 44.9737707 ) , 2 ) + POW( 69.1 * ( -93.5746359 - g.Longitude ) * COS( g.Latitude / 57.3 ) , 2 ) ) < g.Distance) OR g.Geo = 0) AND
((DATEDIFF(CURRENT_DATE, STR_TO_DATE(DOB, '%m/%d/%Y'))/365.25 BETWEEN g.Minimum AND g.Maximum) OR g.Age = 0))))

SQL CONCAT Function

I would like to create a Concate like:
CONCAT(OEEL.orderno+'-'+OEEL.ordersuf+'-'+OEEL.prodcat) AS "Unique"
SELECT OEEL.invoicedt, UCASE(OEEL.whse) AS Whse, OEEL.orderno, OEEL.ordersuf, OEEL.custno, UCASE(OEEL.shipto) AS Shipto, UCASE(OEEL.slsrepin) AS Slsrepin,
UCASE(OEEL.slsrepout) AS Slsrepout, OEEL.returnfl, OEEL.netamt, OEEL.wodiscamt, OEEL.discamtoth, OEEL.qtyship, OEEL.commcost, ICSS.csunperstk,
UCASE(ICSD.name) AS Name, UCASE(ICSD.region) AS Region, UCASE(OEEL.prodcat) AS Prodcat, UCASE(SASTA.descrip) AS Descrip, UCASE(OEEL.transtype)
AS Transtype, UCASE(ARSS.user2) AS User2, OEEL.transdt, ICSS.transdt AS "ICSS.Transdt", ICSD.transdt AS "ICSD.Transdt", SASTA.transdt AS "SASTA.Transdt",
ARSS.transdt AS "ARSS.Transdt", { fn CURDATE() } AS CURDATE1, { fn CURTIME() } AS CURTIME2,
CASE WHEN OEEL.returnfl = '0' THEN (OEEL.netamt - OEEL.wodiscamt - OEEL.discamtoth) ELSE (- 1 * (OEEL.netamt - OEEL.wodiscamt - OEEL.discamtoth))
END AS "SALES", CASE WHEN OEEL.returnfl = '0' THEN (OEEL.qtyship * OEEL.commcost * NVL(ICSS.csunperstk, 1))
ELSE (- 1 * OEEL.qtyship * OEEL.commcost * NVL(ICSS.csunperstk, 1)) END AS "COST",
(CASE WHEN OEEL.returnfl = '0' THEN (OEEL.netamt - OEEL.wodiscamt - OEEL.discamtoth) ELSE (- 1 * (OEEL.netamt - OEEL.wodiscamt - OEEL.discamtoth)) END)
- (CASE WHEN OEEL.returnfl = '0' THEN (OEEL.qtyship * OEEL.commcost * NVL(ICSS.csunperstk, 1))
ELSE (- 1 * OEEL.qtyship * OEEL.commcost * NVL(ICSS.csunperstk, 1)) END) AS GP
FROM { oj { oj { oj { oj PUB.oeel OEEL LEFT OUTER JOIN
PUB.icss ICSS ON OEEL.cono = ICSS.cono AND OEEL.shipprod = ICSS.prod AND OEEL.icspecrecno = ICSS.icspecrecno } LEFT OUTER JOIN
PUB.icsd ICSD ON OEEL.cono = ICSD.cono AND OEEL.whse = ICSD.whse } LEFT OUTER JOIN
PUB.sasta SASTA ON OEEL.cono = SASTA.cono AND OEEL.prodcat = SASTA.codeval } LEFT OUTER JOIN
PUB.arss ARSS ON OEEL.cono = ARSS.cono AND OEEL.custno = ARSS.custno AND OEEL.shipto = ARSS.shipto }
WHERE (OEEL.cono = 1) AND (OEEL.invoicedt BETWEEN { d '2014-06-02' } AND { d '2014-06-03' }) AND (SASTA.codeiden IN ('C', 'c'))
ORDER BY OEEL.custno, OEEL.shipto, OEEL.prodcat
The function has + (which in MySQL is not a concatenation symbol) between the variables.
Try:-
CONCAT_WS('-', OEEL.orderno, OEEL.ordersuf, OEEL.prodcat) AS `Unique`
CONCAT_WS does a concatenation With Separator .the first parameter is the separator with the others concatenated together.

Subquery returns more than 1 row sql query

I had such an error can explain what I did wrong?
I only add this sql query:
(? = (select travel_region_id from relationships where travel_id = travels.id))
error
ActiveRecord::StatementInvalid (Mysql::Error: Subquery returns more than 1 row: select count(*) from travel_start_days, cars, travels
where travels.id = travel_start_days.travel_id and travels.id = travel.car_id and travel_start_days.day > adddate(curdate(), interval '2' day) and (7 = (select travel_region_id from relationships where travel_id = travels.id)) and '2013-08-16' <= travel_start_days.day):
Update Whis is method create query
def conditions
where = ''
param = []
if #region && #region != ''
where += 'travels.region_id = ?'
param += [#region.to_i]
end
if #car && #car != ''
where += ' and ' if where != ''
where += 'cars.id = ?'
param += [#car.to_i]
end
if #relation && #relation != ''
where += ' and ' if where != ''
where += '(? = (select travel_region_id from relationships where travel_id = travels.id))'
param += [#relation.to_i]
end
if #start_port && #start_port != ''
where += ' and ' if where != ''
where += '(? = (select location_id from travel_days where travel_id = travel_start_days.travel_id and day_no = 1 order by arrival asc limit 1))'
param += [#start_port.to_i]
end
return where == '' ? nil : ["travel_start_days.day > adddate(curdate(), interval ? day) and " + where] + [#criteria[5]] + param
end
The issue in this part of conditions:
(7 = (select travel_region_id from relationships where travel_id = travels.id))
Obviously this subquery returns more than one travel_region_id just replace = with IN
(7 IN (select travel_region_id from relationships where travel_id = travels.id))
If you are getting more than 1 row from subquery, then add group by clause to your subquery
SELECT travel_region_id FROM relationships
WHERE travel_id = travels.id
GROUP BY travel_region_id