How to select data using table/column names stored in a table? Dynamically building SQL?
up vote
0
down vote
favorite
I have a table that shows events and vehicles associated with those events:
id | model | colour | cargo
--------------------------------------------
1 | vw | red | tomatoes
2 | bmw | red | golf clubs
I would like to have a table that I can use to manage alerts. There is a column that represents strings to check for, another column that shows which column/table it is applied to and some text that describes the alert:
id | column_name | check_string | alert_string
--------------------------------------------
1 | my_table.colour | red | 'Vehicle is red'
2 | my_table.cargo | [sport,club] | 'Vehicle is carrying sports goods'
or possibly:
id | column_name | check_string | alert_string
--------------------------------------------
1 | my_table.colour | red | 'Vehicle is red'
2 | my_table.cargo | sport | 'Vehicle is carrying sports goods'
3 | my_table.cargo | club | 'Vehicle is carrying sports goods'
I would like to have a query that I can run that would return all the alerts that apply to that row:
id | alert_text
--------------------------------------------
1 | ['Vehicle is red']
2 | ['Vehicle is red', 'Vehicle is carrying sports goods']
The way I was doing this was building up SQL commands in Python and running them against the database but this becomes a burden as the number of rules grow or variables need to be changed/updated (suddenly we don't care about red cars but are very concerned with blue cars).
SELECT id, 'Vehicle is red' as alert_text FROM my_table
WHERE my_table.colour = 'red';
SELECT id, 'Sports goods' as alert_text FROM my_table
WHERE my_table.cargo in ['sport', 'club'];
SELECT <many other rules>;
Is there a better way to do this? Is it worth building a DB table that can dynamically point to a column, strings to check in that column and then alert text associated with the rule? Should I even be using SQL for this problem?
I've got a feeling that SQL is maybe not the right tool for this job but I don't know what I don't know...
sql database postgresql
add a comment |
up vote
0
down vote
favorite
I have a table that shows events and vehicles associated with those events:
id | model | colour | cargo
--------------------------------------------
1 | vw | red | tomatoes
2 | bmw | red | golf clubs
I would like to have a table that I can use to manage alerts. There is a column that represents strings to check for, another column that shows which column/table it is applied to and some text that describes the alert:
id | column_name | check_string | alert_string
--------------------------------------------
1 | my_table.colour | red | 'Vehicle is red'
2 | my_table.cargo | [sport,club] | 'Vehicle is carrying sports goods'
or possibly:
id | column_name | check_string | alert_string
--------------------------------------------
1 | my_table.colour | red | 'Vehicle is red'
2 | my_table.cargo | sport | 'Vehicle is carrying sports goods'
3 | my_table.cargo | club | 'Vehicle is carrying sports goods'
I would like to have a query that I can run that would return all the alerts that apply to that row:
id | alert_text
--------------------------------------------
1 | ['Vehicle is red']
2 | ['Vehicle is red', 'Vehicle is carrying sports goods']
The way I was doing this was building up SQL commands in Python and running them against the database but this becomes a burden as the number of rules grow or variables need to be changed/updated (suddenly we don't care about red cars but are very concerned with blue cars).
SELECT id, 'Vehicle is red' as alert_text FROM my_table
WHERE my_table.colour = 'red';
SELECT id, 'Sports goods' as alert_text FROM my_table
WHERE my_table.cargo in ['sport', 'club'];
SELECT <many other rules>;
Is there a better way to do this? Is it worth building a DB table that can dynamically point to a column, strings to check in that column and then alert text associated with the rule? Should I even be using SQL for this problem?
I've got a feeling that SQL is maybe not the right tool for this job but I don't know what I don't know...
sql database postgresql
2
What is the data type ofcargo
? Why are you storing multiple values in one column instead of using a junction/association table?
– Gordon Linoff
Nov 20 at 11:27
cargo is a string, it is generally multiple words in no specific order or structure. Example "3 boxes of ABC and no hazardous cargo, 2 barrels of ammunition". I need to check for specific things such as "ammunition" in the cargo description. I might need to do multiple checks on a single row. In the questions example I could just separate "sport" and "clubs" into 2 rows instead of one.
– RedM
Nov 20 at 11:29
You must have a front end tool to your database that could easily be used to fulfill such a request without a problem. You could also keep all your queries in views and run each view as needed without typing sql each time.
– NoChance
Nov 20 at 11:32
That's what I'm asking about... What front-end tool should I be using to manage these kind of requests. Should I store the alerts table as-is but do the alert-text building in pandas? Is there a way to do this in SQL? Is it "correct" to do this in SQL? What is best practice for managing dynamic, rule based, periodic queries?
– RedM
Nov 20 at 11:39
It doesn't sound like it'd be that horrible to do without any kind of special front-end tool just using SQL and maybe a few pgPL/SQL stored procedures. Focus on normalizing the data (the "or possibly" form above will work better with SQL than your original form) and add your dynamic table that can point to the appropriate alerts. It'd be a bit more work up front but ultimately be robust and easily adjusted for new checks.
– Feneric
Nov 20 at 11:51
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a table that shows events and vehicles associated with those events:
id | model | colour | cargo
--------------------------------------------
1 | vw | red | tomatoes
2 | bmw | red | golf clubs
I would like to have a table that I can use to manage alerts. There is a column that represents strings to check for, another column that shows which column/table it is applied to and some text that describes the alert:
id | column_name | check_string | alert_string
--------------------------------------------
1 | my_table.colour | red | 'Vehicle is red'
2 | my_table.cargo | [sport,club] | 'Vehicle is carrying sports goods'
or possibly:
id | column_name | check_string | alert_string
--------------------------------------------
1 | my_table.colour | red | 'Vehicle is red'
2 | my_table.cargo | sport | 'Vehicle is carrying sports goods'
3 | my_table.cargo | club | 'Vehicle is carrying sports goods'
I would like to have a query that I can run that would return all the alerts that apply to that row:
id | alert_text
--------------------------------------------
1 | ['Vehicle is red']
2 | ['Vehicle is red', 'Vehicle is carrying sports goods']
The way I was doing this was building up SQL commands in Python and running them against the database but this becomes a burden as the number of rules grow or variables need to be changed/updated (suddenly we don't care about red cars but are very concerned with blue cars).
SELECT id, 'Vehicle is red' as alert_text FROM my_table
WHERE my_table.colour = 'red';
SELECT id, 'Sports goods' as alert_text FROM my_table
WHERE my_table.cargo in ['sport', 'club'];
SELECT <many other rules>;
Is there a better way to do this? Is it worth building a DB table that can dynamically point to a column, strings to check in that column and then alert text associated with the rule? Should I even be using SQL for this problem?
I've got a feeling that SQL is maybe not the right tool for this job but I don't know what I don't know...
sql database postgresql
I have a table that shows events and vehicles associated with those events:
id | model | colour | cargo
--------------------------------------------
1 | vw | red | tomatoes
2 | bmw | red | golf clubs
I would like to have a table that I can use to manage alerts. There is a column that represents strings to check for, another column that shows which column/table it is applied to and some text that describes the alert:
id | column_name | check_string | alert_string
--------------------------------------------
1 | my_table.colour | red | 'Vehicle is red'
2 | my_table.cargo | [sport,club] | 'Vehicle is carrying sports goods'
or possibly:
id | column_name | check_string | alert_string
--------------------------------------------
1 | my_table.colour | red | 'Vehicle is red'
2 | my_table.cargo | sport | 'Vehicle is carrying sports goods'
3 | my_table.cargo | club | 'Vehicle is carrying sports goods'
I would like to have a query that I can run that would return all the alerts that apply to that row:
id | alert_text
--------------------------------------------
1 | ['Vehicle is red']
2 | ['Vehicle is red', 'Vehicle is carrying sports goods']
The way I was doing this was building up SQL commands in Python and running them against the database but this becomes a burden as the number of rules grow or variables need to be changed/updated (suddenly we don't care about red cars but are very concerned with blue cars).
SELECT id, 'Vehicle is red' as alert_text FROM my_table
WHERE my_table.colour = 'red';
SELECT id, 'Sports goods' as alert_text FROM my_table
WHERE my_table.cargo in ['sport', 'club'];
SELECT <many other rules>;
Is there a better way to do this? Is it worth building a DB table that can dynamically point to a column, strings to check in that column and then alert text associated with the rule? Should I even be using SQL for this problem?
I've got a feeling that SQL is maybe not the right tool for this job but I don't know what I don't know...
sql database postgresql
sql database postgresql
edited Nov 20 at 11:37
asked Nov 20 at 11:25
RedM
618
618
2
What is the data type ofcargo
? Why are you storing multiple values in one column instead of using a junction/association table?
– Gordon Linoff
Nov 20 at 11:27
cargo is a string, it is generally multiple words in no specific order or structure. Example "3 boxes of ABC and no hazardous cargo, 2 barrels of ammunition". I need to check for specific things such as "ammunition" in the cargo description. I might need to do multiple checks on a single row. In the questions example I could just separate "sport" and "clubs" into 2 rows instead of one.
– RedM
Nov 20 at 11:29
You must have a front end tool to your database that could easily be used to fulfill such a request without a problem. You could also keep all your queries in views and run each view as needed without typing sql each time.
– NoChance
Nov 20 at 11:32
That's what I'm asking about... What front-end tool should I be using to manage these kind of requests. Should I store the alerts table as-is but do the alert-text building in pandas? Is there a way to do this in SQL? Is it "correct" to do this in SQL? What is best practice for managing dynamic, rule based, periodic queries?
– RedM
Nov 20 at 11:39
It doesn't sound like it'd be that horrible to do without any kind of special front-end tool just using SQL and maybe a few pgPL/SQL stored procedures. Focus on normalizing the data (the "or possibly" form above will work better with SQL than your original form) and add your dynamic table that can point to the appropriate alerts. It'd be a bit more work up front but ultimately be robust and easily adjusted for new checks.
– Feneric
Nov 20 at 11:51
add a comment |
2
What is the data type ofcargo
? Why are you storing multiple values in one column instead of using a junction/association table?
– Gordon Linoff
Nov 20 at 11:27
cargo is a string, it is generally multiple words in no specific order or structure. Example "3 boxes of ABC and no hazardous cargo, 2 barrels of ammunition". I need to check for specific things such as "ammunition" in the cargo description. I might need to do multiple checks on a single row. In the questions example I could just separate "sport" and "clubs" into 2 rows instead of one.
– RedM
Nov 20 at 11:29
You must have a front end tool to your database that could easily be used to fulfill such a request without a problem. You could also keep all your queries in views and run each view as needed without typing sql each time.
– NoChance
Nov 20 at 11:32
That's what I'm asking about... What front-end tool should I be using to manage these kind of requests. Should I store the alerts table as-is but do the alert-text building in pandas? Is there a way to do this in SQL? Is it "correct" to do this in SQL? What is best practice for managing dynamic, rule based, periodic queries?
– RedM
Nov 20 at 11:39
It doesn't sound like it'd be that horrible to do without any kind of special front-end tool just using SQL and maybe a few pgPL/SQL stored procedures. Focus on normalizing the data (the "or possibly" form above will work better with SQL than your original form) and add your dynamic table that can point to the appropriate alerts. It'd be a bit more work up front but ultimately be robust and easily adjusted for new checks.
– Feneric
Nov 20 at 11:51
2
2
What is the data type of
cargo
? Why are you storing multiple values in one column instead of using a junction/association table?– Gordon Linoff
Nov 20 at 11:27
What is the data type of
cargo
? Why are you storing multiple values in one column instead of using a junction/association table?– Gordon Linoff
Nov 20 at 11:27
cargo is a string, it is generally multiple words in no specific order or structure. Example "3 boxes of ABC and no hazardous cargo, 2 barrels of ammunition". I need to check for specific things such as "ammunition" in the cargo description. I might need to do multiple checks on a single row. In the questions example I could just separate "sport" and "clubs" into 2 rows instead of one.
– RedM
Nov 20 at 11:29
cargo is a string, it is generally multiple words in no specific order or structure. Example "3 boxes of ABC and no hazardous cargo, 2 barrels of ammunition". I need to check for specific things such as "ammunition" in the cargo description. I might need to do multiple checks on a single row. In the questions example I could just separate "sport" and "clubs" into 2 rows instead of one.
– RedM
Nov 20 at 11:29
You must have a front end tool to your database that could easily be used to fulfill such a request without a problem. You could also keep all your queries in views and run each view as needed without typing sql each time.
– NoChance
Nov 20 at 11:32
You must have a front end tool to your database that could easily be used to fulfill such a request without a problem. You could also keep all your queries in views and run each view as needed without typing sql each time.
– NoChance
Nov 20 at 11:32
That's what I'm asking about... What front-end tool should I be using to manage these kind of requests. Should I store the alerts table as-is but do the alert-text building in pandas? Is there a way to do this in SQL? Is it "correct" to do this in SQL? What is best practice for managing dynamic, rule based, periodic queries?
– RedM
Nov 20 at 11:39
That's what I'm asking about... What front-end tool should I be using to manage these kind of requests. Should I store the alerts table as-is but do the alert-text building in pandas? Is there a way to do this in SQL? Is it "correct" to do this in SQL? What is best practice for managing dynamic, rule based, periodic queries?
– RedM
Nov 20 at 11:39
It doesn't sound like it'd be that horrible to do without any kind of special front-end tool just using SQL and maybe a few pgPL/SQL stored procedures. Focus on normalizing the data (the "or possibly" form above will work better with SQL than your original form) and add your dynamic table that can point to the appropriate alerts. It'd be a bit more work up front but ultimately be robust and easily adjusted for new checks.
– Feneric
Nov 20 at 11:51
It doesn't sound like it'd be that horrible to do without any kind of special front-end tool just using SQL and maybe a few pgPL/SQL stored procedures. Focus on normalizing the data (the "or possibly" form above will work better with SQL than your original form) and add your dynamic table that can point to the appropriate alerts. It'd be a bit more work up front but ultimately be robust and easily adjusted for new checks.
– Feneric
Nov 20 at 11:51
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
This SQL code would allow you to have a DB table of any size for both Events and Alerts
Declare @vSQL nvarchar(Max)
Declare @vColumnName nvarchar(25)
Declare @vCheckString nvarchar(25)
Declare @vAlertString nvarchar(50)
Declare vCursor CURSOR For
Select [column_name], [check_string], [alert_string] From vehicle_alerts
Open vCursor;
Fetch Next From vCursor Into @vColumnName, @vCheckString, @vAlertString
--Make global temp table from source then delete contents.
--Ensures field lengths are as large as the largest value in the temp table (Not the most efficient but it works)
Select id, alert_string INTO ##alerts From vehicle_alerts
Delete From ##alerts
While @@FETCH_STATUS = 0 --Loop through alerts and interrogate your events using the LIKE operator
Begin
Set @vSQL = 'INSERT INTO ##alerts Select id, '''+@vAlertString+''' As [Alert] From vehicle_events Where ' + @vColumnName + ' Like ''%' + @vCheckString + '%'''
Execute (@vSQL)
Fetch Next From vCursor Into @vColumnName, @vCheckString, @vAlertString
End;
--Use STUFF to combine the alert strings by id
Select id,STUFF((Select ',' + [alert_string] From ##alerts Where id = a.id FOR XML PATH('')),1,1,'') AS Tmp
From ##alerts AS a
Group By id
Drop Table ##alerts
Close vCursor
Deallocate vCursor
That doesn't look like standard SQL. Is that MS SQL Server? The question was tagged for PostgreSQL, so that may not work.
– Feneric
Nov 20 at 19:02
This won't work with Postgres
– a_horse_with_no_name
Nov 20 at 19:20
This is from MS SQL 2014
– level3looper
Nov 20 at 19:54
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',
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%2f53391987%2fhow-to-select-data-using-table-column-names-stored-in-a-table-dynamically-build%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
This SQL code would allow you to have a DB table of any size for both Events and Alerts
Declare @vSQL nvarchar(Max)
Declare @vColumnName nvarchar(25)
Declare @vCheckString nvarchar(25)
Declare @vAlertString nvarchar(50)
Declare vCursor CURSOR For
Select [column_name], [check_string], [alert_string] From vehicle_alerts
Open vCursor;
Fetch Next From vCursor Into @vColumnName, @vCheckString, @vAlertString
--Make global temp table from source then delete contents.
--Ensures field lengths are as large as the largest value in the temp table (Not the most efficient but it works)
Select id, alert_string INTO ##alerts From vehicle_alerts
Delete From ##alerts
While @@FETCH_STATUS = 0 --Loop through alerts and interrogate your events using the LIKE operator
Begin
Set @vSQL = 'INSERT INTO ##alerts Select id, '''+@vAlertString+''' As [Alert] From vehicle_events Where ' + @vColumnName + ' Like ''%' + @vCheckString + '%'''
Execute (@vSQL)
Fetch Next From vCursor Into @vColumnName, @vCheckString, @vAlertString
End;
--Use STUFF to combine the alert strings by id
Select id,STUFF((Select ',' + [alert_string] From ##alerts Where id = a.id FOR XML PATH('')),1,1,'') AS Tmp
From ##alerts AS a
Group By id
Drop Table ##alerts
Close vCursor
Deallocate vCursor
That doesn't look like standard SQL. Is that MS SQL Server? The question was tagged for PostgreSQL, so that may not work.
– Feneric
Nov 20 at 19:02
This won't work with Postgres
– a_horse_with_no_name
Nov 20 at 19:20
This is from MS SQL 2014
– level3looper
Nov 20 at 19:54
add a comment |
up vote
0
down vote
This SQL code would allow you to have a DB table of any size for both Events and Alerts
Declare @vSQL nvarchar(Max)
Declare @vColumnName nvarchar(25)
Declare @vCheckString nvarchar(25)
Declare @vAlertString nvarchar(50)
Declare vCursor CURSOR For
Select [column_name], [check_string], [alert_string] From vehicle_alerts
Open vCursor;
Fetch Next From vCursor Into @vColumnName, @vCheckString, @vAlertString
--Make global temp table from source then delete contents.
--Ensures field lengths are as large as the largest value in the temp table (Not the most efficient but it works)
Select id, alert_string INTO ##alerts From vehicle_alerts
Delete From ##alerts
While @@FETCH_STATUS = 0 --Loop through alerts and interrogate your events using the LIKE operator
Begin
Set @vSQL = 'INSERT INTO ##alerts Select id, '''+@vAlertString+''' As [Alert] From vehicle_events Where ' + @vColumnName + ' Like ''%' + @vCheckString + '%'''
Execute (@vSQL)
Fetch Next From vCursor Into @vColumnName, @vCheckString, @vAlertString
End;
--Use STUFF to combine the alert strings by id
Select id,STUFF((Select ',' + [alert_string] From ##alerts Where id = a.id FOR XML PATH('')),1,1,'') AS Tmp
From ##alerts AS a
Group By id
Drop Table ##alerts
Close vCursor
Deallocate vCursor
That doesn't look like standard SQL. Is that MS SQL Server? The question was tagged for PostgreSQL, so that may not work.
– Feneric
Nov 20 at 19:02
This won't work with Postgres
– a_horse_with_no_name
Nov 20 at 19:20
This is from MS SQL 2014
– level3looper
Nov 20 at 19:54
add a comment |
up vote
0
down vote
up vote
0
down vote
This SQL code would allow you to have a DB table of any size for both Events and Alerts
Declare @vSQL nvarchar(Max)
Declare @vColumnName nvarchar(25)
Declare @vCheckString nvarchar(25)
Declare @vAlertString nvarchar(50)
Declare vCursor CURSOR For
Select [column_name], [check_string], [alert_string] From vehicle_alerts
Open vCursor;
Fetch Next From vCursor Into @vColumnName, @vCheckString, @vAlertString
--Make global temp table from source then delete contents.
--Ensures field lengths are as large as the largest value in the temp table (Not the most efficient but it works)
Select id, alert_string INTO ##alerts From vehicle_alerts
Delete From ##alerts
While @@FETCH_STATUS = 0 --Loop through alerts and interrogate your events using the LIKE operator
Begin
Set @vSQL = 'INSERT INTO ##alerts Select id, '''+@vAlertString+''' As [Alert] From vehicle_events Where ' + @vColumnName + ' Like ''%' + @vCheckString + '%'''
Execute (@vSQL)
Fetch Next From vCursor Into @vColumnName, @vCheckString, @vAlertString
End;
--Use STUFF to combine the alert strings by id
Select id,STUFF((Select ',' + [alert_string] From ##alerts Where id = a.id FOR XML PATH('')),1,1,'') AS Tmp
From ##alerts AS a
Group By id
Drop Table ##alerts
Close vCursor
Deallocate vCursor
This SQL code would allow you to have a DB table of any size for both Events and Alerts
Declare @vSQL nvarchar(Max)
Declare @vColumnName nvarchar(25)
Declare @vCheckString nvarchar(25)
Declare @vAlertString nvarchar(50)
Declare vCursor CURSOR For
Select [column_name], [check_string], [alert_string] From vehicle_alerts
Open vCursor;
Fetch Next From vCursor Into @vColumnName, @vCheckString, @vAlertString
--Make global temp table from source then delete contents.
--Ensures field lengths are as large as the largest value in the temp table (Not the most efficient but it works)
Select id, alert_string INTO ##alerts From vehicle_alerts
Delete From ##alerts
While @@FETCH_STATUS = 0 --Loop through alerts and interrogate your events using the LIKE operator
Begin
Set @vSQL = 'INSERT INTO ##alerts Select id, '''+@vAlertString+''' As [Alert] From vehicle_events Where ' + @vColumnName + ' Like ''%' + @vCheckString + '%'''
Execute (@vSQL)
Fetch Next From vCursor Into @vColumnName, @vCheckString, @vAlertString
End;
--Use STUFF to combine the alert strings by id
Select id,STUFF((Select ',' + [alert_string] From ##alerts Where id = a.id FOR XML PATH('')),1,1,'') AS Tmp
From ##alerts AS a
Group By id
Drop Table ##alerts
Close vCursor
Deallocate vCursor
edited Nov 20 at 18:56
answered Nov 20 at 17:42
level3looper
1395
1395
That doesn't look like standard SQL. Is that MS SQL Server? The question was tagged for PostgreSQL, so that may not work.
– Feneric
Nov 20 at 19:02
This won't work with Postgres
– a_horse_with_no_name
Nov 20 at 19:20
This is from MS SQL 2014
– level3looper
Nov 20 at 19:54
add a comment |
That doesn't look like standard SQL. Is that MS SQL Server? The question was tagged for PostgreSQL, so that may not work.
– Feneric
Nov 20 at 19:02
This won't work with Postgres
– a_horse_with_no_name
Nov 20 at 19:20
This is from MS SQL 2014
– level3looper
Nov 20 at 19:54
That doesn't look like standard SQL. Is that MS SQL Server? The question was tagged for PostgreSQL, so that may not work.
– Feneric
Nov 20 at 19:02
That doesn't look like standard SQL. Is that MS SQL Server? The question was tagged for PostgreSQL, so that may not work.
– Feneric
Nov 20 at 19:02
This won't work with Postgres
– a_horse_with_no_name
Nov 20 at 19:20
This won't work with Postgres
– a_horse_with_no_name
Nov 20 at 19:20
This is from MS SQL 2014
– level3looper
Nov 20 at 19:54
This is from MS SQL 2014
– level3looper
Nov 20 at 19:54
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53391987%2fhow-to-select-data-using-table-column-names-stored-in-a-table-dynamically-build%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
2
What is the data type of
cargo
? Why are you storing multiple values in one column instead of using a junction/association table?– Gordon Linoff
Nov 20 at 11:27
cargo is a string, it is generally multiple words in no specific order or structure. Example "3 boxes of ABC and no hazardous cargo, 2 barrels of ammunition". I need to check for specific things such as "ammunition" in the cargo description. I might need to do multiple checks on a single row. In the questions example I could just separate "sport" and "clubs" into 2 rows instead of one.
– RedM
Nov 20 at 11:29
You must have a front end tool to your database that could easily be used to fulfill such a request without a problem. You could also keep all your queries in views and run each view as needed without typing sql each time.
– NoChance
Nov 20 at 11:32
That's what I'm asking about... What front-end tool should I be using to manage these kind of requests. Should I store the alerts table as-is but do the alert-text building in pandas? Is there a way to do this in SQL? Is it "correct" to do this in SQL? What is best practice for managing dynamic, rule based, periodic queries?
– RedM
Nov 20 at 11:39
It doesn't sound like it'd be that horrible to do without any kind of special front-end tool just using SQL and maybe a few pgPL/SQL stored procedures. Focus on normalizing the data (the "or possibly" form above will work better with SQL than your original form) and add your dynamic table that can point to the appropriate alerts. It'd be a bit more work up front but ultimately be robust and easily adjusted for new checks.
– Feneric
Nov 20 at 11:51