Solved

view union statement difference in quantity review

Posted on 2013-06-16
7
310 Views
Last Modified: 2013-06-17
Hi I need to pull out some information on the first statement and
more information on the second union statement

the way to do it would be

create view sample_vw
select as
order_no as [order no],
--a.branch_name as [branch name],
--c.phone as [mobile phone],
--salesperson as  [Salesperson Name]

union
select order_no as [order_no], a.branch_name as [Branch Name], c.phone as [mobile], salesperson as [Salesperson name].



would that be correct.
first union statement I only want to show the order number nothing else, second union name i want other information.

how can you get a statement not to display in the results?
0
Comment
Question by:Amanda Walshaw
  • 4
  • 3
7 Comments
 

Author Comment

by:Amanda Walshaw
Comment Utility
Can you show different results in a union view?
0
 
LVL 48

Expert Comment

by:PortletPaul
Comment Utility
a union (or union all) MUST use the same number of columns and every column must the be the same data type

This is more likely to work:
create view sample_vw
as

    select as
    order_no as [order no], convert(varchar,NULL) as branch_name , convert(varchar,NULL) as phone, convert(varchar,NULL) as salesperson
    ....
    union
    select order_no, a.branch_name, c.phone, salesperson

Open in new window

However this is quite an unusual way of presenting data in SQL - are you preparing a report?
0
 

Author Comment

by:Amanda Walshaw
Comment Utility
yes thank you for that, it is unusual but i have been asked to do it and ingrained in my head is for sql union view always to be the same columns etc
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:Amanda Walshaw
Comment Utility
Thank you that works, it is an unusual request.
0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points
Comment Utility
pleased it helped :)

2 tips

1. you might find using UNION ALL will perform better
(a UNION will remove "duplicate rows" whereas a "UNION ALL" does not do this so it is a bit faster)

2. you might have difficulty ensuring the rows produced by such a unioned query remain in the intended order. So one possibility is to provide a column to help with a logical sequence, e.g.
create view sample_vw
as

    select as
    1 as mySequence, order_no as [order no], convert(varchar,NULL) as branch_name , convert(varchar,NULL) as phone, convert(varchar,NULL) as salesperson
    ....
    union
    select 2, order_no, a.branch_name, c.phone, salesperson

Open in new window

here the first column mySequence (or whatever you call it) can be used in an order by clause to help put the "header row" at the top. Hope this makes sense.
0
 

Author Closing Comment

by:Amanda Walshaw
Comment Utility
Great Response
0
 
LVL 48

Expert Comment

by:PortletPaul
Comment Utility
v.happy! - thank you. Cheers, Paul
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

Suggested Solutions

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now