Fetch Data from multi table datewise - mysql

Suppose:
Table Name : purchase
id pur_Date pur_value
1 01/02/16 5000.00
2 02/02/16 2500.00
3 05/02/16 7000.00
Table Name : Sale
id sale_Date sale_value
1 02/02/16 5050.00
2 02/02/16 2555.00
3 05/02/16 9000.00
4 05/02/16 7000.00
5 05/02/16 7250.00
Run a query...............
Result would like below:
Date Value
01/02/16 5000.00
02/02/16 2500.00
02/02/16 5050.00
02/02/16 2555.00
05/02/16 7000.00
05/02/16 9000.00
05/02/16 7000.00
05/02/16 7250.00
Please solve it.

Use UNION ALL.
select t.* from
(
select pur_Date as Date, pur_value as Value
from purchase
union all
select sale_Date as Date, sale_value as Value
from sale
)t
order by t.Date;

try this.
(SELECT pur_Date, pur_value FROM purchase)
UNION ALL
(SELECT Date, Value FROM Sale)
ORDER BY 1;

Use Union All query and sort by another assumed string Date by key as:
SQL>
select pur_Date as Date, pur_value as Value
from purchase
union all
select sale_Date as Date, sale_value as Value
from sale
order by 1;

Related

How to select sum of column values using from and to time values? [duplicate]

I have a table with the following structure and sample data:
STORE_ID | INS_TIME | TOTAL_AMOUNT
2 07:46:01 20
3 19:20:05 100
4 12:40:21 87
5 09:05:08 5
6 11:30:00 12
6 14:22:07 100
I need to get the hourly sum of TOTAL_AMOUNT for each STORE_ID.
I tried the following query but i don't know if it's correct.
SELECT STORE_ID, SUM(TOTAL_AMOUNT) , HOUR(INS_TIME) as HOUR FROM VENDAS201302
WHERE MINUTE(INS_TIME) <=59
GROUP BY HOUR,STORE_ID
ORDER BY INS_TIME;
Not sure why you are not considering different days here. You could get the hourly sum using Datepart() function as below in Sql-Server:
DEMO
SELECT STORE_ID, SUM(TOTAL_AMOUNT) HOURLY_SUM
FROM t1
GROUP BY STORE_ID, datepart(hour,convert(datetime,INS_TIME))
ORDER BY STORE_ID
SELECT STORE_ID,
HOUR(INS_TIME) as HOUR_OF_TIME,
SUM(TOTAL_AMOUNT) as AMOUNT_SUM
FROM VENDAS201302
GROUP BY STORE_ID, HOUR_OF_TIME
ORDER BY INS_TIME;

Sum of values from 2 columns from 2 tables AND sorting by a column

I have 2 tables:
Table "credits":
id | amount | type
1 8 1
2 7 2
3 2 1
4 1 1
5 5 3
6 4 2
and
Table "debits":
id | amount
1 3
1 2
3 2
4 1
5 3
5 1
I need to get the sum of all "id's" balances (credit-debit) and grouping it by "type". So far I have this:
SELECT id, SUM(amount) as balance,
FROM
(
SELECT id, amount FROM credits
UNION ALL
SELECT id, -amount FROM debits
)
unified_table
GROUP BY id
But it just gives me the "id's" balances:
id | balance
1 3
2 7
3 0
4 0
5 1
6 4
Ideally, I need something like this:
type | balance
1 3
2 11
3 1
I tried to add the "type" column in the first "select" of the union, and then group by "type". But not working I think because table "debits" dont have column "type". How can I accomplish this? Thank you for your help
I think this would do it:
SELECT c.type, sum(c.amount - IFNULL(d.amount,0))
FROM credits c LEFT OUTER JOIN (SELECT id, sum(amount) FROM debits GROUP BY id) d
ON c.id=d.id
GROUP BY c.type
The idea is to group the debits table first, and then join it with the credits table, which will result in a table that you can group by type
Try this:
SELECT Type, Sum(Amount)
FROM (
SELECT C.Amount - ISNULL(D.Amount, 0) AS Amount, C.Type
FROM Credits C
LEFT JOIN (SELECT Id, Sum(Amount)
FROM Debits
GROUP BY ID) D ON C.Id = D.Id
) A
GROUP BY A.Type
Here is my solution:
SELECT
credits.`type`,
credits.`amount` - IFNULL(t_debit.`d_amount`, 0) AS balance
FROM
credits,
(SELECT id, SUM(amount) AS d_amount FROM debits GROUP BY id)t_debit
WHERE
credits.`id` = t_debit.`id`
GROUP BY
credits.`type`;
First I select sum of amounts from debits table group by id and after I did another select query on the credits table where credit id match to debit id. I don't use UNION operator because the id's column in debits table is an foreign key.

Count distinct does not work in Oracle SQL Developer

I need to count distinct date in one column but I do not want to group by that.
Datasource table:
ID zip5 date volume
11 11111 01/03/16 5
12 11111 01/03/16 6
13 11111 01/04/16 7
My code:
Select zip5, count(ID), count(distinct (date)), sum(volume),
from table
group by zip5
Desired output:
zip5 count(ID) count(distinct (date)) sum(volume)
11111 2 1 11
11111 1 1 7
However, what I got is:
zip5 count(ID) count(distinct (date)) sum(volume)
11111 3 3 18
Most likely, the problem you are facing is that your date column has a time component -- and, alas, it is not shown when you select from the table.
You can truncate the date to remove the time component. So try this:
Select zip5, count(ID), count(distinct trunc(date)), sum(volume),
from table
group by zip5;
To get your desired output simple add the "DATE" column in the GROUP BY expression.
Select zip5, count(ID), count(distinct ("DATE")), sum(volume)
from "TABLE"
group by zip5,"DATE"
order by 1,2;
ZIP5 COUNT(ID) COUNT(DISTINCT("DATE")) SUM(VOLUME)
---------- ---------- ----------------------- -----------
11111 1 1 7
11111 2 1 11
You may group by (i.e. select in disctinct rows) even columns that are not in the select list.

Query orders table and get most recent record for each customer

I have a table of orders. Customers can appear multiple times. The orderID column is an auto increment. I need to run a query to get the most recent order for each customer BUT I need to get the orderID, orderDate and orderProduct of their latest order.
customer orderID orderDate orderProduct
1 1 2015-01-01 shoes
1 2 2015-02-01 food
1 3 2015-03-01 drinks
2 4 2015-01-01 water
2 5 2015-04-01 beer
3 6 2015-01-01 pizza
3 7 2015-07-01 pasta
I had hoped to use:
select orders.*, max(orderDate) as latestOrder from orders group by customer
But this doesn't seem to give me what I need.
The results I am looking for would be:
customer orderID orderDate orderProduct
1 3 2015-03-01 drinks
2 5 2015-04-01 beer
3 7 2015-07-01 pasta
use some kind of self-join here
select t1.* from orders t1
inner join (
select customer, max(orderDate) as latestOrder from orders
group by customer
) t2
where t1.customer = t2.customer AND t1.orderDate = t2.latestOrder
WITH CTE AS(
SELECT customer,orderID,orderDate,orderProduct,
ROW_NUMBER()OVER(PARTITION BY customer ORDER BY orderID desc)
FROM tab
)
select * from tab where orderid in (
select max(orderid) as maxorder from cte group by customer)
This should work with any RDBMS that supports CTEs. Heres a demo

How to (MySQL) multiply columns and then the sum rows?

I've got this table:
id | payment_id | quantity | unit cost
1 633 1 750
2 633 1 750
3 632 2 750
4 632 2 750
What I need is :
payemnt_id | total
633 1500
632 3000
You can also think of this as Countries and then you are trying to find the total for each Country. I was sure there were some tutorials like this but could not find by STFW.
You can simply put the formula (expression) inside SUM:
SELECT payment_id, SUM(`unit cost` * quantity) AS total
FROM myTable
GROUP BY payment_id
SELECT
payment_id,
SUM(subtotal) AS total
FROM(
SELECT
payment_id,
(unit_cost * quantity) AS subtotal
) AS t1
GROUP BY
payment_id
SELECT payemnt_id, SUM(`unit cost` * quantity) as total
FROM Table1
GROUP BY payemnt_id
SELECT COALESCE (SUM(cost_field * quantity_field),0) AS tot FROM Table_Name
I Think This One Is Good, Why ?
Because If Their is No Result It Will Give You Zero