I am trying to create new column by performing some addition, multiplication and subtraction with the values of the columns that is already created with the MS SQL table . In the below code, I created [PremiumByCodes] col. by doing the calculation. Likewise, when I try to create col. [Commission] , it shows me, invalid column name "PremiumBYCodes". I also wanted to do the same kind of multiplication to Deduction column and create a new column. Any suggestions, on how to proceed by doing this ?
select
[PremiumByCodes] = a.[NWP]/(1-c.[Commission%]-c.[Deduction%]),
[Commission] = [PremiumByCodes] /c.[Commission%],
[Deduction] = [PremiumByCodes] /c.[Deduction%],
This sure looks like SQL Server code.
First, databases just do not allow re-using column aliases in the same SELECT where they are defined. The normal approach is to use a subquery or CTE. Another approach uses lateral joins, which is implemented in SQL Server using APPLY:
select v.PremiumByCodes, v.PremiumByCodes / c.[Commission%] as Commission,
v.PremiumByCodes /c.[Deduction%] as Deduction,
. . .
from . . . outer apply
(values (a.[BHSINWP]/(1-c.[Commission%]-c.[Deduction%]) )) v(PremiumByCodes)
. . .
In sql-server, you can not reference a column by its alias except for in the order by of your query.
select
[PremiumByCodes] = a.[BHSINWP]/(1-c.[Commission%]-c.[Deduction%])
, [Commission] = (a.[BHSINWP]/(1-c.[Commission%]-c.[Deduction%]))/c.[Commission%]
, [Deduction] = (a.[BHSINWP]/(1-c.[Commission%]-c.[Deduction%]))/c.[Deduction%]
Related
We have a table, ProductHierarchy, which is set. The # of rows will not change. It has simple parent/child Product Hierarchy data in it.
We also have a Table-valued function which takes a ProductHierarchyId and then returns all rows where IsDescendantOf is true for that Id. Or in other words, it returns that row, plus all of its ancestors.
The problem is that with this Table-valued function, we have to use it with CROSS APPLY, and this is seriously slowing down the query.
My thought is to create either a second permanent table (or a temp table/table variable in the query in question) that has all possible results from the Table-valued function already in it. And then JOIN to that table instead of using the CROSS APPLY. In this table, we would add a column called something like QueryID. So instead of
CROSS APPLY dbo.GetProductLevels(P.ProductHierarchyId)
We could use
LEFT JOIN FutureTable ft ON ft.QueryId = P.ProductHierarchyId
I'm struggling with the query to create that table. Here's what I have so far...
SELECT
*
, 1 AS QueryId
FROM
dbo.ProductHierarchy
WHERE
(SELECT ProductHierarchyNode FROM dbo.ProductHierarchy WHERE ProductHierarchyId = 1).IsDescendantOf(ProductHierarchyNode) = 1
Ok, so that works great for the record where ProductHierarchyId = 1. But then I'd need to repeat that for ProductHierarchyId = 2:
SELECT
*
, 2 AS QueryId
FROM
dbo.ProductHierarchy
WHERE
(SELECT ProductHierarchyNode FROM dbo.ProductHierarchy WHERE ProductHierarchyId = 2).IsDescendantOf(ProductHierarchyNode) = 1
And then for 3, and then for 4, all the way to the last Id, doing a UNION each time, inside a loop -- which is hideous.
I KNOW there is a way to do this all in one query. Something like a recursive CTE. But my brain isn't getting there.
Wouldn't you just do this?
SELECT . . . -- the columns you want
INTO . . . -- where you want them
FROM dbo.ProductHierarchy ph CROSS APPLY
dbo.GetProductLevels(P.ProductHierarchyId);
I have this sql view, this is not my code:
SELECT
`view_combined_rev_data`.`date` AS `date`,
`view_combined_rev_data`.`book_title` AS `book_title`,
`view_combined_rev_data`.`marketplace` AS `marketplace`,
`view_combined_rev_data`.`amazon_kdp_avg_list_price` AS `amazon_kdp_avg_list_price`,
`view_combined_rev_data`.`amazon_kdp_royalty_type` AS `amazon_kdp_royalty_type`,
`view_combined_rev_data`.`amazon_kdp_revenue_in_usd` AS `amazon_kdp_revenue_in_usd`,
`view_combined_rev_data`.`amazon_kdp_royalty_in_usd` AS `amazon_kdp_royalty_in_usd`,
`view_combined_rev_data`.`amazon_kdp_paid_downloads` AS `amazon_kdp_paid_downloads`,
`view_combined_rev_data`.`amazon_kdp_free_downloads` AS `amazon_kdp_free_downloads`,
`view_combined_rev_data`.`amazon_ku_pages_read` AS `amazon_ku_pages_read`,
`view_combined_rev_data`.`amazon_ku_revenue_in_usd` AS `amazon_ku_revenue_in_usd`,
`view_combined_rev_data`.`create_space_revenue_in_usd` AS `create_space_revenue_in_usd`,
`view_combined_rev_data`.`create_space_royalty_in_usd` AS `create_space_royalty_in_usd`,
`view_combined_rev_data`.`create_space_paperbacks_sold` AS `create_space_paperbacks_sold`,
(
(
`view_combined_rev_data`.`amazon_kdp_revenue_in_usd` + `view_combined_rev_data`.`amazon_ku_revenue_in_usd`
) + `view_combined_rev_data`.`create_space_revenue_in_usd`
) AS `daily_total_revenue`,
(
(
`view_combined_rev_data`.`amazon_kdp_royalty_in_usd` + `view_combined_rev_data`.`create_space_royalty_in_usd`
) + `view_combined_rev_data`.`amazon_ku_revenue_in_usd`
) AS `daily_total_royalty`
FROM
`view_combined_rev_marketplace_data` `view_combined_rev_data`
My question is simple:
Why view_combined_rev_marketplace_data is used in this line. I can't find the code using it anywhere else, so can I simply remove it?
FROM
`view_combined_rev_marketplace_data` `view_combined_rev_data`
This is your FROM clause:
FROM `view_combined_rev_marketplace_data` `view_combined_rev_data`
The first name, view_combined_rev_marketplace_data is the name of a table or view (presumably a view) that exists in the database.
The second name, view_combined_rev_data, is a table alias. This is how the table/view is referred to in the query.
I recommend that you use table/view aliases that are abbreviations for the table/view name, something like this:
FROM `view_combined_rev_marketplace_data` vcrmd
Then the references to columns would look like:
SELECT vcrmd.`date` AS `date`,
vcrmd.`book_title` AS `book_title`,
. . .
And this would further simplify to:
SELECT vcrmd.`date`,
vcrmd.`book_title`,
. . .
The column alias (name given after the as) is unnecessary in this case, because it defaults to the column name. Note, though, that local coding styles may recommend having explicit column aliases.
FROM
`view_combined_rev_marketplace_data` `view_combined_rev_data`
These are not two views, but one view view_combined_rev_marketplace_data with an alias view_combined_rev_data.
When an alias is used to reference a table/view/function, then it must be used in the statement instead of the object's name. Usually aliases are meant to provide a shorter or more readable reference to the SQL object. In this case it is relatively long.
I would like to know if is it possible to use the clause "with as" with a variable and/or in a block begin/end.
My code is
WITH EDGE_TMP
AS
(select edge.node_beg_id,edge.node_end_id,prg_massif.longueur,prg_massif.lgvideoupartage,prg_massif.lgsanscable from prg_massif
INNER JOIN edge on prg_massif.asset_id=edge.asset_id
where prg_massif.lgvideoupartage LIKE '1' OR prg_massif.lgsanscable LIKE '1')
,
journey (TO_TOWN, STEPS,DISTANCE,WAY)
AS
(SELECT DISTINCT node_beg_id, 0, 0, CAST(&&node_begin AS VARCHAR2(2000))
FROM EDGE_TMP
WHERE node_beg_id = &&node_begin
UNION ALL
SELECT node_end_id, journey.STEPS + 1
, journey.DISTANCE + EDGE_TMP.longueur,
CONCAT(CONCAT(journey.WAY,';'), EDGE_TMP.node_end_id
)
It create a string as output separated by a ; but i need to get it back as variable or table do you know how? I used a concat to retrieve data in a big string. Can i use a table to insert data
,
A need to use the result to proceed more treatment.
Thank you,
mat
No, WITH is a part of an SQL statement only. But if you describe why you need it in pl/sql, we'll can advice you something.
Edit: if you have SQL statement which produces result you need, you can assign it's value to pl/sql variable. There are several methods to do this, simpliest is to use SELECT INTO statement (add INTO variable clause into your select).
You can use WITH clause as a part of SELECT INTO statement (at least in not-too-very-old Oracle versions).
I am retreiving values from a Table which in turn i am comparing with values from another table using the SQL keywords 'EXCEPT'
My query looks something like follows
SELECT DISTINCT TDC_TREE_FAMILY_CLASSIFICATION AS DPC_Level1,
TDC_TREE_CLASSIFICATION AS DPC_Level2,
TDC_TREE_SUB_CLASSIFICATION AS DPC_Level3
FROM TD_DATA_PACK_CONTENTS
EXCEPT
SELECT DPC_Level1,DPC_Level2,DPC_Level3 FROM DATA_PACK_CATEGORIES
ORDER BY DPC_Level1
Now this query works fine . What i want to do is save the results in a single string variable.
So I declare 3 temps variables to save the values of DPC_Level1,Lvl2,Lvl3 and then i can join them into a single string variable.
So i modify my Query like this.
SELECT DISTINCT #m_DPC_Level11=TDC_TREE_FAMILY_CLASSIFICATION AS DPC_Level1
,#m_DPC_Level2=TDC_TREE_CLASSIFICATION AS DPC_Level2,
,#m_DPC_Level13=TDC_TREE_SUB_CLASSIFICATION AS DPC_Level3
FROM TD_DATA_PACK_CONTENTS
EXCEPT
SELECT DPC_Level1,DPC_Level2,DPC_Level3 FROM DATA_PACK_CATEGORIES
ORDER BY DPC_Level1
But this throws the error
'A SELECT statement that assigns a value to a variable must not be
combined with data-retrieval operations'
. How i resolve this issue. I am using SQL Server 2008
I would go for a subquery
select #m_DPC_LEvel11 = DPC_Level1,
#m_DPC_Level2 = DPC_Level2,
#m_DPC_Level13 = DPC_Level3,
FROM
(SELECT DISTINCT TDC_TREE_FAMILY_CLASSIFICATION AS DPC_Level1,
TDC_TREE_CLASSIFICATION AS DPC_Level2,
TDC_TREE_SUB_CLASSIFICATION AS DPC_Level3
FROM TD_DATA_PACK_CONTENTS
EXCEPT
SELECT DPC_Level1,DPC_Level2,DPC_Level3 FROM DATA_PACK_CATEGORIES
ORDER BY DPC_Level1) s
I am trying to take the results of a select query in SQL and place them in another table in a different database. The table structure is identical. The select query is as follows;
USE Warwick
Go
Select tblOperations.Link, Project.*
From tblOperations
Inner Join Warwick.dbo.Project
On tblOperations.Link= Warwick.dbo.Project.[Project ID]
Where tblOperations.Job# = Warwick.dbo.Project.[Job Number] and
tblOperations.[Status] = 'Active' or tblOperations.[Status] = 'Pending'
The join lets me select just the jobs that are considered active. I need to copy the results into the table WCI_DB.dbo.Project, which already exists. I would lke to append and not overwrite if the record exists.
Any help would be appreciated.
Thanks.
You should tag your question with the database, which seems to be SQL Server. The SQL syntax is insert:
insert into WCI_DB.dbo.Project
<your select here>;
Normally, you want to list columns after the table name:
insert into WCI_DB.dbo.Project(list of columns>
<your select here>;
However, if this is a one-time exercise and you know the columns are the same, then it is small sin to omit them once.
To create a new table, using select into, which is documented here.
select . . .
into WCI_DB.dbo.Project
. . .