Copy SQL result into another table - entity-framework

I need some help. I'm trying to copy a tsql query result into another table. I was able to do it with the below tsql but I need to put some sort of check method to not copy a record if it already exist in the "PageControls" table.
INSERT INTO PageControls (UserId, PageId)
SELECT t1.UserId, t2.PageId FROM
aspnet_users t1, Pages t2
How can I accomplish this?
Thank you.

It looks like you're trying to populate the pagecontrols table with a cartesian product of users and pages. Assuming that's your goal, then you can add not exists to your query to exclude those already in the pagecontrols table:
INSERT INTO PageControls (UserId, PageId)
SELECT t1.UserId, t2.PageId
FROM aspnet_users t1, Pages t2
WHERE NOT EXISTS (
SELECT *
FROM PageControls p
WHERE p.userid = t1.userid and p.pageid = t2.pageid
)
SQL Fiddle Demo

Related

Use postgresql query results to form another query

I am trying to select from one table using the select result from another table. I can run this in two queries but would like to optimize it into just one.
First query.. Select ids where matching other id
select id from lookuptable where paid = '547'
This results in something like this
6316352
6316353
6318409
6318410
6320468
6320469
6320470
6322526
6322527
6324586
6324587
6326648
I would like to then use this result to make another selection. I can do it manually like below. Note, there could be many rows with these values so I've been using a IN statement
select * from "othertable" where id in (6316352,6316353,6318409,6318410,6320468,6320469,6320470,6322526,6322527,6324586,6324587,6326648);
select
ot.*
from
"othertable" as ot
join
lookuptable as lt
on
ot.id = lt.id
where
lt.paid = '547'
The IN operator supports not just value lists but also subqueries, so you can literally write
select * from "othertable" where id in (select id from lookuptable where paid = '547');

How to select unique?

I'm working on a live music database and I can't think of how to do a query that gets artists that have events on.
I have two tables: artist (containing id, name, description, etc.) and event_artist (many to many relationship) having artist_id and event_id columns.
return knex('event_artist')
.distinct('artist_id')
.join('artist', 'event_artist.artist_id', 'artist.id')
.select('*')
.offset(offset)
.limit(howMany)
This returns duplicate ids, which I don't want. How do I fix this?
You are looking for a query that selects artists rows that have rows in event_artist columns.
In SQL it can be written using exists
select
*
from
artists as a
where
exists (
select
*
from
event_artist as ea
where
ea.artist_id = a.id
)
In knex it can be written
knex('artists as a')
.whereExists(
knex
.select('*')
.from('event_artists as ea')
.whereRaw('ea.artist_id = a.id')
)
this should be left join not just Join-
return knex('event_artist')
.leftJoin('artist', 'event_artist.artist_id', 'artist.id')
.offset(offset)
.limit(howMany)

Specifice order to tables in postgres

I just created a temporary table as:
create temporary table userAndProductSales as
select p.p_name, u.u_name, u.s_price, u.quantity
from product p
join userAndStates u
on p.s_id = u.s_id
Now I want to select some columns with a particular order. For example, I want the select to give me an output of:
u_name1 p_name1
u_name1 p_name2
u_name1 p_name3
u_name1 p_name4
...
u_name2 p_name1
u_name2 p_name2
u_name2 p_name3
....
and so on and so forth. How do I get this ouput? I've tried something on the lines of:
select (select u_name from userandproductsales order by u_name), p_name from userandproductsales
but I'm getting an error
UPDATE: Figured out that the table I'm joining isn't giving me the correct data I want. Thanks for the help though.
Here is how to use ORDER BY :
SELECT * from userandstatesales
order by u_name , p_name
Unless there is a reason for creating a temporary table (like needing to access it later in the same session), you should avoid the expense and simply do a order by from your select. For example:
select p.p_name, u.u_name, u.s_price, u.quantity
from product p
join userAndStates u
on p.s_id = u.s_id
order by u.u_name, p.p_name;

t sql select into existing table new column

Hi I have a temp table (#temptable1) and I want to add a column from another temp table (#temptable2) into that, my query is as follows:
select
Customer
,CustName
,KeyAccountGroups
,sum(Weeksales) as Weeksales
into #temptable1
group by Customer
,CustName
,KeyAccountGroups
select
SUM(QtyInvoiced) as MonthTot
,Customer
into #temptalbe2
from SalesSum
where InvoiceDate between #dtMonthStart and #dtMonthEnd
group by Customer
INSERT INTO #temptable1
SELECT MonthTot FROM #temptable2
where #temptable1.Customer = #temptable2.Customer
I get the following: Column name or number of supplied values does not match table definition.
In an INSERT statement you cannot reference the table you are inserting into. An insert works under the assumption that a new row is to be created. That means there is no existing row that could be referenced.
The functionality you are looking for is provided by the UPDATE statement:
UPDATE t1
SET MonthTot = t2.MonthTot
FROM #temptable1 t1
JOIN #temptable2 t2
ON t1.Customer = t2.Customer;
Be aware however, that this logic requires the Customer column in t2 to be unique. If you have duplicate values in that table the query will seem to run fine, however you will end up with randomly changing results.
For more details on how to combine two tables in an UPDATE or DELETE check out my A Join A Day - UPDATE & DELETE post.
If I understand it correctly you want to do two things.
1: Alter table #temptable1 and add a new column.
2: Fill that column with the values of #temptable2
ALTER #temptable1 ADD COLUMN MothTot DATETIME
UPDATE #temptable1 SET MothTot = (
SELECT MonthTot
FROM #temptable2
WHERE #temptable2.Customer = #temptable1.Customer)

Iterating through a TVP before inserting records?

I'd like some help writing the following sproc:
I have SQL Server 2008 sproc that accepts two integer values (#ID1 and #ID2) and a data table/TVP.
The TVP table contains several fields, ie. Title and Description.
I want to iterate through the TVP table and check if the Title or Description already exists in my data table, tbl_Items, where #ID1 = tbl_Items.ID1 and #ID2 = tbl_Items.ID2.
If neither exist then insert the values of #ID1 and ID2 and that TVP row into tbl_Items.
Thanks.
Something like this?
INSERT INTO tbl_Items (ID1, ID2, Title, Description)
SELECT
#ID1, #ID2, TVP.Title, TVP.Description
FROM
#TVP AS TVP
WHERE
NOT EXISTS (SELECT * FROM tbl_Items AS I WHERE TVP.Title = I.Title AND TVP.Description = I.Description)
The requirement seems somewhat unclear but you should be able to use MERGE
;WITH Target As
(
SELECT *
FROM tbl_Items
WHERE ID1=#ID1 AND ID2=#ID2
)
MERGE
INTO Target
USING #TVP AS Source
ON Target.Title = Source.Title OR Target.Description = Source.Description
WHEN NOT MATCHED
THEN INSERT (ID1, ID2, Title, Description)
VALUES (#ID1, #ID2, Title, Description)