Get total ans subtotal with MYSQL QUERY and GROUP BY - mysql

In SQL I want to get total by id. Please suggest me for the query
INPUT
id | qty | price
1 | 3 | 200
1 | 4 | 225
1 | 5 | 250
2 | 7 | 300
2 | 8 | 300
3 | 3 | 500
3 | 5 | 500
3 | 6 | 500
4 | 3 | 700
4 | 2 | 745
OUTPUT
id | total
1 | 2750
2 | 4500
3 | 7000
4 | 3590

This query sums the quantity times price for all records having the same id, for each id value in your table.
SELECT id,
SUM(qty*price) AS total
FROM yourTable
GROUP BY id

Related

subtract two column from different table and group by id and id

after two week search i couldn't write this query please help me
NOTE
TABLE 1 sum(quantity_box) group by id_p and id_w
TABLE 2 sum(quantity_box) group by id_p and id_w
then
TABLE1.sum(quantity_box)- TABLE2.sum(quantity_box) where id_p = id_p and id_w = id_w
table 1 ) wherehouseproduct_add id = id_w
id | name
10 | warehouse1
20 | warehouse2
table2) wherehouse_products
id | id_w |id_p |quantity_box
1 | 10 | 2 | 10
2 | 10 | 2 | 50
3 | 20 | 3 | 100
4 | 20 | 1 | 20
5 | 20 | 1 | 10
6 | 10 | 1 | 10
7 | 10 | 3 | 10
table3) wherehouse_products_sell
id | id_w |id_p |quantity_box
1 | 10 | 2 | 50
2 | 20 | 3 | 30
3 | 20 | 1 | 20
table4) products
id_p | product_name
1 | snack
2 | watebottle
3 | headphone
i want to output like
id_w | id_p | product_name| total_quantity_box
10 | 1 | snack |10
10 | 2 | watebottle |10
10 | 3 | headphone |10
20 | 1 | snack |10
20 | 2 | watebottle |10
20 | 3 | headphone |70
Calculate each type of sum individually in sub selects and join them as per your criteria then do your calculations in outer select
select wp.id_w,
wp.id_p,
wp.quantity - wps.quantity total_quantity_box
from
(select id_w,id_p,sum(quantity_box) quantity from wherehouse_products group by id_w,id_p) wp
join
(select id_w,id_p,sum(quantity_box) quantity from wherehouse_products_sell group by id_w,id_p) wps
using(id_w,id_p)
DEMO

need help a query in sql server 2008

Hi I have doubt in sql server
Table : patient
patientno | refdoctor| loccode | status | sdate
100 | 31 |10 | 1 | 2012-05-03
100 | 32 |10 | 1 |1997-02-04
100 | 36 |10 | 1 |2014-09-16
100 |35 |10 | 1 |2013-05-03
100 | 50 |10 | 1 | 1988-05-08
100 | 20 |10 | 2 |2015-02-05
Table : targetpatient
patientno | refdoctor| loccode | status | sdate
100 | 21 | 10 | 2 | 2004-05-18
100 | 23 | 10 | 2 |2005-07-25
100 | 24 | 10 | 2 | 2006-06-22
100 | 26 | 10 | 2 |2012-05-14
100 | 28 |10 | 2 |2013-05-03
100 |29 |10 | 2 | 2014-09-26
100 | 33 | 10 | 2 | 2012-10-22
100 | 39 | 10 | 2 |2002-12-13
100 |41 | 10 | 2 |2012-05-13
Here I want output patient table relates status
statusvalue=5's sdate is less than or equal to checkvalue=2's sdate and
the difference between the dates should be less than 30days then given count
if that condition not fall then count must be return zero(0)
select o.patientno,o.loccode,o.status,o.sDate, count(*) as cnt
from patient o join targetpatient t on o.patientno=t.patientno and o.loccode=t.loccode and o.status in('1') and t.status in('2') and
o.sDate<=t.sdate
and datediff(dd,o.sdate,t.sdate)<=30
group by o.patientno,o.loccode,o.status,o.sDate
based on above query I got result like below:
patientno | loccode | status | sdate | count
100 | 10 | 1 |2012-05-03 | 2
100 | 10 | 1 |2013-05-03 | 1
100 | 10 | 1 |2014-09-16 | 1
but I want expected result like below
patientno | loccode | status | sdate | count
100 | 10 | 1 |2012-05-03 | 2
100 | 10 | 1 |2013-05-03 | 1
100 | 10 | 1 |2014-09-16 | 1
100 | 10 | 1 | 1997-02-04 | 0
100 | 10 |1 | 1988-05-08 | 0
please tell me how to write query to achive this task in sql server .
If you want to include the records that doesn't have any matches in the joined table you'll want to use a left join instead and do the count on a column in the joined table instead like this:
select o.patientno,o.loccode,o.status,o.sDate, count(t.sdate) as cnt
from patient o
left join targetpatient t on o.patientno = t.patientno
and o.loccode = t.loccode
and o.status in('1')
and t.status in('2')
and o.sDate <= t.sdate
and datediff(dd,o.sdate,t.sdate) <= 30
group by o.patientno,o.loccode,o.status,o.sDate
A left join will return all rows from the table on the left side (patient in your case) plus the matching rows from the right-hand side table. The rows that doesn't have any matches in the right-hand side table will get null as values.

MySql: Summing the latest amounts banked for each category and type

I have four MySql tables (simplified here):
Table 1: factions (just a list to reference)
id | name
1 | FactionName1
2 | FactionName2
Table 2: currencies (just a list to reference)
id | name
1 | Currency1
2 | Currency2
3 | Currency3
Table 3: events (just a list to reference)
id | name | date
1 | Evebt1 | 2013-10-16
2 | Event2 | 2013-10-18 (Note: date out of order)
3 | Event3 | 2013-10-17
Table 4: event_banking (data entered after each event, remaining amount of each currency for each group)
id | faction_id | currency_id | event_id | amount
1 | 1 | 1 | 1 | 10
2 | 1 | 1 | 2 | 20
3 | 1 | 1 | 3 | 30
4 | 1 | 2 | 1 | 40
5 | 1 | 2 | 2 | 50
6 | 1 | 2 | 3 | 60
7 | 1 | 3 | 1 | 70
8 | 1 | 3 | 2 | 80
9 | 1 | 3 | 3 | 90
10 | 2 | 1 | 1 | 100
11 | 2 | 1 | 2 | 110
12 | 2 | 1 | 3 | 120
13 | 2 | 2 | 1 | 130
14 | 2 | 2 | 2 | 140
15 | 2 | 2 | 3 | 150
16 | 2 | 3 | 1 | 160
17 | 2 | 3 | 3 | 170
Note: Faction 2 didn't bank Currency 3 for Event 2
What I'm looking to be able to do is to get, for each currency, the total of the last banked (date wise) for each faction. (ie How much of each currency is currently banked in total if all factions are merged)
So, I need a table looking something like:
currency_id | total
1 | 130 (eg 20 + 110)
2 | 190 (eg 50 + 140)
3 | 250 (eg 80 + 170) <- Uses Event 3 for Group 2 as Event 2 doesn't exist
I can do basic joins etc, but I'm struggling to be able to filter the results so that I get the latest results for each Faction x Currency x Event so I can then sum them together to get the final total amounts for each currency.
I've tried various permutations of LEFT OUTER JOINs, GROUP BYss & HAVING COUNTs, and had some interesting (but incorrect results), and a variety of different error codes, but nothing remotely close to what I need.
Can anyone help?
I guess you can go on with something like this:
select eb.currency_id, sum(amount) as total
from events e
inner join (
select faction_id, currency_id, max(date) as md
from event_banking eb
inner join events e
on eb.event_id = e.id
group by faction_id, currency_id
) a
on e.date = a.md
inner join event_banking eb
on e.id = eb.event_id
and a.faction_id = eb.faction_id
and a.currency_id = eb.currency_id
group by currency_id;
Here is SQL Fiddle

mysql query for conditions on related tables

i have a table 'house' with a house id, a table 'room' with a room id, and a relation table for those two tables
HOUSE
-----
1 | house1
2 | house2
3 | house3
4 | house4
5 | house5
ROOM
------
1 | kitchen
2 | bathroom
3 | garage
HOUSE_ROOMS
------------
id | house_id | room_id | size
=================================
1 | 1 | 1 | 200
2 | 1 | 2 | 300
3 | 2 | 1 | 400
4 | 2 | 2 | 500
5 | 3 | 1 | 500
6 | 4 | 2 | 600
7 | 5 | 1 | 400
8 | 5 | 5 | 300
I'm having trouble writing a query that returns an array of house id's by some combined conditions:
example: get all houses with a kitchen sized between 350 and 450 AND a bathroom sized between 450 and 550
-> result should be an array containing house2
anyone know how i should write this query?
Assuming all your IDs are fixed, the following quick query will work:
SELECT HOUSE_ID
FROM HOUSE_ROOMS
WHERE (ROOM_ID=2 AND SIZE>=450 AND SIZE<=550)
OR (ROOM_ID=1 AND SIZE>=350 AND SIZE<=450)
GROUP BY HOUSE_ID
HAVING COUNT(DISTINCT ROOM_ID)>1

MySQL get total number of orders and sum of total by user

I have a table called orders where I store information about orders made by an User.
order_id | user_id | amount
+++++++++++++++++++++++++++++++++++
1 | 1 | 100
2 | 1 | 200
3 | 2 | 200
4 | 3 | 100
5 | 3 | 100
6 | 4 | 500
What I want as final outcome is,
Number of Orders made by an user and total value of those orders.
So in above case output should look like,
user_id | count | sum
+++++++++++++++++++++++++++++++++++
1 | 2 | 300
2 | 1 | 200
3 | 3 | 600
4 | 1 | 500
What you need here is, just a GROUP BY with COUNT and SUM like so:
SELECT
user_id,
COUNT(User_id) Count,
SUM(amount) Sum
FROM Orders
GROUP BY user_id;
SQL Fiddle Demo
This will give you:
| USER_ID | COUNT | SUM |
-------------------------
| 1 | 2 | 300 |
| 2 | 1 | 200 |
| 3 | 2 | 200 |
| 4 | 1 | 500 |