I am running the query :
select
[temp_table_excel_insert_for_join].person_id,
[temp_table_excel_insert_for_join].city,
city.city_id
from
temp_table_excel_insert_for_join, city
where
temp_table_excel_insert_for_join.city = city.city
without problem now I want to have all these columns as a new table so I used
create table mytable as
( select
[temp_table_excel_insert_for_join].person_id,
[temp_table_excel_insert_for_join].city,
city.city_id
from
temp_table_excel_insert_for_join, city
where
temp_table_excel_insert_for_join.city=city.city)
but it did not work for me what should I do to make it happen? I don't want to create a view I want to have a table.but I am not familiar if I should do a left join or other things
If your goal is to create a new table mytable with the columns person_id, city, city_id then use the select ... into syntax:
select
[temp_table_excel_insert_for_join].person_id,
[temp_table_excel_insert_for_join].city,
city.city_id
into
mytable
from
temp_table_excel_insert_for_join
inner join
city on temp_table_excel_insert_for_join.city = city.city
Note that this will fail if you run it more than once as the table already would exist and you would have to drop it first.
See the documentation for more information on the into clause
Try this
select [temp_table_excel_insert_for_join].person_id
, [temp_table_excel_insert_for_join].city
,city.city_id
into mytable
from temp_table_excel_insert_for_join inner join city on temp_table_excel_insert_for_join.city=city.city)
Try separating the two operations. First:
CREATE TABLE MyTable ( etc)
Then
INSERT INTO MyTable ( <List of columns to insert> )
SELECT <Columns to insert>
FROM etc
WHERE etc
This works if you remove the brackets after your AS clause - at least in SQL Lite. So you can just write:
CREATE TABLE mytable AS
SELECT
[temp_table_excel_insert_for_join].person_id,
[temp_table_excel_insert_for_join].city,
city.city_id
FROM
temp_table_excel_insert_for_join, city
WHERE
temp_table_excel_insert_for_join.city=city.city;
Related
I am trying to write a database script to insert the data from a select.
but now there is an edit in database and I need to insert the same data but in two tables instead of one with foreign key.
Tables:
Membership: Id, Role, AccountName,...
MembershipMapper: Id, MembershipId, Role
I wrote this script:
INSERT INTO [server1].[dbo].[Memberships] ([Role], [AccountName] .....)
SELECT
[Role],
[AccountName],
......
FROM
[server2].[dbo].[Memberships]
but now the Role is in another table. How to insert it? Any ideas?
Possibly this?
INSERT INTO [server1].[dbo].[Memberships] ([Role], [AccountName] .....)
OUTPUT [ID], [Role] INTO MembershipMapper (MembershipId, Role)
SELECT
[Role],
[AccountName],
......
FROM
[server2].[dbo].[Memberships]
You'll probably need a JOIN:
INSERT INTO [server1].[dbo].[Memberships] ([Role], [AccountName] .....)
SELECT
s.[Role],
m.[AccountName],
......
FROM
[server2].[dbo].[Memberships] m
INNER JOIN
[server2].[dbo].[SomeOtherTable] s ON m.ID = s.ID -- or whatever links the two tables.....
Try something like this
INSERT INTO [server1].[dbo].[Memberships]
([Role]
,[AccountName]
.....)
SELECT memmap.[Role]
,mem.[AccountName]
......
FROM [server2].[dbo].[Memberships] AS mem
JOIN [server2].[dbo].[MembershipMapper] AS memmap
ON mem.id = memmap.MembershipId
I'm creating a view that references 3 tables. The view is from the dbo.LoadedFiles table, where I manually insert filenames and filetypes.
The other two are dbo.LandingPages and dbo.ExitPages. Both of those have a FOREIGN KEY reference column from dbo.LoadedFiles ID called LoadedFile_id.
I want the view to show which LoadedFile has not yet been imported into my dbo.LandingPages and dbo.ExitPages tables.
Here is my code thus far, I know it's wrong, but just so you guys have reference.
CREATE VIEW [dbo].[vw_FilesNotYetLoaded]
AS
SELECT
lf.ID,
filename,
filetype
FROM JPStarter.dbo.LoadedFiles lf
JOIN JPStarter.staging.ExitPages AS ep ON lf.ID = ep.LoadedFile_id
JOIN JPStarter.staging.LandingPages AS lp ON lf.ID = lp.LoadedFile_id
WHERE lf.ID NOT IN (
SELECT ID
FROM JPStarter.dbo.LoadedFiles
)
You don't need to join to the tables at all to give the desired result. Notice the UNION operator in the subquery.
CREATE VIEW [dbo].[vw_FilesNotYetLoaded]
AS
SELECT
[ID]
,[filename]
,[filetype]
FROM
[JPStarter].[dbo].[LoadedFiles]
WHERE
[ID] NOT IN (
SELECT
[LoadedFile_id]
FROM
[JPStarter].[staging].[ExitPages]
UNION
SELECT
[LoadedFile_id]
FROM
[JPStarter].[staging].[LandingPages]
);
I'm having some difficulty combining the following queries, so that the results display in one row rather than in multiple rows:
SELECT value FROM dbo.parameter WHERE name='xxxxx.name'
SELECT dbo.contest.name AS Event_Name
FROM contest
INNER JOIN open_box on open_box.contest_id = contest.id
GROUP BY dbo.contest.name
SELECT COUNT(*) FROM open_option AS total_people
SELECT SUM(scanned) AS TotalScanned,SUM(number) AS Totalnumber
FROM dbo.open_box
GROUP BY contest_id
SELECT COUNT(*) FROM open AS reff
WHERE refer = 'True'
I would like to display data from the fields in each column similar to what is shown in the image below. Any help is appreciated!
Tab's solution is fine, I just wanted to show an alternative way of doing this. The following statement uses subqueries to get the information in one row:
SELECT
[xxxx.name]=(SELECT value FROM dbo.parameter WHERE name='xxxxx.name'),
[Event Name]=(SELECT dbo.contest.name
FROM contest
INNER JOIN open_box on open_box.contest_id = contest.id
GROUP BY dbo.contest.name),
[Total People]=(SELECT COUNT(*) FROM open_option),
[Total Scanned]=(SELECT SUM(scanned)
FROM dbo.open_box
GROUP BY contest_id),
[Total Number]=(SELECT SUM(number)
FROM dbo.open_box
GROUP BY contest_id),
Ref=(SELECT COUNT(*) FROM open WHERE refer = 'True');
This requires the Total Scanned and Total Number to be queried seperately.
Update: if you then want to INSERT that into another table there are essentially two ways to do that.
Create the table directly from the SELECT statement:
SELECT
-- the fields from the first query
INTO
[database_name].[schema_name].[new_table_name]; -- creates table new_table_name
Insert into a table that already exists from the INSERT
INSERT INTO [database_name].[schema_name].[existing_table_name](
-- the fields in the existing_table_name
)
SELECT
-- the fields from the first query
Just CROSS JOIN the five queries as derived tables:
SELECT * FROM (
Query1
) AS q1
CROSS JOIN (
Query2
) AS q2
CROSS JOIN (...
Assuming that each of your individual queries only returns one row, then this CROSS JOIN should result in only one row.
There are my tables:
http://i.imgur.com/dzwokhh.png
I want to write a query that return all info order by categoryId and Name.
For example: I want to return from right table id = 2,15,18 (CategoryId=1)
because in the left table they belong to Java (Id=1)
This should help to solve the problem:
select *
from mytable1
join mytable2 on mytable1.ID=mytable2.CategoryID
order by mytable1.ID ,Name
I am trying to build a single select statement from two separate ones.
Basically I have a list of Names in a table which do repeat like so:
Name| Date
John 2014-11-22
John 2013-02-03
Joe 2012-12-12
Jack 2011-11-11
Bob 2010-10-01
Bob 2013-12-22
I need to do a Select distinct Name from Records which returns John, Joe, Jack, Bob.
I then want to so a Select on another table where I pass in the rows returned above.
SELECT Address, Phone From dbo.Details
WHERE Name = {Values from first SELECT query}
Having trouble with the syntax.
If you do not want to return any values from the subquery, you can use either IN or EXISTS
SELECT Address, Phone From dbo.Details
WHERE Name IN (SELECT DISTINCT Name FROM Records)
-- OR --
SELECT Address, Phone From dbo.Details D
WHERE EXISTS (SELECT 1 FROM Records R WHERE R.Name = D.Name)
(In most RDBMS the EXISTS is less resource intensive).
If you want to return values from the subquery, you should use JOIN
SELECT
D.Address,
D.Phone,
R.Name -- For example
FROM
dbo.Details D
INNER JOIN dbo.Records R
ON D.Name = R.Name
SIDENOTE These are sample queries, it is possible that you have to fine tune them to match your exact requirements.
You can use:
SELECT Address, Phone, name
FROM details
-- "in" is the difference from your first query, needed due to multiple values being returned by the subquery
WHERE name in (
SELECT distinct name
FROM namesTable
)
Additionally the following should work:
SELECT d.Address, d.Phone, n.name
FROM details d
inner join (
select distinct name
from namesTable
) n on d.name = n.name
So there are two ways you can go about doing this. One, create a temporary table and perform a join (*actually in retrospect you could also join to your second table as a subquery, or use something like a CTE if you're using SQL SERVER, but the modifications if you wanted to go that route should be pretty obvious)
CREATE TEMPORARY TABLE my_table AS
{your first select query};
SELECT Address, Phone From dbo.Details
INNER JOIN my_table AS mt
ON mt.name = dbo.name
Another option would be to perform an IN or EXISTS query using your select query
SELECT Address, Phone From dbo.Details
WHERE name IN (SELECT name from my_table)
Or, better yet (eg SQL Server IN vs. EXISTS Performance),
SELECT Address, Phone From dbo.Details
WHERE EXISTS (SELECT * from my_table WHERE my_table.name = dbo.name)
You might have to modify the syntax slightly, depending on if you are using MySQL or SQL Server (not sure about that later, honestly). But this should get you started down the right path
This will give you the names and their address and phone number:
SELECT DISTINCT N.Name, D.Address, D.Phone
FROM dbo.Details D INNER JOIN dbo.Names N ON D.Name = N.Name
When using a subquery that is not scalar (doesn't return only one value) in the where clause use IN and of course only one column in the subquery:
SELECT Address, Phone
From dbo.Details
WHERE Name IN (Select Name from Table)