How to select columns based on count of number of records returned?How do I perform an IF…THEN in an SQL SELECT?How to return only the Date from a SQL Server DateTime datatypeHow do I limit the number of rows returned by an Oracle query after ordering?How can I SELECT rows with MAX(Column value), DISTINCT by another column in SQL?SQL exclude a column using SELECT * [except columnA] FROM tableA?How do I UPDATE from a SELECT in SQL Server?'IF' in 'SELECT' statement - choose output value based on column valuesSQL select only rows with max value on a columnSQL - Select distinct based on specific columnsHow do I select each row from a table, ordering them by the most “recent (timestamp)” associated row in join table?
How can a drink contain 1.8 kcal energy while 0 g fat/carbs/protein?
Cauchy reals and Dedekind reals satisfy "the same mathematical theorems"
Do dragons smell of lilacs?
ArcPy Delete Function not working inside for loop?
Playing saxophone without using the octave key
Wordplay subtraction paradox
Mortgage as present value of resale worth
Is it okay for a chapter's POV to shift as it progresses?
How to find abandoned railways in Google Maps?
What is the meaning of [[:space:]] in bash?
What "fuel more powerful than anything the West (had) in stock" put Laika in orbit aboard Sputnik 2?
How can electric field be defined as force per charge, if the charge makes its own, singular electric field?
Which GPUs to get for Mathematical Optimization (if any...)?
Does inertia keep a rotating object rotating forever, or something else?
Is the purpose of sheet music to be played along to? Or a guide for learning and reference during playing?
How many bits in the resultant hash will change, if the x bits are changed in its the original input?
Can a Resident Assistant be told to ignore a lawful order?'
Why do so many pure math PhD students drop out or leave academia, compared to applied mathematics PhDs?
Did Voldemort kill his father before finding out about Horcruxes?
What prompted Cuba to fight against South African Imperialism?
Alternator dying so junk car?
Sending a photo of my bank account card to the future employer
Increasing muscle power without increasing volume
What happens if there is no space for entry stamp in the passport for US visa?
How to select columns based on count of number of records returned?
How do I perform an IF…THEN in an SQL SELECT?How to return only the Date from a SQL Server DateTime datatypeHow do I limit the number of rows returned by an Oracle query after ordering?How can I SELECT rows with MAX(Column value), DISTINCT by another column in SQL?SQL exclude a column using SELECT * [except columnA] FROM tableA?How do I UPDATE from a SELECT in SQL Server?'IF' in 'SELECT' statement - choose output value based on column valuesSQL select only rows with max value on a columnSQL - Select distinct based on specific columnsHow do I select each row from a table, ordering them by the most “recent (timestamp)” associated row in join table?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
This is the table I have
| prdntVrsnNum | matlNum | wrkCtrCd | ritmValFromDt | versnValFromDt | parmVldFromDt | rtgTypeCd | rtgGrpCd | rtgNodeNum | charVal |
| ------------ | ------- | -------- | ------------- | -------------- | ------------- | --------- | -------- | ---------- | ------- |
| V001 | 96228 | TO-HCH2 | 20170407 | 20170407 | 20170407 | N | 50020937 | 1 | 7 |
| V001 | 96228 | TO-HCH2 | 20170407 | 20170407 | 20170407 | N | 50020937 | 1 | 7 |
| V001 | 96228 | TO-HCH2 | 20170407 | 20170407 | 20180101 | N | 50020937 | 1 | 7 |
| V001 | 96228 | TO-HCH2 | 20170407 | 20170407 | 20180101 | N | 50020937 | 1 | 7 |
| V003 | 197774 | TOU-A16 | 20181210 | 20181207 | 20190107 | N | 50018492 | 6 | 1 |
| V003 | 197774 | TOU-A16 | 20181210 | 20181207 | 20190107 | N | 50018492 | 6 | 1 |
| V001 | 66850 | LI-LIN2 | 20180101 | 20141211 | 20141211 | N | 50018966 | 1 | 5 |
| V001 | 66850 | LI-LIN2 | 20180101 | 20141211 | 20151227 | N | 50018966 | 1 | 4.5 |
| V001 | 66850 | LI-LIN2 | 20180101 | 20141211 | 20141211 | N | 50018966 | 1 | 5 |
| V001 | 66850 | LI-LIN2 | 20180101 | 20141211 | 20151227 | N | 50018966 | 1 | 4.5 |
Desired Result:
TOU-A16/V003/197774/20181210
TO-HCH2/V001/96228/20180101
LI-LIN2/V001/66850/20180101
Requirements/Conditions:
Having prdntVrsnNum
, matlNum
, wrkCtrCd
as key columns,
Delete duplicates according to
rtgTypeCd
,rtgGrpCd
,rtgNodeNum
,parmVldFromDt
,charVal
If no records found, do nothing.
If one record found, take
ritmValFromDt
for concatenationIf multiple records found, see how many records have
parmVldFromDt
>=ritmValFromDt
4.1. If one record found, then use
ritmValFromDt
for concatenation.4.2. If multiple records found, then use
parmVldFromDt
for concatenation.
Intermediate result which I am using for explanation:
SELECT distinct * from mytable;
| prdntVrsnNum | matlNum | wrkCtrCd | ritmValFromDt | versnValFromDt | parmVldFromDt | rtgTypeCd | rtgGrpCd | rtgNodeNum | charVal |
| ------------ | ------- | -------- | ------------- | -------------- | ------------- | --------- | -------- | ---------- | ------- |
| V001 | 96228 | TO-HCH2 | 20170407 | 20170407 | 20170407 | N | 50020937 | 1 | 7 |
| V001 | 96228 | TO-HCH2 | 20170407 | 20170407 | 20180101 | N | 50020937 | 1 | 7 |
| V003 | 197774 | TOU-A16 | 20181210 | 20181207 | 20190107 | N | 50018492 | 6 | 1 |
| V001 | 66850 | LI-LIN2 | 20180101 | 20141211 | 20141211 | N | 50018966 | 1 | 5 |
| V001 | 66850 | LI-LIN2 | 20180101 | 20141211 | 20151227 | N | 50018966 | 1 | 4.5 |
From the above table, now I should reduce it to the following final table, which I will use for concatenation.
| prdntVrsnNum | matlNum | wrkCtrCd | ritmValFromDt | versnValFromDt | parmVldFromDt | rtgTypeCd | rtgGrpCd | rtgNodeNum | charVal |
| ------------ | ------- | -------- | ------------- | -------------- | ------------- | --------- | -------- | ---------- | ------- |
| V001 | 96228 | TO-HCH2 | 20170407 | 20170407 | 20180101 | N | 50020937 | 1 | 7 |
| V003 | 197774 | TOU-A16 | 20181210 | 20181207 | 20190107 | N | 50018492 | 6 | 1 |
| V001 | 66850 | LI-LIN2 | 20180101 | 20141211 | 20141211 | N | 50018966 | 1 | 5 |
| V001 | 66850 | LI-LIN2 | 20180101 | 20141211 | 20151227 | N | 50018966 | 1 | 4.5 |
EDIT: Further explanation:
V001|96228|TO-HCH2
comes under condition4.2
.V003|197774|TOU-A16
comes under condition4.1
.V001|66850|LI-LIN2
I think this condition comes by choosingritmValFromDt
since that's the greater one and we don't have any record for this combination havingparmVldFromDt >= ritmValFromDt
But I'm not certain about this though.. :(
DB-Fiddle: https://www.db-fiddle.com/f/qZLyGdyv2spYe3ZokZhYAP/1
What I have tried so far:
SELECT
DISTINCT
CONCAT(wrkCtrCd, '/', prdntVrsnNum , '/', matlNum , '/', ritmValFromDt)
AS outputCol
FROM mytable;
| outputCol |
| ---------------------------- |
| TO-HCH2/V001/96228/20170407 |
| TOU-A16/V003/197774/20181210 |
SELECT
DISTINCT
CONCAT(wrkCtrCd, '/', prdntVrsnNum , '/', matlNum , '/',
CASE
WHEN parmVldFromDt >= ritmValFromDt THEN parmVldFromDt
ELSE ritmValFromDt
END)
AS outputCol
FROM mytable;
| outputCol |
| ---------------------------- |
| TO-HCH2/V001/96228/20170407 |
| TO-HCH2/V001/96228/20180101 |
| TOU-A16/V003/197774/20190107 |
NOTE
Please be as vendor neutral as possible when using SQL. This is not in my hands. I am not querying from source, so my hands are tied. Some vendor specific functions may be available, but this is not a guarantee.
In the db-fiddle, MySQL (and the version used) is for illustration purpose only. As stated previously, I am not querying from an RDBMS, I am querying from a grid, so I have to adjust with what is available and how it is available in the grid.
sql
add a comment |
This is the table I have
| prdntVrsnNum | matlNum | wrkCtrCd | ritmValFromDt | versnValFromDt | parmVldFromDt | rtgTypeCd | rtgGrpCd | rtgNodeNum | charVal |
| ------------ | ------- | -------- | ------------- | -------------- | ------------- | --------- | -------- | ---------- | ------- |
| V001 | 96228 | TO-HCH2 | 20170407 | 20170407 | 20170407 | N | 50020937 | 1 | 7 |
| V001 | 96228 | TO-HCH2 | 20170407 | 20170407 | 20170407 | N | 50020937 | 1 | 7 |
| V001 | 96228 | TO-HCH2 | 20170407 | 20170407 | 20180101 | N | 50020937 | 1 | 7 |
| V001 | 96228 | TO-HCH2 | 20170407 | 20170407 | 20180101 | N | 50020937 | 1 | 7 |
| V003 | 197774 | TOU-A16 | 20181210 | 20181207 | 20190107 | N | 50018492 | 6 | 1 |
| V003 | 197774 | TOU-A16 | 20181210 | 20181207 | 20190107 | N | 50018492 | 6 | 1 |
| V001 | 66850 | LI-LIN2 | 20180101 | 20141211 | 20141211 | N | 50018966 | 1 | 5 |
| V001 | 66850 | LI-LIN2 | 20180101 | 20141211 | 20151227 | N | 50018966 | 1 | 4.5 |
| V001 | 66850 | LI-LIN2 | 20180101 | 20141211 | 20141211 | N | 50018966 | 1 | 5 |
| V001 | 66850 | LI-LIN2 | 20180101 | 20141211 | 20151227 | N | 50018966 | 1 | 4.5 |
Desired Result:
TOU-A16/V003/197774/20181210
TO-HCH2/V001/96228/20180101
LI-LIN2/V001/66850/20180101
Requirements/Conditions:
Having prdntVrsnNum
, matlNum
, wrkCtrCd
as key columns,
Delete duplicates according to
rtgTypeCd
,rtgGrpCd
,rtgNodeNum
,parmVldFromDt
,charVal
If no records found, do nothing.
If one record found, take
ritmValFromDt
for concatenationIf multiple records found, see how many records have
parmVldFromDt
>=ritmValFromDt
4.1. If one record found, then use
ritmValFromDt
for concatenation.4.2. If multiple records found, then use
parmVldFromDt
for concatenation.
Intermediate result which I am using for explanation:
SELECT distinct * from mytable;
| prdntVrsnNum | matlNum | wrkCtrCd | ritmValFromDt | versnValFromDt | parmVldFromDt | rtgTypeCd | rtgGrpCd | rtgNodeNum | charVal |
| ------------ | ------- | -------- | ------------- | -------------- | ------------- | --------- | -------- | ---------- | ------- |
| V001 | 96228 | TO-HCH2 | 20170407 | 20170407 | 20170407 | N | 50020937 | 1 | 7 |
| V001 | 96228 | TO-HCH2 | 20170407 | 20170407 | 20180101 | N | 50020937 | 1 | 7 |
| V003 | 197774 | TOU-A16 | 20181210 | 20181207 | 20190107 | N | 50018492 | 6 | 1 |
| V001 | 66850 | LI-LIN2 | 20180101 | 20141211 | 20141211 | N | 50018966 | 1 | 5 |
| V001 | 66850 | LI-LIN2 | 20180101 | 20141211 | 20151227 | N | 50018966 | 1 | 4.5 |
From the above table, now I should reduce it to the following final table, which I will use for concatenation.
| prdntVrsnNum | matlNum | wrkCtrCd | ritmValFromDt | versnValFromDt | parmVldFromDt | rtgTypeCd | rtgGrpCd | rtgNodeNum | charVal |
| ------------ | ------- | -------- | ------------- | -------------- | ------------- | --------- | -------- | ---------- | ------- |
| V001 | 96228 | TO-HCH2 | 20170407 | 20170407 | 20180101 | N | 50020937 | 1 | 7 |
| V003 | 197774 | TOU-A16 | 20181210 | 20181207 | 20190107 | N | 50018492 | 6 | 1 |
| V001 | 66850 | LI-LIN2 | 20180101 | 20141211 | 20141211 | N | 50018966 | 1 | 5 |
| V001 | 66850 | LI-LIN2 | 20180101 | 20141211 | 20151227 | N | 50018966 | 1 | 4.5 |
EDIT: Further explanation:
V001|96228|TO-HCH2
comes under condition4.2
.V003|197774|TOU-A16
comes under condition4.1
.V001|66850|LI-LIN2
I think this condition comes by choosingritmValFromDt
since that's the greater one and we don't have any record for this combination havingparmVldFromDt >= ritmValFromDt
But I'm not certain about this though.. :(
DB-Fiddle: https://www.db-fiddle.com/f/qZLyGdyv2spYe3ZokZhYAP/1
What I have tried so far:
SELECT
DISTINCT
CONCAT(wrkCtrCd, '/', prdntVrsnNum , '/', matlNum , '/', ritmValFromDt)
AS outputCol
FROM mytable;
| outputCol |
| ---------------------------- |
| TO-HCH2/V001/96228/20170407 |
| TOU-A16/V003/197774/20181210 |
SELECT
DISTINCT
CONCAT(wrkCtrCd, '/', prdntVrsnNum , '/', matlNum , '/',
CASE
WHEN parmVldFromDt >= ritmValFromDt THEN parmVldFromDt
ELSE ritmValFromDt
END)
AS outputCol
FROM mytable;
| outputCol |
| ---------------------------- |
| TO-HCH2/V001/96228/20170407 |
| TO-HCH2/V001/96228/20180101 |
| TOU-A16/V003/197774/20190107 |
NOTE
Please be as vendor neutral as possible when using SQL. This is not in my hands. I am not querying from source, so my hands are tied. Some vendor specific functions may be available, but this is not a guarantee.
In the db-fiddle, MySQL (and the version used) is for illustration purpose only. As stated previously, I am not querying from an RDBMS, I am querying from a grid, so I have to adjust with what is available and how it is available in the grid.
sql
imho the simplest solution to this would be to first create a query that produces the answers/evaluations to all the conditions (1., 2., 3., 4.1 and 4.2) for every prdntVrsnNum-wrkCtrCd combination viaGROUP BY
- let's say it's query Q. then you create the outputCol depending on the values calculated in Q (join Q) by creating with the case statement - which you already may master. There might be some vendor specific black magic that might make it possible without the join of the aggregate ... but you wanted it to be vendor unspecific ;o)
– Jakumi
Mar 26 at 9:38
@Jakumi I don't know how to write a case statement according to number of rows returned. That's one of the parts where I am stuck.
– scientific_explorer
Mar 26 at 9:44
@Jakumi you can give a vendor specific black magic if you want, I can check in my environment if it is available/enabled. :)
– scientific_explorer
Mar 26 at 9:47
@Jakumi I have posted an answer. Please have a look.
– scientific_explorer
Mar 26 at 10:42
add a comment |
This is the table I have
| prdntVrsnNum | matlNum | wrkCtrCd | ritmValFromDt | versnValFromDt | parmVldFromDt | rtgTypeCd | rtgGrpCd | rtgNodeNum | charVal |
| ------------ | ------- | -------- | ------------- | -------------- | ------------- | --------- | -------- | ---------- | ------- |
| V001 | 96228 | TO-HCH2 | 20170407 | 20170407 | 20170407 | N | 50020937 | 1 | 7 |
| V001 | 96228 | TO-HCH2 | 20170407 | 20170407 | 20170407 | N | 50020937 | 1 | 7 |
| V001 | 96228 | TO-HCH2 | 20170407 | 20170407 | 20180101 | N | 50020937 | 1 | 7 |
| V001 | 96228 | TO-HCH2 | 20170407 | 20170407 | 20180101 | N | 50020937 | 1 | 7 |
| V003 | 197774 | TOU-A16 | 20181210 | 20181207 | 20190107 | N | 50018492 | 6 | 1 |
| V003 | 197774 | TOU-A16 | 20181210 | 20181207 | 20190107 | N | 50018492 | 6 | 1 |
| V001 | 66850 | LI-LIN2 | 20180101 | 20141211 | 20141211 | N | 50018966 | 1 | 5 |
| V001 | 66850 | LI-LIN2 | 20180101 | 20141211 | 20151227 | N | 50018966 | 1 | 4.5 |
| V001 | 66850 | LI-LIN2 | 20180101 | 20141211 | 20141211 | N | 50018966 | 1 | 5 |
| V001 | 66850 | LI-LIN2 | 20180101 | 20141211 | 20151227 | N | 50018966 | 1 | 4.5 |
Desired Result:
TOU-A16/V003/197774/20181210
TO-HCH2/V001/96228/20180101
LI-LIN2/V001/66850/20180101
Requirements/Conditions:
Having prdntVrsnNum
, matlNum
, wrkCtrCd
as key columns,
Delete duplicates according to
rtgTypeCd
,rtgGrpCd
,rtgNodeNum
,parmVldFromDt
,charVal
If no records found, do nothing.
If one record found, take
ritmValFromDt
for concatenationIf multiple records found, see how many records have
parmVldFromDt
>=ritmValFromDt
4.1. If one record found, then use
ritmValFromDt
for concatenation.4.2. If multiple records found, then use
parmVldFromDt
for concatenation.
Intermediate result which I am using for explanation:
SELECT distinct * from mytable;
| prdntVrsnNum | matlNum | wrkCtrCd | ritmValFromDt | versnValFromDt | parmVldFromDt | rtgTypeCd | rtgGrpCd | rtgNodeNum | charVal |
| ------------ | ------- | -------- | ------------- | -------------- | ------------- | --------- | -------- | ---------- | ------- |
| V001 | 96228 | TO-HCH2 | 20170407 | 20170407 | 20170407 | N | 50020937 | 1 | 7 |
| V001 | 96228 | TO-HCH2 | 20170407 | 20170407 | 20180101 | N | 50020937 | 1 | 7 |
| V003 | 197774 | TOU-A16 | 20181210 | 20181207 | 20190107 | N | 50018492 | 6 | 1 |
| V001 | 66850 | LI-LIN2 | 20180101 | 20141211 | 20141211 | N | 50018966 | 1 | 5 |
| V001 | 66850 | LI-LIN2 | 20180101 | 20141211 | 20151227 | N | 50018966 | 1 | 4.5 |
From the above table, now I should reduce it to the following final table, which I will use for concatenation.
| prdntVrsnNum | matlNum | wrkCtrCd | ritmValFromDt | versnValFromDt | parmVldFromDt | rtgTypeCd | rtgGrpCd | rtgNodeNum | charVal |
| ------------ | ------- | -------- | ------------- | -------------- | ------------- | --------- | -------- | ---------- | ------- |
| V001 | 96228 | TO-HCH2 | 20170407 | 20170407 | 20180101 | N | 50020937 | 1 | 7 |
| V003 | 197774 | TOU-A16 | 20181210 | 20181207 | 20190107 | N | 50018492 | 6 | 1 |
| V001 | 66850 | LI-LIN2 | 20180101 | 20141211 | 20141211 | N | 50018966 | 1 | 5 |
| V001 | 66850 | LI-LIN2 | 20180101 | 20141211 | 20151227 | N | 50018966 | 1 | 4.5 |
EDIT: Further explanation:
V001|96228|TO-HCH2
comes under condition4.2
.V003|197774|TOU-A16
comes under condition4.1
.V001|66850|LI-LIN2
I think this condition comes by choosingritmValFromDt
since that's the greater one and we don't have any record for this combination havingparmVldFromDt >= ritmValFromDt
But I'm not certain about this though.. :(
DB-Fiddle: https://www.db-fiddle.com/f/qZLyGdyv2spYe3ZokZhYAP/1
What I have tried so far:
SELECT
DISTINCT
CONCAT(wrkCtrCd, '/', prdntVrsnNum , '/', matlNum , '/', ritmValFromDt)
AS outputCol
FROM mytable;
| outputCol |
| ---------------------------- |
| TO-HCH2/V001/96228/20170407 |
| TOU-A16/V003/197774/20181210 |
SELECT
DISTINCT
CONCAT(wrkCtrCd, '/', prdntVrsnNum , '/', matlNum , '/',
CASE
WHEN parmVldFromDt >= ritmValFromDt THEN parmVldFromDt
ELSE ritmValFromDt
END)
AS outputCol
FROM mytable;
| outputCol |
| ---------------------------- |
| TO-HCH2/V001/96228/20170407 |
| TO-HCH2/V001/96228/20180101 |
| TOU-A16/V003/197774/20190107 |
NOTE
Please be as vendor neutral as possible when using SQL. This is not in my hands. I am not querying from source, so my hands are tied. Some vendor specific functions may be available, but this is not a guarantee.
In the db-fiddle, MySQL (and the version used) is for illustration purpose only. As stated previously, I am not querying from an RDBMS, I am querying from a grid, so I have to adjust with what is available and how it is available in the grid.
sql
This is the table I have
| prdntVrsnNum | matlNum | wrkCtrCd | ritmValFromDt | versnValFromDt | parmVldFromDt | rtgTypeCd | rtgGrpCd | rtgNodeNum | charVal |
| ------------ | ------- | -------- | ------------- | -------------- | ------------- | --------- | -------- | ---------- | ------- |
| V001 | 96228 | TO-HCH2 | 20170407 | 20170407 | 20170407 | N | 50020937 | 1 | 7 |
| V001 | 96228 | TO-HCH2 | 20170407 | 20170407 | 20170407 | N | 50020937 | 1 | 7 |
| V001 | 96228 | TO-HCH2 | 20170407 | 20170407 | 20180101 | N | 50020937 | 1 | 7 |
| V001 | 96228 | TO-HCH2 | 20170407 | 20170407 | 20180101 | N | 50020937 | 1 | 7 |
| V003 | 197774 | TOU-A16 | 20181210 | 20181207 | 20190107 | N | 50018492 | 6 | 1 |
| V003 | 197774 | TOU-A16 | 20181210 | 20181207 | 20190107 | N | 50018492 | 6 | 1 |
| V001 | 66850 | LI-LIN2 | 20180101 | 20141211 | 20141211 | N | 50018966 | 1 | 5 |
| V001 | 66850 | LI-LIN2 | 20180101 | 20141211 | 20151227 | N | 50018966 | 1 | 4.5 |
| V001 | 66850 | LI-LIN2 | 20180101 | 20141211 | 20141211 | N | 50018966 | 1 | 5 |
| V001 | 66850 | LI-LIN2 | 20180101 | 20141211 | 20151227 | N | 50018966 | 1 | 4.5 |
Desired Result:
TOU-A16/V003/197774/20181210
TO-HCH2/V001/96228/20180101
LI-LIN2/V001/66850/20180101
Requirements/Conditions:
Having prdntVrsnNum
, matlNum
, wrkCtrCd
as key columns,
Delete duplicates according to
rtgTypeCd
,rtgGrpCd
,rtgNodeNum
,parmVldFromDt
,charVal
If no records found, do nothing.
If one record found, take
ritmValFromDt
for concatenationIf multiple records found, see how many records have
parmVldFromDt
>=ritmValFromDt
4.1. If one record found, then use
ritmValFromDt
for concatenation.4.2. If multiple records found, then use
parmVldFromDt
for concatenation.
Intermediate result which I am using for explanation:
SELECT distinct * from mytable;
| prdntVrsnNum | matlNum | wrkCtrCd | ritmValFromDt | versnValFromDt | parmVldFromDt | rtgTypeCd | rtgGrpCd | rtgNodeNum | charVal |
| ------------ | ------- | -------- | ------------- | -------------- | ------------- | --------- | -------- | ---------- | ------- |
| V001 | 96228 | TO-HCH2 | 20170407 | 20170407 | 20170407 | N | 50020937 | 1 | 7 |
| V001 | 96228 | TO-HCH2 | 20170407 | 20170407 | 20180101 | N | 50020937 | 1 | 7 |
| V003 | 197774 | TOU-A16 | 20181210 | 20181207 | 20190107 | N | 50018492 | 6 | 1 |
| V001 | 66850 | LI-LIN2 | 20180101 | 20141211 | 20141211 | N | 50018966 | 1 | 5 |
| V001 | 66850 | LI-LIN2 | 20180101 | 20141211 | 20151227 | N | 50018966 | 1 | 4.5 |
From the above table, now I should reduce it to the following final table, which I will use for concatenation.
| prdntVrsnNum | matlNum | wrkCtrCd | ritmValFromDt | versnValFromDt | parmVldFromDt | rtgTypeCd | rtgGrpCd | rtgNodeNum | charVal |
| ------------ | ------- | -------- | ------------- | -------------- | ------------- | --------- | -------- | ---------- | ------- |
| V001 | 96228 | TO-HCH2 | 20170407 | 20170407 | 20180101 | N | 50020937 | 1 | 7 |
| V003 | 197774 | TOU-A16 | 20181210 | 20181207 | 20190107 | N | 50018492 | 6 | 1 |
| V001 | 66850 | LI-LIN2 | 20180101 | 20141211 | 20141211 | N | 50018966 | 1 | 5 |
| V001 | 66850 | LI-LIN2 | 20180101 | 20141211 | 20151227 | N | 50018966 | 1 | 4.5 |
EDIT: Further explanation:
V001|96228|TO-HCH2
comes under condition4.2
.V003|197774|TOU-A16
comes under condition4.1
.V001|66850|LI-LIN2
I think this condition comes by choosingritmValFromDt
since that's the greater one and we don't have any record for this combination havingparmVldFromDt >= ritmValFromDt
But I'm not certain about this though.. :(
DB-Fiddle: https://www.db-fiddle.com/f/qZLyGdyv2spYe3ZokZhYAP/1
What I have tried so far:
SELECT
DISTINCT
CONCAT(wrkCtrCd, '/', prdntVrsnNum , '/', matlNum , '/', ritmValFromDt)
AS outputCol
FROM mytable;
| outputCol |
| ---------------------------- |
| TO-HCH2/V001/96228/20170407 |
| TOU-A16/V003/197774/20181210 |
SELECT
DISTINCT
CONCAT(wrkCtrCd, '/', prdntVrsnNum , '/', matlNum , '/',
CASE
WHEN parmVldFromDt >= ritmValFromDt THEN parmVldFromDt
ELSE ritmValFromDt
END)
AS outputCol
FROM mytable;
| outputCol |
| ---------------------------- |
| TO-HCH2/V001/96228/20170407 |
| TO-HCH2/V001/96228/20180101 |
| TOU-A16/V003/197774/20190107 |
NOTE
Please be as vendor neutral as possible when using SQL. This is not in my hands. I am not querying from source, so my hands are tied. Some vendor specific functions may be available, but this is not a guarantee.
In the db-fiddle, MySQL (and the version used) is for illustration purpose only. As stated previously, I am not querying from an RDBMS, I am querying from a grid, so I have to adjust with what is available and how it is available in the grid.
sql
sql
edited Mar 26 at 17:50
scientific_explorer
asked Mar 26 at 9:01
scientific_explorerscientific_explorer
1792 silver badges13 bronze badges
1792 silver badges13 bronze badges
imho the simplest solution to this would be to first create a query that produces the answers/evaluations to all the conditions (1., 2., 3., 4.1 and 4.2) for every prdntVrsnNum-wrkCtrCd combination viaGROUP BY
- let's say it's query Q. then you create the outputCol depending on the values calculated in Q (join Q) by creating with the case statement - which you already may master. There might be some vendor specific black magic that might make it possible without the join of the aggregate ... but you wanted it to be vendor unspecific ;o)
– Jakumi
Mar 26 at 9:38
@Jakumi I don't know how to write a case statement according to number of rows returned. That's one of the parts where I am stuck.
– scientific_explorer
Mar 26 at 9:44
@Jakumi you can give a vendor specific black magic if you want, I can check in my environment if it is available/enabled. :)
– scientific_explorer
Mar 26 at 9:47
@Jakumi I have posted an answer. Please have a look.
– scientific_explorer
Mar 26 at 10:42
add a comment |
imho the simplest solution to this would be to first create a query that produces the answers/evaluations to all the conditions (1., 2., 3., 4.1 and 4.2) for every prdntVrsnNum-wrkCtrCd combination viaGROUP BY
- let's say it's query Q. then you create the outputCol depending on the values calculated in Q (join Q) by creating with the case statement - which you already may master. There might be some vendor specific black magic that might make it possible without the join of the aggregate ... but you wanted it to be vendor unspecific ;o)
– Jakumi
Mar 26 at 9:38
@Jakumi I don't know how to write a case statement according to number of rows returned. That's one of the parts where I am stuck.
– scientific_explorer
Mar 26 at 9:44
@Jakumi you can give a vendor specific black magic if you want, I can check in my environment if it is available/enabled. :)
– scientific_explorer
Mar 26 at 9:47
@Jakumi I have posted an answer. Please have a look.
– scientific_explorer
Mar 26 at 10:42
imho the simplest solution to this would be to first create a query that produces the answers/evaluations to all the conditions (1., 2., 3., 4.1 and 4.2) for every prdntVrsnNum-wrkCtrCd combination via
GROUP BY
- let's say it's query Q. then you create the outputCol depending on the values calculated in Q (join Q) by creating with the case statement - which you already may master. There might be some vendor specific black magic that might make it possible without the join of the aggregate ... but you wanted it to be vendor unspecific ;o)– Jakumi
Mar 26 at 9:38
imho the simplest solution to this would be to first create a query that produces the answers/evaluations to all the conditions (1., 2., 3., 4.1 and 4.2) for every prdntVrsnNum-wrkCtrCd combination via
GROUP BY
- let's say it's query Q. then you create the outputCol depending on the values calculated in Q (join Q) by creating with the case statement - which you already may master. There might be some vendor specific black magic that might make it possible without the join of the aggregate ... but you wanted it to be vendor unspecific ;o)– Jakumi
Mar 26 at 9:38
@Jakumi I don't know how to write a case statement according to number of rows returned. That's one of the parts where I am stuck.
– scientific_explorer
Mar 26 at 9:44
@Jakumi I don't know how to write a case statement according to number of rows returned. That's one of the parts where I am stuck.
– scientific_explorer
Mar 26 at 9:44
@Jakumi you can give a vendor specific black magic if you want, I can check in my environment if it is available/enabled. :)
– scientific_explorer
Mar 26 at 9:47
@Jakumi you can give a vendor specific black magic if you want, I can check in my environment if it is available/enabled. :)
– scientific_explorer
Mar 26 at 9:47
@Jakumi I have posted an answer. Please have a look.
– scientific_explorer
Mar 26 at 10:42
@Jakumi I have posted an answer. Please have a look.
– scientific_explorer
Mar 26 at 10:42
add a comment |
2 Answers
2
active
oldest
votes
I don't think you have defined your conditions correctly. In particular, you for the last condition, you seem to want the latest date.
I would go for window functions for this. I think the logic is:
SELECT CONCAT(wrkCtrCd, '/', prdntVrsnNum , '/', matlNum , '/',
(CASE WHEN cnt = 1 OR cnt_gt = 1
THEN ritmValFromDt
ELSE parmVldFromDt
END)
) AS outputCol,
ritmValFromDt,parmVldFromDt
FROM (SELECT t.*,
COUNT(*) OVER (PARTITION BY prdntVrsnNum, matlNum, wrkCtrCd) as cnt,
SUM(gt_flag) OVER (PARTITION BY prdntVrsnNum, matlNum, wrkCtrCd) as cnt_gt,
ROW_NUMBER() OVER (PARTITION BY prdntVrsnNum, matlNum, wrkCtrCd, gt_flag ORDER BY parmVldFromDt DESC) as seqnum
FROM (SELECT DISTINCT prdntVrsnNum, matlNum, wrkCtrCd, ritmValFromDt, versnValFromDt, parmVldFromDt,
(CASE WHEN parmVldFromDt >= ritmValFromDt THEN 1 ELSE 0 END) as gt_flag
FROM mytable
) t
) t
WHERE (cnt_gt > 0 AND parmVldFromDt >= ritmValFromDt AND seqnum = 1) OR
(cnt_gt = 0);
Here is a db<>fiddle.
Conditions have been given to me by the functional team. I have asked them to come up with better explanations, but nothing so far. Not in my hands :(
– scientific_explorer
Mar 26 at 11:07
@scientific_explorer . . . The last conditions suggests that you want multiple rows for a given combination. However, you seem to only want the latest.
– Gordon Linoff
Mar 26 at 11:36
I agree with you.
– scientific_explorer
Mar 26 at 11:41
Accepting your answer because it gives a very close result compared to my actual data. If you would like me to provide you with the data where it fails, please let me know, so that you can modify your query accordingly.
– scientific_explorer
Mar 26 at 15:00
I have added a few more rows of data, for which your query fails to give desired output. Please have a look
– scientific_explorer
Mar 26 at 17:51
|
show 1 more comment
I came up with an answer I tried on the fiddle. Not sure if it will work in my actual system (have to test it out now). I am not sure if this is the best/only way to achieve it either.
select
distinct
concat(distinctTable.wrkCtrCd, '/',
distinctTable.prdntVrsnNum, '/',
distinctTable.matlNum, '/',
case when countTable.cnt = 1 then min(distinctTable.ritmValFromDt)
else max(distinctTable.parmVldFromDt)
end
) as outputCol
from
(
select
tbl.prdntVrsnNum,
tbl.matlNum,
tbl.wrkCtrCd,
count(*) as cnt
from
(select distinct * from mytable) as tbl
group by
tbl.prdntVrsnNum,
tbl.matlNum,
tbl.wrkCtrCd
) as countTable
inner join (select distinct * from mytable) as distinctTable
on countTable.prdntVrsnNum = distinctTable.prdntVrsnNum
and countTable.matlNum = distinctTable.matlNum
and countTable.wrkCtrCd = distinctTable.wrkCtrCd
group by
distinctTable.prdntVrsnNum,
distinctTable.matlNum,
distinctTable.wrkCtrCd;
| outputCol |
| ---------------------------- |
| TO-HCH2/V001/96228/20180101 |
| TOU-A16/V003/197774/20181210 |
your 4th condition (comparing the count of rows where a certain condition is met or not met) can probably be prepared withSUM(CASE WHEN parmVldFromDt >= ritmValFromDt THEN 1 ELSE 0) as cnt_whatever
and in your select you can use that information. However, this is very similar to Gordon'sgt_flag
, but won't work anymore in his query, but might work in yours (needs an additional group by somewhere in that sub-subquery)
– Jakumi
Mar 26 at 15:12
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55353213%2fhow-to-select-columns-based-on-count-of-number-of-records-returned%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I don't think you have defined your conditions correctly. In particular, you for the last condition, you seem to want the latest date.
I would go for window functions for this. I think the logic is:
SELECT CONCAT(wrkCtrCd, '/', prdntVrsnNum , '/', matlNum , '/',
(CASE WHEN cnt = 1 OR cnt_gt = 1
THEN ritmValFromDt
ELSE parmVldFromDt
END)
) AS outputCol,
ritmValFromDt,parmVldFromDt
FROM (SELECT t.*,
COUNT(*) OVER (PARTITION BY prdntVrsnNum, matlNum, wrkCtrCd) as cnt,
SUM(gt_flag) OVER (PARTITION BY prdntVrsnNum, matlNum, wrkCtrCd) as cnt_gt,
ROW_NUMBER() OVER (PARTITION BY prdntVrsnNum, matlNum, wrkCtrCd, gt_flag ORDER BY parmVldFromDt DESC) as seqnum
FROM (SELECT DISTINCT prdntVrsnNum, matlNum, wrkCtrCd, ritmValFromDt, versnValFromDt, parmVldFromDt,
(CASE WHEN parmVldFromDt >= ritmValFromDt THEN 1 ELSE 0 END) as gt_flag
FROM mytable
) t
) t
WHERE (cnt_gt > 0 AND parmVldFromDt >= ritmValFromDt AND seqnum = 1) OR
(cnt_gt = 0);
Here is a db<>fiddle.
Conditions have been given to me by the functional team. I have asked them to come up with better explanations, but nothing so far. Not in my hands :(
– scientific_explorer
Mar 26 at 11:07
@scientific_explorer . . . The last conditions suggests that you want multiple rows for a given combination. However, you seem to only want the latest.
– Gordon Linoff
Mar 26 at 11:36
I agree with you.
– scientific_explorer
Mar 26 at 11:41
Accepting your answer because it gives a very close result compared to my actual data. If you would like me to provide you with the data where it fails, please let me know, so that you can modify your query accordingly.
– scientific_explorer
Mar 26 at 15:00
I have added a few more rows of data, for which your query fails to give desired output. Please have a look
– scientific_explorer
Mar 26 at 17:51
|
show 1 more comment
I don't think you have defined your conditions correctly. In particular, you for the last condition, you seem to want the latest date.
I would go for window functions for this. I think the logic is:
SELECT CONCAT(wrkCtrCd, '/', prdntVrsnNum , '/', matlNum , '/',
(CASE WHEN cnt = 1 OR cnt_gt = 1
THEN ritmValFromDt
ELSE parmVldFromDt
END)
) AS outputCol,
ritmValFromDt,parmVldFromDt
FROM (SELECT t.*,
COUNT(*) OVER (PARTITION BY prdntVrsnNum, matlNum, wrkCtrCd) as cnt,
SUM(gt_flag) OVER (PARTITION BY prdntVrsnNum, matlNum, wrkCtrCd) as cnt_gt,
ROW_NUMBER() OVER (PARTITION BY prdntVrsnNum, matlNum, wrkCtrCd, gt_flag ORDER BY parmVldFromDt DESC) as seqnum
FROM (SELECT DISTINCT prdntVrsnNum, matlNum, wrkCtrCd, ritmValFromDt, versnValFromDt, parmVldFromDt,
(CASE WHEN parmVldFromDt >= ritmValFromDt THEN 1 ELSE 0 END) as gt_flag
FROM mytable
) t
) t
WHERE (cnt_gt > 0 AND parmVldFromDt >= ritmValFromDt AND seqnum = 1) OR
(cnt_gt = 0);
Here is a db<>fiddle.
Conditions have been given to me by the functional team. I have asked them to come up with better explanations, but nothing so far. Not in my hands :(
– scientific_explorer
Mar 26 at 11:07
@scientific_explorer . . . The last conditions suggests that you want multiple rows for a given combination. However, you seem to only want the latest.
– Gordon Linoff
Mar 26 at 11:36
I agree with you.
– scientific_explorer
Mar 26 at 11:41
Accepting your answer because it gives a very close result compared to my actual data. If you would like me to provide you with the data where it fails, please let me know, so that you can modify your query accordingly.
– scientific_explorer
Mar 26 at 15:00
I have added a few more rows of data, for which your query fails to give desired output. Please have a look
– scientific_explorer
Mar 26 at 17:51
|
show 1 more comment
I don't think you have defined your conditions correctly. In particular, you for the last condition, you seem to want the latest date.
I would go for window functions for this. I think the logic is:
SELECT CONCAT(wrkCtrCd, '/', prdntVrsnNum , '/', matlNum , '/',
(CASE WHEN cnt = 1 OR cnt_gt = 1
THEN ritmValFromDt
ELSE parmVldFromDt
END)
) AS outputCol,
ritmValFromDt,parmVldFromDt
FROM (SELECT t.*,
COUNT(*) OVER (PARTITION BY prdntVrsnNum, matlNum, wrkCtrCd) as cnt,
SUM(gt_flag) OVER (PARTITION BY prdntVrsnNum, matlNum, wrkCtrCd) as cnt_gt,
ROW_NUMBER() OVER (PARTITION BY prdntVrsnNum, matlNum, wrkCtrCd, gt_flag ORDER BY parmVldFromDt DESC) as seqnum
FROM (SELECT DISTINCT prdntVrsnNum, matlNum, wrkCtrCd, ritmValFromDt, versnValFromDt, parmVldFromDt,
(CASE WHEN parmVldFromDt >= ritmValFromDt THEN 1 ELSE 0 END) as gt_flag
FROM mytable
) t
) t
WHERE (cnt_gt > 0 AND parmVldFromDt >= ritmValFromDt AND seqnum = 1) OR
(cnt_gt = 0);
Here is a db<>fiddle.
I don't think you have defined your conditions correctly. In particular, you for the last condition, you seem to want the latest date.
I would go for window functions for this. I think the logic is:
SELECT CONCAT(wrkCtrCd, '/', prdntVrsnNum , '/', matlNum , '/',
(CASE WHEN cnt = 1 OR cnt_gt = 1
THEN ritmValFromDt
ELSE parmVldFromDt
END)
) AS outputCol,
ritmValFromDt,parmVldFromDt
FROM (SELECT t.*,
COUNT(*) OVER (PARTITION BY prdntVrsnNum, matlNum, wrkCtrCd) as cnt,
SUM(gt_flag) OVER (PARTITION BY prdntVrsnNum, matlNum, wrkCtrCd) as cnt_gt,
ROW_NUMBER() OVER (PARTITION BY prdntVrsnNum, matlNum, wrkCtrCd, gt_flag ORDER BY parmVldFromDt DESC) as seqnum
FROM (SELECT DISTINCT prdntVrsnNum, matlNum, wrkCtrCd, ritmValFromDt, versnValFromDt, parmVldFromDt,
(CASE WHEN parmVldFromDt >= ritmValFromDt THEN 1 ELSE 0 END) as gt_flag
FROM mytable
) t
) t
WHERE (cnt_gt > 0 AND parmVldFromDt >= ritmValFromDt AND seqnum = 1) OR
(cnt_gt = 0);
Here is a db<>fiddle.
answered Mar 26 at 11:02
Gordon LinoffGordon Linoff
834k38 gold badges341 silver badges448 bronze badges
834k38 gold badges341 silver badges448 bronze badges
Conditions have been given to me by the functional team. I have asked them to come up with better explanations, but nothing so far. Not in my hands :(
– scientific_explorer
Mar 26 at 11:07
@scientific_explorer . . . The last conditions suggests that you want multiple rows for a given combination. However, you seem to only want the latest.
– Gordon Linoff
Mar 26 at 11:36
I agree with you.
– scientific_explorer
Mar 26 at 11:41
Accepting your answer because it gives a very close result compared to my actual data. If you would like me to provide you with the data where it fails, please let me know, so that you can modify your query accordingly.
– scientific_explorer
Mar 26 at 15:00
I have added a few more rows of data, for which your query fails to give desired output. Please have a look
– scientific_explorer
Mar 26 at 17:51
|
show 1 more comment
Conditions have been given to me by the functional team. I have asked them to come up with better explanations, but nothing so far. Not in my hands :(
– scientific_explorer
Mar 26 at 11:07
@scientific_explorer . . . The last conditions suggests that you want multiple rows for a given combination. However, you seem to only want the latest.
– Gordon Linoff
Mar 26 at 11:36
I agree with you.
– scientific_explorer
Mar 26 at 11:41
Accepting your answer because it gives a very close result compared to my actual data. If you would like me to provide you with the data where it fails, please let me know, so that you can modify your query accordingly.
– scientific_explorer
Mar 26 at 15:00
I have added a few more rows of data, for which your query fails to give desired output. Please have a look
– scientific_explorer
Mar 26 at 17:51
Conditions have been given to me by the functional team. I have asked them to come up with better explanations, but nothing so far. Not in my hands :(
– scientific_explorer
Mar 26 at 11:07
Conditions have been given to me by the functional team. I have asked them to come up with better explanations, but nothing so far. Not in my hands :(
– scientific_explorer
Mar 26 at 11:07
@scientific_explorer . . . The last conditions suggests that you want multiple rows for a given combination. However, you seem to only want the latest.
– Gordon Linoff
Mar 26 at 11:36
@scientific_explorer . . . The last conditions suggests that you want multiple rows for a given combination. However, you seem to only want the latest.
– Gordon Linoff
Mar 26 at 11:36
I agree with you.
– scientific_explorer
Mar 26 at 11:41
I agree with you.
– scientific_explorer
Mar 26 at 11:41
Accepting your answer because it gives a very close result compared to my actual data. If you would like me to provide you with the data where it fails, please let me know, so that you can modify your query accordingly.
– scientific_explorer
Mar 26 at 15:00
Accepting your answer because it gives a very close result compared to my actual data. If you would like me to provide you with the data where it fails, please let me know, so that you can modify your query accordingly.
– scientific_explorer
Mar 26 at 15:00
I have added a few more rows of data, for which your query fails to give desired output. Please have a look
– scientific_explorer
Mar 26 at 17:51
I have added a few more rows of data, for which your query fails to give desired output. Please have a look
– scientific_explorer
Mar 26 at 17:51
|
show 1 more comment
I came up with an answer I tried on the fiddle. Not sure if it will work in my actual system (have to test it out now). I am not sure if this is the best/only way to achieve it either.
select
distinct
concat(distinctTable.wrkCtrCd, '/',
distinctTable.prdntVrsnNum, '/',
distinctTable.matlNum, '/',
case when countTable.cnt = 1 then min(distinctTable.ritmValFromDt)
else max(distinctTable.parmVldFromDt)
end
) as outputCol
from
(
select
tbl.prdntVrsnNum,
tbl.matlNum,
tbl.wrkCtrCd,
count(*) as cnt
from
(select distinct * from mytable) as tbl
group by
tbl.prdntVrsnNum,
tbl.matlNum,
tbl.wrkCtrCd
) as countTable
inner join (select distinct * from mytable) as distinctTable
on countTable.prdntVrsnNum = distinctTable.prdntVrsnNum
and countTable.matlNum = distinctTable.matlNum
and countTable.wrkCtrCd = distinctTable.wrkCtrCd
group by
distinctTable.prdntVrsnNum,
distinctTable.matlNum,
distinctTable.wrkCtrCd;
| outputCol |
| ---------------------------- |
| TO-HCH2/V001/96228/20180101 |
| TOU-A16/V003/197774/20181210 |
your 4th condition (comparing the count of rows where a certain condition is met or not met) can probably be prepared withSUM(CASE WHEN parmVldFromDt >= ritmValFromDt THEN 1 ELSE 0) as cnt_whatever
and in your select you can use that information. However, this is very similar to Gordon'sgt_flag
, but won't work anymore in his query, but might work in yours (needs an additional group by somewhere in that sub-subquery)
– Jakumi
Mar 26 at 15:12
add a comment |
I came up with an answer I tried on the fiddle. Not sure if it will work in my actual system (have to test it out now). I am not sure if this is the best/only way to achieve it either.
select
distinct
concat(distinctTable.wrkCtrCd, '/',
distinctTable.prdntVrsnNum, '/',
distinctTable.matlNum, '/',
case when countTable.cnt = 1 then min(distinctTable.ritmValFromDt)
else max(distinctTable.parmVldFromDt)
end
) as outputCol
from
(
select
tbl.prdntVrsnNum,
tbl.matlNum,
tbl.wrkCtrCd,
count(*) as cnt
from
(select distinct * from mytable) as tbl
group by
tbl.prdntVrsnNum,
tbl.matlNum,
tbl.wrkCtrCd
) as countTable
inner join (select distinct * from mytable) as distinctTable
on countTable.prdntVrsnNum = distinctTable.prdntVrsnNum
and countTable.matlNum = distinctTable.matlNum
and countTable.wrkCtrCd = distinctTable.wrkCtrCd
group by
distinctTable.prdntVrsnNum,
distinctTable.matlNum,
distinctTable.wrkCtrCd;
| outputCol |
| ---------------------------- |
| TO-HCH2/V001/96228/20180101 |
| TOU-A16/V003/197774/20181210 |
your 4th condition (comparing the count of rows where a certain condition is met or not met) can probably be prepared withSUM(CASE WHEN parmVldFromDt >= ritmValFromDt THEN 1 ELSE 0) as cnt_whatever
and in your select you can use that information. However, this is very similar to Gordon'sgt_flag
, but won't work anymore in his query, but might work in yours (needs an additional group by somewhere in that sub-subquery)
– Jakumi
Mar 26 at 15:12
add a comment |
I came up with an answer I tried on the fiddle. Not sure if it will work in my actual system (have to test it out now). I am not sure if this is the best/only way to achieve it either.
select
distinct
concat(distinctTable.wrkCtrCd, '/',
distinctTable.prdntVrsnNum, '/',
distinctTable.matlNum, '/',
case when countTable.cnt = 1 then min(distinctTable.ritmValFromDt)
else max(distinctTable.parmVldFromDt)
end
) as outputCol
from
(
select
tbl.prdntVrsnNum,
tbl.matlNum,
tbl.wrkCtrCd,
count(*) as cnt
from
(select distinct * from mytable) as tbl
group by
tbl.prdntVrsnNum,
tbl.matlNum,
tbl.wrkCtrCd
) as countTable
inner join (select distinct * from mytable) as distinctTable
on countTable.prdntVrsnNum = distinctTable.prdntVrsnNum
and countTable.matlNum = distinctTable.matlNum
and countTable.wrkCtrCd = distinctTable.wrkCtrCd
group by
distinctTable.prdntVrsnNum,
distinctTable.matlNum,
distinctTable.wrkCtrCd;
| outputCol |
| ---------------------------- |
| TO-HCH2/V001/96228/20180101 |
| TOU-A16/V003/197774/20181210 |
I came up with an answer I tried on the fiddle. Not sure if it will work in my actual system (have to test it out now). I am not sure if this is the best/only way to achieve it either.
select
distinct
concat(distinctTable.wrkCtrCd, '/',
distinctTable.prdntVrsnNum, '/',
distinctTable.matlNum, '/',
case when countTable.cnt = 1 then min(distinctTable.ritmValFromDt)
else max(distinctTable.parmVldFromDt)
end
) as outputCol
from
(
select
tbl.prdntVrsnNum,
tbl.matlNum,
tbl.wrkCtrCd,
count(*) as cnt
from
(select distinct * from mytable) as tbl
group by
tbl.prdntVrsnNum,
tbl.matlNum,
tbl.wrkCtrCd
) as countTable
inner join (select distinct * from mytable) as distinctTable
on countTable.prdntVrsnNum = distinctTable.prdntVrsnNum
and countTable.matlNum = distinctTable.matlNum
and countTable.wrkCtrCd = distinctTable.wrkCtrCd
group by
distinctTable.prdntVrsnNum,
distinctTable.matlNum,
distinctTable.wrkCtrCd;
| outputCol |
| ---------------------------- |
| TO-HCH2/V001/96228/20180101 |
| TOU-A16/V003/197774/20181210 |
answered Mar 26 at 10:42
scientific_explorerscientific_explorer
1792 silver badges13 bronze badges
1792 silver badges13 bronze badges
your 4th condition (comparing the count of rows where a certain condition is met or not met) can probably be prepared withSUM(CASE WHEN parmVldFromDt >= ritmValFromDt THEN 1 ELSE 0) as cnt_whatever
and in your select you can use that information. However, this is very similar to Gordon'sgt_flag
, but won't work anymore in his query, but might work in yours (needs an additional group by somewhere in that sub-subquery)
– Jakumi
Mar 26 at 15:12
add a comment |
your 4th condition (comparing the count of rows where a certain condition is met or not met) can probably be prepared withSUM(CASE WHEN parmVldFromDt >= ritmValFromDt THEN 1 ELSE 0) as cnt_whatever
and in your select you can use that information. However, this is very similar to Gordon'sgt_flag
, but won't work anymore in his query, but might work in yours (needs an additional group by somewhere in that sub-subquery)
– Jakumi
Mar 26 at 15:12
your 4th condition (comparing the count of rows where a certain condition is met or not met) can probably be prepared with
SUM(CASE WHEN parmVldFromDt >= ritmValFromDt THEN 1 ELSE 0) as cnt_whatever
and in your select you can use that information. However, this is very similar to Gordon's gt_flag
, but won't work anymore in his query, but might work in yours (needs an additional group by somewhere in that sub-subquery)– Jakumi
Mar 26 at 15:12
your 4th condition (comparing the count of rows where a certain condition is met or not met) can probably be prepared with
SUM(CASE WHEN parmVldFromDt >= ritmValFromDt THEN 1 ELSE 0) as cnt_whatever
and in your select you can use that information. However, this is very similar to Gordon's gt_flag
, but won't work anymore in his query, but might work in yours (needs an additional group by somewhere in that sub-subquery)– Jakumi
Mar 26 at 15:12
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55353213%2fhow-to-select-columns-based-on-count-of-number-of-records-returned%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
imho the simplest solution to this would be to first create a query that produces the answers/evaluations to all the conditions (1., 2., 3., 4.1 and 4.2) for every prdntVrsnNum-wrkCtrCd combination via
GROUP BY
- let's say it's query Q. then you create the outputCol depending on the values calculated in Q (join Q) by creating with the case statement - which you already may master. There might be some vendor specific black magic that might make it possible without the join of the aggregate ... but you wanted it to be vendor unspecific ;o)– Jakumi
Mar 26 at 9:38
@Jakumi I don't know how to write a case statement according to number of rows returned. That's one of the parts where I am stuck.
– scientific_explorer
Mar 26 at 9:44
@Jakumi you can give a vendor specific black magic if you want, I can check in my environment if it is available/enabled. :)
– scientific_explorer
Mar 26 at 9:47
@Jakumi I have posted an answer. Please have a look.
– scientific_explorer
Mar 26 at 10:42