SQL sort and last record - sql

this is my first post so pardon me if my question is not in it's appropriate places or tittle
I have a table like this
ID DATE Cat VALUE
-------------------------
1 07/07/2018 A 100
2 07/07/2018 A 200
3 07/07/2018 B 300
4 07/07/2018 B 400
5 07/07/2018 C 500
6 07/07/2018 C 600
7 08/07/2018 A 700
8 08/07/2018 A 800
9 08/07/2018 B 900
10 08/07/2018 B 110
11 08/07/2018 C 120
I would like to return
distinct category, sum of value, last record of the category
something like this
Cat sumValue lastrecord
--------------------------
A 1800 800
B 1710 110
C 1220 120
is it possible to do it in a single query
thanks
I am able to find the SUM
SELECT cat, SUM(value) FROM table GROUP BY cat;
and
find the last ID (autonumber key) using MAX
SELECT MAX(ID), cat FROM table GROUP BY cat;
but i just can't get the value for the last record

SQLFiddle
SELECT
t.cat,
SUM(t.value) as sumValue,
(
SELECT
t3.value
FROM
`table` t3
WHERE
t3.id = MAX(t2.id)
) as lastrecord
FROM
`table` t
JOIN
`table` t2 ON t.id = t2.id
GROUP BY
cat
EDIT shorter Version:
SELECT
t.cat,
SUM(t.value) as sumValue,
(SELECT value FROM `table` t2 WHERE t2.id = MAX(t.id)) lastValue
FROM
`table` t
GROUP BY
t.cat

This should do it
declare #t table (id int, cat char, value int);
insert into #t values
(1, 'A', 100),
(2, 'A', 200),
(3, 'B', 300),
(4, 'B', 400),
(5, 'C', 500),
(6, 'C', 600),
(7, 'A', 700),
(8, 'A', 800),
(9, 'B', 900),
(10, 'B', 110),
(11, 'C', 120);
select cat, value, sum
from
( select *
, sum(value) over (partition by cat) as sum
, ROW_NUMBER() over (partition by cat order by id desc) as rn
from #t
) tt
where tt.rn = 1

I hope you're looking for something like this,
Please replace the table name with your table name.
SELECT A.id,
A.cat,
A.date,
A.total_value,
A1.value
FROM (SELECT Max(id) AS id,
cat,
Max(date) AS Date,
Sum(value) AS Total_Value
FROM tbl_sof
GROUP BY cat) AS A
INNER JOIN tbl_sof A1
ON A.id = A1.id

Related

SQL query to fetch distinct records

Can someone help me out with this sql query on postgres which I have to write but I just can't come up with, I have tried my best to simplify the problem from 1 million records and more constraints to this, I know this looks easy, but I am still unable to resolve this somehow :-
Table_name = t
Column_1_name = id
Column_2_name = st
Column_1_elements = [1,1,1,1,2,2,2,3,3]
Column_2_elements = [a,b,c,d,a,c,d,b,d]
Now I want to print to those distinct ids from id where they do not have their corresponding st equals to 'b' or 'a'.
For example, for the above example, the ouput should be [2,3] as 2 does not have corresponding 'b' and 3 does not have 'a'. [even though 3 does not have c also, but we are not concerned about 'c']. id=1 is not returned in solution as it has a relation with both 'a' and 'b'.
Let me know if you need more clarity.
Thanks in advance for helping.
edit1:- The number of elements for id = 1,2,3 could be anything. I just want those ids where there corresponding st does not "contain" 'a' or 'b'.
if there is an id=4 which has just one st which is 'r', and there is an id=5 which contains 'a','b','c','d','e','f','k','z'.
Then we want id=4 in the output as well as it does not contain 'a' or 'b'..
You might need to correct the syntax a little bit based on you SQL engine but this one is a working solution in Google BigQuery -
with temp as (
select 1 as id, 'a' as st union all
select 1 as id, 'b' as st union all
select 1 as id, 'c' as st union all
select 1 as id, 'd' as st union all
select 2 as id, 'a' as st union all
select 2 as id, 'c' as st union all
select 2 as id, 'd' as st union all
select 3 as id, 'b' as st union all
select 3 as id, 'd' as st union all
select 4 as id, 'e' as st union all
select 5 as id, 'g' as st union all
select 5 as id, 'h' as st
)
-- add 2 columns for is_a and is_b flags
, temp2 as (
select *
, case when st = 'a' then 1 else 0 end is_a
,case when st = 'b' then 1 else 0 end as is_b
from temp
)
-- IDs that have both the flags as 1 should be filtered out (like ID = 1)
select id
from temp2
group by 1
having max(is_a) + max(is_b) < 2
This solution takes care of the problem you mentioned with ID 4 . Let me know if this works for you.
See if this works:
create table t (id integer, st varchar);
insert into t values (1, 'a'), (1, 'b'), (1, 'c'), (1, 'd'), (2, 'a'), (2, 'c'), (2, 'd'), (3, 'b'), (3, 'd'), (4, 'r');
insert into t values (5, 'a'), (5, 'b'), (5, 'c'), (5, 'd'), (5, 'e'), (5, 'f'), (5, 'k'), (5, 'z');
select id, array['a', 'b'] <# array_agg(st)::text[] as tf from t group by id;
id | tf
----+----
3 | f
5 | t
4 | f
2 | f
1 | t
select * from (select id, array['a', 'b'] <# array_agg(st)::text[] as tf from t group by id) as agg where agg.tf = 'f';
id | tf
----+----
3 | f
4 | f
2 | f
In the first select query the array_agg(st) aggregates all the st values for an id via the group by id. array['a', 'b'] <# array_agg(st)::text[] then asks if the a and b are both in the array_agg.
The query is then turned into a sub-query where the outer query selects those rows that where 'f'(false), in other words did not have both a and b in the aggregated id values.

Pivot and Sum in Amazon Redshift

I have a following tables
table1
id name
1 A
3 B
table2
id label value
1 tag a
1 tag b
1 time 10
1 time 20
1 score 20
2 tag a
2 time 30
2 score 40
3 tag b
3 time 50
3 time 55
3 score 60
first I'd like to join table2 as follows
select *
from table1 left join on table2 using(id)
where label in ('tag')
id name tag
1 A a
1 A b
3 B b
and then join table2 with id and pivot and sum up them
id name tag time score
1 A a 10 20
1 A b 10 20
3 B b 50 60
I guess it is very complicated, are there any way to achieve this?
In Redshift it seems that there is no way to pivot them.
Thanks.
This looks to be a pivot query. I think this does what you are looking for:
create table table1 (id int, name varchar(16));
insert into table1 values
(1, 'A'),
(3, 'B')
;
create table table2 (id int, label varchar(16), value varchar(16));
insert into table2 values
(1, 'tag', 'a'),
(1, 'tag', 'b'),
(1, 'time', '10'),
(1, 'score', '20'),
(2, 'tag', 'a'),
(2, 'time', '30'),
(2, 'score', '40'),
(3, 'tag', 'b'),
(3, 'time', '50'),
(3, 'score', '60')
;
select t2.id, a.name, a.tag_value, sum(decode(label, 'time', value::int)) as total_time, sum(decode(label, 'score', value::int)) as total_score
from table2 t2
join (
select id, name, value as tag_value
from table1 t1 left join table2 t2 using(id)
where t2.label in ('tag')
) a
on t2.id = a.id
group by 1, 2, 3
order by 1, 2, 3
;

change data format from rows to columns

I have a SQL table in the below format. Every ID has 2 entries in the table for a particular date.
Input:
ID date rownum subid value1 value2
A 200911 1 X 10 20
A 200911 2 Y 15 25
B 201001 2 S 60 35
B 201001 1 R 40 50
I want to write a SQL query to change this to the below format, so that every ID/date combination has only 1 entry as shown below. The rownum is already included and the values should be represented so that rownnum 1 is displayed first and then the value with rownum second as shown below.
Output:
ID date row1subid row1value1 row1value2 row2subid row2value1 row2value2
A 200911 X 10 20 Y 15 25
B 201001 R 40 50 S 60 35
Let me know if something is not clear.
Thanks for all your help!
Here is what you need to do as a SQL Fiddle.
And for reference:
CREATE TABLE TestData
([ID] varchar(1),
[date] int,
[rownum] int,
[subid] varchar(1),
[value1] int,
[value2] int)
;
INSERT INTO TestData
([ID], [date], [rownum], [subid], [value1], [value2])
VALUES
('A', 200911, 1, 'X', 10, 20),
('A', 200911, 2, 'Y', 15, 25),
('B', 201001, 2, 'S', 60, 35),
('B', 201001, 1, 'R', 40, 50)
;
SELECT A.ID, A.date, A.rownum, A.subid, A.value1, A.value2, B.value1 AS r2value1, B.value2 AS r2value2
FROM TestData AS A
INNER JOIN TestData B ON A.id = B.id AND B.rownum = 2
WHERE A.rownum = 1

select id with max date and keep all same max date SQL

I have a sample dataset
id category date value
1 a 2013-01-02 7
2 a 2013-01-02 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 would like to return the following table as output
id date
1 2013-01-02
2 2013-01-02
6 2013-01-03
7 2013-01-03
8 2013-01-03
I use the following code to get result,but date only return once. I would like to keep both.
SELECT id,date
FROM order t1
INNER JOIN
(
SELECT id, MAX(date) as maxdate
FROM order
GROUP BY category
) t2
ON t1.id = t2.id
AND t1.date = t2.maxdate
Please advice if I have something wrong.
From your example - you seem to want a query that gives you all the rows that match the max date in each category?
If so, you should group across the category (don't grab the ID from your t2). The subselect should give you the category and the maximum date, the outer correlated join will give you all the rows that match that category and date.
SELECT category,id,date
FROM order t1
INNER JOIN
(
SELECT category, MAX(date) as maxdate
FROM order
GROUP BY category
) t2
ON t1.category = t2.category
AND t1.date = t2.maxdate
If you are using sql-server-2012 and above you can also use this.
DECLARE #T TABLE (id INT, category VARCHAR(5), [date] date, value int)
INSERT INTO #T VALUES
(1, 'a', '2013-01-02', 7),
(2, 'a', '2013-01-02', 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)
SELECT id, [date] FROM (
SELECT id, [date], RANK() OVER( PARTITION BY category order by [date] desc) RNK from #T
) AS t
WHERE RNK = 1
Result:
id date
----------- ----------
1 2013-01-02
2 2013-01-02
6 2013-01-03
7 2013-01-03
8 2013-01-03
I would do this as by using subquery only :
select o.*
from order o
where date = (select max(o1.date)
from order o1
where o1.category = o.category
);

Error querying SQL Server

This is my table
create table #t(id int, amt int)
insert into #t values(1, 40), (1, 20), (1, 10), (2, 100), (2, 120), (2, 400)
I need output like this!
id amt
1 70
1 70
1 70
2 620
2 620
2 620
I tried
SELECT
id, SUM(amt)
FROM
#t
GROUP BY
id
Try This !
select id,sum(amt) over(partition by id) as amt from #t
select id
, sum(amt) over (partition by id)
from #t
Example at SQL Fiddle.
select a.id, c.amt from #t as a
left outer join
(SELECT b.id, SUM(b.amt) as amt FROM #t as b GROUP BY id ) as c on c.id=a.id