I have a table with 3 columns. First column is an identifier, 2nd is value, 3rd is date. Each date has values for many identifiers. I would like to pivot this table in such a way that I have a row for each date and each column has an identifier.
Every answer I find uses CASE to pivot, however in my case I have many (50+) identifiers, and they would change as time goes on.
Here is the code I used, which did not work
with prep as (
select concat(ticker,' ',date) as papel , (pu+pu_juros) as PU, date from table where date >='2021-12-31' and ticker_bench = 'ticker')
select * from prep
pivot (avg(PU) for papel in (select distinct papel from prep))
I tried a second version, defining a table with the values in which to pivot, this also did not work.
with prep_papel as (
select distinct concat(ticker,' ',date) as papel from table where date>='2021-12-31' and ticker_bench = 'ticker'
)
select * from (
select concat(ticker,' ',date) as papel , (pu+pu_juros) as PU, date from table where date >='2021-12-31' and ticker_bench = 'ticker')
as P
pivot (sum(p.PU) for papel in (select * from prep_papel) ) as pv1
Thank you
Related
I have this table and I need to create an SQL statement WHERE
I need to get ID which content are BOOK with value 05 and content is PAPER with value 'Y',
Result must:
id
1
3
You can group by id, filter the table on your conditions and check in the HAVING clause if both conditions are satisfied:
SELECT id
FROM tablename
WHERE (content, value) IN (('BOOK', '05'), ('PAPER', 'Y'))
GROUP BY id
HAVING COUNT(*) = 2
See the demo.
You can use subquery.
You can select in one query the id-s that meet the condition and use it as a condition
Try:
create table `content`(
id int(9) ,
content varchar (50),
value varchar (10) );
insert into content values (1,'BOOK','05'), (2,'BOOK','05'),
(3,'BOOK','05'), (4,'BOOK','07'), (5,'BOOK','07'), (1,'PAPPER','Y'),
(2,'PAPPER','N'), (3,'PAPPER','Y'), (4,'PAPPER','Y'), (5,'PAPPER','N');
select id from content where content='BOOK' and value ='05' and id in (
select distinct id from content where content='PAPPER' and value ='Y');
I have tables
CREATE TABLE one (
op INT,
value INT
);
and
CREATE TABLE two (
tp INT,
value INT
);
Now I want to get all op values for which the set of values for the op contains all values for a given tp.
I would write this as:
SELECT op FROM one AS o1 WHERE (
(SELECT value FROM one AS o2 WHERE o1.op = o2.op)
CONTAINS ALL
(SELECT value FROM two WHERE tp=<specific-value>)
)
Unfortunately, I couldn't find such a CONTAINS ALL operator and nothing which would be close that.
Table one contains 50M entries, table two contains 1M entries. On average, there are 20 different values for a single op and tp.
Consider your tables name ops and tps.
SELECT
ops.op
FROM ops
INNER JOIN tps ON tps.value = ops.value
WHERE tps.tp = 1
GROUP BY ops.op
HAVING COUNT(DISTINCT ops.value) = (SELECT COUNT(DISTINCT tps.value) FROM tps WHERE tps.tp = 1); --- You can replace 1 with any tp value.
Here is the Sample Data I Have
INSERT INTO MA_NewRevised VALUES
('Vishal','SD','Col',2),
('Shivu','SD','Col',3),
('Pavithra','SD','Col',4),
('Keerthi','SD','Col',5),
('Bharath','SD','Col',6),
('Radhu','SD','Col',2),
('Vanitha','SD','Col',3),
('Anirudh','SD','Col',4),
('Amit','SD','Col',5)
INSERT INTO MA_OldValues VALUES
('Vishal','SD','Col',2),
('Shivu','SD','Col',3),
('Pavithra','SD','Col',2),
('Keerthi','SD','Col',3),
('Bharath','SD','Col',6),
('Radhu','SD','Col',2),
('Vanitha','SD','Col',3),
('Raju','SD','Col',1)
First Code:
SELECT * FROM MA_NewRevised n
EXCEPT
SELECT * FROM MA_OldValues o
The above code will give me.
Second Code
SELECT Name,Groups,CommonColumn FROM MA_NewRevised
EXCEPT
SELECT Name,Groups,CommonColumn FROM MA_OldValues
The Above Code will give me:
But the required Output is
That is, Distinct values with respect to Name, Groups, CommonColumn should be Selected along with the quantity of the new Revised table(First Table)
To your query, which does not return the extra column, but return correct rows, join the original table and retrieve the value of the extra column. Since there is no unique field in your data, you may have to join on all fields.
;with cte as (
SELECT Name,Groups,CommonColumn FROM MA_NewRevised
EXCEPT
SELECT Name,Groups,CommonColumn FROM MA_OldValues
)
select cte.*, n.Quatity -- or n.Quantity, not sure which is the correct name of your column
from cte
inner join MA_NewRevised n on cte.Name = n.Name and cte.Groups = n.Groups and cte.CommonColumn = n.CommonColumn
I want to combine three reports. I want to take count in each tables (smr_trn_tsalesenquiry, smr_trn_treceivequotation, smr_trn_tsalesorder), total count from three tables and using where condition employee_gid from hrm_mst_temployee, group by all primary key from above three tables.
select count(*),
(
select count(x.enquiry_gid)
from smr_trn_tsalesenquiry x
where x.created_by=a.employee_gid
) as enquiry_count,
(
select count(y.quotation_gid)
from smr_trn_treceivequotation y
where y.created_by=a.employee_gid
) as quotation_count,
(
select count(z.salesorder_gid)
from smr_trn_tsalesorder z
where z.created_by=a.employee_gid and z.salesorder_status not in('SO Amended','Cancelled','Rejected')
) as sales_count
from hrm_mst_temployee a
group by a.employee_gid;
You can do this by creating scalar Function by passing employee_id and
return the grouped count from it.
This link will help you to understand how to work with functions
I've got a select query I'm using to pick out contacts in my DB that haven't been spoken to in a while. I'd like to run an INSERT query to enter in a duplicate note for all the records that are returned with this select query... problem is I'm not exactly sure how to do it.
The SELECT query itself is likely a bit of a convoluted mess. I basically want to have the most recent note from each partner selected, then select ONLY partners that haven't got a note from a certain date and back... the SELECT query goes:
SELECT * FROM
(
SELECT * FROM
(
SELECT
partners.partners_id,
partners.CompanyName,
notes.Note,
notes.DateCreated
FROM
notes
JOIN
partners ON notes.partners_id = partners.partners_id
ORDER BY notes.DateCreated DESC
) AS Part1
GROUP BY partners_id
ORDER BY DateCreated ASC
) AS Part2
WHERE
DateCreated <= '2013-01-15'
How would a run an INSERT query that would only go into the same records as this SELECT?
The insert would enter records such as:
INSERT INTO notes
(
notes_id,
partners_id,
Note,
CreatedBy,
DateCreated
)
SELECT
UUID(),
partners.partners_id,
'Duplicated message!',
'User',
'2013-02-14'
FROM
partners
If you want to do this all in SQL, you could use an UPDATE statement.
UPDATE tablename SET note='duplicate' where id in ( your statement here);
Note that in order for this to work 'id' needs to be a column from 'tablename'. Then, your statement has to return a single column, not *. The column returned needs to be the id that will let your update statement know which rows to update in 'tablename'.