INSERT INTO NEWTABLE
(Street,
Number,
NuDate,
XValue)
SELECT
a1.Street,
a2.Number,
a2.NuDate,
a2.XValue
FROM
ABC.dbo.Faculty a1 INNER JOIN
ABC.dbo.Faculty2 a2
ON a1.NameID = a2.NameID
WHERE
a1.Bologna = 'True'
GROUP BY
a1.Street,
a2.Number,
a2.NuDate,
a2.XValue
In this completely fictitious SQL statement, is the GROUP by needed to insert properly into NEWTABLE? and/or does the group by need to match up perfectly with the INSERT INTO for this statement to work properly?
EDIT: Sorry, I realized I had the wrong values for the GROUP BY statement, they're supposed to match the INSERT INTO
In this completely fictitious SQL statement, is the GROUP by needed to insert properly into NEWTABLE?
It's not necessary if you don't mind duplicates
If you don't want duplicate rows then yes, you'll need to use GROUP BY (or DISTINCT):
SELECT DISTINCT
a1.Street,
a2.Number,
a2.NuDate,
a2.XValue
does the group by need to match up perfectly with the INSERT INTO for this statement to work properly?
Yes, the selected columns must match.
There are cases when what you group by doesn't match what you select but that's when you aggregating:
SELECT
column1, -- no aggregation, must match
sum(column2) -- aggregation, so does not need to match
FROM a
GROUP BY column1
Related
I am trying to insert the number_of orders into storiacloud.schl_storia_school_status_try.The calculations are done in another table (storiacloud.vw_storia_oms_orders). The promblem is that it is trying to insert school_ucn as well but i am just using that for group by and do not want to insert that. Can somebody please help
INSERT INTO storiacloud.schl_storia_school_status_try
(no_of_orders)
select school_ucn,count(otc_order_number)
from storiacloud.vw_storia_oms_orders
group by school_ucn;
You can just remove it from the select:
INSERT INTO storiacloud.schl_storia_school_status_try (no_of_orders)
select count(otc_order_number)
from storiacloud.vw_storia_oms_orders
group by school_ucn;
It will still group, just not report it. I'm not sure what use a bunch of random numbers in a table is though.
... or add the missing column in the INSERT's list of columns:
INSERT INTO storiacloud.schl_storia_school_status_try
(no_of_orders, [YOUR_NB_ORDER_COLUMN] )
select school_ucn,count(otc_order_number)
from storiacloud.vw_storia_oms_orders
group by school_ucn;
I want to do something like this:
QSqlQuery q;
q.prepare("insert into Norm values(select from Disc id_disc WHERE name_disc=?, select from Spec code_spec WHERE name_spec=?,?");
q.addBindValue(MainModel->data(MainModel->index(MainModel->rowCount()-1, 1)).toString());
q.addBindValue(ui->comboBox->currentText());
q.addBindValue(MainModel->data(MainModel->index(MainModel->rowCount()-1, 2)).toString());
q.exec();
But it's not working. Surely for someone obviously where is the error and maybe he tells me how to do it right.
First of all your you have done spelling mistake. Its "INSERT" not "INCERT"
And yes we can insert SELECT query inside INSERT query.
eg:
INSERT INTO table2
(column_name(s))
SELECT column_name(s)
FROM table1;
INSERT ... SELECT ... is used when you want to insert multiple records, or when most values to be inserted come from the same record.
If you want to insert one record with values coming from several tables, you can use subqueries like you tried to do, but you have to use the correct syntax:
scalar subqueries must be written inside parentheses, and you must write the SELECT correctly as SELECT value FROM table:
INSERT INTO Norm
VALUES ((SELECT id_disc FROM Disc WHERE name_disc = ?),
(SELECT code_spec FROM Spec WHERE name_spec = ?),
?)
If you want data from two tables, you must first write a query which return pretended data - using JOIN, UNION, subqueries, ...
Then, just do
INSERT INTO target_table SELECT ...
I'm doing a data conversion between systems and have prepared a select statement that identifies the necessary rows to pull from table1 and joins to table2 to display a pair of supporting columns. This select statement also places blank columns into the result in order to format the result for the upload to the destination system.
Beyond this query, I will also need to update some column values which I'd like to do in a separate statement operation in a new table. Therefore, I'm interested in running the above select statement as a subquery inside a SELECT INTO that will essentially plop the results into a staging table.
SELECT
dbo_tblPatCountryApplication.AppId, '',
dbo_tblPatCountryApplication.InvId,
'Add', dbo_tblpatinvention.disclosurestatus, ...
FROM
dbo_tblPatInvention
INNER JOIN
dbo_tblPatCountryApplication ON dbo_tblPatInvention.InvId = dbo_tblPatCountryApplication.InvId
ORDER BY
dbo_tblpatcountryapplication.invid;
I'd like to execute the above statement so that the results are dumped into a new table. Can anyone please advise how to embed the statement into a subquery that will play nicely with a SELECT INTO?
You can simply add an INTO clause to your existing query to create a new table filled with the results of the query:
SELECT ...
INTO MyNewStagingTable -- Creates a new table with the results of this query
FROM MyOtherTable
JOIN ...
However, you will have to make sure each column has a name, as in:
SELECT dbo_tblPatCountryApplication.AppId, -- Cool, already has a name
'' AS Column2, -- Given a name to create that new table with select...into
...
INTO MyNewStagingTable
FROM dbo_tblPatInvention INNER JOIN ...
Also, you might like to use aliases for your tables, too, to make code a little more readable;
SELECT a.AppId,
'' AS Column2,
...
INTO MyNewStagingTable
FROM dbo_tblPatInvention AS i
INNER JOIN dbo_tblPatCountryApplication AS a ON i.InvId = a.InvId
ORDER BY a.InvId
One last note is that it looks odd to have named your tables dbo_tblXXX as dbo is normally the schema name and is separated from the table name with dot notation, e.g. dbo.tblXXX. I'm assuming that you already have a fully working select query before adding the into clause. Some also consider using Hungarian notation in your database (tblName) to be a type of anti-pattern to avoid.
If the staging table doesn't exist and you want to create it on insert then try the following:
SELECT dbo_tblPatCountryApplication.AppId,'', dbo_tblPatCountryApplication.InvId,
'Add', dbo_tblpatinvention.disclosurestatus .......
INTO StagingTable
FROM dbo_tblPatInvention
INNER JOIN dbo_tblPatCountryApplication
ON dbo_tblPatInvention.InvId = dbo_tblPatCountryApplication.InvId;
If you want to insert them in a specific order then use try using a sub-query in the from clause:
SELECT *
INTO StagingTable
FROM
(
SELECT dbo_tblPatCountryApplication.AppId, '', dbo_tblPatCountryApplication.InvId,
'Add', dbo_tblpatinvention.disclosurestatus .......
FROM dbo_tblPatInvention
INNER JOIN dbo_tblPatCountryApplication ON
dbo_tblPatInvention.InvId = dbo_tblPatCountryApplication.InvId
order by dbo_tblpatcountryapplication.invid
) a;
Try
INSERT INTO stagingtable (AppId, ...)
SELECT ... --your select goes here
I am confused about this query where it uses insert statement and then ignore and then select. Can someone explain me this? Thanks
INSERT IGNORE INTO
myTable
SELECT
$var1 AS `CMMNCTID`,
$var2 AS `ENCID`,
variableID,
ProductID,
CustomerID,
Age,
"" AS `myJ`,
"" AS `myI`
FROM
table2
JOIN
table3
ON
table2.ID= table3.ID
INSERT IGNORE in a keyword mysql: If the insert will cause a Primary key or Unique key error, it should skip that row.
INSERT ... SELECT: is used to copy data from another table.
http://dev.mysql.com/doc/refman/5.1/en/insert.html
The SELECT is a subquery. What the INSERT is doing is inserting whatever is contained in the join of table2 and table3, into myTable. The IGNORE tells it to ignore any entries for keys that already exist in myTable and to just insert whatever doesn't yet exist.
It seems the purpose of the query is to combine the data that is associated between table2 and table3 and dump it into myTable.
It takes results from the SELECT query on tables table2 and table3 and inserts the results into table myTable. IGNORE keyword simply tells mysql to ignore any errors that occur while executing the INSERT query.
I am attempting to use a case statement but keep getting errors. Here's the statement:
select TABLE1.acct,
CASE
WHEN TABLE1.acct_id in (select acct_id
from TABLE2
group by acct_id
having count(*) = 1 ) THEN
(select name
from TABLE3
where TABLE1.acct_id = TABLE3.acct_id)
ELSE 'All Others'
END as Name
from TABLE1
When I replace the TABLE1.acct_id in the THEN expression with a literal value, the query works. When I try to use TABLE1.acct_id from the WHEN part of the query, I get a error saying the result is more than one row. It seems like the THEN expression is ignoring the single value that the WHEN statement was using. No idea, maybe this isn't even a valid use of the CASE statement.
I am trying to see names for accounts that have one entry in TABLE2.
Any ideas would be appreciated, I'm kind of new at SQL.
First, you are missing a comma after TABLE1.acct. Second, you have aliased TABLE1 as acct, so you should use that.
Select acct.acct
, Case
When acct.acct_id in ( Select acct_id
From TABLE2
Group By acct_id
Having Count(*) = 1 )
Then ( Select name
From TABLE3
Where acct.acct_id = TABLE3.acct_id
Fetch First 1 Rows Only)
Else 'All Others'
End as Name
From TABLE1 As acct
As others have said, you should adjust your THEN clause to ensure that only one value is returned. You can do that by add Fetch First 1 Rows Only to your subquery.
Then ( Select name
From TABLE3
Where acct.acct_id = TABLE3.acct_id
Fetch First 1 Rows Only)
Fetch is not accepting in CASE statement - "Keyword FETCH not expected. Valid tokens: ) UNION EXCEPT. "
select name from TABLE3 where TABLE1.acct_id = TABLE3.acct_id
will give you all the names in Table3, which have a accompanying row in Table 1. The row selected from Table2 in the previous line doesn't enter into it.
Must be getting more than one value.
You can replace the body with...
(select count(name) from TABLE3 where TABLE1.acct_id = TABLE3.acct_id)
... to narrow down which rows are returning multiples.
It may be the case that you just need a DISTINCT or a TOP 1 to reduce your result set.
Good luck!
I think that what is happening here is that your case must return a single value because it will be the value for the "name" column. The subquery (select acct_id from TABLE2 group by acct_id having count(*) = 1 ) is OK because it will only ever return one value. (select name from TABLE3 where TABLE1.acct_id= TABLE3.acct_id) could return multiple values depending on your data. The problem is you trying to shove multiple values into a single field for a single row.
The next thing to do would be to find out what data causes multiple rows to be returned by (select name from TABLE3 where TABLE1.acct_id= TABLE3.acct_id), and see if you can further limit this query to only return one row. If need be, you could even try something like ...AND ROWNUM = 1 (for Oracle - other DBs have similar ways of limiting rows returned).