MSSQL Query should return same as MYSQL - sql-server

I've MYSQL Query and working fine
The QUERY IS:
select tst_type, count(tst_type) tot from tst_type where project='JupiQA';
The above Query returns single record If I'm adding GROUP BY tst_type in this query it returns multiple values.
The Query I tried in MSSQL without GROUP BY tst_type ,its showing Error
ERROR IS: Column 'tst_type.tst_type' is invalid in the select list
because it is not contained in either an aggregate function or the
GROUP BY clause.
After that added GROUP BY tst_type in MSSQL query,then working fine and returns multiple value.
But my requirement is it should return same as MYSQL without adding GROUP BY fn OR should return single value like MYSQL

MySQL allows this behavior, but I don't know of any other DBMS that lets it happen. If you want MySQL and MSSQL queries to behave the same, add the SQL Mode ONLY_FULL_GROUP_BY. MySQL will now throw the error properly.
Also check out this SO post:
Why does MySQL allow "group by" queries WITHOUT aggregate functions?

When doing a COUNT() as you're doing here, the GROUP BY is required to get the correct results. See the MySQL docs on the function
You can use a count(*) without any other fields to get a count of the total number of records. Otherwise, you must use GROUP BY.
IMHO your requirement for this query is backwards; aggregate functions require GROUP BY to behave properly/consistently when non-aggregate fields are included in the select. MSSQL is behaving properly, MySQL is not.

Related

RowMapper returns different column Ordering

I am implementing a row mapper to format the results from a specific query. When I query my DB through PG admin, or just using a simple jdbctemplate.queryforlist the columns return in one order but when running using my RowMapper it returns in a completely different order that is in no way related to how i run the other queries (example query: select * from blah.mytable). Any ideas why a RowMapper would cause the columns to return in a different order?
Thanks,
Brian
The issue is that I was using a HashMap instead of a LinkedHasMap when I returned my results.
Thanks,
Brian

PowerApps - Filtering SQL table on Collection Column

As the title says, I'm trying to filter a SQL table by the values in a collection. I'm attempting to use the Filter() function and an in formula to achieve this.
ClearCollect(NewCollection, Filter('SqlTable', ID in ExistingCollection.ID))
... where ID is a column belonging to 'SqlTable'.
I'm getting the error "Right side of 'in' operator is not a column name". My Collection 'ExistingCollection' has data in it, and the SqlTable also has data.
Possible issue may be the size of the Expenses table, it has 20k plus rows, but this filter should return a very small subset of that, ~200 rows. Am I running into the 5000 row limit here? Or is this kind of filtering not possible?
You can ignore the error message, as it is not correct. In any case it is not an error but a blue underline warning. This means that the query will not be delegated so that results will only come from the first 500 records of SqlTable.
You can test this by including in ExistingCollection.ID a value that is present in the first 500 records of SqlTable.
PowerApps documentation currently says that the in operator is delegated for SQL Server, but in fact it is not in the usage as a membership operator.
Please see point 5 of this reference.
The reference talks about Azure, but the same limitations apply to SQL Server.

Mule - Record cannot be mapped as it contains multiple columns with the same label

I need to do join query to MS SQL Server 2014 DB based on a column name value. The same query runs when doing query directly to DB, but when doing query through Mule I'm getting error. The query looks something like this :
SELECT * FROM sch.emple JOIN sch.dept on sch.emple.empid = sch.dept.empid;
The above query work fine while doing query directly to MS SQL Server DB, but gives the following error through mulesoft.
Record cannot be mapped as it contains multiple columns with the same label. Define column aliases to solve this problem (java.lang.IllegalArgumentException). Message payload is of type: String
Request you to please help me out.
Specify columns list directly:
SELECT e.<col1>, e.<col2>, ...., d.<col1>,...
FROM sch.emple AS e
JOIN sch.dept AS d
ON e.empid = d.empid;
Remarks:
You could use aliases instead of schema.table_name
SELECT * in production code in 95% cases is bad practice
The column that has duplicate is empid(or more). You could add alias for it e.empid AS emple_empid and d.empid AS dept_empid or just specify e.empid once.
To avoid specifying all columns manually, you could drag and drop them from object explorer to query pane like Drag and Drop Column List into query window.
Second way is to use plugin like Redgate Prompt to expand SELECT *:
Image from: https://www.simple-talk.com/sql/sql-tools/sql-server-intellisense-vs.-red-gate-sql-prompt/
Addendum
But the same query works directly.
It works because you don't bind them. Please read carefully link I provided for SELECT * antipattern and especially:
Binding Problems
When you SELECT *, it's possible to retrieve two columns of the same name from two different tables. This can
often crash your data consumer. Imagine a query that joins two
tables, both of which contain a column called "ID". How would a
consumer know which was which? SELECT * can also confuse views (at
least in some versions SQL Server) when underlying table structures
change -- the view is not rebuilt, and the data which comes back can
be nonsense. And the worst part of it is that you can take care
to name your columns whatever you want, but the next guy who comes
along might have no way of knowing that he has to worry about adding a
column which will collide with your already-developed names.
But the same query works directly.
by Dave Markle

Combining MSSQL results with Oracle into CFSPREADSHEET

I have a report that generates an excel file daily with data extracted from a MS-SQL database. I now have to add additional columns to my spreadsheet from an Oracle database where the ID matches the ID in the MS-SQL query results.
My problem is that I have about 1200-1400+ unique IDs generated on this report from the first query. When I plug them into an IN list with the Oracle query and try to do a CFDUMP to see if the results will come out as it should, I receive a CF error saying that query cannot list more than 1000 results from the oracle query.
I basically set the values from the first query into a valuelist for the ID column and then put that into the IN clause for the Oracle query. I then do a cfdump on the Oracle where I receive that error. I've also tried wrapping cfloop query = "firstquery"> around the Oracle query and just placing #firstquery.columnIDname# but that does not work either.
So two questions I have here is ..
How do I handle the limit on Oracle with 1k limit and if I only have read only access to the Oracle database with ColdFusion?
After #1 is figured out, how could I combine the results from the Oracle Query with my MSSQL query or in other words, add the columns I'm pulling from the Oracle query to the spreadsheet for the matching ID.
Thanks.
For your quick, dirty, and sub-optimal approach, visit cflib.org and look for a function called ListSplit(). It converts a long list to an array of short lists.
You then loop through this array and run a query each time. Make sure the query name changes with each loop iteration.
After the loop, do a query of queries union query. Then do whatever you have to do to combine that data with what you got from sql server.
Note that you will probably have to use array notation to access your dynamically named query objects.

Multiple query results

I am currently running a query in MS Access where i am searching for matching fields, the only problem is is that it is returning multiple fields which are all identical, is there anyway of limiting the results if the are identical?
What you need is DISTINCT.
Try something like this.
SELECT DISTINCT
table.*
FROM table

Resources