My table and data is like this:
How do I group them and get result like this:
select ROW_NUMBER() OVER(PARTITION BY categoryid order by categoryid asc) as Num, categoryid,value from democat
Related
I have a query like the one shown below:
SELECT
SUM (LIMIT_AMOUNT)
FROM
(SELECT
CTR_NO
, REPORT_DATE
, LIMIT_AMOUNT
, ROW_NUMBER()
OVER (PARTITION BY CTR_NO, REPORT_DATE ORDER BY REPORT_DATE) rn
FROM LOD_CONTRACT
WHERE
br_cst_code='3432434'
AND REPORT_DATE BETWEEN '20-FEB-15' AND '28-FEB-15') b
WHERE
b.rn=1;
How can I build a SQL query for a JPA-managed entity like:
SELECT
SUM (o.limit_amount)
FROM
(SELECT
o.ctr_no
, o.rpt_dt
, o.limit_amount
, ROW_NUMBER()
OVER (PARTITION BY o.ctr_no, o.rpt_dt ORDER BY o.rpt_dt) rn
FROM LOD_CONTRACT o
WHERE
o.br_cst_code='3432434'
AND o.rpt_dt BETWEEN '20-FEB-15' AND '28-FEB-15') b
WHERE
b.rn=1;
JPQL doesn't currently support Oracle partition tables. To use this feature you will have to stick to Native queries.
I am hoping I can get some clarification on how to best handle getting the data set correct and efficiently.
Here are three queries from three different tables I am working with. The Donor_ID is key between the tables, but as you can see - there are multiple records associated with each Donor_ID - with the runid_gmt column having differing dates.
Ideally, I would like use the max(runid_gmt) for each record - and join the EMAIL and ADDRESSES tables on the Donor_ID but only select the max(runid_gmt) record in each of those tables as well.
I believe that is what I need to do - but not sure. Any suggestions on how to tackle this problem?
SELECT donor_id, last_name, birthdate, runid_gmt
FROM [dbo].TBL_DONORS where donor_id = '51999441' order by runid_gmt desc;
SELECT donor_id, city, state, zip, runid_gmt
FROM [dbo].TBL_ADDRESSES where donor_id = '51999441' order by runid_gmt desc;
SELECT donor_id, donor_email, runid_gmt
FROM [dbo].TBL_EMAIL where donor_id = '51999441' order by runid_gmt desc;
Try with row_number window function:
select * from
(select *, row_number() over(partition by donorid order by gmt desc) rn
from donors) t1 join
(select *, row_number() over(partition by donorid order by gmt desc) rn
from addresses) t2 on t1.donorid = t2.donorid join
(select *, row_number() over(partition by donorid order by gmt desc) rn
from emails) t3 on t1.donorid = t3.donorid
where t1.rn = 1 and t2.rn = 1 and t3.rn = 1
I have a table with two columns:
UserId (auto int)
Email(Nvarchar)
I want to retrieve the email that was last inserted on table.
I've tried some options, but nothing seems to be working.
Thanks in advance.
Perhaps simply:
SELECT TOP 1 email FROM dbo.Table ORDER BY UserId DESC
or
SELECT UserId, Email
FROM dbo.Table
WHERE UserId = (SELECT MAX(UserId) FROM dbo.Table)
However, it's not good practise to abuse a primary-key column for information like "last inserted". Add a datetime column for this.
You could also use the ROW_NUMBER function:
WITH x AS (
SELECT UserId, Email,
rn = Row_number() OVER(ORDER BY UserId DESC)
FROM dbo.table)
SELECT UserId, Email
FROM x
WHERE rn = 1
I made a select using the WITH query something like this:
WITH test AS (select id, sum(value) as value from test group by id)
SELECT name, t.value from names JOIN test t ON (t.id = names.id)
But I need to use 2 WITH queries, is there any way to make it?
WITH
name1 AS (SELECT ...),
name2 AS (SELECT ...)
SELECT ...
I want to select rows for all employess without repeating the data in one column.
For example I have two rows where salary (before raise) is displayed, how can I display only the largest figure without duplication.
You can use Row_Number function
Here is a sample code
select * from (
select *,
row_number() over (partition by empid, name, department order by salary desc) as rn
from employee
) employee where rn = 1
You can find Row_Number() with Partition By clause sample at http://www.kodyaz.com
If I'm understanding the question correctly, then a simple MAX function and GROUP BY would work.
SELECT EmployeeId, OtherColumns, MAX(Salary)
FROM tblEmployees
GROUP BY EmployeeId, OtherColumns