SELECT bestseller books from DB - abap

I'm new on ABAP, and I'm trying to get the "best seller" products, so, I need to do a SUM with a GROUP BY product ID, I'm trying by this way but it doesn't work:
SELECT SUM(CANTIDAD) into (suma) FROM ZVENTAS_DETALLE GROUP BY ID_PRODUCTO.
Any help? thanks
UPDATE
Now, I'm trying to do this with Inner Join to show Product NAme and Total Sales, So, I have this code:
SELECT SUM( CANTIDAD ) vd~ID_PRODUCTO prd~STOCK_PRODUCTO
INTO CORRESPONDING FIELDS OF TABLE tabla
FROM ZVENTAS_DETALLE AS vd
INNER JOIN ZPRODUCTOS AS prd ON prd~ID_PRODUCTO = vd~ID_PRODUCTO
GROUP BY vd~ID_PRODUCTO.
LOOP at tabla into ven.
WRITE:/ ven~PRODUCTO_NOMBRE.
ENDLOOP.
So, I get this error
Could you help me? Thanks

Try this:
TYPES: BEGIN OF ty_data,
id_producto type zventas_detalle-id_producto,
suma type zventas_detalle-suma,
END OF ty_data.
DATA it_data TYPE STANDARD TABLE OF ty_data. " <-- The internal table
SELECT id_producto SUM( cantidad ) as suma
INTO CORRESPONDING FIELDS OF TABLE it_data
FROM ZVENTAS_DETALLE GROUP BY id_producto.
SORT it_data BY suma DESCENDING. "<-- The best sellers will be the first
Hope it helps.

Use that form:
SELECT id_producto, SUM( cantidad ) as sum
FROM zventas_detalle
GROUP BY id_producto
INTO TABLE #DATA(lt_bestselling) .

Related

BigQuery Sum Columns with Case When

I want to calculate the sum of the column with an if condition. Here my code:
SELECT
SUM(CASE WHEN properties = 'revenue' THEN property.value ELSE 0 END) AS TotalRevenue,
FROM deneme-319116.events.eventstable;
Sample data:
Properties Property.value
Search / null
Revenue / 15
Count / 25
Revenue / 40
I need to find 55 (15+40) as a result
In my eventstable I have columns properties and the property.value. I want to sum the property.value values that properties equal to 'revenue' but bigquery gives error: 'cannot acces field key on a value with type ARRAYvalue....
Plz help, thx
Hmmm . . . then you need to unnest(). If you want the sum per row in the original table:
SELECT (SELECT SUM(el.value)
FROM unnest(et.properties) el
WHERE el.name = 'revenue' -- or whatever the name is
) as TotalRevenue,
FROM deneme-319116.events.eventstable et;
If you want the overall sum over all rows:
SELECT SUM(el.value) as TotalRevenue,
FROM deneme-319116.events.eventstable et CROSS JOIN
unnest(et.properties) el
WHERE el.name = 'revenue' -- or whatever the name is

How to add a column with data based on another column

I have a BQ table that looks look this:
and I want to add a column that divides the shipping_handling based on the number of rows under the same invoice, in this case since there are 5 entries under invoice J513183, it will be 461.71/5 = 92.34
which would look like this:
tried adding this code "shipping_handling/count(invoice) as shipping" but it's not working. I hope anyone can help me find a solution on this google big query sql
Below is for BigQuery Standard SQL
#standardSQL
SELECT *,
ROUND(shipping_handling / (COUNT(*) OVER(PARTITION BY invoice)), 2) AS shipping
FROM `project.dataset.table`
You can use a window function:
select invoice_date,
invoice,
description,
shipping_handling,
avg_line_total,
shipping_handling / count(*) partition by (invoice) as shipping
from the_table;
Depending on your DBMS, you might need to cast one of the values in the division to a numeric data type otherwise it might use integer division which does not yield decimal digits.
One possible solution would be to create temp table with count of groups and than use it in your query
-- DROP TABLE IF EXISTS
IF OBJECT_ID(N'tempdb..#tmpInvoice') IS NOT NULL
BEGIN
DROP TABLE #tmpInvoice
END
-- GROUP INVOICES AND INSERT INTO TEMP TABLE
SELECT invoice as invoiceId, COUNT(invoice) invoiceCount
INTO #tmpInvoice
FROM BQ GROUP BY invoice
-- GET SHIPPING WITH ADDITIONAL SELECT
SELECT invoice_date,
invoice,
description,
shipping_handling,
avg_line_total,
cast((shipping_handling/(SELECT invoiceCount from #tmpInvoice where invoiceId = invoice)) as numeric(10,2)) Shipping
FROM BQ
-- GET SHIPPING WITH INNER JOIN
SELECT bq.invoice_date,
bq.invoice,
bq.description,
bq.shipping_handling,
bq.avg_line_total,
cast((bq.shipping_handling/ti.invoiceCount) as numeric(10,2)) Shipping
FROM BQ bq
INNER JOIN #tmpInvoice ti on bq.invoice = ti.invoiceId

MS-ACCESS / SQL - How to apply where clause in multiple conditions

SELECT Stock.*
FROM Stock
WHERE (
(
(Stock.ComputerPartNumber) In (SELECT [ComputerPartNumber] FROM [Stock] As Tmp GROUP BY [ComputerPartNumber] HAVING Count(*)=2)
)
AND
(
(Stock.EquipmentName)="EquipmentA" Or (Stock.EquipmentName)="EquipmentB")
)
OR (
(
(Stock.ComputerPartNumber) In (SELECT [ComputerPartNumber] FROM [Stock] As Tmp GROUP BY [ComputerPartNumber] HAVING Count(*)=1)
)
AND (
(Stock.EquipmentName)="EquipmentA" Or (Stock.EquipmentName)="EquipmentB"
)
);
I am using the above SQL to achieve below 3 items:-
Find out all of the ComputerPartNumber which used by EquipmentA and/or EquipmentB only
Filter out the query result if the ComputerPartNumber used by equipment other than EquipmentA and EquipmentB.
If the ComputerPartNumber is used by both EquipmentA and EquipmentC, filter out the result also.
However the item 3 cannot be filtered out successfully. What should I do in order to achieve the item3?
Table and Query snapshots are attached. Thanks in advance!
Table
Query
What you need to do is to check if the total number of times a part is used in all pieces of Equipment is equal to the total number of times a part is used by either Equipment A or B:
SELECT S.StorageID, S.ComputerPartNumber, S.EquipmentName, S.Result
FROM Stock AS S
WHERE
(SELECT COUNT(*) FROM Stock AS S1 WHERE S1.ComputerPartNumber=S.ComputerPartNumber)
=(SELECT COUNT(*) FROM Stock AS S2 WHERE S2.ComputerPartNumber=S.ComputerPartNumber AND S2.EquipmentName IN("EquipmentA","EquipmentB"))
Regards,
You can use not exists:
select s.*
from stock as s
where not exists (select 1
from stock as s2
where s2.ComputerPartNumber = s.ComputerPartNumber and
s2.EquipmentName not in ("EquipmentA", "EquipmentB")
);

AnalysisException: subqueries are not supported in the select list

I get this error code shown in title when using this following query. I'm trying query two tables to find total patients with hearing issues and the total of those patients with hearing issues who have undergone some sort of scan (MR,SC,CT).
SELECT (
SELECT COUNT(*)
FROM hearing_evaluation
where severity_of_hearing_loss <> 'Normal'
AND severity_of_hearing_loss <> 'insufficient data'
) AS patients_with_hearing_loss
, AVG(number_of_scans) AS avg_number_of_scans
FROM (
SELECT patient_id, COUNT(*) AS number_of_scans
from imaging
where patient_id IN (
SELECT patient_id
from hearing_evaluation
where severity_of_hearing_loss <> 'Normal'
and severity_of_hearing_loss <> 'insufficient data'
)
AND modality IN ('CT','MR','SC')
GROUP BY patient_id
) AS scans
Any help would be appreciated.
I tried, pls refer to below SQL - this will work in impala. Only issue i can see is, if hearing_evaluation has multiple patient ids for a given patient id, you need to de-duplicate the data.
There can be case when patient id doesnt exist in image table - in such case you need to apply RIGHT JOIN.
SELECT
COUNT(patient_id) AS patients_with_hearing_loss
, AVG(rs.number_of_scans) AS avg_number_of_scans
FROM (
SELECT i.patient_id patient_id, COUNT(*) AS number_of_scans
from imaging i ,hearing_evaluation h
where i. patient_id = h.patient_id
and h.severity_of_hearing_loss <> 'Normal'
and h.severity_of_hearing_loss <> 'insufficient data'
AND modality IN ('CT','MR','SC')
GROUP BY i.patient_id ) rs

How to count each instance where a certain product was sold after a different product? SQL or DAX

Sorry if the title seems confusing, it was the best I could come up with.
I can work with both excel(Dax since its a power query) and sql:
I have a situation where there are two product types being purchased, Type_A and Type_B.
I want to calculate a count of how many unique Loc_ID have purchased a "Type_A" Product type, AFTER purchasing a "Type_B" Product type.
From my example there are a total of 3 unique Loc_ID which would fall in this filter: Loc_01, Loc_02, and Loc_04
Any help is greatly appreciated
Try this (it works good if each loc_id purchased both type of products as in your example.
select count(*)
from
(select loc_id , max(date_purchased) dt
from table t where product_type = 'type_a'
group by loc_id) a,
(select loc_id , max(date_purchased) dt
from table t where product_type = 'type_b'
group by loc_id) b
where a.loc_id=b.loc_id and a.dt>b.dt;
This will work even if certain loc_id did not purchase both type of products
Try this:-
Select count(a.loc_id) as cnt_locations
from
your_table_name a
inner join
(
Select a.loc_id,b.date_purchased,b.Product_type
from
(
Select loc_id, min(date_purchased) as date_purchased
from
your_table_name
group by loc_id
) a
inner join
your_table_name b
on a.loc_id=b.loc_id and a.date_purchased =b.date_purchased
where Product_type ='Type_B'
) b
on
a.loc_id=b.loc_id
where a.date_purchased >b.date_purchased and a.Product_type ='Type_A'