Solved

sql server

Posted on 2014-04-07
9
244 Views
Last Modified: 2014-08-29
Table
Location      Branch
19      0
19      0
21      0
21      0
22      0
22      0
22      101
22      101
 

Please help to write an sql from table 1 to get the result

Result needed
Location      Branch
19      0
19      0
21      0
21      0
22      101
22      101

Drop null branch values if branch has a non null value.
0
Comment
Question by:swpa_wnt
9 Comments
 
LVL 35

Accepted Solution

by:
Terry Woods earned 250 total points
ID: 39984883
Is this for MySQL or SQL Server? The question title and selected Topic Area don't match.

Are there Branch values that are actually null, as your data seems to show them as 0?

Anyway, this might work for you:

select *
from my_table as m
where Branch is not null
or not exists (select 1 from my_table where Location=m.Location and Branch is not null)

Open in new window

0
 
LVL 10

Expert Comment

by:HuaMinChen
ID: 39984958
Try
select *
from tab1
where (location,branch) in(select location,max(branch)
from tab1
group by location)

Open in new window

0
 

Author Comment

by:swpa_wnt
ID: 39985980
Thanks to both for our replies . Will try them and let you know. This is for Sql server.
0
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.

 
LVL 1

Assisted Solution

by:MartinChadderton
MartinChadderton earned 250 total points
ID: 40137964
Slightly convoluted but:

Select location,branch
INTO #tmp
from <yourtable>

Delete from #tmp
where branch = 0 and location in (select location from #tmp where branch > 0)

Select location,branch
from #tmp
order by location
0
 

Author Comment

by:swpa_wnt
ID: 40271343
I've requested that this question be deleted for the following reason:

Hi,

It appears our account was used to post this question, but we cannot locate the asker.  I do not know if the experts' suggestions worked, or if a different solution was found, or even if the question became moot.  It's also possible the asker is no longer here.  I apologize for the inconvenience.
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 40271344
I think it's more appropriate to just accept a solution here.
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 40278451
I recommend a split between http:#a39984883 and http:#a40137964

I'm reasonably sure the technique used in the other given solution (by HuaMinChen) would not work, as I don't think you can use IN with multiple columns. Someone please correct me if I'm wrong.
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

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
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…
Viewers will learn how the fundamental information of how to create a table.

920 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

13 Experts available now in Live!

Get 1:1 Help Now