I can't select data from two tables using mysql. I have 2 tables sales and receipt.
The sale table contains
id date total
26 2014-07-16 5000
27 2014-07-16 7000
Receipt table structure
id date nettotal
18 2014-07-16 2000
19 2014-07-16 1000
I want to get result like as
date total nettotal
2014-07-16 5000
2014-07-16 7000
2014-07-16 2000
2014-07-16 1000
How get the result ?
Anybody help me?
Can you union them together?
SELECT date, total, null as nettotal
FROM sales
UNION
SELECT date, null, nettotal
FROM receipt
Related
I have to create a reports in one currency. I need to do query in MySQL without using PHP process. but unable to figure it out.
There is a table called currency_exchange_rate table as follows, (exchange rate in LKR to other currency).this table is updating like one record for each currency in LKR in every month
exchange_rates
id currency_id start_date exchange_rate
1 5 2017-01-2 155
2 4 2017-01-3 25
3 6 2017-01-3 53
4 5 2017-02-1 156
5 4 2017-02-1 24
6 6 2017-02-1 54
There is a project table as follows
pro_id name value currency_id status_id owner_id date
1 studio1 500 5 1 44 2017-01-20
2 lotus 120 5 1 42 2017-01-21
3 auro 300 4 2 45 2017-01-21
4 studio2 400 6 1 44 2017-01-22
5 holland 450 4 3 46 2017-02-05
6 studio3 120 4 3 47 2017-02-06
7 studio4 400 6 3 48 2017-02-06
how to generate reports in one currency(DKK but exchange rate in LKR) like status wise,monthly total, total by owner, etc..
and we have to consider currency id,currency to be convert and exchange rate for the month for those currency types to get relevant value for project row.
hope you are clear about my scenario. your help is much appreciated.
I don't need every report. just want a sql for convert values in project table using exchange rates table or status wise report as follows
status_id value_in_one_currency
1 xxxx
2 xxxx
3 xxxx
Try this:
SELECT A.status_id, A.`value` * B.exchange_rate `value_in_one_currency`
FROM project A JOIN exchange_rates B
ON A.currency_id=C.currency_id
AND DATE_FORMAT(A.`date`,'%m-%Y')=DATE_FORMAT(B.`start_date`,'%m-%Y');
See MySQL Join Made Easy for some insight.
This is what I finalize:
I took currency_id=5 as the final currency to be converted
SELECT A.*,C.exchange_rate AS DKK,D.exchange_rate AS LKR, (order_value * D.exchange_rate /C.exchange_rate ) AS `converted_value`
FROM projects A
LEFT JOIN exchange_rates C ON (DATE_FORMAT(C.start_date,'%Y-%m')=DATE_FORMAT(A.`date`,'%Y-%m') AND C.currency_id=5)
LEFT JOIN exchange_rates D ON DATE_FORMAT(D.start_date,'%Y-%m')=DATE_FORMAT(A.`date`,'%Y-%m') AND D.currency_id=A.currency_id
I'm stuck on this query. I need to do a group by date, card_id and only show the highest hits. I have this data:
date card_name card_id hits
29/02/2016 Paul Stanley 1345 12
29/02/2016 Phil Anselmo 1347 16
25/02/2016 Dave Mustaine 1349 10
25/02/2016 Ozzy 1351 17
23/02/2016 Jhonny Cash 1353 13
23/02/2016 Elvis 1355 15
20/02/2016 James Hethfield 1357 9
20/02/2016 Max Cavalera 1359 12
My query at the moment
SELECT DATE(card.create_date) `day`, `name`,card_model_id, count(1) hits
FROM card
Join card_model ON card.card_model_id = card_model.id
WHERE DATE(card.create_date) >= DATE(DATE_SUB(NOW(), INTERVAL 1 MONTH)) AND card_model.preview = 0
GROUP BY `day`, card_model_id
;
I want to group by date, card_id and filter the higher hits result showing only one row per date. As if I run a max(hits) with group by but I won't work
Like:
date card_name card_id hits
29/02/2016 Phil Anselmo 1347 16
25/02/2016 Ozzy 1351 17
23/02/2016 Elvis 1355 15
20/02/2016 Max Cavalera 1359 12
Any light on that will be appreciated. Thanks for reading.
Here is one way to do this. Based on your sample data (not the query):
select s.*
from sample s
where s.hits = (select max(s2.hits)
from sample s2
where date(s2.date) = date(s.date)
);
Your attempted query seems to have no relationship to the sample data, so it is unclear how to incorporate those tables (the attempted query has different columns and two tables).
Sorry it seems like a simple query but I am stuck on it. I am trying to get daily hours and wages from Totals table grouped by employee ID. The table has TimeinSeconds, WageAmount, EmpID, location and other information.
I am using this:
Select EmpID, Loc, Sum(timeinseconds/3600.0) as Hours, Paydate, wageamt
from Totals
where loc = 'locID'
and paycode IN (paycodenames)
and date > (getdate()-21)
Group by EmpID, Loc, date, wageamt
But I am getting breakdown and not daily Totals by Emp. It works if I don't use wageamt in the query. So, how can I Sum on both hours and wages?
EmpID Loc Hours Paydate wageamt
112 7 0.733333 08/06/14 14.666667
112 7 2.533333 08/06/14 50.666667
112 7 4 08/06/14 80
112 7 1.25 08/07/14 25
112 7 4 08/07/14 80
200 81 3.983333 08/06/14 31.866667
450 703 3.733333 08/06/14 108.789333
I am using SQL 2008. Thanks for any suggestions!
Use a GROUP BY and SUM like this:
select
EmpID
,Loc
,sum(Hours) as Hours
,PayDate
,sum(WageAmt) as WageAmt
from DataTable
group by
EmpID
,Loc
,PayDate
I am trying for join of two tables receipt and sale. but i dont get thse result.I want to get result between two date intervel
My sale table structure
id date total
26 2014-07-16 9000
27 2014-07-15 6000
Receipt table structure
id date nettotal
18 2014-07-16 1000
19 2014-07-15 2500
I want to get the result like
date total nettotal
2014-07-16 9000
2014-07-16 1000
2014-07-15 6000
2014-07-15 2500
Any body know these select query for get these result?
select date, total, nettotal
from (select date, total, null as nettotal, 2 as sort_col
from sale
union all
select date null as total, nettotal, 1 as sort_col
from receipt) as a
order by date desc, sort_col desc
Note: You need to show what you've tried
I have one table accounts. I have written following query
chk_account= mysql_query("SELECT GROUP_CONCAT( DISTINCT user_id ) AS userlist
FROM `accounts`
");
From this I get users id only. With this query I also want to fetch data price and date with column name price and created but I need only to select with lowest date
I have table structure like this:
id user_id price created
1 31 10 2013-04-09 17:30:15
2 32 20 2013-04-10 20:24:40
3 31 30 2013-04-11 04:44:25
4 33 40 2013-04-12 05:47:18
5 34 50 2013-04-13 19:54:15
6 34 50 2013-04-14 14:27:15
7 35 10 2013-04-15 13:54:45
8 35 60 2013-04-16 12:24:35
9 35 10 2013-04-17 20:34:10
I suspect that you want the earliest date and price for each user. You can do this using group_concat(), using a query such as:
select USER_ID,
substring_index(group_concat(price order by created), ',', 1) as price,
min(created)
from accounts a
group by user_id