Solved

sql server

Posted on 2014-04-07
9
241 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Choosing SSD drives for SQL Server 32 77
MS SQL Backup 24 70
Date conversion in sql server 2012 6 25
Test a query 23 11
I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

708 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

12 Experts available now in Live!

Get 1:1 Help Now