I have below mentioned table:
ID Type Date Status1 Status2 Status3
1 458 2018-01-01 15:04:06 AAA A B
2 471 2017-12-31 15:04:06 AAA A B
3 458 2018-01-15 15:04:06 BBC C D
5 458 2018-01-18 15:04:06 AAA X D
There are many same value in Type column, i want to fetch the data with below mentioned condition:
Where any value in Type (should have more than one time in table) have Status1 as AAA and other rows with same value in Type are greater than Previous Date, with Status2 as C and Status3 as D together or Status3 must be D.
Output:
ID Type Date Status1 Status2 Status3
1 458 2018-01-01 15:04:06 AAA A B
3 458 2018-01-15 15:04:06 BBC C D
5 458 2018-01-18 15:04:06 AAA X D
I am using below mentioned query:
SELECT a.ID,a.Type,a.Date,b.Status1,a.Status2,a.Status3
From Table1 a
inner join Table2 b
on a.abc=b.xyz
inner join Table2 c
on a.efg=c.xyz
GROUP BY a.Type
HAVING Count(a.Type)>0
AND b.Status1='AAA'
UNION SELECT a.ID,a.Type,a.Date,b.Status1,a.Status2,a.Status3
FROM Table1 a
inner join Table2 b
on a.abc=b.xyz
inner join Table2 c
on a.efg=c.xyz
GROUP BY a.Type
HAVING Count(a.Type)>0
AND a.Date > (SELECT Date From Table1 GROUP BY Type HAVING b.Status1='AAA' AND Count(Type)>0)
AND ( Status2='A' AND Status3='D' )
OR Status3='D';
You can try something like below
SELECT t.* FROM `t` INNER JOIN
(
SELECT `type` , COUNT(`type`) ct FROM t GROUP BY `type` HAVING ct > 1
) t1
ON t1.type = t.type ORDER BY DATE ASC
I hope this will return what you want.
Try this query .
Hope this will give you the desire output .
SELECT
ID ,
Type ,
Date ,
Status1 ,
Status2 ,
Status3
FROM
[Tabel_Name]
GROUP BY
Type
HAVING
Count(Type)>0
AND
Status1='AAA'
UNION
SELECT
ID ,
Type ,
Date ,
Status1 ,
Status2 ,
Status3
FROM
[Tabel_Name]
GROUP BY
Type
HAVING
Count(Type)>0
AND
Date > (SELECT Date From [Tabel_Name] GROUP BY Type HAVING Status1='AAA' AND Count(Type)>0)
AND
( Status2='C' AND Status3='D' )
OR
Status3='D'
Related
So basically I run this code in sql:
SELECT p.Nome as Nome_pub, a.Nome as Nome_area, COUNT(*) as total
FROM publicacao p, emprestimo e, area_tematica a
WHERE p.Id=e.Publicacao_Id and p.Area_Tematica_Id=a.Id and (Data_hora>='2021-01-01' AND Data_de_devolucao<='2021-06-31')
GROUP by p.Nome
ORDER BY Nome_area;
and I get the following output
Nome_pub
Nome_area
total
name1
a
1
name2
b
1
name3
c
1
name4
d
3
name5
d
2
name6
d
2
name7
e
1
but I want an output that gives me the max word3 based on word2 basically: (in my case remove line with "name5" and "name6"
Nome_pub
Nome_area
total
name1
a
1
name2
b
1
name3
c
1
name4
d
3
name7
e
1
is there any way to do this??
Thanks in advance!!
If ROW_NUMBER is available in your version.
SELECT Nome_pub, Nome_area, total
(
SELECT
p.Nome as Nome_pub
, a.Nome as Nome_area
, COUNT(*) as total
, ROW_NUMBER() OVER (PARTITION BY a.Nome ORDER BY COUNT(*) DESC) as RN
FROM publicacao p
JOIN emprestimo e
ON p.Id = e.Publicacao_Id
JOIN area_tematica a
ON a.Id = p.Area_Tematica_Id
WHERE ( Data_hora >= DATE '2021-01-01' AND
Data_de_devolucao < DATE '2021-07-01')
GROUP by p.Nome, a.Nome
) q
WHERE RN = 1
ORDER BY Nome_area;
I am new to MYSQl, Please help me out..
Following query is partially working fine, but for some rows its double the values, it happens when both tables have a same date in two or more rows like here 2015-08-11 are 3 in first table and 2 in another table.
select all_dates.value1 as "Date",
sum(coalesce(g.Qty1, '-')) as "Inward(B_Qty)",
sum(coalesce(f.Qty2, '-')) as "Outward(B_Qty)"
from
( select distinct Date1 as value1 from inward where Name = 'A' union
select distinct Date2 from outward where Name = 'A'
) as all_dates
left join inward g
on g.Date1 = all_dates.value1 and g.Name = 'A'
left join outward f
on f.Date2 = all_dates.value1 and f.Name = 'A' group by all_dates.value1,f.Date2
table no. 1 :- Inward
Name Qty1 Date1
A 25000 2015-08-11
A 15000 2015-08-12
A 45000 2015-08-11
B 150000 2015-09-11
B 85000 2015-07-08
B 15000 2015-07-08
table no 1:Outward
Name Qty2 Date2
A 15000 2015-08-01
A 25000 2015-08-09
A 15000 2015-08-11
A 45000 2015-08-11
B 25000 2015-07-25
Expected OUtput
Date Inward Outward
2015-08-11 70000 60000
2015-08-09 - 25000
2015-08-01 - 15000
2015-08-12 15000 -
Actual ouput:
Date Inward(B_Qty) Outward(B_Qty)
2015-08-01 0 15000
2015-08-09 0 25000
2015-08-11 140000 120000
2015-08-12 15000 0
third rows of actual output is doubling the values..Why it is happening dont know.
Try joining to derived tables already containing sums per Date1, Date2:
select all_dates.value1 as `Date`,
coalesce(`Inward(B_Qty)`, '-') as `Inward(B_Qty)`,
coalesce(`Outward(B_Qty)`, '-') as `Outward(B_Qty)`
from
(
select distinct Date1 as value1 from inward where Name = 'A' union
select distinct Date2 from outward where Name = 'A'
) as all_dates
left join
(
select `Date1`, sum(Qty1) AS `Inward(B_Qty)`
from inward
where Name = 'A'
group by `Date1`
) as g
on g.Date1 = all_dates.value1
left join
(
select `Date2`, sum(Qty2) AS `Outward(B_Qty)`
from outward
where Name = 'A'
group by `Date2`
) as f
on f.Date2 = all_dates.value1
group by all_dates.value1
Demo here
Try this way:
select all_dates.value1 as "Date",
sum(coalesce(g.Qty1, '-')) as "Inward(B_Qty)",
sum(coalesce(f.Qty2, '-')) as "Outward(B_Qty)"
from
( select distinct value1 from (Select Date1 as value1 from inward where Name = 'A' union
select Date2 from outward where Name = 'A') as TmpDates
) as all_dates
left join inward g
on g.Date1 = all_dates.value1 and g.Name = 'A'
left join outward f
on f.Date2 = all_dates.value1 and f.Name = 'A' group by all_dates.value1,f.Date2
For an example I have MySQL table like:
id category date value
1 a 2013-01-02 7
2 a 2013-01-01 2
3 a 2013-01-01 3
4 b 2013-01-01 1
5 b 2013-01-02 4
6 b 2013-01-03 5
7 c 2013-01-03 4
8 c 2013-01-03 8
I need select records with MAX(date) for each category, but if date is the same, with MAX(id).
So, expected result must be:
id category date value
1 a 2013-01-02 7
6 b 2013-01-03 5
8 c 2013-01-03 8
As you can see, dates for same category can be not sorted (like id=4,5).
I tried query like:
SELECT * FROM mytable t1
INNER JOIN
(
SELECT category, MAX(date) AS MAXDATE
FROM mytable
GROUP BY category
) t2
ON t1.category = t2.category
AND t1.date = t2.MAXDATE
It works fine, when we have no same dates in table. But for my case, it will return records with lowest ids:
id category date value
1 a 2013-01-01 7
6 b 2013-01-03 5
7 c 2013-01-03 4
You are on the right track but you need to change your join.
I think what you want is the following:
SELECT * FROM mytable t1
INNER JOIN
(
SELECT MAX(id) AS ID, MAX(date) AS MAXDATE
FROM mytable
GROUP BY category
) t2
ON t1.id = t2.ID
AND t1.date = t2.MAXDATE
As mentioned, the previous wont work for every use case. The following might.
SELECT a.*
FROM mytable AS a,
(
SELECT category, date, MAX(id) AS ID
FROM mytable
GROUP BY category, date
) AS b,
(
SELECT category, MAX(date) AS date
FROM mytable
GROUP BY category
) AS c
WHERE b.category = c.category
AND b.date = c.date
AND a.id = b.id
I have two tables:
Table A
ID | DATE | VALUE | KEY|
1 30.8.14 100 11
2 25.8.14 500 11
2 20.8.14 250 11
Table B
ID | DATE | VALUE | KEY|
1 30.8.14 AB 11
2 25.8.14 CD 11
3 10.8.14 EF 11
These two tables should be merged, key is used to define which entries should be merged WHERE KEY = '11'
IF there is a date in TABLE A that is also in TABLE B, it becomes on entry with both values
IF there is no date in TABLE A that is also in TABLE B, the value for B becomes (null)
And in the End, there should be only 1 date field.
Columns should also be be a unique name..
I created this example table, how my output should look like
joinedDate | aValue | bValue
30.8.14 100 AB
25.8.14 500 CD
20.8.14 250 (null)
10.8.14 (null) EF
Im using MySQL version 5.5 on Maria DB
Could someone help me here?
You seem to want full outer join, which MySQL doesn't offer. Here is one method:
select d.date, a.value as avalue, b.value as bvalue
from ((select date from a union
select date from b
)
) d left join
a
on a.date = d.date left join
b
on b.date = d.date;
select a.date, a.value as avalue, b.value as bvalue
from tablea a
left join tableb b
on a.date = b.date
union all
select b.date, null, b.value
from tableb b
left join tablea a
on a.date = b.date
where a.date is null
Fiddle:
http://sqlfiddle.com/#!2/09ab9e8/4/0
I want to count how many times each user has rows within '5' of eachother.
For example, Don - 501 and Don - 504 should be counted, while Don - 501 and Don - 1600 should not be counted.
Start:
Name value
_________ ______________
Don 1235
Don 6012
Don 6014
Don 6300
James 9000
James 9502
James 9600
Sarah 1110
Sarah 1111
Sarah 1112
Sarah 1500
Becca 0500
Becca 0508
Becca 0709
Finish:
Name difference_5
__________ _____________
Don 1
James 0
Sarah 2
Becca 0
Use the ABS() function, in conjunction with a self-join in a subquery:
So, something like:
SELECT name, COUNT(*) / 2 AS difference_5
FROM (
SELECT a.name name, ABS(a.value - b.value)
FROM tbl a JOIN tbl b USING(name)
WHERE ABS(a.value - b.value) BETWEEN 1 AND 5
) AS t GROUP BY name
edited as per Andreas' comment.
Assuming that each name -> value pair is unique, this will get you the count of times the value is within 5 per name:
SELECT a.name,
COUNT(b.name) / 2 AS difference_5
FROM tbl a
LEFT JOIN tbl b ON a.name = b.name AND
a.value <> b.value AND
ABS(a.value - b.value) <= 5
GROUP BY a.name
As you'll notice, we also have to exclude the pairs that are equal to themselves.
But if you wanted to count the number of times each name's values came within 5 of any value in the table, you can use:
SELECT a.name,
COUNT(b.name) / 2 AS difference_5
FROM tbl a
LEFT JOIN tbl b ON NOT (a.name = b.name AND a.value = b.value) AND
ABS(a.value - b.value) <= 5
GROUP BY a.name
See the SQLFiddle Demo for both solutions.
Because the OP also wants de zero counts, we'll need a self- left join. Extra logic is needed if one person has two exactly the same values, these should also be counted only once.
WITH cnts AS (
WITH pair AS (
SELECT t1.zname,t1.zvalue
FROM ztable t1
JOIN ztable t2
ON t1.zname = t2.zname
WHERE ( t1.zvalue < t2.zvalue
AND t1.zvalue >= t2.zvalue - 5 )
OR (t1.zvalue = t2.zvalue AND t1.ctid < t2.ctid)
)
SELECT DISTINCT zname
, COUNT(*) AS znumber
FROM pair
GROUP BY zname
)
, names AS (
SELECT distinct zname AS zname
FROM ztable
GROUP BY zname
)
SELECT n.zname
, COALESCE(c.znumber,0) AS znumber
FROM names n
LEFT JOIN cnts c ON n.zname = c.zname
;
RESULT:
DROP SCHEMA
CREATE SCHEMA
SET
CREATE TABLE
INSERT 0 14
zname | znumber
-------+---------
Sarah | 3
Don | 1
Becca | 0
James | 0
(4 rows)
NOTE: sorry for the CTE, I had not seen th mysql tag,I just liked the problem ;-)
SELECT
A.Name,
SUM(CASE WHEN (A.Value < B.Value) AND (A.Value >= B.Value - 5) THEN 1 ELSE 0 END) Difference_5
FROM
tbl A INNER JOIN
tbl B USING(Name)
GROUP BY
A.Name