Panda 5888
asked on
SQL Query by row number based on category
Dear Expert,
How to construct sql from table below,
Col A Col B Col C
XX1 XX2 XX3
XX1 XX2 XX4
XX1 XX2 XX5
So, that it return out as per below:
Col A Col B Col C
XX1 XX2 XX3
it returns first row of Col C, when i select distinct all columns.
Pls Help
Thks.
How to construct sql from table below,
Col A Col B Col C
XX1 XX2 XX3
XX1 XX2 XX4
XX1 XX2 XX5
So, that it return out as per below:
Col A Col B Col C
XX1 XX2 XX3
it returns first row of Col C, when i select distinct all columns.
Pls Help
Thks.
ASKER
but it's not numerical column, it's string
please read this article to solve the issue:
https://www.experts-exchange.com/A_3203.html
https://www.experts-exchange.com/A_3203.html
to get the result as per your requirment you need to do it like this
Is there any numarical unique id in the table ??
IF yes then you can query like this ..assume colId as unique numarical column
select colid ,col1,col2,colc from tablename
where colid in
(
Select min(colid ) from tablename
Group by col1,col2 )
Is there any numarical unique id in the table ??
IF yes then you can query like this ..assume colId as unique numarical column
select colid ,col1,col2,colc from tablename
where colid in
(
Select min(colid ) from tablename
Group by col1,col2 )
ASKER
my table didn't contains unique key and numerical column, any suggestion????
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Exactly how do you define first row/entry for that column? The first one actually inserted? I don't think you will be able to get consistent results with that. For standard heap tables Oracle will just insert data where it can, and physical arrangement can and will change, especially when you backup/restore/transfer data.
Suggest you take time to add a numerical column and assign numbers for each row. For maintenance, use a sequence to automatically assign the value of your numerical column upon insert of new data. This way you can now a query like the one mentioned by pratima
Suggest you take time to add a numerical column and assign numbers for each row. For maintenance, use a sequence to automatically assign the value of your numerical column upon insert of new data. This way you can now a query like the one mentioned by pratima
ASKER
thks i got the solution by using following method :
ROW_NUMBER() OVER ( PARTITION BY AREA,IE_RESOURCE,FW_RESOUR CE, STEP, TECH ORDER BY AREA,IE_RESOURCE,FW_RESOUR CE,STEP,TE CH ASC ) rn
This is my SQL:
SELECT DISTINCT AREA,IE_RESOURCE,FW_RESOUR CE, STEP,TECH, RECIPE
FROM
(
SELECT DISTINCT AREA,IE_RESOURCE,FW_RESOUR CE, STEP,TECH, RECIPE, DEVICE,
ROW_NUMBER() OVER ( PARTITION BY AREA,IE_RESOURCE,FW_RESOUR CE, STEP, TECH ORDER BY AREA,IE_RESOURCE,FW_RESOUR CE,STEP,TE CH ASC ) rn
FROM (
SELECT SUBSTR(A.IE_RESOURCE,0,3) AS AREA , A.TECH AS TECH, A.STEP AS STEP, A.FW_RESOURCE AS FW_RESOURCE, A.IE_RESOURCE AS IE_RESOURCE , A.RECIPE AS RECIPE,A.DEVICE AS DEVICE
FROM IEDC_TECH_WPH_R2_TST A
WHERE ( A.REMARKS NOT LIKE '%Step-NotActive%' OR A.REMARKS IS NULL)
UNION
SELECT SUBSTR(B.IE_RESOURCE,0,3) AS AREA, B.TECH AS TECH, B.STEP AS STEP, B.FW_RESOURCE AS FW_RESOURCE, B.IE_RESOURCE AS IE_RESOURCE,B.RECIPE AS RECIPE,B.DEVICE AS DEVICE
FROM IEDC_TECH_WPH_R2ALT B
WHERE ( B.REMARKS NOT LIKE '%Step-NotActive%' OR B.REMARKS IS NULL)
)
)
WHERE rn = 1
ORDER BY AREA,IE_RESOURCE,FW_RESOUR CE, STEP,RECIPE, TECH
Anyway how to add where condition in
ROW_NUMBER() OVER ( PARTITION BY AREA,IE_RESOURCE,FW_RESOUR CE, STEP, TECH ORDER BY AREA,IE_RESOURCE,FW_RESOUR CE,STEP,TE CH ASC ) rn
i just row number WHERE DEVICE = 'DEFAULT'
Pls help.
ROW_NUMBER() OVER ( PARTITION BY AREA,IE_RESOURCE,FW_RESOUR
This is my SQL:
SELECT DISTINCT AREA,IE_RESOURCE,FW_RESOUR
FROM
(
SELECT DISTINCT AREA,IE_RESOURCE,FW_RESOUR
ROW_NUMBER() OVER ( PARTITION BY AREA,IE_RESOURCE,FW_RESOUR
FROM (
SELECT SUBSTR(A.IE_RESOURCE,0,3) AS AREA , A.TECH AS TECH, A.STEP AS STEP, A.FW_RESOURCE AS FW_RESOURCE, A.IE_RESOURCE AS IE_RESOURCE , A.RECIPE AS RECIPE,A.DEVICE AS DEVICE
FROM IEDC_TECH_WPH_R2_TST A
WHERE ( A.REMARKS NOT LIKE '%Step-NotActive%' OR A.REMARKS IS NULL)
UNION
SELECT SUBSTR(B.IE_RESOURCE,0,3) AS AREA, B.TECH AS TECH, B.STEP AS STEP, B.FW_RESOURCE AS FW_RESOURCE, B.IE_RESOURCE AS IE_RESOURCE,B.RECIPE AS RECIPE,B.DEVICE AS DEVICE
FROM IEDC_TECH_WPH_R2ALT B
WHERE ( B.REMARKS NOT LIKE '%Step-NotActive%' OR B.REMARKS IS NULL)
)
)
WHERE rn = 1
ORDER BY AREA,IE_RESOURCE,FW_RESOUR
Anyway how to add where condition in
ROW_NUMBER() OVER ( PARTITION BY AREA,IE_RESOURCE,FW_RESOUR
i just row number WHERE DEVICE = 'DEFAULT'
Pls help.
can you please clarify with output samples of what you get and what you want?
I am not 100% sure ...
I am not 100% sure ...
Select col1,col2,min(colc) from tablename
Group by col1,col2