Re: [sqlite] Dynamic SQL for SQLite?
I tried using case myself as the first thing but it didn't work, I though I need dynamic sql for that. Didn't realize it's that simple - just the column name. On Wed, May 11, 2011 at 9:04 PM, Johnwrote: > yes, using Case worked for me too and that's exactly what I need since I > need only today's value. Thank you so much guys! My kudos to you, Igor :) > > > On Wed, May 11, 2011 at 9:02 PM, John wrote: > >> too much logic >> >> >> On Wed, May 11, 2011 at 9:01 PM, John wrote: >> >>> I should have done it like that (normal referential table). But now I'm >>> at the end of this project and too logic uses that table already. So perhaps >>> I'll refactor some day. >>> >>> >>> On Wed, May 11, 2011 at 8:56 PM, Igor Tandetnik wrote: >>> On 5/11/2011 8:52 PM, John wrote: > here is the view: > > create view today_goal as > select 'monday' dow, monday_goal value from user_goals > union all > select 'tuesday' dow, tuesday_goal value from user_goals > union all > select 'wednesday' dow, wednesday_goal value from user_goals > union all > select 'thursday' dow, thursday_goal value from user_goals > union all > select 'friday' dow, friday_goal value from user_goals > union all > select 'saturday' dow, saturday_goal value from user_goals > union all > select 'sunday' dow, sunday_goal value from user_goals; > > select dow, value from today_goal; I kind of wonder why you can't just set up your original table this way to begin with, and not bother with the view. -- Igor Tandetnik ___ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users >>> >>> >>> >>> -- >>> ~John >>> >> >> >> >> -- >> ~John >> > > > > -- > ~John > -- ~John ___ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
Re: [sqlite] Dynamic SQL for SQLite?
yes, using Case worked for me too and that's exactly what I need since I need only today's value. Thank you so much guys! My kudos to you, Igor :) On Wed, May 11, 2011 at 9:02 PM, Johnwrote: > too much logic > > > On Wed, May 11, 2011 at 9:01 PM, John wrote: > >> I should have done it like that (normal referential table). But now I'm at >> the end of this project and too logic uses that table already. So perhaps >> I'll refactor some day. >> >> >> On Wed, May 11, 2011 at 8:56 PM, Igor Tandetnik wrote: >> >>> On 5/11/2011 8:52 PM, John wrote: >>> > here is the view: >>> > >>> > create view today_goal as >>> > select 'monday' dow, monday_goal value from user_goals >>> > union all >>> > select 'tuesday' dow, tuesday_goal value from user_goals >>> > union all >>> > select 'wednesday' dow, wednesday_goal value from user_goals >>> > union all >>> > select 'thursday' dow, thursday_goal value from user_goals >>> > union all >>> > select 'friday' dow, friday_goal value from user_goals >>> > union all >>> > select 'saturday' dow, saturday_goal value from user_goals >>> > union all >>> > select 'sunday' dow, sunday_goal value from user_goals; >>> > >>> > select dow, value from today_goal; >>> >>> I kind of wonder why you can't just set up your original table this way >>> to begin with, and not bother with the view. >>> -- >>> Igor Tandetnik >>> >>> ___ >>> sqlite-users mailing list >>> sqlite-users@sqlite.org >>> http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users >>> >> >> >> >> -- >> ~John >> > > > > -- > ~John > -- ~John ___ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
Re: [sqlite] Dynamic SQL for SQLite?
too much logic On Wed, May 11, 2011 at 9:01 PM, Johnwrote: > I should have done it like that (normal referential table). But now I'm at > the end of this project and too logic uses that table already. So perhaps > I'll refactor some day. > > > On Wed, May 11, 2011 at 8:56 PM, Igor Tandetnik wrote: > >> On 5/11/2011 8:52 PM, John wrote: >> > here is the view: >> > >> > create view today_goal as >> > select 'monday' dow, monday_goal value from user_goals >> > union all >> > select 'tuesday' dow, tuesday_goal value from user_goals >> > union all >> > select 'wednesday' dow, wednesday_goal value from user_goals >> > union all >> > select 'thursday' dow, thursday_goal value from user_goals >> > union all >> > select 'friday' dow, friday_goal value from user_goals >> > union all >> > select 'saturday' dow, saturday_goal value from user_goals >> > union all >> > select 'sunday' dow, sunday_goal value from user_goals; >> > >> > select dow, value from today_goal; >> >> I kind of wonder why you can't just set up your original table this way >> to begin with, and not bother with the view. >> -- >> Igor Tandetnik >> >> ___ >> sqlite-users mailing list >> sqlite-users@sqlite.org >> http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users >> > > > > -- > ~John > -- ~John ___ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
Re: [sqlite] Dynamic SQL for SQLite?
I should have done it like that (normal referential table). But now I'm at the end of this project and too logic uses that table already. So perhaps I'll refactor some day. On Wed, May 11, 2011 at 8:56 PM, Igor Tandetnikwrote: > On 5/11/2011 8:52 PM, John wrote: > > here is the view: > > > > create view today_goal as > > select 'monday' dow, monday_goal value from user_goals > > union all > > select 'tuesday' dow, tuesday_goal value from user_goals > > union all > > select 'wednesday' dow, wednesday_goal value from user_goals > > union all > > select 'thursday' dow, thursday_goal value from user_goals > > union all > > select 'friday' dow, friday_goal value from user_goals > > union all > > select 'saturday' dow, saturday_goal value from user_goals > > union all > > select 'sunday' dow, sunday_goal value from user_goals; > > > > select dow, value from today_goal; > > I kind of wonder why you can't just set up your original table this way > to begin with, and not bother with the view. > -- > Igor Tandetnik > > ___ > sqlite-users mailing list > sqlite-users@sqlite.org > http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users > -- ~John ___ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
Re: [sqlite] Dynamic SQL for SQLite?
On 5/11/2011 8:52 PM, John wrote: > here is the view: > > create view today_goal as > select 'monday' dow, monday_goal value from user_goals > union all > select 'tuesday' dow, tuesday_goal value from user_goals > union all > select 'wednesday' dow, wednesday_goal value from user_goals > union all > select 'thursday' dow, thursday_goal value from user_goals > union all > select 'friday' dow, friday_goal value from user_goals > union all > select 'saturday' dow, saturday_goal value from user_goals > union all > select 'sunday' dow, sunday_goal value from user_goals; > > select dow, value from today_goal; I kind of wonder why you can't just set up your original table this way to begin with, and not bother with the view. -- Igor Tandetnik ___ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
Re: [sqlite] Dynamic SQL for SQLite?
here is the view: create view today_goal as select 'monday' dow, monday_goal value from user_goals union all select 'tuesday' dow, tuesday_goal value from user_goals union all select 'wednesday' dow, wednesday_goal value from user_goals union all select 'thursday' dow, thursday_goal value from user_goals union all select 'friday' dow, friday_goal value from user_goals union all select 'saturday' dow, saturday_goal value from user_goals union all select 'sunday' dow, sunday_goal value from user_goals; select dow, value from today_goal; On Wed, May 11, 2011 at 8:51 PM, Johnwrote: > ok, I will try. Meanwhile I created the actual view that does what I need. > (considering I have many mondays, tuesdays... I replaced UNION with UNION > ALL) > > > On Wed, May 11, 2011 at 8:44 PM, Mr. Puneet Kishor wrote: > >> >> On May 11, 2011, at 7:37 PM, John wrote: >> >> > Igor, >> > What you are suggesting will not work. You can only select values not >> > columns using case. >> > >> > >> > select case strftime('%w', 'now') >> > when 0 then sunday_value >> > when 1 then monday_value >> > ... >> > else saturday_value >> > end >> > from seven_days; >> > >> > >> >> >> You must be new here. You have to understand the first rule of SQL. Igor >> is never wrong. His query works just fine. See below -- >> >> sqlite> CREATE TABLE seven_days ( >> ...> mon INTEGER, >> ...> tue INTEGER, >> ...> wed INTEGER >> ...> ); >> sqlite> INSERT INTO seven_days VALUES (5, 3, 2); >> sqlite> INSERT INTO seven_days VALUES (1, 4, 3); >> sqlite> INSERT INTO seven_days VALUES (7, 8, 3); >> sqlite> SELECT CASE Strftime('%w', 'now') >> ...> WHEN 1 THEN mon >> ...> WHEN 2 THEN tue >> ...> ELSE wed >> ...> END AS day_val >> ...> FROM seven_days; >> day_val >> >> -- >> 2 >> 3 >> 3 >> sqlite> >> >> >> > >> > >> > On Wed, May 11, 2011 at 8:30 PM, Igor Tandetnik >> wrote: >> > >> >> On 5/11/2011 8:14 PM, John wrote: >> >>> I am in situation where I need to keep as much logic as possible >> within >> >>> SQLite. However the query that I need to perform does not seem to be >> >>> possible to perform. >> >>> >> >>> let's say I have a table with columns for each day of the week >> >>> >> >>> create table seven_days >> >>> (monday_value integer, >> >>> tueday_value integer, >> >>> wednesday_value integer, >> >>> ... ); >> >>> >> >>> I want to select value from whatever day it is today. So if today is >> >>> Tuesday, select from tuesday_value. >> >>> >> >>> Can I do it with pure SQlite? >> >> >> >> select case strftime('%w', 'now') >> >> when 0 then sunday_value >> >> when 1 then monday_value >> >> ... >> >> else saturday_value >> >> end >> >> from seven_days; >> >> >> >> -- >> >> Igor Tandetnik >> >> >> >> ___ >> >> sqlite-users mailing list >> >> sqlite-users@sqlite.org >> >> http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users >> >> >> > >> > >> > >> > -- >> > ~John >> > ___ >> > sqlite-users mailing list >> > sqlite-users@sqlite.org >> > http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users >> >> ___ >> sqlite-users mailing list >> sqlite-users@sqlite.org >> http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users >> > > > > -- > ~John > -- ~John ___ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
Re: [sqlite] Dynamic SQL for SQLite?
On 5/11/2011 8:30 PM, Igor Tandetnik wrote: > select case strftime('%w', 'now') > when 0 then sunday_value > when 1 then monday_value > ... > else saturday_value > end > from seven_days; Make it select case cast(strftime('%w', 'now') as integer) ... -- or select case strftime('%w', 'now') when '0' then sunday_value when '1' then monday_value ... strftime returns a string, so a bit of care is needed to make it work with integers. -- Igor Tandetnik ___ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
Re: [sqlite] Dynamic SQL for SQLite?
ok, I will try. Meanwhile I created the actual view that does what I need. (considering I have many mondays, tuesdays... I replaced UNION with UNION ALL) On Wed, May 11, 2011 at 8:44 PM, Mr. Puneet Kishorwrote: > > On May 11, 2011, at 7:37 PM, John wrote: > > > Igor, > > What you are suggesting will not work. You can only select values not > > columns using case. > > > > > > select case strftime('%w', 'now') > > when 0 then sunday_value > > when 1 then monday_value > > ... > > else saturday_value > > end > > from seven_days; > > > > > > > You must be new here. You have to understand the first rule of SQL. Igor is > never wrong. His query works just fine. See below -- > > sqlite> CREATE TABLE seven_days ( > ...> mon INTEGER, > ...> tue INTEGER, > ...> wed INTEGER > ...> ); > sqlite> INSERT INTO seven_days VALUES (5, 3, 2); > sqlite> INSERT INTO seven_days VALUES (1, 4, 3); > sqlite> INSERT INTO seven_days VALUES (7, 8, 3); > sqlite> SELECT CASE Strftime('%w', 'now') > ...> WHEN 1 THEN mon > ...> WHEN 2 THEN tue > ...> ELSE wed > ...> END AS day_val > ...> FROM seven_days; > day_val > > -- > 2 > 3 > 3 > sqlite> > > > > > > > > On Wed, May 11, 2011 at 8:30 PM, Igor Tandetnik > wrote: > > > >> On 5/11/2011 8:14 PM, John wrote: > >>> I am in situation where I need to keep as much logic as possible within > >>> SQLite. However the query that I need to perform does not seem to be > >>> possible to perform. > >>> > >>> let's say I have a table with columns for each day of the week > >>> > >>> create table seven_days > >>> (monday_value integer, > >>> tueday_value integer, > >>> wednesday_value integer, > >>> ... ); > >>> > >>> I want to select value from whatever day it is today. So if today is > >>> Tuesday, select from tuesday_value. > >>> > >>> Can I do it with pure SQlite? > >> > >> select case strftime('%w', 'now') > >> when 0 then sunday_value > >> when 1 then monday_value > >> ... > >> else saturday_value > >> end > >> from seven_days; > >> > >> -- > >> Igor Tandetnik > >> > >> ___ > >> sqlite-users mailing list > >> sqlite-users@sqlite.org > >> http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users > >> > > > > > > > > -- > > ~John > > ___ > > sqlite-users mailing list > > sqlite-users@sqlite.org > > http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users > > ___ > sqlite-users mailing list > sqlite-users@sqlite.org > http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users > -- ~John ___ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
Re: [sqlite] Dynamic SQL for SQLite?
On May 11, 2011, at 7:37 PM, John wrote: > Igor, > What you are suggesting will not work. You can only select values not > columns using case. > > > select case strftime('%w', 'now') > when 0 then sunday_value > when 1 then monday_value > ... > else saturday_value > end > from seven_days; > > You must be new here. You have to understand the first rule of SQL. Igor is never wrong. His query works just fine. See below -- sqlite> CREATE TABLE seven_days ( ...> mon INTEGER, ...> tue INTEGER, ...> wed INTEGER ...> ); sqlite> INSERT INTO seven_days VALUES (5, 3, 2); sqlite> INSERT INTO seven_days VALUES (1, 4, 3); sqlite> INSERT INTO seven_days VALUES (7, 8, 3); sqlite> SELECT CASE Strftime('%w', 'now') ...> WHEN 1 THEN mon ...> WHEN 2 THEN tue ...> ELSE wed ...> END AS day_val ...> FROM seven_days; day_val -- 2 3 3 sqlite> > > > On Wed, May 11, 2011 at 8:30 PM, Igor Tandetnikwrote: > >> On 5/11/2011 8:14 PM, John wrote: >>> I am in situation where I need to keep as much logic as possible within >>> SQLite. However the query that I need to perform does not seem to be >>> possible to perform. >>> >>> let's say I have a table with columns for each day of the week >>> >>> create table seven_days >>> (monday_value integer, >>> tueday_value integer, >>> wednesday_value integer, >>> ... ); >>> >>> I want to select value from whatever day it is today. So if today is >>> Tuesday, select from tuesday_value. >>> >>> Can I do it with pure SQlite? >> >> select case strftime('%w', 'now') >> when 0 then sunday_value >> when 1 then monday_value >> ... >> else saturday_value >> end >> from seven_days; >> >> -- >> Igor Tandetnik >> >> ___ >> sqlite-users mailing list >> sqlite-users@sqlite.org >> http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users >> > > > > -- > ~John > ___ > sqlite-users mailing list > sqlite-users@sqlite.org > http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users ___ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
Re: [sqlite] Dynamic SQL for SQLite?
Are you asking if SQLite itself can figure out which day it is and retrieve the appropriate data? Don Ireland -Original Message- From: John <tauru...@gmail.com> To: sqlite-users@sqlite.org Sent: Wed, 11 May 2011 7:15 PM Subject: [sqlite] Dynamic SQL for SQLite? I am in situation where I need to keep as much logic as possible within SQLite. However the query that I need to perform does not seem to be possible to perform. let's say I have a table with columns for each day of the week create table seven_days (monday_value integer, tueday_value integer, wednesday_value integer, ... ); I want to select value from whatever day it is today. So if today is Tuesday, select from tuesday_value. Can I do it with pure SQlite? -- ~John ___ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users ___ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
Re: [sqlite] Dynamic SQL for SQLite?
On 5/11/2011 8:37 PM, John wrote: > What you are suggesting will not work. You can only select values not > columns using case. Will too. Try it. -- Igor Tandetnik ___ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
Re: [sqlite] Dynamic SQL for SQLite?
Igor, What you are suggesting will not work. You can only select values not columns using case. select case strftime('%w', 'now') when 0 then sunday_value when 1 then monday_value ... else saturday_value end from seven_days; On Wed, May 11, 2011 at 8:30 PM, Igor Tandetnikwrote: > On 5/11/2011 8:14 PM, John wrote: > > I am in situation where I need to keep as much logic as possible within > > SQLite. However the query that I need to perform does not seem to be > > possible to perform. > > > > let's say I have a table with columns for each day of the week > > > > create table seven_days > > (monday_value integer, > > tueday_value integer, > > wednesday_value integer, > > ... ); > > > > I want to select value from whatever day it is today. So if today is > > Tuesday, select from tuesday_value. > > > > Can I do it with pure SQlite? > > select case strftime('%w', 'now') >when 0 then sunday_value >when 1 then monday_value >... >else saturday_value > end > from seven_days; > > -- > Igor Tandetnik > > ___ > sqlite-users mailing list > sqlite-users@sqlite.org > http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users > -- ~John ___ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
Re: [sqlite] Dynamic SQL for SQLite?
Nico, thanks a lot! This is awesome. create view today_dow as select monday_goal from user_goals union select tuesday_goal from user_goals union select wednesday_goal from user_goals union select thursday_goal from user_goals union select friday_goal from user_goals union select saturday_goal from user_goals union select sunday_goal from user_goals; > Nico Williams writes: > > On May 11, 2011 7:14 PM, "John"wrote: > > > let's say I have a table with columns for each day of the week > > > > > > create table seven_days > > > (monday_value integer, > > > tueday_value integer, > > > wednesday_value integer, > > > ... ); > > > > > > I want to select value from whatever day it is today. So if today is > > > Tuesday, select from tuesday_value. > > > > > > Can I do it with pure SQlite? > > > > Sure. First setup a view that consists of a union of queries one for > each > > day, with a column whose value is the day of the week. Then query that > > view. > > > > Nico > > -- > > ___ > > sqlite-users mailing list > > sqlite-users@sqlite.org > > http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users > > ___ > sqlite-users mailing list > sqlite-users@sqlite.org > http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users > -- ~John ___ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
Re: [sqlite] Dynamic SQL for SQLite?
On 5/11/2011 8:14 PM, John wrote: > I am in situation where I need to keep as much logic as possible within > SQLite. However the query that I need to perform does not seem to be > possible to perform. > > let's say I have a table with columns for each day of the week > > create table seven_days > (monday_value integer, > tueday_value integer, > wednesday_value integer, > ... ); > > I want to select value from whatever day it is today. So if today is > Tuesday, select from tuesday_value. > > Can I do it with pure SQlite? select case strftime('%w', 'now') when 0 then sunday_value when 1 then monday_value ... else saturday_value end from seven_days; -- Igor Tandetnik ___ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
Re: [sqlite] Dynamic SQL for SQLite?
Wonderful. The other answer is that one probably should not have a table with seven columns and one row when one could have a table with two columns (day of week and value) and seven rows. Like the view you are suggesting. --David Nico Williams writes: > On May 11, 2011 7:14 PM, "John"wrote: > > let's say I have a table with columns for each day of the week > > > > create table seven_days > > (monday_value integer, > > tueday_value integer, > > wednesday_value integer, > > ... ); > > > > I want to select value from whatever day it is today. So if today is > > Tuesday, select from tuesday_value. > > > > Can I do it with pure SQlite? > > Sure. First setup a view that consists of a union of queries one for each > day, with a column whose value is the day of the week. Then query that > view. > > Nico > -- > ___ > sqlite-users mailing list > sqlite-users@sqlite.org > http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users ___ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
Re: [sqlite] Dynamic SQL for SQLite?
On May 11, 2011 7:14 PM, "John"wrote: > let's say I have a table with columns for each day of the week > > create table seven_days > (monday_value integer, > tueday_value integer, > wednesday_value integer, > ... ); > > I want to select value from whatever day it is today. So if today is > Tuesday, select from tuesday_value. > > Can I do it with pure SQlite? Sure. First setup a view that consists of a union of queries one for each day, with a column whose value is the day of the week. Then query that view. Nico -- ___ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
[sqlite] Dynamic SQL for SQLite?
I am in situation where I need to keep as much logic as possible within SQLite. However the query that I need to perform does not seem to be possible to perform. let's say I have a table with columns for each day of the week create table seven_days (monday_value integer, tueday_value integer, wednesday_value integer, ... ); I want to select value from whatever day it is today. So if today is Tuesday, select from tuesday_value. Can I do it with pure SQlite? -- ~John ___ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users