Solved

sql server

Posted on 2014-04-07
9
249 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

734 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