I am working on existing project which is developed on Laravel + Mysql. Now, I have migrated my database from Mysql to Sql server. So all the queries written in project using Mysql syntax.
Now, I have an query given below:
SELECT table_1.*, table_3.date as sem1date
FROM table_1, table_2, table_3
WHERE table_3.ID=table_2.DID AND block_datelu.BID=table_1.final_exam_date AND table_1.centreid=1234 AND table_1.course=1
GROUP BY table_1.start_date, table_1.end_date
When I am converting above query into sql server syntax, then sql server giving me error msg "Column 'table_1.Id' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause."
Can anyone tell me how can I use group by for multiple columns having joins?
Related
I have a Dblink in place between my Oracle database and Microsoft SQL Server, fetching data from SQL server while on Oracle. Connectivity is fine but I am getting invalid column names whenever I try to include any column name in my select query.
On Oracle with Dblink:
select COLUMN1 from table1#dblink
Fails with:
ORA-00904: COLUMN1: invalid identifier
On Oracle with Dblink, this works fine.
select "COLUMN1" from table1#dblink
On SQL server this works fine:
select column1,COLUMN1 from table1
Seems case sensitivity is not enforced when on SQLserver, however, while trying from oracle it is enforced, any idea how I can bypass this,
The collation on my SQL server is SQL_Latin1_General_CP1_CI_AS.
I am using MS SQL Server Version 2008 and MariaDB 10.5.
My table and data :
Following is the query i am running on both the databases.
select distinct distributor from market order by city
Found this query is failing in MS SQL Server database with error
SQL Error [145] [S0001]: ORDER BY items must appear in the select list if SELECT DISTINCT is specified.
Understood the reason why it failing
Reason for - ORDER BY items must appear in the select list if SELECT DISTINCT is specified
when i ran the same query run with MariaDB it ran successful without any error and gave me below output.
So i have doubt why MariaDB is behave different here? ideally it should fail right?
Thanks in advance.
Please try this query
Select distinct col from (
Select col from table order by col
)
I linked an Oracle Database to my SQL Server in Microsoft SQL Server Management Studio 18. Server Objects -> Linked Servers.
I have a SQL Statement that when I run on the Oracle Developer Tool/Platform it returns the information as expected. But when I run the exact same query on the SQL Server it returns the incorrect results (The actual values in the rows and columns do not match).
What I know.
The table I am query in lives in the Oracle Database.
I can get the same/matching results on the Oracle Developer and SQL Server if I exclude in my WHERE statement anything involving a DATE.
Any thoughts?
The example of the query below.
Works on Oracle Developer but not on MSSQL
SELECT * FROM TABLE1
WHERE status = 'Deviation' and trunc(SRC_ROW_UPDT) BETWEEN TO_DATE('01/03/2020', 'DD/MM/YYYY') AND TO_DATE('10/12/2020','DD/MM/YYYY');
The example of the query below.
Works on both Oracle Developer and MSSQL
SELECT * FROM TABLE1
WHERE status = 'Deviation' and BATCHID = 'ThisBAtchID';
You cannot use ORACLE specific functions like TO_DATE in SQL Server calls. You have to execute them remotely using OPENQUERY. OPENQUERY in MSDN
SELECT * FROM OPENQUERY (OracleSvr, 'SELECT * FROM TABLE1
WHERE status = ''Deviation'' and trunc(SRC_ROW_UPDT) BETWEEN TO_DATE(''01/03/2020'', ''DD/MM/YYYY'') AND TO_DATE(''10/12/2020'',''DD/MM/YYYY'');');
I have successfully linked an Oracle 11g database to SQL Server 2008. I can run a simple query in SQL Server which displays Oracle data:
SELECT PRODUCT_CODE
FROM [ORACLE-LINK]..ORACLE_SCHEMA.PRODUCTS_TABLE
This does exactly what I would expect it to do.
The problem comes when I try to do a simple join:
SELECT ProductName, [ORACLE-LINK]..ORACLE_SCHEMA.PRODUCTS_TABLE.PRODUCT_NAME
FROM SqlServer_table
INNER JOIN [ORACLE-LINK]..ORACLE_SCHEMA.PRODUCTS_TABLE
ON SqlServer_table.Product_ID=[ORACLE-LINK]..ORACLE_SCHEMA.PRODUCTS_TABLE.PRODUCT_CODE
This causes a 'The multi-part identifier could not be bound' error on the Oracle part of the JOIN statement. I know from research that the syntax has to be exact, and I think I've tried almost every permutation. Perhaps there's something in the SQL Server settings/registry...
Following #shiva's helpful suggestion I reformatted the query with aliases. This threw a server collation mismatch error, which proved much more illuminating than the 'The multi-part identifier' message I had been getting earlier. It turns out the joined columns had different collations (SQL_Latin1_General_CP1_CI_AS vs Latin1_General_CI_AS). Adding a simple COLLATE to the end of the JOIN AS part of the query sorted this out.
Have you tried alias-ing the table names? Like so
SELECT SqlSvr.ProductName AS Sql_ProductName
, Orcl.PRODUCT_NAME AS Orcl_ProductName
FROM SqlServer_table AS SqlSvr
INNER JOIN [ORACLE-LINK]..ORACLE_SCHEMA.PRODUCTS_TABLE AS Orcl
ON SqlSvr.Product_ID = Orcl.PRODUCT_CODE
I'm using the following SQL to query 3 simple tables and pull back results. When I run this query in SQL Server 2005 EM (database that's being used), the correct recordset results are returned. When I execute this from my webpage (ASP) via an SQL statement or a stored procedure, I get blank values for some of the columns. Also, I noticed that when I change the order of the tables being selected in my FROM clause, some of the previous columns then return blank values. Is there something wrong with my SQL for it not to work when calling it? I developed my app locally and it works fine. When I deployed it to the client's network, the problems started... The client is running Win 2000 SP4 as their application server and my app is developed in ASP 3.0 with a SQL Server 2005 datastore.
SELECT
Scorecard_Measure.Measure,
Scorecard_Measure.Target,
Scorecard_Measure.YTD,
Scorecard_Measure.Status,
Scorecard_Measure.Explanation,
Scorecard_Measure.Division,
Scorecard_Measure.ZIndex,
Scorecard_Measure.LastUpdated,
Scorecard_Measure.ID,
Scorecard_Objectives.Details,
Scorecard_Objectives.ZIndex,
Scorecard_ObjectiveCats.IdentityLetter
FROM
[Scorecard_Measure],
[Scorecard_Objectives],
[Scorecard_ObjectiveCats]
WHERE
Scorecard_Measure.ObjID=Scorecard_Objectives.ID
AND Scorecard_Objectives.ObjCatID = Scorecard_ObjectiveCats.ID
AND Scorecard_Measure.FiscalYear = '2011'
AND Scorecard_Measure.Publish='Y'
ORDER BY Scorecard_Measure.LastUpdated DESC
Scorecard_Objectives.ID is a foreign key in the Scorecard_Measure table, Scorecard_ObjectiveCats.ID is a foreign key in the Scorecard_Objectives table.
Also, a weird occurence. I have two column names that are the same and when I reference those columns, the server isn't throwing an error like I've seen before saying "your results have columns with the same name, reference them using the tables they come from"... e.g. rs("Scorecard_Objectives.ZIndex") and rs("Scorecard_Measure.ZIndex") -- when I use these references, I get an error from IIS.
Any points is appreciated. Thanks in advance!
Access Both ZINDEX with different variables. Try this join query.
SELECT
Scorecard_Measure.Measure,
Scorecard_Measure.Target,
Scorecard_Measure.YTD,
Scorecard_Measure.Status,
Scorecard_Measure.Explanation,
Scorecard_Measure.Division,
Scorecard_Measure.ZIndex as MZIndex,
Scorecard_Measure.LastUpdated,
Scorecard_Measure.ID,
Scorecard_Objectives.Details,
Scorecard_Objectives.ZIndex as OZIndex,
Scorecard_ObjectiveCats.IdentityLetter
FROM
Scorecard_Measure INNER JOIN Scorecard_Objectives
ON Scorecard_Measure.ObjID=Scorecard_Objectives.ID
AND Scorecard_Measure.FiscalYear = '2011'
AND Scorecard_Measure.Publish='Y'
INNER JOIN Scorecard_ObjectiveCats
ON Scorecard_Objectives.ObjCatID = Scorecard_ObjectiveCats.ID
ORDER BY Scorecard_Measure.LastUpdated DESC