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
Related
I have a table that shows , for each date, a list of customer ids - shows customers who were active on any particular day. So each date can include ids that are also present in another date.
bdate customer_id
2012-01-12 111
2012-01-13 222
2012-01-13 333
2012-01-14 111
2012-01-14 333
2012-01-14 666
2012-01-14 777
I am looking to write a query which calculates the total number of unique ids between two dates - the starting date is the row date and the ending date is a particular date in the future.
My query looks like this:
select
bdate,
count(distinct customer_id) as cts
from users
where bdate between bdate and current_date
group by 1
order by 1
But this produces a count of unique users for each date like this:
bdate customer_id
2012-01-12 1
2012-01-13 2
2012-01-14 4
my desired result is ( for a count of users between starting row date and 2012-01-14 )
bdate customer_id
2012-01-12 5 - includes (111,222,333,666,777)
2012-01-13 5 - includes (222,333,111,666,777)
2012-01-14 4 - includes (111,333,666,777)
Like #Strawberry said, you can make a join like this:
select
t1.bdate,
count(distinct t2.customer_id) as cts
from users t1
join users t2 on t2.bdate >= t1.bdate
where t1.bdate between t1.bdate and current_date
group by t1.bdate
order by t1.bdate
join t2 can get you all the users between particular day and current_date, then count t2's customer_id, that's it.
SqlFiddle Demo Here
I've got a table in my database which looks like this:
payment_id customer_id amount payment_date
1 32 20.00 2005-01-25 11:30:37
2 32 10.00 2005-01-26 11:30:37
3 11 25.00 2005-03-25 11:30:37
Now I want to sum all amounts a customer (customer_id) made in the respective month.
I need a query that looks which month exists and which customers have an entry for this month.
The result should look like this:
customer_id month amount
32 01 30.00
11 03 25
I tried this:
SELECT DISTINCT month(payment_date) AS month, customer_id, sum(amount) AS amount
FROM table
But it just sums all amount values of the whole table.
You have to use a GROUP BY query:
SELECT
customer_id,
month(payment_date) as month,
sum(amount) as total_amount
FROM
tablename
GROUP BY
customer_id,
month(payment_date)
I have a table, which looks like the following:
Table users:
id points date
1 100 2014-07-01
2 500 2014-07-02
3 200 2014-07-01
4 100 2014-07-03
5 100 2014-07-01
6 400 2014-07-02
7 800 2014-07-02
8 200 2014-07-02
Now, how is it possible to select each unique date and count the sum of the points on those days?
So I's need a result something like this:
points date
400 2014-07-01
1900 2014-07-02
100 2014-07-03
SELECT SUM(`points`) AS points, `date`
FROM users
GROUP BY `date`
Try this:
SELECT SUM(points) points, date
FROM users
GROUP BY date
ORDER BY date ASC
This group-and-aggregate operation is a standard query pattern and can be solved following these steps1.
Use a GROUP BY date to aggregate the dates into groups, by date.
Select SUM(points) to tally the points in each aggregated [date] group.
Select the date column, which represents each group, to include it in the results.
Finally, apply ORDER BY date to ensure the results are ordered.
1 It is good etiquette - and results in better answers/discussion - when discussing attempted solutions (ie queries) in questions, and why they didn't work [correctly].
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
I have a cron script that writes the total number of active users to a table every day. I'm trying to now generate a simple report that would show the "high water mark" for each month. Because some accounts expire during the month it's possible the highest number may NOT be at the end of the month.
Here's a sample of my table structure
tblUserLog
-----------
record_id INT(11) // PRIMARY KEY
run_date DATE // DATE RUN
ttl_count INT(11) // TOTAL FOR DAY
Sample data:
record_id run_date ttl_count
1 2013-06-01 500
2 2013-06-10 510
3 2013-06-20 520
4 2013-06-30 515
5 2013-07-01 525
6 2013-07-10 530
7 2013-07-20 540
8 2013-07-31 550
9 2013-08-01 560
What I would like returned is:
record_id run_date ttl_count
3 2013-06-20 520
8 2013-07-31 550
9 2013-08-01 560
I've tried two queries that are close...
// This will give me the total for the first of the month
SELECT s.record_id, s.run_date, s.ttl_count
FROM tblStatsIndividual s
JOIN (
SELECT record_id
FROM tblStatsIndividual
GROUP BY DATE_FORMAT(run_date, '%Y %m')
HAVING MAX(ttl_count)
) s2
ON s2.record_id = s.record_id
ORDER BY run_date DESC
This returns the total for the first of each month, along with the record_id and correct date for the total.
Tried this...
SELECT record_id,max(run_date), max(ttl)
FROM (
SELECT record_id,run_date, max(ttl_count) AS ttl
FROM tblStatsIndividual
GROUP BY DATE_FORMAT(run_date, '%Y %m')
) a
GROUP BY DATE_FORMAT(run_date, '%Y %m')
ORDER BY run_date DESC
This one appears to get the correct "high water mark" but it's not returning the record_id, or the run_date for the row that IS the high water mark.
How do you get the record_id and the run_date for the highest total?
Something like
Select detail.Record_ID, detail.Run_Date, detail.ttl_Count
From tblStatsIndividual detail
Inner Join
(Select Year(run_date) as Year, Month(Run_date) as Month, Max(ttl_count) as ttl
From tblStatsIndividual
Group By Year(run_date), Month(Run_date)) maximums
On maximums.Year = Year(detail.Run_date) and maximums.Month = Month(detail.Run_date)
and maximums.ttl = detail.ttl_count
Should do it. NB based on your requirement if you had two records in the same month with the same (and highest in the month) ttl_count, they would both be returned.
Based on the help from #Tony Hopkinson, This query gets me the info. The one caveat is it shows the ID and date for the first occurrence of the MAX total, so if the total is the same three days in a row on a month, the first day's ID is returned. For my purpose, the last ID would be more ideal, but I can live with this:
SELECT s.Record_ID, s.Run_Date, s.ttl_Count
FROM tblStatsIndividual s
INNER JOIN (
SELECT YEAR(run_date) AS yr, MONTH(run_date) AS mon, MAX(ttl_count) AS ttl
FROM tblStatsIndividual
GROUP BY DATE_FORMAT(run_date, '%Y %m')
) maximums
ON maximums.yr = YEAR(s.run_date)
AND maximums.mon = MONTH(s.run_date)
AND maximums.ttl = s.ttl_Count
GROUP BY ttl_count
ORDER BY run_date DESC