How do I create table in SQL Server using group by query - sql-server

I'm using a query to create a new table in SQL Server using a select query.
My query is
create table test as
select id, sum(marks) as marks
from student
group by id
I'm getting error message in SQL Server. In Stackoverflow I found that instead of create table, SQL Server uses insert into. But I'm not getting how to write group by query using insert into. Can you please guide me?

Try this:
select id,sum(marks) as marks
INTO TEST
from student
group by id
Using INTO clause you are allowed to materialized the result in a table (it can be temporary table as well). There are few limitations - columns must have name (alias) and duplicate names are not allowed.

Select (columns Name or Functions (Max , Min , Sum ...) 1 , 2 , ...)
INTO (Destination table)
From (Source table)
GROUP BY (cloumns Name 1 , 2 , ...)

Related

How to get dynamically generated CREATE statement with SQL query?

In MySQL you can do SELECT CREATE TABLE .... and it will return the CREATE statement that was used to create this table. I need to do the same in SQL Server.
Is there any similar functionality in SQL Server? I have a table name test_table.
I need to run a SELECT statement that would return the CREATE TABLE string that was used to create this table. I tried this but it didn't work. How can I achieve this result in SQL Server?
You could try SELECT INTO and specify the name of a temporary table (prefixed with #). It also works with physical tables (although I've not found a use for this). Something like this
select '123' as colName into #newTable;
select * from #newTable;
colName
123

Same Queries returning different results in 2 different Oracle databases of same version

Table Creation
CREATE TABLE demo
(
id number(10) NOT NULL,
ct number(10) ,
CONSTRAINT id_pk PRIMARY KEY (id)
);
ROWS INSERTION
insert into demo(id,ct) values(1,4);
insert into demo(id,ct) values(2,2);
insert into demo(id,ct) values(3,3);
insert into demo(id,ct) values(4,2);
select * from demo
select q2.id,q2.ct from (
select a1.id id,(SELECT sum(ct) from demo a2
where a2.id = a1.id) ct
from demo a1 ) q2
group by q2.id
the above query is failing in one DB
ORA-00979: not a GROUP BY expression
00979. 00000 - "not a GROUP BY expression"
*Cause:
*Action: Error at Line: 1 Column: 14
In another db it is returning the results with counts
DB Version : 11.2.0.4.0
Both databases are running on same version. The results of the below query is same in both DBs
SELECT * FROM PRODUCT_COMPONENT_VERSION;
Group by should only by used with group functions (sum,count, avg etc).
Old databases engine used to accept group by as mean of order by , which is not the correct use.
The "group by" does not really have a meaning other than perhaps "order by"
The query is wrong. You need to apply an aggregate function on q2.ct (e.g. sum(q2.ct)) or include it in the group by clause.

How to treat unique constraint violations in a data migration script?

Here I'm trying to migrate some data from a table column to a brand new table in which destination column have an unique constraint. Basically I'm trying to:
INSERT INTO FooTable VALUES (SELECT BarTable.Code FROM BarTable)
FooTable have only 2 columns: ID and Code (column with unique constraint).
But on BarTable.Code, maybe there are some duplicate values that I need to treat and fit them in the new constraint (maybe: Code = Code + 1 or else).
Any ideas on how to do that?
I am using MS SQL Server 2008 R2.
Thank you in advance.
You can use the MERGE command and insert a different record when the codes match.
Here is an example based on your scenario:
MERGE FooTable AS T
USING BarTable AS S
ON (T.Code = S.Code)
WHEN NOT MATCHED BY TARGET
THEN INSERT(Code) VALUES(S.Code)
WHEN MATCHED
THEN INSERT(Code) VALUES(S.Code+1)
You can use Not Exists
INSERT INTO FooTable VALUES (SELECT distinct br.Code FROM BarTable br where NOT EXISTS(SELECT * FROM FooTable bs where br.code=bs.code ) )

Relation between tables in different databases using SQL Server 2005

is there any way to add a relation between 2 tables in different databases ?
For example:
db1.dbo.table1 field id = db2.dbo.table2 field id
the nearest thing that i´m looking for is something like:
CREATE TRIGGER RELATIONAL on IDCLIENT
FOR INSERT
As
IF (SELECT COUNT(*) FROM table1) = 1
INSERT INTO db2.dbo.table2(ID)
SELECT ID FROM table1
You can create a synonym for the other table in another db, and then you can reference it by its synonym name.

How do I retrieve data from SQL server table which has two columns say PatientID and TestNo, Differnt TestNo can have same patient ID

I have a database (SQL server 2005 Express) table wth columns PatientID (more than 1 records can have same patient ID) and TestNo. I want to retrieve the maximum of testNo column among all the records with same PatientID.What should be the SQL statement to do so?I am using RecordSet pointer to access the records in a vc++ application.
Use this SQL:
SELECT MAX(TestNo), PatientID
FROM dbo.YourTable
GROUP BY PatientID
The following query should do your work:
Select max(TestNo) as TestNo, PatientId from TableName group by PatientId
This will return you the max of the test no for each of the patient. You can add where condition if you need to take for a particular patient.

Resources