Top-10 SAP HANA - sap

Is it possible to create a view to list top-N or bottom-N for consumption in design studio 1.2?
I have figured out the SQLScript needed to create a stored procedure to get me the same which is captured below -
BEGIN
select
"REGION",
"COUNTRY",
"CITY",
"Customer Name",
sum("Gross Sales"),
sum("Net Sales")
from "USER1"."EXPO_SALES"
GROUP BY "Customer Name",CITY,COUNTRY,REGION
ORDER BY sum("Net Sales") Desc
limit 10;
END;
How do I make bring the output into a view to consume with SAP Design Studio 1.2?

Simply create a view:
CREATE VIEW "USER1"."EXPO_SALES_10" (
"REGION",
"COUNTRY",
"CITY",
"Customer Name",
"SUMOFGROSSSALES",
"SUMOFNETSALES" )
AS SELECT
"REGION",
"COUNTRY",
"CITY",
"Customer Name",
sum("Gross Sales"),
sum("Net Sales")
from "USER1"."EXPO_SALES"
GROUP BY "Customer Name",CITY,COUNTRY,REGION
ORDER BY sum("Net Sales") Desc
limit 10
WITH READ ONLY;

Related

Select Unique Rows When Exists

I have 3 tables as follow:
Customer table (custNbr varchar(7) , custRef char(3),........)
Item table (itemNbr varchar(10), custRef char(3), itemName varchar(50),..........);
example values
Customer
Customer
custRef
"0000001"
"100"
"0000002"
"120"
Item
Item
custRef
Name
"item000001"
"100"
"item 1"
"item000001"
"120"
"item 1"
"item000001"
"130"
"item 1"
"item000002"
"100"
"item 2"
"item000002"
"130"
"item 2"
I need to run a query to get the list of items for each customer, but the criteria is like this:
If the customer custref contains "100" then I need to get only the items with custRef "100"
If the customner has a custRef other than 100, (for example "120" then:
it the item has both custRef of "120" and "100" get only the row with same custref as customer, which is "120"
if the item does not have a custRef like the customer "120". then return the row with custRef "100"
So in the example above, if I want to return items for customer "0000001"
it should return
Customer
Item
custRef
Name
"0000001"
"item000001"
"100"
"item 1"
"0000001"
"item000002"
"100"
"item 2"
If I want to return items for customer "0000002", it should return
Customer
Item
custRef
Name
"0000001"
"item000001"
"120"
"item 1"
"0000001"
"item000002"
"100"
"item 2"
Is there a way to do this in one query ? currently I am doing join where item.custRef in (customer.custRef, "100")
and then I have to iterate on the cursor and eliminate the "100" row if I find that there is another one with a custRef like the customer.
I am using sqlserver 2014
You can prioritize the output with row_number(). If I've got it right
select top(1) with ties c.Customer, i.*
from Customer c
join Item i on c.custRef = 100 and i.custRef = 100
or c.custRef > 100 and i.custRef in (100, c.custRef)
order by row_number() over(partition by c.Customer, i.Item order by i.custRef desc);
db<>fiddle

teradata - encountering error 3526 with COUNT and group by

I believe that I'm encountering an error regarding the Count, but i'm not necessarily sure how to remedy this. In this instance, our "SE" table contains unique values where Equipment does not repeat. in our "WO" table, EquipmentCode will repeat as multiple 'CR's can be present per ID. My initial idea was to create a count value that expressed how many times the equipment appeared in our "WO" table with the where clauses.
SELECT
SE.EquipmentSurfaceAssetBK AS Field,
SE.ParentEquipmentCodeBK AS "Parent",
SE.EquipmentCodeBK AS Equipment,
SE.EquipmentDescription AS Description,
SE.EquipmentClassBK AS "Class",
SE.EquipmentCostCode AS "Cost Code",
SE.EquipmentCriticality AS Criticality,
SE.EquipmentStatus AS Status,
COUNT(WO.WorkOrderEquipmentCodeBK) AS "Corrective Repairs"
FROM IDW_PL_Surface.DIMWorkOrder WO JOIN IDW_PL_Surface.DIMSurfaceEquipment SE
ON WO.WorkOrderEquipmentCodeBK = SE.EquipmentCodeBK
WHERE WO.WorkOrderJobType = 'CR'
AND Status not in ('D')
AND WO.WorkOrderStatus not in ('CANC','REJ')
Group By Field, "Parent", Equipment, Description, "Class", "Cost Code", Criticality, Status, "Corrective Repairs"
remove "Corrective Repairs" column from group by statement:
SELECT
SE.EquipmentSurfaceAssetBK AS Field,
SE.ParentEquipmentCodeBK AS "Parent",
SE.EquipmentCodeBK AS Equipment,
SE.EquipmentDescription AS Description,
SE.EquipmentClassBK AS "Class",
SE.EquipmentCostCode AS "Cost Code",
SE.EquipmentCriticality AS Criticality,
SE.EquipmentStatus AS Status,
COUNT(WO.WorkOrderEquipmentCodeBK) AS "Corrective Repairs"
FROM IDW_PL_Surface.DIMWorkOrder WO JOIN IDW_PL_Surface.DIMSurfaceEquipment SE
ON WO.WorkOrderEquipmentCodeBK = SE.EquipmentCodeBK
WHERE WO.WorkOrderJobType = 'CR'
AND Status not in ('D')
AND WO.WorkOrderStatus not in ('CANC','REJ')
Group By Field, "Parent", Equipment, Description, "Class", "Cost Code", Criticality, Status
because when you group by, it only should be includes the columns that you want to calculate the aggregation function based on them and you don't need to include the aggregation column ( in your case count())

ORACLE SQL I am having problem with a select statement that should show the MAX balance for every branch [duplicate]

This question already has answers here:
Fetch the rows which have the Max value for a column for each distinct value of another column
(35 answers)
Select latest row for each group from oracle
(3 answers)
Get top results for each group (in Oracle)
(5 answers)
Closed 2 years ago.
Hi guys and thanks to everyone will help me.
I have this query:
SELECT c.accNum.bID.bID AS "Branch ID",
--c.accNum.accNumber AS "Account Number",
--c.accHolder.PrintFullName() AS "Customer Name",
MAX(c.accNum.balance) AS "Balance"
FROM tabCustomersAccounts c
GROUP BY c.accNum.bID.bID--, c.accNum.accNumber, c.accHolder.PrintFullName(), c.accNum.balance
ORDER BY c.accNum.bID.bID;
Results
When I change the code to this:
SELECT c.accNum.bID.bID AS "Branch ID",
c.accNum.accNumber AS "Account Number",
c.accHolder.PrintFullName() AS "Customer Name",
MAX(c.accNum.balance) AS "Balance"
FROM tabCustomersAccounts c
GROUP BY c.accNum.bID.bID, c.accNum.accNumber, c.accHolder.PrintFullName(), c.accNum.balance
ORDER BY c.accNum.bID.bID;
Results
From this results you can see that I they are wrong because I want only the MAX balance to be displayed for each Branch.
Use row_number(). Your actual column names are a bit unclear, but something like this:
SELECT ca.*
FROM (SELECT ca.*,
ROW_NUMBER() OVER (PARTITION BY ca.bid ORDER BY c.balance DESC) as seqnum
FROM tabCustomersAccounts ca
) ca
WHERE seqnum = 1;

Exclude a column of select statement in group by clause

I have the following query which generates a report
select
cust.firstname as "FIRST NAME",
cust.lastname as "LAST NAME",
dept.name as "DEPARTMENT",
subdept.name as "SUB DEPARTMENT",
count(*) "cust type"
from cust
join dept on dept.deptid = cust.deptid
join subdept on subdept.deptid = cust.subdeptid
join custactivity on custactivity.custid = cust.custid
where custactivity.swipetime > (trunc(sysdate - 1))
group by cust.firstname,cust.lastname,dept.name,subdept.name
having count(*) > 5
order by "DEPARTMENT" ;
Now, I want to include the column "custactivity.swipetime" only in the select statement but not group by clause as it is a time field and I get incorrect results if I include that in group by clause. How's is it possible?
I tried the solution in ORACLE Select and group by excluding one field
but it didn't work out.
Can anyone help?
There can be many custactivities per customer. So you must tell the DBMS which swipetime to choose. For instance the latest:
, max(custactivity.swipetime) as "last swipe time"
In the comment below you say you want a result row per cust.firstname, cust.lastname, dept.name, subdept.name, and custactivity.swipetime. So swipetime must be added to the group by clause.
Here is your query with swipetime added to the group by clause. You need the analytic function SUM over your original group. And as you cannot use analytic functions in your having clause, you need a surrounding query.
select *
from
(
select
cust.firstname as "FIRST NAME",
cust.lastname as "LAST NAME",
dept.name as "DEPARTMENT",
subdept.name as "SUB DEPARTMENT",
custactivity.swipetime as "SWIPE TIME",
sum(count(*)) over (partition by
cust.firstname,
cust.lastname,
dept.name,
subdept.name) as "cust type"
from cust
join dept on dept.deptid = cust.deptid
join subdept on subdept.deptid = cust.subdeptid
join custactivity on custactivity.custid = cust.custid
where custactivity.swipetime > (trunc(sysdate - 1))
group by
cust.firstname,
cust.lastname,
dept.name,
subdept.name,
custactivity.swipetime
)
where "cust type" > 5
order by "DEPARTMENT";

Finding the MAX [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Oracle SQL - How to Retrieve highest 5 values of a column
I have a simple question for someone who knows anything about SQL but since i'm very new and although I have tried many different ways, I can never seem to get the syntax correct. I want to display only the average hotel which is the MAX result. Currently it displays the average of all the hotels individually. Is this something I can use the MAX function for? I am using oracle
SELECT HOTEL.H_NAME "HOTEL NAME", ROUND(AVG (R_PRICE), 1) "AVERAGE ROOM PRICE"
FROM ROOM JOIN HOTEL ON HOTEL.HOTEL_NO = ROOM.HOTEL_NO
WHERE HOTEL.H_NAME = 'Shangra_La'
OR HOTEL.H_NAME = 'Hilton'
OR HOTEL.H_NAME = 'Sheraton'
GROUP BY HOTEL.H_NAME
ORDER BY HOTEL.H_NAME;
In Oracle you can use the ROWNUM pseudo column.
SELECT * FROM
(
SELECT
HOTEL.H_NAME "HOTEL NAME",
ROUND(AVG (R_PRICE), 1) "AVERAGE ROOM PRICE"
FROM
ROOM
JOIN HOTEL ON HOTEL.HOTEL_NO = ROOM.HOTEL_NO
WHERE HOTEL.H_NAME IN ('Shangra_La','Hilton','Sheraton')
GROUP BY HOTEL.H_NAME
) v
WHERE ROWNUM = 1
ORDER BY "AVERAGE ROOM PRICE" DESC;