Exclude lines within t_sql - sql

I have a table
I need to exclude all lines where having round( sum(QtyInvoiced),1) = 0 and round(sum(Amount),1) =0
I try the following script but it dosen't work, how to correct it?
select sum( QtyInvoiced ) , sum(Amount) ,salesNumber
from invoicesales
group by salesNumber
except
select sum( QtyInvoiced ) , sum(Amount) ,salesNumber
from invoicesales
group by salesNumber
having round( sum(QtyInvoiced),1) = 0 and round(sum(Amount),1) =0

try like below using <> not equal
select sum( QtyInvoiced ) , sum(Amount) ,salesNumber
from invoicesales
group by salesNumber
having round( sum(QtyInvoiced),1) <> 0 and round(sum(Amount),1)<>0

If you want to exclude those rows, perhaps you want a NOT?
HAVING NOT(ROUND( SUM(QtyInvoiced),1) = 0 AND ROUND(SUM(Amount),1) = 0);
Otherwise yes, your SQL will do exactly what you've asked for to return the rows where the sum of QtyInvoiced and Amount equals 0 (not exclude those that do).

Related

SQL/Oracle issue, I receive this error " REPORTS.WEK_MTD_BILL_SUM_HIST_T.BILL_CYCLE: invalid identifier at line 48" , is there anyway to fix this?

enter image description here
above is the image of the code, my objective is to have 4 columns demonstrating the prices for a bill cycle, and 4 columns demonstrating the average of all bill cycles one year before the initial bill cycle.
the first inner select works by itself and adds the 4 columns to the right in the picture below, the problem starts when I want to add second inner select the 4 columns for yearly average. initially I didn't have the "select * from", but I couldn't find a better way
the base of the problem is that I can't implement 2 Where clauses on the same column.
below is the snap shot of what I get from running only the first inner select.
enter image description here
I have tried union , join, cross join ... but none seem to work, one person suggested "window function" but I haven't worked with that yet.
this is the error below:
enter image description here
RAW CODE TEXT IS BELOW:
VARIABLE START_DATE CHAR
EXEC :START_DATE :=''
VARIABLE END_DATE CHAR
EXEC :END_DATE=''
select * from
(SELECT
"REPORTS"."WEK_MTD_BILL_SUM_HIST_T"."BILL_CYCLE",
"REPORTS"."WEK_MTD_BILL_SUM_HIST_T"."MTD_TOT" AS NET_TOTAL,
SUM(
SUM(
CASE WHEN "REPORTS"."WEK_MTD_BILL_SUM_HIST_T"."BLI_TYPE" = 'Charges'
THEN "WEK_MTD_BILL_SUM_HIST_T"."MTD_TOT" END)) OVER() AS TOTAL_CHARGES,
SUM(
SUM(
CASE WHEN "REPORTS"."WEK_MTD_BILL_SUM_HIST_T"."BLI_TYPE" = 'Credits'
THEN "WEK_MTD_BILL_SUM_HIST_T"."MTD_TOT" END)) OVER() AS TOTAL_CREDIT,
SUM(
SUM(
CASE WHEN "REPORTS"."WEK_MTD_BILL_SUM_HIST_T"."BLI_TYPE" = 'Previous Weekly Billing Net Total'
THEN "WEK_MTD_BILL_SUM_HIST_T"."MTD_TOT" END)) OVER() AS PRV_INVOICED,
SUM(
SUM(
CASE WHEN "REPORTS"."WEK_MTD_BILL_SUM_HIST_T"."BLI_TYPE" = 'Total Due / Receivable'
THEN "WEK_MTD_BILL_SUM_HIST_T"."MTD_TOT"
ELSE 0 END)) OVER() AS Net_Due_Total
FROM "REPORTS"."WEK_MTD_BILL_SUM_HIST_T"),
(SELECT
"REPORTS"."WEK_MTD_BILL_SUM_HIST_T"."BILL_CYCLE",
"REPORTS"."WEK_MTD_BILL_SUM_HIST_T"."MTD_TOT" AS NET_TOTAL,
(SUM(
SUM(
CASE WHEN "REPORTS"."WEK_MTD_BILL_SUM_HIST_T"."BLI_TYPE" = 'Total Due / Receivable' AND ( "REPORTS"."WEK_MTD_BILL_SUM_HIST_T"."BILL_CYCLE" BETWEEN '1-JAN-2022 4' AND :END_DATE)
THEN "WEK_MTD_BILL_SUM_HIST_T"."MTD_TOT" ELSE 0 END)) OVER()/COUNT(DISTINCT(BILL_CYCLE))) AS X
FROM
"REPORTS"."WEK_MTD_BILL_SUM_HIST_T"
WHERE
("REPORTS"."WEK_MTD_BILL_SUM_HIST_T"."BILL_CYCLE" BETWEEN :START_DATE AND :END_DATE )
)
GROUP BY
"REPORTS"."WEK_MTD_BILL_SUM_HIST_T"."MTD_TOT" ,
"REPORTS"."WEK_MTD_BILL_SUM_HIST_T"."BILL_CYCLE"
;

SQL for begin and end of data rows

I've got the following table:
and I was wondering if there is an SQL query, which would give me the begin and end Calender week (CW), where the value is greater than 0.
So in the case of the table above, a result like below:
Thanks in advance!
You can assign a group by counting the number of zeros and then aggregating:
select article_nr, min(year), max(year)
from (select t.*,
sum(case when amount = 0 then 1 else 0 end) over (partition by article_nr order by year) as grp
from t
) t
where amount > 0
group by article_nr, grp;
select Atricle_Nr, min(Year&CW) as 'Begin(Year&CW)',max(Year&CW) as 'End(Year&CW)'
from table where Amount>0 group by Atricle_Nr;

how to combine these two queries in a single query

How to combine these two queries into single query
1. select sum(amount) as received from voucher where amount>0
2. select sum(amount) as repaid from voucher where amount<0
You may use conditional aggregation
select sum( case when amount > 0 then amount else 0 end ) as received,
sum( case when amount < 0 then amount else 0 end ) as repaid
FROM t
You may use FILTER in Postgres 9.4 +
select sum( amount ) filter (where amount > 0 ) as received,
sum( amount ) filter (where amount < 0 ) as repaid
FROM t
Try this.,
select case when amount>0 then sum(amount) end as 'received',
case when amount<0 then sum(amount) end as 'repaid' from voucher

Simplify multiple subquery

I have a table with name loan_emi. I want select last emi payment date, last emi date, total no. of emi, no. of paid emi and no. of unpaid emi. here is my query.
SELECT MAX(emi_date)AS pay_date,
(SELECT MAX(emi_date) FROM loan_emi WHERE l_id=a.l_id AND is_paid=0
)AS last_date,
(SELECT COUNT(id) FROM loan_emi WHERE l_id=a.l_id
)AS tenor,
(SELECT COUNT(id) FROM loan_emi WHERE l_id=a.l_id AND is_paid=1
)AS paid,
(SELECT COUNT(id) FROM loan_emi WHERE l_id=a.l_id AND is_paid=0
)AS unpaid
FROM loan_emi a
WHERE id =" + lId + "
AND is_paid=1 GROUP BY l_id
But here is multiple sub queries. Please help me to simplify that. Thanks in advance.
just put into a single query with a case/when for each field. This way it is one pass for all the records for the ID in question, no joins, no subqueries. Also, as I have it, you probably want to PARAMETERIZE your query where indicated for the ID of the account in question...
SELECT
MAX( case when is_paid = 1 then emi_date else null end ) as Pay_Date,
MAX( case when is_paid = 0 then emi_date else null end ) as Last_Date,
COUNT(*) as Tenor,
SUM( case when is_paid = 1 then 1 else 0 end ) as Paid,
SUM( case when is_paid = 0 then 1 else 0 end ) as Unpaid
from
loan_emi
where
id = ?ParameterizeYourIncomingID
group by
l_id

Subtracting Two Column With Null

I use the following
select TotalCredits - TotalDebits as Difference
from
(
select
(select sum(TOTALAMOUNT) from journal where memberid=48 and CREDIT =1) as TotalCredits,
(select SUM(totalamount) from Journal where MEMBERID=48 and DEBIT =1) As TotalDebits
) temp
this returns one field with my difference, the problem i am occuring is that if the table has no credit, but has debits, the temp table contains a NULL value in the TotalCredits Field which prohibts math being done. (Vica Versa on has Credits but no Debits) I have tried coalese but cant seem how to make it work.
rationally i need to check if:
sum(TOTALAMOUNT) from journal where memberid=48 and CREDIT =1 as TotalCredits is
null then totalcredits = 0 and visa versa
sql server 2008
select ISNULL(TotalCredits,0) - ISNULL(TotalDebits,0) as Difference
from
(
select
(select sum(TOTALAMOUNT) from journal where memberid=48 and CREDIT =1) as TotalCredits,
(select SUM(totalamount) from Journal where MEMBERID=48 and DEBIT =1) As TotalDebits
) temp
Change your query to conditional aggregation and it fixes the problem:
select sum(case when credit = 1 then TotalAmount else -TotalAmount end) as Difference
from Journal
where memberid = 48 and (credit = 1 or debit = 1);
EDIT:
If you have the case where credit and debit could both be 1, then use:
select (sum(case when credit = 1 then TotalAmount else 0 end) -
sum(case when debit = 1 then TotalAmount else 0 end)
) as Difference
from Journal
where memberid = 48 and (credit = 1 or debit = 1);
Hello may be this can also can give the expected result
select COALESCE(TotalCredits,0) - COALESCE(TotalDebits,0) as Difference
from
(
select
(select sum(TOTALAMOUNT) from journal where memberid=48 and CREDIT =1) as TotalCredits,
(select SUM(totalamount) from Journal where MEMBERID=48 and DEBIT =1) As TotalDebits
) temp
Create a sub query using isnull to replace the null with 0 and then make the sum querys and then the total substraction
Ouch. That query makes my head hurt. Discriminant functions are your friend and case lets you create them easily in SQL. Just state the problem simply.
select total_credits = sum( case j.credit when 1 then 1 else 0 end ) ,
total_debits = sum( case j.debit when 1 then 1 else 0 end ) ,
total_delta = sum( case j.credit when 1 then 1 else 0 end )
- sum( case j.debit when 1 then 1 else 0 end )
from journal j
where j.memberid = 48