Solved

view union statement difference in quantity review

Posted on 2013-06-16
7
313 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
ID: 39252265
Can you show different results in a union view?
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39252305
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
ID: 39252319
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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 

Author Comment

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

Accepted Solution

by:
PortletPaul earned 500 total points
ID: 39252349
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
ID: 39252370
Great Response
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39252384
v.happy! - thank you. Cheers, Paul
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
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…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

821 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