Solved

sql server

Posted on 2014-04-07
9
245 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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
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…
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
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.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

808 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