i have the following sql query
SELECT P.catalogid,
Sum(numitems) numitems,
Sum(ignoreditems) ignoreditems,
P.supplierid,
P.cname,
P.cprice,
P.cstock,
P.ccode,
P.minstock,
P.pother4,
P.processedsucssessfully,
P.notprocessed,
Y.backorder
INTO ##tempt
FROM ##temporderstable P
JOIN supporder Y
ON P.catalogid = Y.catalogid
GROUP BY P.catalogid,
P.supplierid,
P.cname,
P.cprice,
P.cstock,
P.ccode,
P.minstock,
P.pother4,
Y.backorder,
P.processedsucssessfully,
P.notprocessed
i am trying to save the result of join into another temporty table, but when i try to access the new table from another query it says invalid object name, is this the right way to save the result of this query to ##tempt?
Try using explicit temp table declaration and then check!!
Like this
CREATE TABLE ##tempt (
supplierid int,
other fields ..)
Related
I have 2 tables and want to create a third one when ta asin_id and tb asin have the same value,
with the
SELECT * FROM ta JOIN tb ON ta.asin_id = tb.asin;
i can have the view from the third table but i cant create it.
i already tried this:
CREATE TABLE tc AS SELECT * FROM ta JOIN tb ON ta.asin_id = tb.asin;
ERROR: syntax error at or near "tc"
LINE 1: CREATE tc AS SELECT * FROM items JOIN bs_audiotv ON items.as...
i attached a pic from the shell here: https://photos.app.goo.gl/LLjHi2wn9WQhxXkR8
CREATE T is not a right syntax. You have to use CREATE TABLE TABLE_NAME AS
When you are using CTAS you should specify which columns you need one by one. If same column name exists in your tables then you will receive an error again.
CTAS
Here is you script.
CREATE TABLE TC
AS
SELECT ITEMS.ASIN_ID,
ITEMS.TITLE,
ITEMS.BRAND,
ITEMS.DESCRIPTION,
ITEMS.CATEGORIES,
ITEMS.SPECIFICATIONS,
ITEMS.IMAGES,
BS_AUDIOTV.ASIN,
BS_AUDIOTV.LINK,
BS_AUDIOTV.PRICE_L,
BS_AUDIOTV.PRICE_U,
BS_AUDIOTV.PRICE
FROM ITEMS JOIN BS_AUDIOTV ON ITEMS.ASIN_ID = BS_AUDIOTV.ASIN;
I have a query which has a column (PROCESS) which was obtained using a concat function. Now i need to lookup this column in a column which is on another table (Table2) and then return a value from the same table (table 2).
Query:
My Current output will look like this.
I have a reference Table like this.
I need to lookup "Process" (Query Result) in "Type" (the reference Table) and return "Description" (Ref table) in "Process Column".
Final Output should look like this
I'm unable to figure out how to modify my query to do this. pls help.
You need a join. Join your reference table to your other query based on the concatenated value and you can display the description you are looking for.
FROM #Source As s) D INNER JOIN [Reference Table] AS rt ON d.Process = rt.type
You need an INNER JOIN between Source and Reference tables for your nested query with the JOIN condition as in the following :
SELECT ... -- all columns of your queries outer part
(
SELECT s.Date, s.Station, s.worktype, s.tasktype, description as process, ....
FROM source s
INNER JOIN reference r on ( concat(s.worktype,s.tasktype) = r.type )
) D
GROUP BY D.date, D.station, D.worktype, D.accountno;
SQL Fiddle Demo
I'm attempting to use the tables trip, guide and reservation in order to make a normalized table. I'm getting the error "ORA-00933: SQL command not properly ended."
I'm not certain what is wrong with ALTER TABLE.
SELECT trip.TRIP_ID, trip.State, trip.Max_grp_size, trip.type, trip.season, guide.Guide_num, guide.last_name, guide.First_name, guide.address, guide.city, guide.Hire_date, trip_guides.guide_num, reservations.trip_price
FROM trip
JOIN trip_guides ON trip.TRIP_ID = trip_guides.trip_id
JOIN Guide ON trip_guides.guide_num = guide.Guide_num
ALTER TABLE trip ADD (price_trip CHAR)
JOIN reservations ON trip.price_trip = reservations.trip_price
ORDER BY trip.trip_ID;
It sounds like what you want is a "CREATE TABLE ... AS (SELECT...)" type of statement. Essentially creating a NEW table based on the result set of a SQL statement.
This would look like:
CREATE TABLE your_1st_level_normalized_table AS
(
SELECT trip.TRIP_ID,
trip.STATE,
trip.Max_grp_size,
trip.type,
trip.season,
guide.Guide_num,
guide.last_name,
guide.First_name,
guide.address,
guide.city,
guide.Hire_date,
trip_guides.guide_num,
reservations.trip_price,
NULL as PRICE_TRIP /*new field in the new table set to NULL*/
FROM trip
JOIN trip_guides ON trip.TRIP_ID = trip_guides.trip_id
JOIN Guide ON trip_guides.guide_num = guide.Guide_num
JOIN reservations ON trip.price_trip = reservations.trip_price
);
I've removed the ORDER BY since that's not allowed in this statement.
I am using MS Access 2010. I have inner joined two tables (2 columns from one table, and all columns from the other) and would like to use an alias on one of the columns from the table that I select all the columns. Here as an example of the current SQL
SELECT
Cars.brand, Cars.owner, *
FROM
Cars INNER JOIN Inspections ON Cars.vin = Inspections.vin
Right now when I run this, the vin column is returned as 'Inspections.vin'. I would like it to just read 'vin'. Is this possible?
Just add table name, currently you select all columns from both tables:
SELECT
Cars.brand, Cars.owner, Inspections.*
FROM
Cars INNER JOIN Inspections ON Cars.vin = Inspections.vin
You can only assign an alias to columns that are explicitly included in your select statement. Otherwise, all those within the all part of your query will return its original column name
SELECT
Cars.brand, Cars.owner, [vin] = Cars.vin, *
FROM
Cars INNER JOIN Inspections ON Cars.vin = Inspections.vin
I think this is what you're looking for since you want all the columns from Inspections. You can't use * if you want aliases; instead you will need to specify every column you want from Inspections. In my example it's as if Inspections has only two variables; just add more Inpections.whatever until you have all the columns.
SELECT Car.brand, Cars.owner, Inspections.vin as vin, Inspections.othervar
FROM Cars
INNER JOIN Inspections
ON Cars.vin = Inspections.vin
Struggling with a create table statement which is based on this select into statement below:
#MaxAPDRefundAmount money = 13.00
...
select pkd.*, pd.ProviderReference, per.FirstName, per.Surname, #MaxAPDRefundAmount [MaxAPDRefundAmount],commission.Type [Commission] into #StagedData from CTE_PackageData pkd
inner join J2H.dbo.Package pk on pkd.Reference = pk.Reference
inner join J2H.dbo.Product pd on pk.PackageId = pd.PackageId
inner join J2H.dbo.FlightReservation fr on pd.ProductId = fr.ProductId
and fr.FlightBoundID = 1
inner join J2H.dbo.ProductPerson pp on pd.ProductId = pp.ProductID
and pp.StatusId < 7
inner join J2H.dbo.Flight f on fr.FlightId = f.FlightID
inner join J2H.dbo.Person per on pk.PackageId = per.PackageId
and per.PersonId = pp.PersonId
inner join J2H.dbo.PersonType pt on per.PersonTypeId = pt.PersonTypeID
We are changing a select into to just normal insert and select, so need a create table (we are going to create a temp (hash tag table) and not declaring a variable table. Also there is a pkd.* at the start as well so I am confused in knowing which fields to include in the create table. Do I include all the fields in the select statement into the create statement?
Update:
So virtually I know I need to include the data types below but I can just do:
create table #StagedData
(
pkd.*,
pd.ProviderReference,
per.FirstName,
per.Surname,
#MaxAPDRefundAmount [MaxAPDRefundAmount],
commission
)
"Do I include all the fields in the select statement into the create statement?" Well, that depends, if you need them, than yes, if not than no. It's impossible for us to say whether you need them... If you're running this exact query as insert, than yes.
As for the create statement, you can run the query you have, but replace into #StagedData with something like into TEMP_StagedData. In management studio you can let sql server build the create query for you: right-click the newly created TEMP_StagedData table in the object explorer (remember to refresh), script Table as, CREATE To and select New Query Editor Window.
The documentation of the CREATE TABLE statement is pretty straightforward.
No. Clearly, you cannot use pkd.* in a create table statement.
What you can do is run your old SELECT INTO statement as a straight SELECT (remove the INTO #stagedata) part, and look at the columns that get returned by the SELECT.
Then write a CREATE TABLE statement that includes those columns.
To create a table from a SELECT without inserting data, add a WHERE clause that never returns True.
Like this:
SELECT * INTO #TempTable FROM Table WHERE 1=0
Once the table with the columns for your SELECT, you can add additional columns with ALTER TABLE.
ALTER TABLE #TempTable ALL ExtraColumn INT
Then do your INSERT/SELECT.