Creating extra column based on start/end date in SQL - sql

I have a table with the enrollment information of our members that includes the start date and end date of their membership. It looks like the following:current table format
The goal is to have the data output in the following layout:
desired data output

First you would need to get the data from that table. Then handle it with an external programming languages like PHP or JAVA.
The algorithm would be like this:
get all start values and end values
merge them into an array
sort them by date
create a new table with the dates (create a database with the external code)
foreach member go through the date array and check if it's between the memberdates and directly set a 1 or 0 into the new table (sql update) or create a temp-array to prepare an insert.
That should work. I could write it for you if you tell me your code languages.

Related

SQL server query : how to check value within range

In application I am working on.
I have to input from user through excel and first put it in temporary sql table & then from temporary table to final target table.
My query is failing while putting data from temporary table to target table.
Because some values present in temporary table are out of range of columns in target table.
How can I check if values present in temporary table are within range of column of target table?
I have to check like this
20 < len(temporary_table.column1) < 50
or is there any better way
If you are using SQL server you can use below query for data checking.
temporary_table.column1 between 20 and 50
If you are looking based on the column max length. For example, your columns have datatype varchar(100) then you can use the condition like this
where len(temporary_table.column1)<=100
Extending on the above answer you can just use col_length instead of hard coding the value on the target column. This makes it more automated and less prone to mistakes (entering a value mistakenly)
where len(temporary_table.column1) <= COL_LENGTH ( 'target_table' , 'column1' )

Can you concatenate two strings to create dynamic column names in PostgreSQL?

I have a form where people can type in a start and end date, as well as a column name prefix.
In the backend, I want to do something along the lines of
SELECT *, CAST('{{startDate}}' AS TIMESTAMP) AS ({{prefix}} + '_startDate')
Is this possible? Basically, I want to dynamically create the name of the new column. The table is immediately returned to the user, so I don't want to mutate the underlying table itself. Thanks!
You can execute dynamic query that you have prepared by using EXECUTE keyword, otherwise it is not possible to have dynamic structure of SQL.
Since you are preparing your SQL outside database, you can use something like:
SELECT *, CAST('{{startDate}}' AS TIMESTAMP) AS {{prefix}}_startDate
Assuming that {{prefix}} is replaced with some string by your template before it is sent to database.

Transferring several similar named tables in SSIS

I want to create an interface between 2 databases on SQL Server 2008+ to copy several similar named tables into one.
I have n tables that all have the same naming convention, for example:
SalesInvoicePlanning2014ver1
SalesInvoicePlanning2015ver1
SalesInvoicePlanning2015ver2
etc.
The numbers can vary and do not have a set start (or end), but are always of the "int"-Datatype.
I also have a table "tabledir" that contains all table names as list. (one field) There are a total of 30-40 entries in that list with (for me) undesired entries. In the above example I would need 3 of the 30 tables.
The plan is to use a loop container to
select Top 1([name]) from [tabledir] where name like 'SalesinvoicePlanning%'
and then use the result as variable in the following SSIS Data transfer task:
Select * from [variable]
However, I'm stuck with the SQL statement to give me the desired tablename on each iteration.
Performance is not really an issue. Any advice? Am I wrong trying to use a loop-container?
You can follow below steps -
Step 1 - You can first create SQL task to get all table names into one variable lets say, TableNames of type Object(recordset) using you query.
e.g. select ([name]) as TableName from [tabledir] where name like 'SalesinvoicePlanning%'
Step 2 - Add foreach loop container to iterate over this variable TableNames to take single table name into new variable current_table and add data flow into the container to import data to destination table. Your source query will be expression like -
Select column_names from current_table

Retrieving data from a dynamically generated PL/SQL table

I’ve been trying to retrieve some daily generated data but I’m having trouble figuring out how to dynamically set a daily-created table name in the FROM clause (Oracle db). Each day a new table is generated with a date stamp as part of the table name. This will used in a daily scheduled report that must always pull from the current day’s table (i.e. today’s table name = “STATDB01.A150417001_AINF” where ‘150417’ is the current date). How can I retrieve data from the new daily table without having to update my query each day?
I can create the new table mane dynamically in the SELECT statement but I can’t get it to work in the FROM statement.
Here’s my query:
SELECT
sta.statist “Stats”
,sta.indept “Department”
,sta.ondate “Date”
,sta.skitem “SKU”
--< I can get this to work in the SELECT clause >--
,'STATDB01.A'||TO_CHAR(SYSDATE, 'YYMMDD')||'001_AINF' “New Table”
FROM
--< Calling the table directly works fine >--
--STATDB01.A150417001_AINF sta
--< but this does not work in the FROM clause >--
'STATDB01.A'||TO_CHAR(SYSDATE, 'YYMMDD')||'001_AINF'
WHERE
sta.run_stat LIKE '%341%'
ORDER BY
sta.elap_time
;
You could write (some part of) your report in PL/sql, build your query (including the table name of the day) into a string variable, and run your query-in-a-string with EXECUTE IMMEDIATE.
Alternatively, you could write a shell script and have it output the query with the table name of the day, and feed that (into a file if needed and then) into sqlplus.
Edit: By request, an example of EXECUTE IMMEDIATE in action.

having trouble with writing a proper sql query

I have a table datatable with following data
No Data
1 access author basic chapter data database information
2 author basic black data chapter creating overview project scope using code
3 basic data edition author special any assumed been book contained corporation damages
I am trying to write a query which does pattern matching with the help of LIKE clause
this is my query
rs=s.executeQuery("SELECT No from datatable where Data LIKE '%author basic data%');
which is supposed to retrieve all the records that is No 1, 2 and 3 but it returns nothing however
if i execute this query
rs=s.executeQuery("SELECT No from datatable where Data LIKE '%author basic%');
which gives output as No 1 and 2 but not No 3
so how can i write such a query which gives me the desirable output
thanks in advance
You query checks if there is a full substring like %author basic data%. Try this :
SELECT
No
FROM
datatable
WHERE
Data LIKE '%author%' AND
Data LIKE '%basic%' AND
Data LIKE '%data%'
Looks like you have something like
CREATE TABLE datatable('No' INT, 'Data' VARCHAR(...))
by grouping a lot of attributes together in a single column (VARCHAR, CLOB, BLOB...) you cannot harness the power of SQL.
If you rather do something like
CREATE TABLE <meaningful name>('No' INT, AUTHOR VARCHAR(...), TITLE VARCHAR, ...)
you avoid using LIKE for all your queries and you can, as a commenter said, create indices on the columns most often used to access the data to speed up access.
Other benefits include using aggregating functions on numerical data and joining with other tables.