I have a SUM function in my SELECT but don't want it to show - sql

I want to see the companies which deliver the most sold items in my DB.
Currently i have this Code:
SELECT L."Firma", SUM("B"."Anzahl")
FROM "Bestelldetails" B, "Artikel" A, "Lieferanten" L
WHERE L."Lieferanten-Nr" = A."Lieferanten-Nr"
AND A."Artikel-Nr" = B."Artikel-Nr"
GROUP BY L."Firma"
ORDER BY 2 DESC
The Output that i get:
Firma
2
Company 1
2756
Company 2
2377
Company 3
2063
...many more...
..XXX..
The Output that i want:
Firma
Company 1
Company 2
Company 3
...many more...
Code with output as image
But i don't want the row with the number 2 to show. I just want the Company-Names to show. How would i do so?

Simply ORDER BY the SUM():
SELECT L."Firma"
FROM "Bestelldetails" B
JOIN "Artikel" A
ON A."Artikel-Nr" = B."Artikel-Nr"
JOIN "Lieferanten" L
ON L."Lieferanten-Nr" = A."Lieferanten-Nr"
GROUP BY L."Firma"
ORDER BY SUM("B"."Anzahl") DESC
(Now using proper, explicit JOIN syntax.)

select Firma from
(
SELECT L."Firma", SUM("B"."Anzahl")
FROM "Bestelldetails" B, "Artikel" A, "Lieferanten" L
WHERE L."Lieferanten-Nr" = A."Lieferanten-Nr"
AND A."Artikel-Nr" = B."Artikel-Nr"
GROUP BY L."Firma"
ORDER BY 2 DESC
);

Related

ORA-00904: "Q"."ITEM_NO": invalid identifier - Oracle SQL group by issue

I have this query to fetch item no's and their quantities filtered by a specific unit.
select q.item_no, sum(q.quantity) from item_unit_quantity q
where q.unit_no = 'PH00000096' group by q.item_no
Now I want to add price column which I get from another two tables
There is a condition, Price is always the maximum of the latest three GRN dates.
select max(price) from (select price from grn_item gi join grn g on gi.grn_no = g.grn_no
where gi.item_no = 'IT00001896' order by g.grn_date desc) where rownum <= 3
When I combined the queries as below,
select q.item_no, sum(q.quantity),
(select max(price) from (select price from grn_item gi join grn g on gi.grn_no = g.grn_no
where gi.item_no = q.item_no order by g.grn_date desc) where rownum <= 3) "PRICE"
from item_unit_quantity q where q.unit_no = 'PH00000096' group by q.item_no
I get
ORA-00904: "Q"."ITEM_NO": invalid identifier
But if I change the above as "gi.item_no = 'IT00001896'" I get
ITEM_NO | Sum(Quantity) | Price
--------------------------------------
IT00012824 | 14 | 3.68
IT00006345 | 494 | 3.68
IT00001896 | 5 | 3.68
I still want to display the Item no, Item quantity and price for each Item no in this query, how can I do this?
Your question is a bit hard to follow without sample data. However, subqueries with GROUP BY can be finicky. A simple solution is to move the subquery to the FROM clause:
select q.item_no, sum(q.quantity), max(max_price)
from item_unit_quantity q left join
(select gi.item_no, max(g.price) as max_price
from grn_item gi join
grn g
on gi.grn_no = g.grn_no
group by gi.item_no
) gi
on gi.item_no = q.item_no
where q.unit_no = 'PH00000096'
group by q.item_no

SQL subquery for function count

The first column is the name of the locality,the second column is the number of voters in every locality..
my problem is on the third column because im trying to count voters that are unemployment but is counting all voters.sorry for my english.
select l.nombre as "Nombre",count(v.dni) as "Votantes",
(select count(v.dni) from localidades l,votantes v
where v.localidad=l.idlocalidad and v.situacionlaboral='Parado') as "Parados"
from votantes v,localidades l
where v.localidad=l.idlocalidad
group by l.nombre;
this is the out:
"Nombre" "Votantes" "Parados"
Sevilla 3 4
Baena 1 4
Córdoba 3 4
Montilla 1 4
Madrid 3 4
Utrera 3 4
Carmona 1 4
Badalona 1 4
Getafe 1 4
Try this:
select l.nombre as "Nombre"
, count(v.dni) as "Votantes"
, sum(case when situacionlaboral = 'Parado' then 1
else 0
end) as "Parados"
from votantes v
join localidades l on v.localidad=l.idlocalidad
group by l.nombre;
When you join two tables please do it like this:
from localidades l
join votantes v on v.localidad = l.idlocalidad
And not like this:
from localidades l,votantes v
where v.localidad=l.idlocalidad
If you want the total votes, use window functions:
select l.nombre, count(*) as Votantes,
sum(count(*)) over () as parados
from votantes v join
localidades l
on v.localidad = l.idlocalidad
group by l.nombre;
Also learn to use proper, explicit, standard JOIN syntax.

Supply chain SQL query

I am having issues with my SQL code.
I want to display the total count of stores that correspond with a certain Distribution Center/ WH. I want both to be tied to a certain item.
For example: I have one WH that gave a certain item to 50 stores. I want the query to tell me if I give it the item# and WH# it will give me the amount of stores that received that item.
SELECT
COUNT(*) AS TOTAL_STORES
FROM
(SELECT
a.WH_i, a.STORE_i
FROM
WH a, STORES b
WHERE
a.item_i = 2201
AND a.WH_i IN (10)
GROUP BY
a.WH_i, B.STORE_i
HAVING
COUNT(a.item_i) = 1) a;
Table WH has the warehouse numbers and item numbers and store has the store numbers.
I am new to SQL so I am not 100% confident with joins just yet. Any help is greatly appreciated though!
EDIT: I tried joining the two tables without actually using the JOIN clause and it is still not giving proper results.
SELECT COUNT(*) AS TOTAL_STORES
FROM(
SELECT a.WH_i, b.STORE_i
FROM WH a, STORES b
WHERE a.item_i = b.sku_i
AND a.stock_i = b.stock_i
AND a.item_i = 2201
AND a.WH_i IN (10)
GROUP BY a.WH_i, B.STORE_i
HAVING COUNT(a.item_i) = 1
)a;
You only want the count of stores so just pull stores in your subquery.
SELECT COUNT(STORE_i) AS TOTAL_STORES
FROM(
SELECT b.STORE_i
FROM WH a
JOIN STORES b
ON a.item_i = b.sku_i
AND a.stock_i = b.stock_i
WHERE
a.item_i = 2201
AND a.WH_i = 10
GROUP BY b.STORE_i
HAVING COUNT(a.item_i) = 1
) a;
SELECT a.WH_i, a.item_i, count(DISTINCT b.store_i)
FROM WH a
JOIN STORES b
ON a.item_i = b.sku_i
GROUP BY a.WH_i, a.item_i

Get count from table given input from another

I have 3 tables: filer_info, filer_persent and persent_email, connected via:
filer_info.filer_info_id = filer_persent.filer_info_id
filer_persent.persent_info_id = persent_email.persent_info_id
I want to find all rows where I have multiple type PRIMARY in the persent_email table (ie count > 1). And the only thing I want to return in the query is filer_info.filer_ident and the count.
This gives me every row, but I only want the data where filer_ident > 1 in the returned rows.
select * from filer_info f
inner join filer_persent fp on f.filer_info_id=fp.filer_info_id
inner join persent_email p on fp.persent_info_id=p.persent_info_id
where fp.filer_persent_kind_cd = 'FILER' and p.persent_email_kind_cd='PRIMARY'
order by f.filer_ident
SELECT filer_ident, cnt
FROM (
SELECT filer_info_id, COUNT(*) cnt
FROM filer_persent fp
JOIN persent_email p
USING (persent_info_id)
WHERE (fp.filer_persent_kind_cd, p.persent_email_kind_cd) = ('FILER', 'PRIMARY')
HAVING COUNT(*) > 1
) с
JOIN filer_info f
USING (filer_info_id)
WHERE filer_ident > 1
ORDER BY
f.filer_ident

Adding a new column in this SQL Query?

I am querying data from the WIP and Employee tables:
WIP
Id,Name
Employee
Id,Name,Orgnization
Joining both I can query:
select w.ID,e.Organization,w.ConsultantName,e.OrganizationID, w.ConsultantID
from vwWIPRecords w
inner join vwEmployees e on w.ConsultantID=e.ID;
Resutls:
1 VHAA Web User 1 1
2 VHAA NZ RP 1 3
3 VHAA Ghom Mure 1 2
4 VHAA Ghom Mure 1 2
Requirment:
In query add anther column which will concatenate and group by e.Organization and e.ConsultantName but it will be only for first unique record. For next (where name and organization is same) it will not show anything. This column will show unique Consultants of a company. Please see record number 3 and 4 in second example.
1 VHAAWeb User 1 1
2 VHAANZ RP 1 3
3 VHAAGhom Mure 1 2
4 1 2
Thanks a lot for your help
Here is a start. The final column is a flag indicating the row should be blank. Let me know if this works for you so far and I can help further.
select w.ID,e.Organization, w.ConsultantName,
e.OrganizationID, w.ConsultantID, CASE WHEN D.Dup > 1 AND D.ID <> w.ID THEN 'Y'
ELSE 'N' END As HideMe
from vwWIPRecords w
inner join vwEmployees e on w.ConsultantID=e.ID
inner join
(
select MIN(w.ID) As ID, e.Organization,w.ConsultantName,
e.OrganizationID, w.ConsultantID, COUNT(*) AS Dup
from vwWIPRecords w
inner join vwEmployees e on w.ConsultantID=e.ID
) D
ON D.Organization = w.Organization
AND D.ConsultantName = w.ConsultantName
AND D.OrganizationID = w.OrganizationID
AND D.ConsultantID = w.ConsultantID