Solved

view union statement difference in quantity review

Posted on 2013-06-16
7
311 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
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 

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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Parse field in SQL View 15 98
SQL Query Conversion of IIF statement into CASE - Syntax issue 17 33
SQL Query stumper 3 37
Serach for record in Microsoft SQL Management Studio 8 20
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.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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 video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

867 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

21 Experts available now in Live!

Get 1:1 Help Now