I have 2 tables in oracle . users and profile table.
i am running following query to get results
SELECT fu.user_name ,papf.SSN
FROM apps.fnd_user fu, apps.per_all_people_f papf
WHERE fu.employee_id = papf.person_id
AND (TO_CHAR(papf.EFFECTIVE_END_DATE) IS NULL
OR papf.EFFECTIVE_END_DATE > SYSDATE )
Thing is that ,
Profile table has multiple records for SSN.
So this query is returning dupes for SSN.
I just want to get one distinct record for SSN.
for eg result is like
ABC 123
ABC 123
How do i just display unquie values for SSN
Like
ABC 123
U tried distinct but did not work
Any suggestions ?
Proper use of Distinct for your case...
SELECT Distinct fu.user_name , papf.SSN
FROM apps.fnd_user fu
, apps.per_all_people_f papf
WHERE fu.employee_id = papf.person_id
AND (TO_CHAR(papf.EFFECTIVE_END_DATE) IS NULL
OR papf.EFFECTIVE_END_DATE > SYSDATE )
Query using group by can be like below to have a single record:
SELECT fu.user_name ,
papf.SSN
FROM apps.fnd_user fu,
apps.per_all_people_f papf
WHERE fu.employee_id = papf.person_id
AND ( papf.EFFECTIVE_END_DATE IS NULL
OR papf.EFFECTIVE_END_DATE > trunc(SYSDATE) --assuming effective_end_date doesnot have timestamp with it.
)
group by fu.user_name,
papf.SSN ;
Related
as a newbie in SQL I am lost regarding nested queries.
I am trying to achieve the following: getting a table grouped by month with a count from all values of a column, then the same count filtered by status.
So for instance in January I could have the following result:
Jan 22 Count= 100 Count with Status filter= 57
I tried several variations along these lines:
SELECT
FORMAT ( [CreatedDate.table2] , 'yyyyMM' ) as create_month,
RecordTypeName__c,
count(*) as count_all,
count_filtered
FROM
(SELECT
FORMAT ( [CreatedDate] , 'yyyyMM' ) as create_month,
RecordTypeName__c,
Count(*) AS count_filtered
FROM DM_AccessNoAgg.DimLead
WHERE [CreatedDate] >= '2022-01-01'
AND [Status]='Qualifiziert'
GROUP BY RecordTypeName__c,FORMAT ( [CreatedDate] , 'yyyyMM' )
)
Basically I am using the same value in both cases, just that the second count has to be filtered. What's the best method to get this done?
Thanks for your help!
Pauline.
I'm trying to replace a placeholder string inside a selection of 10 random records with a random string (a name) taken from another table, using only sqlite statements.
i've done a subquery in order to replace() of the placeholder with the results of a subquery. I thought that each subquery loaded a random name from the names table, but i've found that it's not the case and each placeholder is replaced with the same string.
select id, (replace (snippet, "%NAME%", (select
name from names
where gender = "male"
) )
) as snippet
from imagedata
where timestamp is not NULL
order by random()
limit 10
I was expecting for each row of the SELECT to have different random replacement every time the subquery is invoked.
hello i'm %NAME% and this is my house
This is the car of %NAME%, let me know what you think
instead each row has the same kind of replacement:
hello i'm david and this is my house
This is the car of david, let me know what you think
and so on...
I'm not sure it can be done inside sqlite or if i have to do it in php over two different database queries.
Thanks in advance!
Seems that random() in the subquery is only evaluated once.
Try this:
select
i.id,
replace(i.snippet, '%NAME%', n.name) snippet
from (
select
id,
snippet,
abs(random()) % (select count(*) from names where gender = 'male') + 1 num
from imagedata
where timestamp is not NULL
order by random() limit 10
) i inner join (
select
n.name,
(select count(*) from names where name < n.name and gender = 'male') + 1 num
from names n
where gender = 'male'
) n on n.num = i.num
Example:
It does not work.
UPDATE column_name SET rownum FROM table_name
This work!
UPDATE table_name SET column_name = rownum;
This works but the update is performed incorrectly
SELECT * FROM table_name ORDER BY column_name;
UPDATE table_name SET column_name = rownum;
I wish the following update behavior:
Note:'rownum ' It is not a physical column of the table
/*
pc_comentario = tableName
cod_comentario = columnName (Reference column for sorting)
dtc_andamento = columnDay (Reference column to update the "columnName" according to the order of this column)
*/
rownum | columnName | columnDay
1 1 day 1
2 5 day 5
3 7 day 2
After change with update
rownum | columnName (Update this column) | columnDay (sort by this column)
1 1 day 1
2 2 day 2
3 3 day 5
ALMOST DONE! this column 'cod_comentario_1 "which was materialized in RAM is correct. I need this column" cod_comentario_1 "that does not exist in the table is acknowledged in the consultations with java.
SELECT cod_comentario, dtc_andamento, cod_processo ,
ROW_NUMBER()
OVER (PARTITION BY cod_processo
ORDER BY dtc_andamento) cod_comentario_1
FROM pc_comentario
upadate do not work this way:
UPDATE (
SELECT cod_processo
ROW_NUMBER()
OVER (PARTITION BY cod_processo
ORDER BY dtc_andamento)cod_comentario_1
FROM pc_comentario
) SET cod_comentario_1)
order by Seq
I must enter the values of this consultation in a new column that I created
SELECT
ROW_NUMBER()
OVER (PARTITION BY cod_processo
ORDER BY dtc_andamento DESC)
FROM pc_comentario
Try:
UPDATE table_name
SET column_name = rownum
Shouldn't it be like below rather; I believe UPDATE statement has no FROM clause
UPDATE table_name SET column_name = rownum;
Again, it will work only if rownum is an existing column in your table. If you are trying to use Oracle rownum instead then consider using row_number() function rather
update table_name set column_name =
select rn from ( select column_name, row_number() over (order by column_name) rn
from table_name ) xx;
As you state yourself, rownum is a virtual column. It assigns a sequential value to each row of a particular result set. Which means that the row number of a row could be completely different in the result set of a different query.
If you really want to show the row number as part of the result set, specify it as you would any column:
select rownum as columnName, columnDay
from table
order by ...;
I am using MS SQL 2008 R2. One of my table have more than 10 lac rows — 1 lac is 105 or 100,000, so 10 lac is 1,000,000).
I want to bind this to ASP Gridview. I tried custom paging with page size and index. But grid not binded. Timeout Error occured.
Tried directly execute stored procedure, but it takes a long time.
How can I optimize this procedure ?
My procedure
ALTER PROCEDURE SP_LOAN_APPROVAL_GET_LIST
#USERCODE NVARCHAR(50) ,
#FROMDATE DATETIME = NULL ,
#TODATE DATETIME = NULL ,
#PAGESIZE INT ,
#PAGENO INT ,
#TOTALROW BIGINT OUTPUT
AS
BEGIN
SELECT *
FROM ( SELECT DOC_NO ,
DOC_DATE_GRE ,
EMP_CODE ,
EMP_NAME_ENG as Name ,
LOAN_AMOUNT ,
DESC_ENG as Discription ,
REMARKS ,
ROW_NUMBER() OVER(
ORDER BY ( SELECT 1 )
) AS [ROWNO]
from VW_PER_LOAN
Where isnull( POST_FLAG , 'N' ) = 'N'
and ISNULl( CANCEL_FLAG , 'N' ) != 'Y'
and DOC_DATE_GRE between ISNULL(#FROMDATE , DOC_DATE_GRE )
and ISNULL(#TODATE , DOC_DATE_GRE )
and BRANCH in ( SELECT *
FROM DBO.FN_SSP_GetAllowedBranches(#USERCODE)
)
) T
WHERE T.ROWNO BETWEEN ((#PAGENO-1)*#PAGESIZE)+1 AND #PAGESIZE*(#PAGENO)
SELECT #TOTALROW=COUNT(*)
from VW_PER_LOAN
Where isnull(POST_FLAG,'N')= 'N'
and ISNULl(CANCEL_FLAG,'N')!='Y'
and DOC_DATE_GRE between ISNULL(#FROMDATE,DOC_DATE_GRE)and ISNULL(#TODATE,DOC_DATE_GRE)
and BRANCH in ( SELECT *
FROM DBO.FN_SSP_GetAllowedBranches(#USERCODE)
)
END
Thanks
The first thing to do is to look at your execution plan and discuss it with a DBA if you don't understand it.
The obvious thing that stands out is that your where clause has pretty much every column reference wrapped in some sort of function. That makes them expressions and make the SQL optimizer unable to use any covering indices that might exist.
It looks like you are calling a table-valued function as an uncorrelated subquery. That would worry me with respect to performance. I'd probably move that out of the query. Instead run it just once and populate a temporary table.
I want to get data of different ranges like "SUBSCRIBER_ZIP between '30000' and '31999'"
and "SUBSCRIBER_ZIP between '39813' and '39901'" and "SUBSCRIBER_ZIP between '32000' and '34999'".
I want to get all the Active records(means status=1) having this search criteria. I have used Union for this but it does not provide me unique records:
Select distinct(SUBSCRIBER_EMAIL), SUBSCRIBER_Id FROM
(select distinct(SUBSCRIBER_EMAIL),SUBSCRIBER_ID from SUBSCRIBER where SUBSCRIBER_ACTIVE = 1 and SUBSCRIBER_ZIP between '30000' and '31999'
Union
select distinct(SUBSCRIBER_EMAIL),SUBSCRIBER_ID from SUBSCRIBER where SUBSCRIBER_ACTIVE = 1 and SUBSCRIBER_ZIP between '39813' and '39901'
Union
select distinct(SUBSCRIBER_EMAIL),SUBSCRIBER_ID from SUBSCRIBER where SUBSCRIBER_ACTIVE = 1 and SUBSCRIBER_ZIP between '32000' and '34999'
) x
But it does not provide me unique results. Please help me how can i do it.
Why not use OR?
select distinct SUBSCRIBER_EMAIL, SUBSCRIBER_ID
from SUBSCRIBER
where SUBSCRIBER_ACTIVE = 1
and (
SUBSCRIBER_ZIP between '30000' and '31999' or
SUBSCRIBER_ZIP between '39813' and '39901' or
SUBSCRIBER_ZIP between '32000' and '34999'
)
SELECT DISTINCT [SUBSCRIBER_EMAIL],[SUBSCRIBER_ID] FROM SUBSCRIBER WHERE ([SUBSCRIBER_ACTIVE] = 1) AND
(
(CAST(SUBSCRIBER_ZIP AS INT) BETWEEN 30000 AND 31999)
OR
(CAST(SUBSCRIBER_ZIP AS INT) BETWEEN 39813 AND 39901)
OR
(CAST(SUBSCRIBER_ZIP AS INT) BETWEEN 32000 AND 34999)
)
Note : Don't use quotations for the numbers