Selecting values by last date - mysql

I have table with several entries for one point, is it possible to show only last entry for each point?
Example
points | date
A1 2016-02-12
A1 2016-02-15
A1 2016-03-12
B1 2016-01-11
B1 2016-03-15
B1 2015-09-28
C1 2016-01-28
C2 2016-03-03
D1 2015-12-12
D1 2016-01-12
E2 Null
E3 Null
F1 Null
I want to get something like this, without ignoring Null values.
points | date
A1 12.03.2016.
B1 12.03.2016.
C1 03.03.2016.
D1 12.01.2016.
E2 Null
E3 Null
F1 Null
I edited question because I tried group by and it didn't work for me( I forgot to mention before) It showed only entries with date, and I need points with null value as well
Something like this:
A1 12.03.2016.
B1 12.03.2016.
C1 03.03.2016.
D1 12.01.2016.

You can get by using MAX FUNTION
SELECT points,MAX(date)
FROM table_name
GROUP BY points;
if you want to change date format you can use DATE_FORMAT function

Related

Is it possible to modify data from UNION query in this way?

The data that came out of the UNION query I wrote came out like this.
COLUMNS1 COLUMNS2 DATE1 DATE2
--------------------------------------------------------------------
A B 2022-01-10 05:40:12 NULL
A B NULL 2022-01-25 12:40:00
C D 2022-01-15 05:40:12 NULL
C D NULL 2022-01-17 12:40:00
...
How can I get the above data to come out as a query?
COLUMNS1 COLUMNS2 DATE1 DATE2
--------------------------------------------------------------------
A B 2022-01-10 05:40:12 2022-01-25 12:40:00
C D 2022-01-15 05:40:12 2022-01-17 12:40:00
....
Similarly, you can use a LEFT OUTER JOIN , but I don't want to use it.
It seems very simple, but I can't find an answer at all. Maybe it's impossible, but am I trying?
You can group the results of your query and aggregate the date columns, such as:
select COLUMNS1, COLUMNS2,
max(DATE1) DATE1,
max(DATE2) DATE2
from (...)t
group by COLUMNS1, COLUMNS2;

How to create new column and new row based on two tables?

I have two tables:
Table 1
MARKET ATC3 ATC4 PRODUCT BOOLEAN FLAG JOINING COLUMN
A1 B1 B1C1 D1 1 ATC4
A2 B1 B1C2 D2 1 ATC4
A2 B1 B1C3 ATC4
FAMILY A B1 ATC3
Table 2:
PRODUCT ATC3 ATC4 VALUES
D1 B1 B1C1 10
D1 B1 B1C1 20
D2 B1 B1C2 15
D2 B1 B1C2 25
D2 B1 B1C2 10
D3 B1 B1C3 5
My desired output:
PRODUCT ATC3 ATC4 VALUES MARKET VALUES
D1 B1 B1C1 10 A1 10
D1 B1 B1C1 20 A1 20
D2 B1 B1C2 15 A2 15
D2 B1 B1C2 25 A2 25
D2 B1 B1C2 10 A2 10
D3 B1 B1C3 5 A2 5
ALL D1+D2+D3 FAMILY A 85
The idea is, Table 2 has many rows and products but does not have Market. Table 1 helps you find out which product in Table 2 belongs to which Market-based on the Joining column. For example, There are 3 Markets present in Table 1, I want to then assign a new column Market in Table 2 such that all PRODUCTS in Table 2 with the ATC4 code of B1C1 belongs to the Market A1. Why? Because in Table 1, it says that Market A1 should follow the Joining Column of ATC4 - which corresponds to the code B1C1. In Table 1, we also provided a Product column, this is just for our purpose of identifying our own companies product name. Now if you see that for Table 1, there are two rows of Market A2, with different ATC4, this is very normal, because maybe Product D2 and D10 belong to Market A2, but both may contain different ATC4!
There is also one more nuance to it, we have Family A! This is merely a combination of A1+A2, but in my Table 2, there is no such row value that sums up to Family A. So I need to achieve two things:
I want to make a new column Market in Table 2 so that each product is mapped to the market.
I want to create extra rows to account for the Market Family A (A1+A2) and call the product Name "Lovely Family A" or something. The above table 3 provides an expected output.
Since I am new to SQL, I tried to first use CASE Statements, to map slowly one by one, but soon it gets tedious and I wonder if there's some tricks.
My CASE looks like this
,CASE WHEN ATC4 LIKE '%B1C1%' THEN 'A1'
WHEN ATC4 LIKE '%B1C2%' OR ATC4 LIKE '%B1C3%' THEN 'A2' ELSE ATC4 END AS MARKET_NAME
but have yet to figure out how to add the additional row where I can sum up A1+A2.
You seem to want something like this:
with rows as (
select PRODUCT, ATC3, ATC4, VALUES
from table2
union all
select 'ALL D1+D2+D3', ATC3, NULL, SUM(VALUES)
from table2
group by ATC3
)
select r.*, t1.market
from rows r join
table1 t1
on (t1.joining_column = 'ATC3' and t1.atc3 = r.atc3) or
(t1.joining_column = 'ATC4' and t1.atc4 = r.atc4);
I see no reason to repeat the values column. And values is a really bad name for a column because it is a SQL keyword.

mysql display multiple rows in one row

I have a table tbl_usi in mysql with records as below:
present_date usi_value deal_count
----------------------------------------------------------
2015-10-13 b1 c1
2015-10-12 b2 c2
2015-10-11 b3 c3
I want to write a query that will do this using present_date field to select the present date and the date before it and display them together:
present_date usi_value deal_count previous_date previous_usi_value previous_deal_count
----------------------------------------------------------
2015-10-13 b1 c1 2015-10-12 b2 c2
2015-10-12 b2 c2 2015-10-11 b3 c3
2015-10-11 b3 c3 2015-10-10 b4 c4
How do I achieve this. Thanks
Select everything from your table, then join it to itself, making sure the 2 joined tables are given different names so you can distinguish them (I used 'a' and 'b' here). The join offsets the dates by 1 day. Then you can select the fields you want from the joined table.
select
a.present_date,
a.usi_value,
a.deal_count,
b.present_date as previous_present_date,
b.usi_value as previous_usi_value,
b.deal_count as previous_deal_count
from
tbl_usi as a
left join tbl_usi as b
on b.present_date = a.present_date - interval 1 day;
If you didn't already have one before, you will now want an index for the present_date column too BTW.
Alternative, which works when there are date gaps.
select
a.present_date,
a.usi_value,
a.deal_count,
b.present_date as previous_present_date,
b.usi_value as previous_usi_value,
b.deal_count as previous_deal_count
from
tbl_usi as a
join tbl_usi as b
where
b.present_date = (select max(present_date) from tbl_usi where present_date < a.present_date);
As with previous solution the same table is joined twice, but this time the previous row is found by way of a subquery.

Display specific results mysql

I have the following table
ID Name Progress Date
------------------------------
1 A1 First Stage 1/1/2013
2 A1 Second Stage 1/2/2013
3 A2 First Stage 1/1/2013
4 A2 Second stage 1/2/2013
5 A3 First Stage 1/2/2013
6 A1 Closed 1/5/2013
I would like to display the stage of each name except the one that is ultimately closed.
For example the output of this should be
ID Name Progress Date
------------------------------
3 A2 First Stage 1/1/2013
4 A2 Second stage 1/2/2013
5 A3 First Stage 1/2/2013
Not A1 as A1 is ultimately Closed.
My query display Select * from table where Progress not like 'Closed' obviously displays all the results except that row.
Thanks
Use a subquery to filter all the names that you want to exclude:
select distinct name from table where progress = 'Closed'
Now, use it in your query:
select *
from table
where name not in (select distinct name from table where progress = 'Closed');
Hope this helps you
SELECT x.*
FROM my_table x
LEFT
JOIN my_table y
ON y.name= x.name
AND y.progress = 'closed'
WHERE y.id IS NULL;

Select data: paginating data from multiple tables with inconsistent dates

I have 8 separate tables. Each table has id, a datetime field and some text.
I am presenting data combined from all tables on a single page in a timeline view, most recent entries are at the top, and entries from each table are mixed in this timeline.
Now the hard part - I need to add pagination to this website, so on each page I want to show exactly 10 days (except for the last page, that may have less than 10).
Each table may have variable number of rows.
I've been struggling with this for quite some time but have not yet come up with an elegant solution.
Here's an example (I'll pick only two tables for this example to make it simpler).
tableA
Apr 1 | a1
Apr 5 | a2
Apr 7 | a3
tableB
Apr 2 | b1
Apr 2 | b2
Apr 5 | b3
Apr 6 | b4
Global timeline would look like this
Apr 7 a3
Apr 6 b4
Apr 5 a2
Apr 5 b3
Apr 2 b2
Apr 2 b1
Apr 1 a1
And if each page shows only 3 days, I need for it to look like this:
--- p1 ---
Apr 7 a3
Apr 6 b4
Apr 5 a2
--- p2 ---
Apr 5 b3
Apr 2 b2
Apr 2 b1
--- p3 ---
Apr 1 a1
The problem is - I can't figure out a way to query for this data in an elegant way. Here's some live query that I've been messing with:
select date(d.entryTime) entryDate, date(wc.changeTime) wcDate
from diary_entry d
join water_change wc
on d.aquariumId = wc.aquariumId
where d.aquariumId = 2
group by entryDate
order by entryDate
limit 10, 5
so, for one table I have this query:
select date(d.entryTime) entryDate
from diary_entry d
where d.aquariumId = 2
group by entryDate
it yields 13 results
entryDate
2012-01-13
2012-01-14
2012-01-25
2012-01-26
2012-01-31
2012-02-04
2012-02-17
2012-02-20
2012-02-25
2012-03-17
2012-03-31
2012-04-01
2012-04-06
and for another:
select date(wc.changeTime) changeDate
from water_change wc
where wc.aquariumId = 2
group by changeTime
it yields 8 results
2012-01-22
2012-01-26
2012-02-17
2012-02-25
2012-03-04
2012-03-10
2012-04-04
2012-04-24
There are three common days between the two
2012-01-26
2012-02-17
2012-02-25
So the query that I need to produce would have to have
13 + 8 - 3 rows = 18 rows
And solution is found!
(select date(d.entryTime) activityDate
from diary_entry d
where d.aquariumId = 1
group by activityDate)
union
(select date(wc.changeTime) activityDate
from water_change wc
where wc.aquariumId = 1
group by activityDate
order by activityDate)
limit 10, 10
Query it using a Union.
SELECT TheDate,
TheText
FROM Table_A
WHERE [your critera]
UNION
SELECT TheDate,
TheText
FROM Table_B
WHERE [your critera]
...
If you need to only select the distinct ones, you can then wrap this as follows:
Select Distinct
TheDate,
TheText
From
(
SELECT TheDate,
TheText
FROM Table_A
WHERE [your critera]
UNION
SELECT TheDate,
TheText
FROM Table_B
WHERE [your critera]
...
);
It sounds like you've got the select and joins in place and are just missing the pageination. If that's the case, your script will have to keep track of what page you're on, and each click on "next page" will have to tell MySQL where to return the next 10 records from by appending this to your SELECT query:
SELECT...JOIN...WHERE...ORDER BY...LIMIT 20, 10
Will return the next 10 rows beginning with the 21st row in the full record set