Solved

view union statement difference in quantity review

Posted on 2013-06-16
7
312 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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
Via a live example, show how to shrink a transaction log file down to a reasonable size.

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