I have to use If Else in Sql Server Query.
Lets Consider the Table below
Product Kg Qty Rate
----------------------------
Mango 1 3 70
Orange 0 2 80
Apple 3 4 90
I need to sum Kg, qty and rate(kgQtyrate) as Total. But when kg is 0 i need to sum only qty*rate.
I need a output like,
Product Kg Qty Rate Total
-----------------------------------
Mango 1 3 70 210
Orange 0 2 80 160
Apple 3 4 90 1080
How can I use If Else here???
You could use CASE:
SELECT
*,
(CASE Kg WHEN 0 THEN 1 ELSE Kg END) * Qty * Rate AS Total
FROM Products
Please have a look here.
you can use this query
select *, case when kg > 0
then kg + qty + rate
else
qty + rate
end as "total"
from table3
look at attached image
Related
I have a table that looks like this:
customer_id item price cost
1 Shoe 120 36
1 Bag 180 50
1 Shirt 30 9
2 Shoe 150 40
3 Shirt 30 9
4 Shoe 120 36
5 Shorts 65 14
I am trying to find the most expensive item each customer bought along with the cost of item and the item name.
I'm able to do the first part:
SELECT customer_id, max(price)
FROM sales
GROUP BY customer_id;
Which gives me:
customer_id price
1 180
2 150
3 30
4 120
5 65
How do I get this output to also show me the item and it's cost in the output? So output should look like this...
customer_id price item cost
1 180 Bag 50
2 150 Shoe 40
3 30 Shirt 9
4 120 Shoe 36
5 65 Shorts 14
I'm assuming its a Select statement within a Select? I would appreciate the help as I'm fairly new to SQL.
One method that usually has good performance is a correlated subquery:
select s.*
from sales s
where s.price = (select max(s2.price)
from sales s2
where s2.customer_id = s.customer_id
);
I have table with productcode and mrp like
Pcode MRP
1 30
2 30
2 35
3 100
4 150
4 150
5 45
6 120
6 122
6 125
I want to find which productcodes have more than two mrp.
Thanks in advance.
If you get the count and use the having clause, you should get what you are looking for.
select pcode, count(pcode)
From tab
group by pcode
having count(pcode) > 1
I have a table report of production, and I want to bridge between minus value and positive value on QTY field.
I want to make a new column with positive value, and another column with negative value selected from the QTY field.
mtl_trx qty uom
1 20 1230 KG
2 39 950 KG
3 45 100 LBR
4 91 250 KG
5 118 -500 KG
6 125 -284 KG
7 137 -120 KG
8 143 -80 KG
If I understand correctly, you want to select two columns, one showing the positiv values, one the negative ones? Use a case construct to decide whether to show a value or not.
select mtl_trx, qty, uom,
case when qty > 0 then qty end as qty_pos,
case when qty < 0 then qty end as qty_neg
from mytable;
select mtl_trx, qty, uom,
case when qty > 0 then qty else 0 end as positive,
case when qty < 0 then qty else 0 end as negative
from production;
I can't figure out the correct query on Oracle SQL
I have the following data:
Name Monthly_amount Start_date
Bob 100 April 2014
Mike 120 June 2014
Steve 80 Sept 2014
Bob 50 Dec 2014
And I would like to get the following result
Name |Jan-14| Feb-14| Mar-14| Apr-14 |May-14| Jun-14| Jul-14 |Aug-14|Sep-14|Oct-14| Nov-14| Dec-14
Bob 0 0 0 100 100 100 100 100 100 100 100 150
Mike 0 0 0 0 0 120 120 120 120 120 120 120
Steve 0 0 0 0 0 0 0 0 80 80 80 80
Something along the lines of this should work for you. If you need the column definitions to be dynamic, you would have to create it dynamically, but it's better to do that kind of things in your application.
SELECT Name,
SUM(CASE WHEN Start_date <= TO_DATE('01-JAN-2014','DD-MON-YYYY')
THEN Monthly_Amount ELSE 0 END) AS Jan14,
SUM(CASE WHEN Start_date <= TO_DATE('01-FEB-2014','DD-MON-YYYY')
THEN Monthly_Amount ELSE 0 END) AS Feb14,
(etc.)
FROM table1
GROUP BY Name;
(This assumes Name uniquely defines a person)
I can't figure this question out by myself so I am wondering if you guys could help me. I tried to find my answer on google, stackoverflow ans several other locations but without any result..
I made a query:
SELECT leverancier.leveranciers_id,
medicijn.artikelnr,
medicijn.naam,
medicijn.in_voorraad,
medicijn.min_voorraad,
min(order_medicijn.inkoopprijs) AS price
FROM leverancier
INNER JOIN voorraadorder
ON leverancier.leveranciers_id = voorraadorder.leverancier_id
INNER JOIN order_medicijn
ON order_medicijn.voorraadorder_id = voorraadorder.voorraadorder_id
INNER JOIN medicijn
ON medicijn.artikelnr = order_medicijn.artikel_id
GROUP BY leverancier.leveranciers_id,
medicijn.artikelnr,
medicijn.naam,
medicijn.in_voorraad,
medicijn.min_voorraad
ORDER BY artikelnr
Which gives the following result:
Leveranciers_ID / artikelnr / naam in_voorraad / min_voorraad / price
2 1 Aspirine 100 50 0.74
1 1 Aspirine 100 50 0.75
2 2 Abacivr 200 180 4.50
4 2 Abacivr 200 180 4.00
4 3 Acetazolamide 100 90 1.20
5 4 Ciclovir 145 120 0.50
3 5 levoceterizine 125 120 2.00
Here comes the question:
What query would I need to get the same result but only with the records where the price is the lowest for each artikelnr. So the result would be like this:
Leveranciers_ID / artikelnr / naam in_voorraad / min_voorraad / price
2 1 Aspirine 100 50 0.74
4 2 Abacivr 200 180 4.00
4 3 Acetazolamide 100 90 1.20
5 4 Ciclovir 145 120 0.50
3 5 levoceterizine 125 120 2.00
If there is any additional information required to answer this question, please ask me.
Thanks in advance
One of the possible solutions: Adding one more inner SQL that computes the min price of each artikelnr and using the HAVING clause, which uses the inner SQL, to filter the result.
Cheers