Creating a new column in a view

this may sound like a stupid question but within a view if i wanted to add a column that would contain numbers how would i do this, is it a cast function or something?

john
pepps11976Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
where do those "numbers" come from?
0
pepps11976Author Commented:
it would be something i would manually add
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
well, let's start with this "explanation".

say your view is like this:
create view your_view
as
select t.col1, t.col2 
from yourtable 

Open in new window


you could add a new column like this:
create view your_view
as
select t.col1, t.col2 
, 123 as new_col
from yourtable 

Open in new window


if this does not solve your problem, please try to explain with some data samples what you are trying to achieve
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

pepps11976Author Commented:
Ok this is my View
SELECT     TOP (100) PERCENT SUM(CASE WHEN dbo.itran.it_fcrate = 0 THEN (dbo.itran.it_quan * dbo.itran.it_price / 100) 
                      ELSE (dbo.itran.it_quan * dbo.itran.it_price / dbo.itran.it_fcrate / 100) END) AS [Total it_total_gbp], dbo.[Anal Code].ss_prod
FROM         dbo.[Anal Code] INNER JOIN
                      dbo.itran ON dbo.[Anal Code].ss_acode = dbo.itran.it_anal RIGHT OUTER JOIN
                      dbo.ihead ON dbo.itran.it_doc = dbo.ihead.ih_doc
WHERE     (dbo.itran.it_status = 'A') AND (dbo.itran.it_anal <= 'ZZZ') AND (dbo.itran.it_doc <= 'WOR') AND (dbo.ihead.ih_sorder <> ' ') AND (dbo.ihead.ih_orddate >= '07/01/2010') 
                      AND (dbo.ihead.ih_orddate <= '06/30/2011')
GROUP BY dbo.[Anal Code].ss_prod

Open in new window


and i would like to add another column call Forecast that would accept number values
0
Roman GhermanSenior Software EngineerCommented:
I think you want to return something like a RowID,
here is a link that could help you: http://extremedev.blogspot.com/2011/03/ms-sql-rowid.html

You will need to use ROW_NUMBER() OVER(ORDER BY SalesYTD DESC)

where inside (...) you will put the column(s) on which to generate that rowid
0
sachinpatil10dCommented:
try this

this will create a serial number column in the view
create view vw_ViewName
as
select col1, col2
row_number() over (order by col1,col2) srno
from TableName

Open in new window

0
Roman GhermanSenior Software EngineerCommented:

if you just want a column of type INT then:


SELECT     TOP (100) PERCENT SUM(CASE WHEN dbo.itran.it_fcrate = 0 THEN (dbo.itran.it_quan * dbo.itran.it_price / 100) 
                      ELSE (dbo.itran.it_quan * dbo.itran.it_price / dbo.itran.it_fcrate / 100) END) AS [Total it_total_gbp], dbo.[Anal Code].ss_prod,
CONVERT(INT, NULL) AS Forecast 
FROM         dbo.[Anal Code] INNER JOIN
                      dbo.itran ON dbo.[Anal Code].ss_acode = dbo.itran.it_anal RIGHT OUTER JOIN
                      dbo.ihead ON dbo.itran.it_doc = dbo.ihead.ih_doc
WHERE     (dbo.itran.it_status = 'A') AND (dbo.itran.it_anal <= 'ZZZ') AND (dbo.itran.it_doc <= 'WOR') AND (dbo.ihead.ih_sorder <> ' ') AND (dbo.ihead.ih_orddate >= '07/01/2010') 
                      AND (dbo.ihead.ih_orddate <= '06/30/2011')
GROUP BY dbo.[Anal Code].ss_prod

Open in new window

0
sachinpatil10dCommented:
change in you view
check if it works
SELECT     TOP (100) PERCENT SUM(CASE WHEN dbo.itran.it_fcrate = 0 THEN (dbo.itran.it_quan * dbo.itran.it_price / 100) 
                      ELSE (dbo.itran.it_quan * dbo.itran.it_price / dbo.itran.it_fcrate / 100) END) AS [Total it_total_gbp], dbo.[Anal Code].ss_prod
row_number() over (order by dbo.[Anal Code].ss_prod) newIdColumn                      
FROM         dbo.[Anal Code] INNER JOIN
                      dbo.itran ON dbo.[Anal Code].ss_acode = dbo.itran.it_anal RIGHT OUTER JOIN
                      dbo.ihead ON dbo.itran.it_doc = dbo.ihead.ih_doc
WHERE     (dbo.itran.it_status = 'A') AND (dbo.itran.it_anal <= 'ZZZ') AND (dbo.itran.it_doc <= 'WOR') AND (dbo.ihead.ih_sorder <> ' ') AND (dbo.ihead.ih_orddate >= '07/01/2010') 
                      AND (dbo.ihead.ih_orddate <= '06/30/2011')
GROUP BY dbo.[Anal Code].ss_prod

Open in new window

0
sachinpatil10dCommented:
Column type with int
SELECT     TOP (100) PERCENT SUM(CASE WHEN dbo.itran.it_fcrate = 0 THEN (dbo.itran.it_quan * dbo.itran.it_price / 100) 
                      ELSE (dbo.itran.it_quan * dbo.itran.it_price / dbo.itran.it_fcrate / 100) END) AS [Total it_total_gbp], dbo.[Anal Code].ss_prod
,0 newIdColumn                      
FROM         dbo.[Anal Code] INNER JOIN
                      dbo.itran ON dbo.[Anal Code].ss_acode = dbo.itran.it_anal RIGHT OUTER JOIN
                      dbo.ihead ON dbo.itran.it_doc = dbo.ihead.ih_doc
WHERE     (dbo.itran.it_status = 'A') AND (dbo.itran.it_anal <= 'ZZZ') AND (dbo.itran.it_doc <= 'WOR') AND (dbo.ihead.ih_sorder <> ' ') AND (dbo.ihead.ih_orddate >= '07/01/2010') 
                      AND (dbo.ihead.ih_orddate <= '06/30/2011')
GROUP BY dbo.[Anal Code].ss_prod

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pepps11976Author Commented:
sachinpatil10d

you code does allow me to add a column but i cannot manually add numbers into it
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
you cannot put values into a view's data.
you can only do that in some table, and query that into your view ...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.