I need to combine two columns in SQL Server. However, I need to give one column priority.
Make | Model | Segment make | Segment model
--------+------------+--------------+--------------
Ferarri | California | Sport | Null
Ferarri | F40 | Sport | Null
Porsche | 911 | Sport | Null
Porsche | Cayenne | Sport | SUV
BMW | M5 | Null | Sport
I need a table with all the models and the segment of each car. All models have the segment in one of the two columns with segment. And, if there is a segment in both columns, would I like the segment for model to override the segment for the make, as in the example with the Porsche. This is the result I need:
Make | Model | Segment
--------+------------+--------
Ferarri | California | Sport
Ferarri | F40 | Sport
Porsche | 911 | Sport
Porsche | Cayenne | SUV
BMW | M5 | Sport
I have searched and found Rank(), but it does not seem to do what I want. Any suggestions?
Thanks in advance
Use coalesce(). It returns it's first non-NULL parameter:
select, Make, Model, coalesce(Segmentmodel, Segmentmake) as segment
from tablename
I'd recomment something like
select isnull(model,make) as segment
Because then you will always select the model, unless the model is empty. In that case it will select the make.
Try :
Select Make,Model,Coalesce([Segment model], [Segment make]) Segment From YourTable
select coalesce([Segment make], [Segment model]) as Segment
Related
I have a table (as table1)comes from HBase that has certain things that I would like to filter out. I have recreated the table, my SQL query, and the output I receive below. What happens is that when I try to filter out the string value it stays in the table, even if I want it out.
table1 ( some positions are fully capitalized some arent, want to make them all capitalized and filter out positions )
name | company | personal_id | position
Joe | Applebees| 32 | manager
Jack | Target | 12 | CLERK
Jim | Chipotle | 22 | COOK
Ron | Starbucks| 13 | barista
query
df = sqlContext.sql("select name, company, personal_id, UCASE(position) as position
from table1
where position != 'BARISTA'") #tried lower & upper case
Output Reieved
name | company | personal_id | position
Joe | Applebees| 32 | MANAGER
Jack | Target | 12 | CLERK
Jim | Chipotle | 22 | COOK
Ron | Starbucks| 13 | BARISTA /*dont want this output*/
Why did the row Ron | Startbucks| 13 | BARISTA not filter with my where clause?
try
where UCASE(position) != 'BARISTA'
Why are you grouping the result. there is no need to group the result until aggregate function is used. Try below query -
select name, company, personal_id, UCASE(position) as position
from table1
where upper(position) != 'BARISTA'
Product: Sybase ASE 11/12/15/16
I am looking to update a Stored Procedure that gets called by different applications, so changing the application(s) isn't an option. What is needed is best explained in examples:
Current results:
type | breed | name
------------------------------------
dog | german shepherd | Bernie
dog | german shepherd | James
dog | husky | Laura
cat | british blue | Mr Fluffles
cat | other | Laserchild
cat | british blue | Sleepy head
fish | goldfish | Goldie
What I need is for the First column's data to be cleared on duplicates. For example, the above data should look like:
type | breed | name
------------------------------------
dog | german shepherd | Bernie
| german shepherd | James
| husky | Laura
cat | british blue | Mr Fluffles
| other | Laserchild
| british blue | Sleepy head
fish | goldfish | Goldie
I know I can do a cursor, but there are around 10,000 records and that doesn't seem proficient. Looking for a select command, don't want to change the data in the database.
After mulling over this, I found a solution that would work and not use a cursor.
select Type,breed,name
into #DontDisplay
from #MyDataList as a1
group by breed
Having breed= (select max(name)
from #MyDataList a2
where a1.breed= a2.breed)
order by breed, name
select n.Type,d.Breed,d.Name
from #MyDataList as d
left join #DontDisplay as n
on d.Breed= n.Breed and d.Name= n.Name
order by Breed
Works great and the solution was based on another solution Sybase SQL Select Distinct Based on Multiple Columns with an ID
Quite basic, but I am stuck at the moment.
On an Informix database (no pivot option), I am searching for a dynamic way to transform the following table using SQL:
book | info | value
-----------------------------
Moby Dick | price | high
Moby Dick | stock | few
Hamlet | price | low
Hamlet | stock | many
Faust | price | medium
Faust | stock | normal
Resulting table:
book | price | stock
-----------------------------
Moby Dick | high | few
Hamlet | low | many
Faust | medium | normal
Thanks for your help!
You can aggregate based on CASE expression grouped by book. Try something like this.
SELECT book,
MAX(CASE WHEN info = 'price' THEN value END) as price,
MAX(CASE WHEN info = 'stock' THEN value END) as stock
FROM table1
GROUP BY book
I'm facing problem when trying to extract row that based on condition from another row.
Sample table:
GroupID | Name | Salary | Car
--------------------------------------------------------------------------------
9009 | Isaac | 10,000 | Honda
9009 | Ricky | | Nissan
9080 | Patrick | 20,000 | Ferrari
9080 | Susan | 30,000 | Nissan
Questions:
How should I query if I want to extract data like this:
GroupID | Name | Salary | Car
--------------------------------------------------------------------------------
9009 | Isaac | 10,000 | Honda
9080 | Patrick | 20,000 | Ferrari
based on condition car = Nissan?
Try this:
SELECT GroupID, Name, Salary, Car
FROM tableName
WHERE GroupID IN (SELECT GroupID
FROM tableName
WHERE Car = 'Nissan') -- << Your input
AND car <> 'Nissan'
Read more about subqueries here.
SELECT * FROM yourTable where Car <> 'Nissan';
Would seem to do what you asked for.
You need to be more explicit about what you are trying to achieve here.
Consider the following table structure with data -
AdjusterID | CompanyID | FirstName | LastName | EmailID
============================================================
1001 | Sterling | Jane | Stewart | janexxx#sterlin.com
1002 | Sterling | David | Boon | dav#sterlin.com
1003 | PHH | Irfan | Ahmed | irfan#phh.com
1004 | PHH | Rahul | Khanna | rahul#phh.com
============================================================
Where AdjusterID is the primary key. There are no. of adjusters for a company.
I need to have a query that will list single adjuster per company. i.e. I need to get the result as -
========================================================
1001 | Sterling | Jane | Stewart | janexxx#sterlin.com
1003 | PHH | Irfan | Ahmed | irfan#phh.com
========================================================
If any one could help me that will be great.
One way:
SELECT * FROM Adjusters
WHERE AdjusterID IN(SELECT min(AdjusterID)
FROM Adjusters GROUP BY CompanyID)
There are a handful of other ways involving unions and iteration, but this one is simple enough to get you started.
Edit: this assumes you want the adjuster with the lowest ID, as per your example
I know the answer from Jeremy is a valid one, so I will not repeat it. But you may try another one using a so called tie-breaker:
--//using a tie-breaker. Should be very fast on the PK field
--// but it would be good to have an index on CompanyID
SELECT t.*
FROM MyTable t
WHERE t.AdjusterID = (SELECT TOP 1 x.AdjusterID FROM MyTable x WHERE x.CompanyID = t.CompanyID ORDER BY AdjusterID)
It could be better performance-wise. But even more useful it is if you had another column in the table and you wanted to select not just one for each company but the best for each company using some other column ranking as a criteria. So instead of ORDER BY AdjusterID, you would order by that other column(s).