Solved

view union statement difference in quantity review

Posted on 2013-06-16
7
316 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 

Author Comment

by:Amanda Walshaw
ID: 39252265
Can you show different results in a union view?
0
 
LVL 49

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

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

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 49

Expert Comment

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

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

623 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