Here's my code:
SELECT B1_COD, SB1030.B1_DESC, B2_CM1, B2_QATU, SUM (B2_QATU)
FROM SB1030
INNER JOIN SB2030 ON B1_COD = B2_COD
WHERE (B1_TIPO='PA') AND (B2_QATU <> '0')
I'd like to:
multiply b2_cm1 by b2_qatu creating b2_VAL
sum all b2_qatu with same b1_cod creating b2_SAL
divide b2_VAL by b2_SAL
Could anyone help me?????
Thanks
Here is what I think you are looking for. A list of b1_cods with sums and a calculation based on the sums.
select
b1_cod,
sb1030.b1_desc,
sum(b2_cm1) as sum_cm1,
sum(b2_qatu) as b2_sal,
sum(b2_qatu * b2_cm1) as b2_val,
sum(b2_qatu) / sum(b2_qatu * b2_cm1) as new_field
from sb1030
inner join sb2030 on b1_cod = b2_cod
where b1_tipo = 'pa' and b2_qatu <> '0'
group by b1_cod, sb1030.b1_desc
order by b1_cod, sb1030.b1_desc;
Do you only want the result b2_VAL and b2_SAL?
Try using WITH CTE:
WITH CTE AS(
SELECT SUM(B1_COD) AS B1_COD, SB1030.B1_DESC,B2_CM1*B2_QATU AS b2_VAL, SUM (B2_QATU) AS B2_QATU
FROM SB1030
INNER JOIN SB2030 ON B1_COD = B2_COD
WHERE (B1_TIPO='PA') AND (B2_QATU <> '0'))
SELECT B1_COD+B2_QATU, b2_VAL
FROM CTE
Is it something like that you are looking for?
Related
I have this two query sql and i'm trying to do a sum of both 2 counts in the query together but when i try to do the UNION ALL but it kept prompt me to enter parameter for CountOfTools_Number2. All i wan is the sum of the two count and return me a value.
SELECT Count(Test.Tool_Number) AS CountOfTool_Number1
FROM Test INNER JOIN Test_Tool ON Test.Tool_Number = Test_Tool.Current_Tool
GROUP BY Test_Tool.Current_Test
HAVING (((Test_Tool.Current_Test) Like "*E1*"));
SELECT Count(Test.Tool_Number) AS CountOfTool_Number2
FROM Test_Tool, Test INNER JOIN Previous_Tool ON Test.Tool_Number = Previous_Tool.Previous_Tool
GROUP BY Test_Tool.Current_Test
HAVING (((Test_Tool.Current_Test) Like "*E1*"));
Presuming your two example queries work, this should do the trick:
select
sum(CountOfTool_Number1) as CountOfTool
from (
SELECT Count(Test.Tool_Number) AS CountOfTool_Number1
FROM Test INNER JOIN Test_Tool ON Test.Tool_Number = Test_Tool.Current_Tool
GROUP BY Test_Tool.Current_Test
HAVING (((Test_Tool.Current_Test) Like "*E1*"))
union all
SELECT Count(Test.Tool_Number) AS CountOfTool_Number2
FROM Test_Tool, Test INNER JOIN Previous_Tool ON Test.Tool_Number = Previous_Tool.Previous_Tool
GROUP BY Test_Tool.Current_Test
HAVING (((Test_Tool.Current_Test) Like "*E1*"))
) as q;
Hopefully the code below should demonstrate what I'm trying to achieve.
The issue is that none of the input selects are resolved by the time I try to calculate VatableCash so I get "Invalid Column" when trying to select it.
Sorry if there's something plainly obvious I can do here. SQL isn't one of my strong suits.
select
OrderHeader.ID,
sum(OrderLine.NetPrice) as OrderLineNetPrice,
sum(OrderLine.GrossPrice) as OrderLineGrossPrice,
sum(
case when PaymentOption_ID = 8
then Payment.Amount
else 0
end
) as TotalCashAmount,
((OrderLineGrossPrice - OrderLineNetPrice) / OrderLineGrossPrice) * TotalCashAmount as VatableCash
from OrderHeader
inner join Payment on Payment.OrderHeader_ID = OrderHeader.ID
inner join OrderLine on OrderLine.OrderHeader_ID = OrderHeader.ID
group by OrderHeader.ID
You need to use sub query.
you can try this.
;WITH CTE AS
(
select
OrderHeader.ID,
sum(OrderLine.NetPrice) as OrderLineNetPrice,
sum(OrderLine.GrossPrice) as OrderLineGrossPrice,
sum(
case when PaymentOption_ID = 8
then Payment.Amount
else 0
end
) as TotalCashAmount
from OrderHeader
inner join Payment on Payment.OrderHeader_ID = OrderHeader.ID
inner join OrderLine on OrderLine.OrderHeader_ID = OrderHeader.ID
group by OrderHeader.ID
)
SELECT *,
((OrderLineGrossPrice - OrderLineNetPrice) / OrderLineGrossPrice) * TotalCashAmount as VatableCash
FROM CTE
Love the cross apply! Use it whenever you want some handy extra columns.
select
OrderHeader.ID,
sum(OrderLine.NetPrice) as OrderLineNetPrice,
sum(OrderLine.GrossPrice) as OrderLineGrossPrice,
TotalCashAmount,
((OrderLineGrossPrice - OrderLineNetPrice) / OrderLineGrossPrice) * TotalCashAmount as VatableCash
from OrderHeader
inner join Payment on Payment.OrderHeader_ID = OrderHeader.ID
inner join OrderLine on OrderLine.OrderHeader_ID = OrderHeader.ID
cross apply ( select sum(
case when PaymentOption_ID = 8
then Payment.Amount
else 0
end
)) as subquery(TotalCashAmount)
group by OrderHeader.ID
help needed.
i want to multiply two column and display result to a new column(totalQty).
TotalQty= (ITY00.CMPITQTY * MPOS_GP_InvTransaction.Quantity)
HOW CAN I ACHIEVE THIS ?
Select
MPOS_GP_InvTransaction.id,
MPOS_GP_InvTransaction.[Type],
MPOS_GP_InvTransaction.Vendor,
MPOS_GP_InvTransaction.Currency,ITY00.CMPTITNM,
MPOS_GP_InvTransaction.BatchId,
MPOS_GP_InvTransaction.UserId,
MPOS_GP_InvTransaction.ItemNo,
MPOS_GP_InvTransaction.SiteId,
ITY00.CMPITQTY,
MPOS_GP_InvTransaction.Quantity,
MPOS_GP_InvTransaction.IntegrationFlag
From DB_37788.dbo.MPOS_GP_InvTransaction
INNER JOIN TWCL.dbo.ITY00 ON ITY00.ITEMNMBR=MPOS_GP_InvTransaction.ItemNo
Where (MPOS_GP_InvTransaction.ItemNo like '%-GTYR%' )
OR (MPOS_GP_InvTransaction.ItemNo like '%-JKOP%' )
And (MPOS_GP_InvTransaction.SiteId IN('MM-DC-ZZQW','MM-DC-TTYR') )
And (MPOS_GP_InvTransaction.IntegrationFlag = 0 )
SELECT MPOS_GP_InvTransaction.id,
MPOS_GP_InvTransaction.Type,
MPOS_GP_InvTransaction.Vendor,
MPOS_GP_InvTransaction.Currency,
ITY00.CMPTITNM,
MPOS_GP_InvTransaction.BatchId,
MPOS_GP_InvTransaction.UserId,
MPOS_GP_InvTransaction.ItemNo,
MPOS_GP_InvTransaction.SiteId,
ITY00.CMPITQTY,
MPOS_GP_InvTransaction.Quantity,
MPOS_GP_InvTransaction.IntegrationFlag,
ITY00.CMPITQTY * MPOS_GP_InvTransaction.Quantity AS TotalQty
FROM DB_37788.dbo.MPOS_GP_InvTransaction
INNER JOIN TWCL.dbo.ITY00 ON ITY00.ITEMNMBR = MPOS_GP_InvTransaction.ItemNo
WHERE(MPOS_GP_InvTransaction.ItemNo LIKE '%-GTYR%')
OR (MPOS_GP_InvTransaction.ItemNo LIKE '%-JKOP%')
AND (MPOS_GP_InvTransaction.SiteId IN('MM-DC-ZZQW', 'MM-DC-TTYR'))
AND (MPOS_GP_InvTransaction.IntegrationFlag = 0);
I have a query:
SELECT A.AHSHMT AS SHIPMENT, A.AHVNAM AS VENDOR_NAME, D.UNITS_SHIPPED, D.ADPON AS PO, B.NUMBER_OF_CASES_ON_TRANSIT, C.NUMBER_OF_CASES_RECEIVED FROM AHASNF00 A
INNER JOIN (SELECT IDSHMT, COUNT(*) AS NUMBER_OF_CASES_ON_TRANSIT FROM IDCASE00 WHERE IDSTAT = '01' GROUP BY IDSHMT) B
ON (A.AHSHMT = B.IDSHMT)
LEFT JOIN (SELECT IDSHMT, (COUNT(*) AS NUMBER_OF_CASES_RECEIVED FROM IDCASE00 WHERE IDSTAT = '10' GROUP BY IDSHMT) C
ON (A.AHSHMT = C.IDSHMT)
INNER JOIN (SELECT ADSHMT, ADPON, SUM(ADUNSH) AS UNITS_SHIPPED FROM ADASNF00 GROUP BY ADSHMT, ADPON) D
ON (A.AHSHMT = D.ADSHMT)
WHERE A.AHSHMT = '540041134';
On the first JOIN statement I have a COUNT(*), on this count sometimes I will get NULL. I need to replace this with a "0-zero", I know think I know how to do it in SQL
ISNULL(COUNT(*), 0)
But this doesn't work for DB2, how can I accomplish this? All your help is really appreciate it.
Wrap a COALESCE around each of the nullable totals in your SELECT list:
SELECT A.AHSHMT AS SHIPMENT,
A.AHVNAM AS VENDOR_NAME,
COALESCE( D.UNITS_SHIPPED, 0 ) AS UNITS_SHIPPED,
D.ADPON AS PO,
COALESCE( B.NUMBER_OF_CASES_ON_TRANSIT, 0 ) AS NUMBER_OF_CASES_ON_TRANSIT,
COALESCE( C.NUMBER_OF_CASES_RECEIVED, 0 ) AS NUMBER_OF_CASES_RECEIVED
FROM ...
The inner joins you're using for expressions B and D mean that you will only receive rows from A that have one or more cases in transit (expression B) and have one or more POs in expression D. Is that the way you want your query to work?
Instead of using ISNULL(COUNT(*), 0),
try using COALESCE(COUNT(*),0)
use IFNULL(COUNT(*), 0) for DB2
Here is my SELECT query:
SELECT
a.id_auto,
SUM(pozicane_dni * a.poplatok_denny + najazdene_km * a.poplatok_km) celkova_trzba
FROM Auta a
INNER JOIN (SELECT
id_auto,
(SUM(koniec_pozicania - zaciatok_pozicania)) pozicane_dni,
(SUM(najazdene_km)) najazdene_km,
zaloha
FROM Zakaznik GROUP BY id_auto) z
ON z.id_auto = a.id_auto
INNER JOIN (SELECT
id_auto,
poplatok_denny,
poplatok_km
FROM Auta_zaloha) az
ON az.id_auto = a.id_auto
GROUP BY a.id_auto;
But I'm getting this error:
ORA-00979: not a GROUP BY expression
Anybody knows where could be the problem? I'm a little bit confused. I have GROUP BY clause everywhere where I use aggregate function SUM().
EDIT:
One more thing, it stop working when I add a CASE WHEN to the query:
SELECT
a.id_auto, a.poplatok_denny, a.poplatok_km,
CASE WHEN z.zaloha IS NULL THEN
(pozicane_dni * a.poplatok_denny + najazdene_km * a.poplatok_km)
ELSE
(pozicane_dni * az.poplatok_denny + najazdene_km * az.poplatok_km)
END
celkova_trzba
FROM Auta a
INNER JOIN (SELECT
id_auto,
(SUM(koniec_pozicania - zaciatok_pozicania)) pozicane_dni,
(SUM(najazdene_km)) najazdene_km,
zaloha
FROM Zakaznik GROUP BY id_auto, zaloha) z
ON z.id_auto = a.id_auto
INNER JOIN (SELECT
id_auto,
poplatok_denny,
poplatok_km
FROM Auta_zaloha) az
ON az.id_auto = a.id_auto
GROUP BY a.id_auto;
In your first inner SELECT, you should either remove "zaloha", GROUP BY it or apply some aggregate function to it.
The problem will be in using the value of
a.poplatok_km
If you're grouping by a.id_auto, you need to give a rule on what to do if there are multiple rows in the result set (even if there is only one possible row, that is something the SQL doesn't "know").
Two ways around this:
add all columns from table a that are required in the calculation in the group by clause
use a "pseudo-Function" like min (a.poplatok_km) which doesn't change anything
Your second inner select:
INNER JOIN (SELECT
id_auto,
poplatok_denny,
poplatok_km
FROM Auta_zaloha) az
Appears to be missing a group by clause like:
INNER JOIN (SELECT
id_auto,
poplatok_denny,
poplatok_km
FROM Auta_zaloha group by id_auto) az
Try using NVL2 function in place of CASE WHEN, at least it could yield more understandable error message:
NVL2(z.zaloha,
(pozicane_dni * az.poplatok_denny + najazdene_km * az.poplatok_km),
(pozicane_dni * a.poplatok_denny + najazdene_km * a.poplatok_km)
) celkova_trzba