SQL Server correlated query want to take sum - sql

select
SUM(Convert(int, (Select SUM(Convert(int, amount))
from ChargesType
where ChargesType.ID = Charges.ChargesTypeID))) as Amount
from Charges
Where Charges.RollNo = 1
This is my query; I want to get sum of amount column.

Try this instead:
select
SUM(t.Amount) as Amount
from Charges AS c
INNER JOIN
(
SELECT Convert(int, amount) AS Amount, ID
FROM ChargesType
) AS t ON t.ID = c.ChargesTypeID
Where c.RollNo = 1

Related

SQL MAX in two columns by statistic

I have this query:
DECLARE #startTime DATETIME = DATEADD(MINUTE, -100, GETDATE()) --StartTime
SELECT
COUNT(*) Frecuency, mes.receivedqty AS Qty, ac.item AS Item
FROM
mesReservationReceipts mes (nolock)
INNER JOIN
ACCS_Reservation ac (nolock) ON ac.IDReservation = mes.idReservation
WHERE
ac.item IN (SELECT ac2.item
FROM mesReservationReceipts m2
INNER JOIN ACCS_Reservation ac2 ON ac2.IDReservation = m2.idReservation
WHERE m2.receivedate > #startTime)
GROUP BY
mes.receivedqty, ac.item
I get this result, but only I want the yellow highlighted rows - how can I get those? Please help!:
Note: I tried with MAX(Frequency) but that does not work because it should be grouped by the qty, and its the same case. I put a MAX(Qty), but for example, if the Qty is more than Statistic, add in the table result (and I only want the real statistic qty).
You can write something like this
SELECT * FROM(SELECT Frequency,Receivedqty,Item,
ROW_NUMBER() OVER(Partition by Item ORDER BY Quantity desc) as RowId
FROM (
----your query-----
))as q
Where q.RowId = 1
You can use row_number() to get the highest amount in each column. Then filter:
select item, Frecuency, qty
from (select ac.item as Item, count(*) as Frecuency, mes.receivedqty as Qty,
row_number() over (order by count(*) desc) as seqnum_f,
row_number() over (order by mes.receivedqty desc) as seqnum_r
from mesReservationReceipts mes join
ACCS_Reservation ac
on ac.IDReservation = mes.idReservation
where ac.item in (select ac2.item
from mesReservationReceipts m2 inner join
ACCS_Reservation ac2
on ac2.IDReservation = m2.idReservation
where m2.receivedate > #startTime
)
group by mes.receivedqty, ac.item
) ma
where 1 in (seqnum_f, seqnum_r);
Use rank() if you want duplicates, in the event that the highest values have duplicates.

SQL: How to select the highest priced used item for each day

I need to produce a query that would give me the highest priced used product for each day where the total price of products sold that day exceeds 200.
SELECT *, max(price)
FROM products
WHERE products.`condition` = 'used' and products.price > 200
GROUP BY date_sold
Here is my products table http://prntscr.com/of3hjd
You could try using a join with sum for price > 200 group by date_sol
select m.date_sold, max(m.price)
from my_table m
inner join (
select date_sold, sum(price)
from my_table
group by date_sold
having sum(price)>200
) t on t.date_sold = m.date_sold
group by m.date_sold
You can use window functions for this:
select p.*
from (select p.*,
sum(price) over (partition by date_sold) as sum_price,
row_number() over (partition by date_sold, condition order by price desc) as seqnum
from products p
) p
where sum_price > 200 and
condition = 'used' and
seqnum = 1;
SELECT *, max(price) FROM products
where products.`condition` = 'used' and sum(products.price) > 200
GROUP BY day(date_sold)

SQL Server : sum and multiplies on 2 tables

I need help to SUM and MULTIPLIES on join 2 tables:
tb1
tb2
In tb1, I need to sum QTY and multiples SKU with PRICE without repeating same SKU (21135208, 21035621):
Current query:
SELECT
tb1.DOC_NO,
CAST(SUM(tb2.QTY) AS FLOAT) AS QTY_TOTAL,
ROUND(CAST(SUM(tb2.QTY * tb2.PRICE) AS FLOAT), 2) AS PRICE_TOTAL,
tb1.DATE,
tb1.STATUS_A,
tb2.STATUS_B
FROM
tb1
INNER JOIN
tb2 ON tb1.DOC_NO = tb2.DOC_NO
WHERE
tb1.STATUS_B = '0'
GROUP BY
tb1.DOC_NO, tb1.DATE,
tb1.STATUS_A, tb1.STATUS_B
ORDER BY
COH.DOC_NO_REQ_TO_ULI DESC
My result is:
Expected result is:
I believe that you could filter out duplicates by using a subquery like SELECT DISTINCT ... FROM tb1, while leaving the rest of the query untouched:
SELECT
tb0.DOC_NO,
CAST ( SUM ( tb2.QTY ) AS FLOAT ) AS QTY_TOTAL,
ROUND( CAST ( SUM ( tb2.QTY * tb2.PRICE ) AS FLOAT ), 2) AS PRICE_TOTAL,
tb0.DATE,
tb2.STATUS_A,
tb2.STATUS_B
FROM
(SELECT DISTINCT DOC_NO, CM, SKU, PRICE, QTY, DATE FROM tb1) AS tb0
INNER JOIN tb2 ON tb0.DOC_NO = tb2.DOC_NO
WHERE
tb2.STATUS_B = '0'
GROUP BY
tb0.DOC_NO,
tb0.DATE,
tb2.STATUS_A,
tb2.STATUS_B
ORDER BY
COH.DOC_NO_REQ_TO_ULI DESC
NB: there are some issues with the table aliases in your query:
columns STATUS_A or STATUS_B should be prefixed tb2, not tb1 (I fixed that)
alias COH which is being used in the ORDER BY clause is not declared anywhere in the query (that's a syntax error)

calculate the balance amount and insert another table

I need some help on building an sql query, I have below two queries, I want to combine the sum of debit substracted from credit and then insert result as balance into another table
select sum(amount)
from ACCOUNT_TRANSACTIONS
where CUSTOMER_USER_NAME='55555' and transaction_type='credit' and account_type='customer' and IS_DELETED='false'
select sum(amount)
from ACCOUNT_TRANSACTIONS
where CUSTOMER_USER_NAME='55555' and transaction_type='debit' and account_type='customer' and IS_DELETED='false'
Could use CROSS APPLY
select a.*, b.CreditSum, c.DebitSum
from ACCOUNT_TRANSACTIONS a
cross apply
(select sum(amount) as CreditSum
from ACCOUNT_TRANSACTIONS
where CUSTOMER_USER_NAME='55555' and transaction_type='credit' and account_type='customer' and IS_DELETED='false'
) b
cross apply
(select sum(amount) as DebitSum
from ACCOUNT_TRANSACTIONS
where CUSTOMER_USER_NAME='55555' and transaction_type='debit' and account_type='customer' and IS_DELETED='false'
) c
where a.CUSTOMER_USER_NAME='55555' and a.account_type='customer' and a.IS_DELETED='false'
You can do this using conditional aggregation:
select sum(case when transaction_type = 'credit' then amount when transaction_type = 'debit' then - amount end) as balance
from ACCOUNT_TRANSACTIONS
where CUSTOMER_USER_NAME = '55555' and
account_type = 'customer' and
IS_DELETED = 'false' ;
You would then insert this into another table using insert, although I'm not sure how a single value in a single row would be useful.

CASE statement based on SUM results in query

I have a query where I am using SUM to total up bill detail values. I would like to create a CASE statement that works off the results of that sum, rather than the values being summed. Below is a partial example of what my query currently looks like.
SELECT
bill.billnumber_id,
billdetail.billclass,
SUM(billdetail.amount) AS Amount
GROUP BY bill.billnumber_id, billdetail.billclass
Say this totals 4 lines of billdetail (2, 0, 5, 3) for a summed amount of 10. I would like to do a CASE statement based on the 10 amount, rather than the individual values. Thanks.
Is this what you want?
SELECT bill.billnumber_id, billdetail.billclass, SUM(billdetail.amount) AS Amount,
(case when SUM(billdetail.amount) > 10 then 'BigOne'
else 'Little'
end)
GROUP BY bill.billnumber_id, billdetail.billclass
You need to use a sub-query:
SELECT CASE WHEN Amount = 0 THEN 'Nothing' ELSE 'somthing' END AmountComment
FROM(
SELECT bill.billnumber_id, billdetail.billclass, SUM(billdetail.amount) AS Amount
GROUP BY bill.billnumber_id, billdetail.billclass
)x
SELECT bill.billnumber_id, billdetail.billclass, (Select SUM(billdetail.amount) from billdetail)
AS Amount
from bill inner join billdetail on bill.id = buildetail.kBillId
/* include from join (not added above)*/
GROUP BY bill.billnumber_id, billdetail.billclass
I believe this is what you are after... you might wanat to include the same from structure in your inner select statement as:
SELECT bill.billnumber_id, billdetail.billclass, (Select SUM(billdetail.amount) from bill inner
join billdetail on bill.id = buildetail.kBillId ) AS Amount
from bill inner join billdetail on bill.id = buildetail.kBillId
/* include from join (not added above)*/
GROUP BY bill.billnumber_id, billdetail.billclass
Let me know if this is what you meant.
You can either make it a subquery:
SELECT billnumber_id, billclass, CASE WHEN Amount = 10 THEN ... ELSE ... END
FROM
(
SELECT bill.billnumber_id, billdetail.billclass, SUM(billdetail.amount) AS Amount
GROUP BY bill.billnumber_id, billdetail.billclass
) A
Or just repeat the aggregation:
SELECT bill.billnumber_id, billdetail.billclass, SUM(billdetail.amount) AS Amount
, CASE WHEN SUM(billdetail.amount) = 10 THEN ... ELSE ... END
GROUP BY bill.billnumber_id, billdetail.billclass