How can I execute the MSSQL query without error? - mysql

I'm trying to execute mssql query but its showing error.The same query I executed in MySQL its working fine.
The Query is:
SELECT tst_flow_name, tst_flow_desc,COUNT(tst_flow) tot
FROM test_flow_details
LEFT OUTER JOIN tst_flow ON tst_flow_name=tst_flow
AND test_flow_details.project=tst_flow.project
WHERE test_flow_details.project='JupiterQA'
ERROR IS:
Column 'test_flow_details.tst_flow_name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
How can I execute the MSSQL query without error.

You can't mix normal column selects with aggregate function call like count().
Group by the columns you want to be unique and then you can add count()
SELECT tst_flow_name, tst_flow_desc, COUNT(*) tot
FROM test_flow_details
LEFT OUTER JOIN tst_flow ON tst_flow_name=tst_flow
AND test_flow_details.project=tst_flow.project
WHERE test_flow_details.project='JupiterQA'
GROUP BY tst_flow_name, tst_flow_desc

Related

Laravel 5.8, Tricky subquery with Count(*) in where clause

I need to write a little tricky subquery in where clause
So, I have a query like this
Select * FROM table1
JOIN table2 ...
LEFT JOIN table3 ...
WHERE (SELECT COUNT(*) FROM some_table where some_condition) = 0;
The SQL works fine, but how can I move it into the Laravel?
Yeah, I know that I should use DB::raw() but the problem is the subquery returns count. So, for example, the count is 5454. After that, I'm having something like this.
->where(5454, '=', 0)
and it gives me obvious error message: Unknown column 5454 ...
I've tried also to use AS count for the subquery but in that case, I'm having another obvious error message: Syntax error 0_0
So, any suggestions?
Try
whereRaw("(SELECT COUNT(*) FROM some_table where some_condition) = 0")
This will help you.

Rewrite the SQL in version 5.7

I have a following SQL which works in mysql version 5.6 but is breaking in mysql version 5.7.x.
SELECT * FROM (SELECT * FROM photos WHERE photoable_type = 'Mobilehome'
AND photoable_id IN (SELECT id FROM mobilehomes WHERE
mobilehomes.community_id = 1) AND photos.image_file_size IS NOT NULL
AND photos.is_published IS TRUE ORDER BY photos.priority ASC) AS tmp_table
GROUP BY photoable_id
It's throwing me following error:
Expression #1 of SELECT list is not in GROUP BY clause and contains
nonaggregated column 'tmp_table.id' which is not functionally
dependent on columns in GROUP BY clause; this is
incompatible with sql_mode=only_full_group_by
In this case or you change the sql mode for instrcut the db to work as mysql 5.6 version or you can adeguate your query to the new behavior
In this second case
If you use group by whithout aggregation function this mean that for all the column different from photoable_id you accept casual result
This mean that you could, probably, also accepted an aggregated result based greagtion function eg: on min() or max ()
assuming your tables containg col1, col2, .. the you must declare explicitally the column you need
SELECT photos.photoable_id, min(col1), min(col2),....
FROM photos
INNER JOIN mobilehomes ON mobilehomes.community_id = 1
AND photos.photoable_type = 'Mobilehome'
AND photos.photoable_id = mobilehomes.id
AND photos.image_file_size IS NOT NULL
AND photos.is_published IS TRUE
GROUP BY photos.photoable_id
ORDER BY photos.priority ASC
Looking to your code seems also that you could avoid the subquery

WorkbenchJ - Error: aggregates not allowed in GROUP BY clause

I found a few other threads with this error message on the site but the solutions there did not seem to work for me.
This is the query I am trying to run:
SELECT
o.name as Name,
o.vrank_tav__c as Vrank,
COUNT(c.enterprise_id) AS #_users_enterprise
FROM
(community_csv_james c JOIN
salesforce_data_opportunity o ON
c.enterprise_id = o.enterprise_id__c)
GROUP BY #_users_enterprise, Name, Vrank
ORDER BY #_users_enterprise DESC;
When I run it on SQL Workbench J, I get the following error:
SELECT
o.name as Name,
o.vrank_tav__c as Vrank,
COUNT(c.enterprise_id) AS #_users_enterprise
FROM
(community_csv_james c JOIN
salesforce_data...
ERROR: aggregates not allowed in GROUP BY clause
I've tried a few variations of this but I that promoted different error messages. How should I write this query?
Thanks!
You are not supposed to include the results from your aggregate function (your Count()) in your group by. The count is going to be associated with a distinct name/Vrank so you would only need to group on those. That's why it's giving you that specific error.
GROUP BY Name, Vrank
MySQL documentation for GROUP BY

Prestashop - List view filter

I insert an other column in the list view of my module inserting the values with getList function, I modified the sql to filter in the renderList function but I can't use the alias in where clause.
How can I fix it?
The error i got is the next:
Uncaught Unknown column 'product_supplier_name' in 'where clause'<br /><br />
SELECT SQL_CALC_FOUND_ROWS a.* , s.name AS product_supplier_name FROM ps_supplier_bill a LEFT JOIN ps_supplier s ON s.id_supplier = a.id_product_supplier WHERE 1 AND product_supplier_name LIKE '%fa%' ORDER BY product_supplier_name asc LIMIT 0,50
The proper query should be this:
SELECT SQL_CALC_FOUND_ROWS a.* , s.`name` AS product_supplier_name FROM `ps_supplier_bill` a LEFT JOIN `ps_supplier` s ON s.`id_supplier` = a.`id_product_supplier` WHERE 1 AND s.`name` LIKE '%fa%' ORDER BY s.`name` asc LIMIT 0,50
It's not possible use directly an alias in WHERE, because chronologically, WHERE happens before SELECT, which always is the last step in the execution chain. REFER
From MySQL doc:
Standard SQL disallows references to column aliases in a WHERE clause. This restriction is imposed because when the WHERE clause is evaluated, the column value may not yet have been determined.
MySQL doc

MySQL. Queries. Unknown column [duplicate]

This question already has answers here:
Unknown Column In Where Clause
(16 answers)
Closed 8 years ago.
I have this following query and I want to display the results where masini > 2 but when I run the query it says that 'masini' is not an existing column but it's the name of a custom column I defined on the first row. I am new to MySQL.. can anyone point me in the right direction?
This is my query:
SELECT pers.serie_buletin AS persoana, COUNT(prop.serie_buletin) AS masini
FROM persoana pers
JOIN proprietate prop
ON pers.id_persoana = prop.serie_buletin
WHERE masini > 2
GROUP BY persoana ;
I defined the column on this line, in this part "COUNT(prop.serie_buletin) AS masini" but it says "Error Code: 1054. Unknown column 'masini' in 'where clause'". What am I missing?
Change WHERE to HAVING.
GROUP BY persoana
HAVING masini > 2;
The MySQL HAVING clause is used in the SELECT statement to specify
filter conditions for group of rows or aggregates.
The MySQL HAVING clause is often used with the GROUP BY clause. When
using with the GROUP BY clause, you can apply a filter condition to
the columns that appear in the GROUP BY clause. If the GROUP BY clause
is omitted, the MySQL HAVING clause behaves like the WHERE clause.
Notice that the MySQL HAVING clause applies the condition to each
group of rows, while the WHERE clause applies the condition to each
individual row.
source
The where clause is evaluated first, so MySQL don't know what is masini there. Here are some similar questions.
Getting unknown column error when using 'as' in mysql statement
Unknown Column In Where Clause
As explained in the questions above and another answers here, you can only use alias from sub-queries, or in clauses that are evaluated after the alias is assigned as ORDER BY, GROUP BY or HAVING, in your case you can use the having clause.
SELECT pers.serie_buletin AS persoana,
COUNT(prop.serie_buletin) AS masini
FROM persoana pers
JOIN proprietate prop
ON pers.id_persoana = prop.serie_buletin
GROUP BY pers.serie_buletin
HAVING COUNT(prop.serie_buletin) > 2;
You can't put column aliases in the where clause. Ever.
In this case, though, you actually need a having clause:
SELECT pers.serie_buletin AS persoana, COUNT(prop.serie_buletin) AS masini
FROM persoana pers
JOIN proprietate prop
ON pers.id_persoana = prop.serie_buletin
GROUP BY persoana
HAVING masini > 2;