Is group by 1 a standard SQL? - sql

Is group by 1 a standard SQL?
The SQL like this:
select c_name, count(1) as number from table_one group by 1
This SQL is right in MySQL. But I want to know that can it work in SQL server or Oracle? I do not have SQL server or oracle service, please help me.

Related

Count sessions from Oracle SQL to HQL

I want to translate the following Oracle SQL statement to HQL
select count(*) cnt from v$session where upper(machine) like upper(?)
I found this here in the net: https://docs.jboss.org/hibernate/orm/3.2/api/org/hibernate/stat/Statistics.html
But I don't get, how I can the exact same query as above

Solution to not being able to use ORDER BY in Partition in SQL Server 2008

I have the following query which works great on SQL Server 2012:
SELECT Name,
WeekNumber,
SUM(NumberOfSecondsWorked) AS totalDaily,
StartTime,
EndTime,
SUM(SUM(NumberOfSecondsWorked)) OVER (PARTITION BY WeekNumber ORDER BY EndTime) AS totalWeekly
FROM #temp AS T1
But unfortunately I get the following error when running this query on a SQL Server 2008 DB:
Incorrect syntax near 'order'.
My desired outcome of the above query is to add the NumberOfSecondsWorked by Day for each week. Here is my desired output:
But without the ORDER BY, I just get a total for each week without the increment by day:
Anybody know how to run the above query in SQL Server 2008? Or a mechanism to get the same result? Thanks!
SQL Server 2008 doesn't support cumulative sums. My recommendation is to upgrade to a supported version of SQL Server.
That said, you can implement this using a correlated subquery or lateral join (i.e. apply):
SELECT t.*,
(SELECT SUM(t2.totalDaily)
FROM #temp t2
WHERE t2.WeekNumber = t.WeekNumber AND
t2.EndTime <= t.EndTime
) as running_weekly
FROM #temp t2
I suggest you to read the following article : https://dba.stackexchange.com/questions/47116/in-microsoft-sql-server-2008-syntax-generates-the-error-the-parallel-data-ware
It explains that what you want to do is not supported until SQL2012.
You can use the workaround suggested by the collegue but the best solution is to migrate to sql server version that supports what you want to do.
Bye,
Simone

SQL DESC on two coulmns in Oracle 10g express edition user HR

Excuse me can anyone tell me how these two queries works?
1- SELECT last_name,department_id,salary FROM employees ORDER BY department_id,salary
2- SELECT last_name,department_id,salary FROM employees ORDER BY department_id,salary DESC
I'm trying to execute them on Oracle 10g Express Edition in user HR, the second one assumed to order the data according to the 'department_id' but it isn't !

How to do this select?

I must perform this operation under oracle 9 and sql server 9
I have on table with images inside and it can be quite heavy.
I want to select the ten first rows then the ten next etc... until the end of the table.
If possible I would like to have the same request to do that for oracle and sql server for maintenance reasons. I saw things like rownum and stuff but I didn't understand how it works and if it suits what I have.
Thx for your help.
You can do the following in both Oracle and SQL Server:
with cte as (
select t.*, row_number() over (order by id) as seqnum
from table t
)
select cte.*
from cte
where seqnum between X and Y;
You need to plug in the values of X and Y for each call.
This assumes that SQL Server 9 really means SQL Server 2005+. I don't recognize SQL Server 9 as a valid version (see here for a list of versions).

sql Fetch records from sql server database table in parts

I want to fetch records from a table in my sql server database in parts. Like, in one query I want to see first 1000 records, in next query next 1000 records. Likewise..
Is it possible with sql server ? I am using sql server 2008. While googling, I found LIMIT clause for mysql, but it does not work for sql server. So can any one give in Sql. Please help.
First 1000 records:
SELECT TOP 1000 *
FROM mytable
ORDER BY
mycolumn
General solution (supports offset)
SELECT *
FROM (
SELECT *,
ROW_NUMBER() OVER (ORDER BY mycolumn) rn
FROM mytable
) q
WHERE rn BETWEEN 1001 AND 2000
ORDER BY
mycolumn
Have also a look at T-sql: how to perform optimised Paging?