473,748 Members | 2,227 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need help on Create View

Hi.

I created a simple view with the following statements:

CREATE VIEW dbo.VIEW1
AS
SELECT dbo.VIEW_ALL.ID ,
dbo.VIEW_ALL.Co de,
Another.dbo.OTH ER_VIEW.Label as SpecialCode
FROM dbo.VIEW_ALL LEFT OUTER JOIN
Another.dbo.OTH ER_VIEW ON
(dbo.VIEW_ALL.C ode + '_0') = Another.dbo.OTH ER_VIEW.Label

When I run :

select * from VIEW1 where code = 'abcde',

I would get all 10 rows that are of the same data where the code is
'abcde'. But what I thought it should return is really just one row
since the rest of the 9 rows are exactly the same! I know that I can
get my result if I use
select DISTINCT * from VIEW1 where code = 'abcde'

But, is there any way I could change my SQL statements(CREA TE VIEW)
above so that I automatically get 1 row instead of 10 rows of exactly
the same value without using DISTINCT?

Thank you for any help.
Jul 20 '05 #1
7 1785
kackson (ka*****@yahoo. com) writes:
I created a simple view with the following statements:

CREATE VIEW dbo.VIEW1
AS
SELECT dbo.VIEW_ALL.ID ,
dbo.VIEW_ALL.Co de,
Another.dbo.OTH ER_VIEW.Label as SpecialCode
FROM dbo.VIEW_ALL LEFT OUTER JOIN
Another.dbo.OTH ER_VIEW ON
(dbo.VIEW_ALL.C ode + '_0') = Another.dbo.OTH ER_VIEW.Label

When I run :

select * from VIEW1 where code = 'abcde',

I would get all 10 rows that are of the same data where the code is
'abcde'. But what I thought it should return is really just one row
since the rest of the 9 rows are exactly the same! I know that I can
get my result if I use
select DISTINCT * from VIEW1 where code = 'abcde'

But, is there any way I could change my SQL statements(CREA TE VIEW)
above so that I automatically get 1 row instead of 10 rows of exactly
the same value without using DISTINCT?


I'm afraid that you need to supply more information. There is no way
anyone can guess what's in your tables. The normal suggestion for this
type of question is that you proivde:

o CREATE TABLE statements for the involved tables.
o Sample data in form of INSERT statements.
o The expected result give the sample.
o A short narrative of what you want to achieve.

This can give you a tested solution. Providing table and data in other
forms may give you a solution, but probably untested. And not giving
any useful information at all, is not likely to help you at all.

--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #2
Hi.

If given 2 tables, say tableA and tableB. And 2 fields called
tableA.Code1 and tableB.code2. All data in tableA.Code1 are unique
while data in tableB.code2 are not unique (e.g. there could be 2
entries with data as "DEF"). How do I make a view so that when I list
a view that is composed by joining tableA and tableB, only unique
values are presented?

e.g. I want result of
tableA.Code1 | tableB.code2
-----------------------------
ABC | DEF
XYZ | OPQ

instead of

tableA.Code1 | tableB.code2
-----------------------------
ABC | DEF
ABC | DEF
XYZ | OPQ

Any help please?

Erland Sommarskog <es****@sommars kog.se> wrote in message news:<Xn******* *************** @127.0.0.1>...
kackson (ka*****@yahoo. com) writes:
I created a simple view with the following statements:

CREATE VIEW dbo.VIEW1
AS
SELECT dbo.VIEW_ALL.ID ,
dbo.VIEW_ALL.Co de,
Another.dbo.OTH ER_VIEW.Label as SpecialCode
FROM dbo.VIEW_ALL LEFT OUTER JOIN
Another.dbo.OTH ER_VIEW ON
(dbo.VIEW_ALL.C ode + '_0') = Another.dbo.OTH ER_VIEW.Label

When I run :

select * from VIEW1 where code = 'abcde',

I would get all 10 rows that are of the same data where the code is
'abcde'. But what I thought it should return is really just one row
since the rest of the 9 rows are exactly the same! I know that I can
get my result if I use
select DISTINCT * from VIEW1 where code = 'abcde'

But, is there any way I could change my SQL statements(CREA TE VIEW)
above so that I automatically get 1 row instead of 10 rows of exactly
the same value without using DISTINCT?


I'm afraid that you need to supply more information. There is no way
anyone can guess what's in your tables. The normal suggestion for this
type of question is that you proivde:

o CREATE TABLE statements for the involved tables.
o Sample data in form of INSERT statements.
o The expected result give the sample.
o A short narrative of what you want to achieve.

This can give you a tested solution. Providing table and data in other
forms may give you a solution, but probably untested. And not giving
any useful information at all, is not likely to help you at all.

Jul 20 '05 #3
On 7 Sep 2004 20:41:44 -0700, kackson wrote:
Hi.

If given 2 tables, say tableA and tableB. And 2 fields called
tableA.Code1 and tableB.code2. All data in tableA.Code1 are unique
while data in tableB.code2 are not unique (e.g. there could be 2
entries with data as "DEF"). How do I make a view so that when I list
a view that is composed by joining tableA and tableB, only unique
values are presented?

e.g. I want result of
tableA.Code1 | tableB.code2
-----------------------------
ABC | DEF
XYZ | OPQ

instead of

tableA.Code1 | tableB.code2
-----------------------------
ABC | DEF
ABC | DEF
XYZ | OPQ

Any help please?


Hi Kackson,

As Erland already indicated: "Providing table and data in other forms may
give you a solution, but probably untested".

This is an untested query:

SELECT A.Code1, B.code2
FROM tableA AS A
INNER JOIN (SELECT DISTINCT code2
FROM tableB) AS B
ON whatever your join criteria are
Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 20 '05 #4
On 7 Sep 2004 20:41:44 -0700, kackson wrote:
Hi.

If given 2 tables, say tableA and tableB. And 2 fields called
tableA.Code1 and tableB.code2. All data in tableA.Code1 are unique
while data in tableB.code2 are not unique (e.g. there could be 2
entries with data as "DEF"). How do I make a view so that when I list
a view that is composed by joining tableA and tableB, only unique
values are presented?

e.g. I want result of
tableA.Code1 | tableB.code2
-----------------------------
ABC | DEF
XYZ | OPQ

instead of

tableA.Code1 | tableB.code2
-----------------------------
ABC | DEF
ABC | DEF
XYZ | OPQ

Any help please?


Or (also untested)

SELECT DISTINCT A.Code1, B.code2
FROM tableA AS A
INNER JOIN tableB AS B
ON whatever your join criteria are

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 20 '05 #5
ka*****@yahoo.c om (kackson) wrote in message news:<90******* *************** ****@posting.go ogle.com>...
Hi.

I created a simple view with the following statements:

CREATE VIEW dbo.VIEW1
AS
SELECT dbo.VIEW_ALL.ID ,
dbo.VIEW_ALL.Co de,
Another.dbo.OTH ER_VIEW.Label as SpecialCode
FROM dbo.VIEW_ALL LEFT OUTER JOIN
Another.dbo.OTH ER_VIEW ON
(dbo.VIEW_ALL.C ode + '_0') = Another.dbo.OTH ER_VIEW.Label

When I run :

select * from VIEW1 where code = 'abcde',

I would get all 10 rows that are of the same data where the code is
'abcde'. But what I thought it should return is really just one row
since the rest of the 9 rows are exactly the same! I know that I can
get my result if I use
select DISTINCT * from VIEW1 where code = 'abcde'

But, is there any way I could change my SQL statements(CREA TE VIEW)
above so that I automatically get 1 row instead of 10 rows of exactly
the same value without using DISTINCT?

Thank you for any help.

How about using distinct in the create view statement?
Jul 20 '05 #6
ka*****@yahoo.c om (kackson) wrote in message news:<90******* *************** **@posting.goog le.com>...
Hi.

If given 2 tables, say tableA and tableB. And 2 fields called
tableA.Code1 and tableB.code2. All data in tableA.Code1 are unique
while data in tableB.code2 are not unique (e.g. there could be 2
entries with data as "DEF"). How do I make a view so that when I list
a view that is composed by joining tableA and tableB, only unique
values are presented?

e.g. I want result of
tableA.Code1 | tableB.code2
-----------------------------
ABC | DEF
XYZ | OPQ

instead of

tableA.Code1 | tableB.code2
-----------------------------
ABC | DEF
ABC | DEF
XYZ | OPQ

Any help please?

Erland Sommarskog <es****@sommars kog.se> wrote in message news:<Xn******* *************** @127.0.0.1>...
kackson (ka*****@yahoo. com) writes:
I created a simple view with the following statements:

CREATE VIEW dbo.VIEW1
AS
SELECT dbo.VIEW_ALL.ID ,
dbo.VIEW_ALL.Co de,
Another.dbo.OTH ER_VIEW.Label as SpecialCode
FROM dbo.VIEW_ALL LEFT OUTER JOIN
Another.dbo.OTH ER_VIEW ON
(dbo.VIEW_ALL.C ode + '_0') = Another.dbo.OTH ER_VIEW.Label

When I run :

select * from VIEW1 where code = 'abcde',

I would get all 10 rows that are of the same data where the code is
'abcde'. But what I thought it should return is really just one row
since the rest of the 9 rows are exactly the same! I know that I can
get my result if I use
select DISTINCT * from VIEW1 where code = 'abcde'

But, is there any way I could change my SQL statements(CREA TE VIEW)
above so that I automatically get 1 row instead of 10 rows of exactly
the same value without using DISTINCT?


I'm afraid that you need to supply more information. There is no way
anyone can guess what's in your tables. The normal suggestion for this
type of question is that you proivde:

o CREATE TABLE statements for the involved tables.
o Sample data in form of INSERT statements.
o The expected result give the sample.
o A short narrative of what you want to achieve.

This can give you a tested solution. Providing table and data in other
forms may give you a solution, but probably untested. And not giving
any useful information at all, is not likely to help you at all.


Hi kackson,

Can't you create a view with simple join instead of Left outer join to
get unique rows from both the tbls.

Thank you
Raju
Jul 20 '05 #7
kackson (ka*****@yahoo. com) writes:
If given 2 tables, say tableA and tableB. And 2 fields called
tableA.Code1 and tableB.code2. All data in tableA.Code1 are unique
while data in tableB.code2 are not unique (e.g. there could be 2
entries with data as "DEF"). How do I make a view so that when I list
a view that is composed by joining tableA and tableB, only unique
values are presented?

e.g. I want result of
tableA.Code1 | tableB.code2
-----------------------------
ABC | DEF
XYZ | OPQ

instead of

tableA.Code1 | tableB.code2
-----------------------------
ABC | DEF
ABC | DEF
XYZ | OPQ

Any help please?


You didn't seem to care about the advice I gave you. Look, if you insist
on posting vague questions, you will get vague answers back. Here is
a rewrite of you original query, but it is all guesswork on my part:
SELECT dbo.VIEW_ALL.ID ,
dbo.VIEW_ALL.Co de,
(dbo.VIEW_ALL.C ode + '_0') as SpecialCode
FROM dbo.VIEW_ALL
WHERE EXISTS (SELECT *
FROM Another.dbo.OTH ER_VIEW
WHERE (dbo.VIEW_ALL.C ode + '_0') =
Another.dbo.OTH ER_VIEW.Label)

This will not give the same result, because if there is no matching
row in OTHER_VIEW, then the row from VIEW_ALL will not be listed. But
you have not explained why you are using outer join anyway, so I'm
just guess that you were wrong in doing that.

Anyway, I say it again. Include:

o CREATE TABLE statements for the involved tables.
o Sample data in form of INSERT statements.
o The expected result give the sample.
o A short narrative of what you want to achieve.

This can give you a tested solution. Providing table and data in other
forms may give you a solution, but probably untested. And not giving
any useful information at all, is not likely to help you at all.

--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
55993
by: js | last post by:
I am trying to create a primary key constraint on a view in the following statement. However, I got an error ORA-00907: missing right parenthesis. If the CONSTRAINT clause is removed, then the view is created fine. Does anyone know how to creat Primary Key Constraint for a View? Thanks. CREATE OR REPLACE VIEW RPT_VW_WMN ( CARD, EC_D_CODE, EC_M_CODE, START_DATE, PR_NAME, PR_ID, ELIG, ANNUALY, MONTHLY, PENDING, /* ORA-00907: missing...
3
17993
by: M. Mehta | last post by:
It seems that you can not create a materialized view if you are using outer joins...can someone please verify this? Thanks M. Mehta Please follow my example below: created 2 tables:
9
2936
by: sk | last post by:
I have an applicaton in which I collect data for different parameters for a set of devices. The data are entered into a single table, each set of name, value pairs time-stamped and associated with a device. The definition of the table is as follows: CREATE TABLE devicedata ( device_id int NOT NULL REFERENCES devices(id), -- id in the device
2
2937
by: Mike Button | last post by:
Hello all, I am really really desperate on what I should do, and I am asking for help from anyone in this newsgroup, here's the situation: I am creating a form that is being run on a server where there is no scripts allowed running (the software is from Opentext called Livelink)- therefore I need javascript to do the tasks listed below: 1. validate the form - this has been completed 2. pop up another window that will go ahead and...
19
4107
by: James Fortune | last post by:
I have a lot of respect for David Fenton and Allen Browne, but I don't understand why people who know how to write code to completely replace a front end do not write something that will automate the code that implements managing unbound controls on forms given the superior performance of unbound controls in a client/server environment. I can easily understand a newbie using bound controls or someone with a tight deadline. I guess I need...
10
8821
by: Zack Sessions | last post by:
Has anyone tried to create a SQL7 view using the CREATE VIEW command and ADO.NET? If so, is there a trick in trapping a SQL error when trying to create the view? I have a VB.NET app that, amoung other things, can create views based on existing tables and a table of column name equivalents. If I accidently introduce a column name equivalent that is the same as another column, I end up with a CREATE VIEW SQL statement that is in error, if...
0
3958
by: U S Contractors Offering Service A Non-profit | last post by:
Brilliant technology helping those most in need Inbox Reply U S Contractors Offering Service A Non-profit show details 10:37 pm (1 hour ago) Brilliant technology helping those most in need Inbox Reply from Craig Somerford <uscos@2barter.net> hide details 10:25 pm (3 minutes ago)
20
4282
by: mike | last post by:
I help manage a large web site, one that has over 600 html pages... It's a reference site for ham radio folks and as an example, one page indexes over 1.8 gb of on-line PDF documents. The site is structured as an upside-down tree, and (if I remember correctly) never more than 4 levels. The site basically grew (like the creeping black blob) ... all the pages were created in Notepad over the last
4
1978
by: svgeorge | last post by:
I NEED TO COLLECT FROM THE GRIDVIEW(DATASELECTED) IN TO A TABLE(SelectedPayment) -------------------------------------------------------------------------------- How TO COLLECT THE ROWS CHECKED IN CHECK BOX IN THE DATASELECTED TO ANOTHER GRID VIEW ON CLICLING BUTTON I NEED TO COLLECT FROM THE GRIDVIEW(DATASELECTED) IN TO A TABLE(SelectedPayment) SIMILLAR TO HOTMAIL MODEL.....CHECK THE MAILS AND BRING THE CHECKED DATA TO ANOTHER PAGE
0
8991
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8831
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9326
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8245
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6076
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4607
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3315
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2787
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.