How to select entire row based on if else condition SAP-HANA-DB - hana

I Have query which returns the following result set.
RNO prodid FLAG CREATED_BY
1 2313 ZA TEST
2 2313 KA TEST1
From the above resultset, i want to generate a final output like the grouped by prod id has flag ZA, then that entire row has to be selected else for the same prod id, we need to select row with flag KA.
Please help to give the query in SAP HANA DB

Related

Exclude entire group when one condition matches

Let's say I have a table like such:
Store productId
Test 1 6524
Test 1 6521
Test 1 6523
Test 2 6234
Test 2 6264
Test 3 6395
I am trying to find a way to check whether the store has the product 6524 and if it does it should remove the entire group (store) from the list so that the result can look like the below.
Expected Outcome:
Test 2
Test 3
I have tried doing a GROUP by followed by HAVING productId <> 6524 but this of course just gets rid of the single row not the entire group.
You can use NOT EXISTS such as
SELECT DISTINCT Store
FROM tab t
WHERE NOT EXISTS ( SELECT 0
FROM tab
WHERE Store = t.Store
AND productId = 6524 )
which filters out the matching values of Store column
select store
from your_table
group by store
having sum(productId=6524)=0;

need to pull a specific record

There is 1 record having duplicate values except in 1 column having x and y
record status
XXXXXXXXXX A
XXXXXXXXXX B
Need to pull A only and remove the other duplicate B
Select record
case
when status in ("'a', 'b'") then ('a')
from xyz
Let suppose you have data as below where Status is repeating for First column
but you are interesting in the status which is of having lower value as given below:
In this case following SQL may help. Here, we are partitioning on key field and ordering the Status so that we can apply filter on rank to get desired result.
WITH sampleData AS
 (SELECT '1234' as Field1,  'A' as STATUS UNION ALL 
  SELECT '1234',  'C' UNION ALL
  SELECT '5678', 'A' UNION ALL 
  SELECT '5678',  'B' )
 select * except(rank) from (
 select *, rank() over (partition by Field1 order by STATUS ASC) rank from sampleData)
 where rank = 1
 order by Field1
Consider below approach
select * from sampledata
qualify 1 = row_number() over win
window win as (partition by field1 order by if(status='A',1,2) )
if applied to sample data in your question - output is

Qualifying a column in a SQL select statement

I'm looking to generate a query that pulls from several tables. Most are rather straightforward and I can pull a value from a table directly but there is one table that is pivoted so that the value I want depends on the value in another column.
The table looks like the below:
ID Condition Value
1 Stage1 6
2 Stage2 9
3 Stage3 5
4 Stage4 2
So I'm looking to write a query that essentially "qualifies" the value I want by telling the table which condition.
An example of my SQL:
Select Attribute1, Stage1Value, Stage2Value, Stage3Value
From attribute, stage
where attribute = project1
So I can't just pull the "Value" column as it needs to know which stage in the query.
There are 30 columns I am trying to pull - of which 13 fall into this category. Thanks for any help you can provide.
So, you want conditional aggregation something :
select a.<col>,
sum(case when s.Condition = 'Stage1' then s.value else 0 end),
. . .
sum(case when s.Condition = 'Stage4' then s.value else 0 end)
from attribute a inner join
stage s
on s.<col> = a.<col>
group by a.<col>

The MIN() Function Ms Access

this is a sample sql query that i created ms access query. i am trying to get only one row the min(DATE). how ever when i run my query i get multiple lines. any hits? thanks
SELECT tblWarehouseItem.whiItemName,
tblWarehouseItem.whiQty,
tblWarehouseItem.whiPrice,
Min(tblWarehouseItem.whiDateIn) AS MinOfwhiDateIn,
tblWarehouseItem.whiExpiryDate,
tblWarehouseItem.whiwrhID
FROM tblWarehouseItem
GROUP BY tblWarehouseItem.whiDateIn,
tblWarehouseItem.whiItemName,
tblWarehouseItem.whiQty,
tblWarehouseItem.whiPrice,
tblWarehouseItem.whiExpiryDate,
tblWarehouseItem.whiwrhID;
If i have my sql code like that is working as it should:
SELECT MIN(tblWarehouseItem.whiDateIn) FROM tblWarehouseItem;
In the first query, you group by a number of columns. That means the minimum value will be calculated for each group, which in turn means you may have multiple rows. On the other hand, the second query will only get the minimum value for the specified column from all rows, so that there is only one row in the result set.
A simple example is shown below to illustrate the above.
Table:
Key Value
1 1
1 2
2 3
2 4
On Group By Key:
GroupKey MinValue
1 = min(1,2) = 1 -> Row 1
2 = min(3,4) = 3 -> Row 2
On Min (Value)
MinValue
=min(1,2,3,4) = 1 -> Row 1
For a table like above, if you want to select all rows and also show the minimum value from whole table rather than per group, you can do something like this:
select key, (select min(value) from table)
from table
SELECT WI.*
FROM tblWarehouseItem AS WI INNER JOIN (SELECT whiimtID, MIN(tblWarehouseItem.whiDateIn) AS whiDateIn
FROM tblWarehouseItem
GROUP BY whiimtID) AS MinWI ON (WI.whiDateIn = MinWI.whiDateIn) AND (WI.whiimtID = MinWI.whiimtID);

How do I SELECT the row with maximum value for an attribute in SQL

This is my table
BRANCH NUMBER_OF_PHONES
B001 3
B002 2
B003 1
B004 2
How can I select the row that has the highest number of phones for the branch?
BRANCH NUMBER_OF_PHONES
B001 3
This is my SQL query that generated the first table:
SELECT BRANCH_PHONES.BRANCH_NO AS 'BRANCH', COUNT(BRANCH_PHONES.BRANCH_TEL) as 'NUMBER_OF_PHONE_NUMBERS'
FROM [Practices].dbo.BRANCH BRANCH, [Practices].dbo.BRANCH_PHONES BRANCH_PHONES
WHERE BRANCH.BRANCH_NO = BRANCH_PHONES.BRANCH_NO
GROUP BY BRANCH_PHONES.BRANCH_NO;
I assume the example data you showed is the result of your first attempt (the query you posted).
You want the branch that has the most phones. You didn't specify your DBMS, but if it is MySQL you can use LIMIT like this. Sort by COUNT(Phones), then only show the first row of the result set.
SELECT Branche, COUNT(Phones) FROM Branch
GROUP BY Branche
ORDER BY COUNT(Phones) DESC
LIMIT 1
Note: this will give an arbitrary result if there is a tie for the most phones.