SQL: copying data up

Tags
copying, data, programming, sql

Login to Reply

 
Thread Tools Search this Thread
# 1  
Old 1 Week Ago
SQL: copying data up

I need to fix an SQL statement in MySQL that should calculate a field using values from two of the columns and I prefer to do this using set-based programming, ie not procedural. What needs to happen is that in a separate column called "delta" the value of "level" is copied depending on whether "in_event" is 0 or 1. If it is 1, then the value of level is copied, else the last known value of delta is copied.
This is similar to how data is copied down in spreadsheets for underlying cells that have no value, except that in this case data is copied up instead of down.

These are the statements I am using:
Code:
create table `events` (
  `date` date default null,
  `sortorder` int(11) not null default '0',
  `level` float default null,
  `in_event` int(1) not null default '0'
);

insert into events values 
('2018-09-04', 2, 1547.8, 0),
('2018-09-03', 3, 1571, 1),
('2018-08-31', 4, 1541.6, 0),
('2018-08-30', 5, 1575.4, 0),
('2018-08-29', 6, 1596.6, 0),
('2018-08-28', 7, 1626.4, 0),
('2018-08-24', 8, 1575, 0),
('2018-08-23', 9, 1536, 0),
('2018-08-22', 10, 1579, 0),
('2018-08-21', 11, 1559, 1),
('2018-08-20', 12, 1554, 0),
('2018-08-17', 13, 1534, 0),
('2018-08-16', 14, 1543, 0),
('2018-08-15', 15, 1542.2, 0),
('2018-08-14', 16, 1646.8, 0),
('2018-08-13', 17, 1680.4, 0),
('2018-08-10', 18, 1667.6, 0),
('2018-08-09', 19, 1711.6, 0),
('2018-08-08', 20, 1726.6, 0),
('2018-08-07', 21, 1736.2, 1),
('2018-08-06', 22, 1678, 0),
('2018-08-03', 23, 1692.8, 0),
('2018-08-02', 24, 1655.4, 0),
('2018-08-01', 25, 1717.4, 0),
('2018-07-31', 26, 1732.8, 0),
('2018-07-30', 27, 1678, 0),
('2018-07-27', 28, 1691.4, 0),
('2018-07-26', 29, 1687.4, 0),
('2018-07-25', 30, 1700, 0),
('2018-07-24', 31, 1695, 0),
('2018-07-23', 32, 1604.6, 1),
('2018-07-20', 33, 1599.8, 0),
('2018-07-19', 34, 1622, 0),
('2018-07-18', 35, 1691, 0),
('2018-07-17', 36, 1666.2, 1);

I have gotten as far as the following, but it copies the data down instead of up:
Code:
alter table events add column delta decimal(10, 2);
update events set delta = level where in_event = 1;
select date, sortorder, level, in_event, case when delta is not NULL && @d := delta then delta else @d end as delta
from events
order by date desc;

This is the result that I am looking for:
Code:
insert into events values 
('2018-09-04', 2, 1547.8, 0, 1571),  -- value of 1571 is copied up, because it is the last known value and in_event is not 1
('2018-09-03', 3, 1571, 1, 1571),    -- value of 1571 is attained, because in_event is 1.
('2018-08-31', 4, 1541.6, 0, 1559),
('2018-08-30', 5, 1575.4, 0, 1559),
('2018-08-29', 6, 1596.6, 0, 1559),
('2018-08-28', 7, 1626.4, 0, 1559),
('2018-08-24', 8, 1575, 0, 1559),
('2018-08-23', 9, 1536, 0, 1559),
('2018-08-22', 10, 1579, 0, 1559),
('2018-08-21', 11, 1559, 1, 1736.2),
('2018-08-20', 12, 1554, 0, 1736.2),
('2018-08-17', 13, 1534, 0, 1736.2),
('2018-08-16', 14, 1543, 0, 1736.2),
('2018-08-15', 15, 1542.2, 0, 1736.2),
('2018-08-14', 16, 1646.8, 0, 1736.2),
('2018-08-13', 17, 1680.4, 0, 1736.2),
('2018-08-10', 18, 1667.6, 0, 1736.2),
('2018-08-09', 19, 1711.6, 0, 1736.2),
('2018-08-08', 20, 1726.6, 0, 1736.2),
('2018-08-07', 21, 1736.2, 1, 1736.2),
('2018-08-06', 22, 1678, 0, 1604.6),
('2018-08-03', 23, 1692.8, 0, 1604.6),
('2018-08-02', 24, 1655.4, 0, 1604.6),
('2018-08-01', 25, 1717.4, 0, 1604.6),
('2018-07-31', 26, 1732.8, 0, 1666.2),
('2018-07-30', 27, 1678, 0, 1666.2),
('2018-07-27', 28, 1691.4, 0, 1666.2),
('2018-07-26', 29, 1687.4, 0, 1666.2),
('2018-07-25', 30, 1700, 0, 1666.2),
('2018-07-24', 31, 1695, 0, 1666.2),
('2018-07-23', 32, 1604.6, 1, 1666.2),
('2018-07-20', 33, 1599.8, 0, 1666.2),
('2018-07-19', 34, 1622, 0, 1666.2),
('2018-07-18', 35, 1691, 0, 1666.2),
('2018-07-17', 36, 1666.2, 1, 1666.2);

# 2  
Old 6 Days Ago
Maybe you should simplify this and INSERT the data in the table in a loop versus trying to import all the data in one single INSERT chuck? Sometimes doing things in a simple way (INSERT in loop) have easier results to understand that some sweeping method, all in one statement method (INSERT).
# 3  
Old 4 Days Ago
This is what I have come up with in the meantime:
Code:
select * 
from (select events.*, case when in_event then @delta := level else @delta end delta from events, (select @delta := 0) e order by date) delta
order by date desc;

Login to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Similar Threads More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Shell to SQL for data gksenthilkumar Shell Programming and Scripting 3 07-12-2015 02:36 PM
Copying data from USB CD Drive. videsh77 Red Hat 3 04-22-2014 03:32 PM
Copying part of a data file into another latsyrc UNIX for Dummies Questions & Answers 8 07-08-2013 07:38 AM
Copying data from one file server to another br1an Solaris 2 05-21-2013 08:36 PM
Copying data from files to directories newbie2010 Shell Programming and Scripting 2 05-17-2013 01:06 PM
SQL: copying data down figaro Programming 8 12-08-2011 02:35 PM
Error while copying huge amount of data in aix samsungsamsung AIX 3 06-01-2011 05:34 PM
Doubt in C programming (copying data from one file to another) Lyric Programming 5 10-12-2010 03:56 PM
How to use sql data file in unix csv file as input to an sql query from shell Nareshp Shell Programming and Scripting 2 11-09-2009 02:15 AM
How do I use SQL to query based off file data? whoknows UNIX for Dummies Questions & Answers 4 06-30-2009 11:02 AM
Copying data from excel file 5ahen Shell Programming and Scripting 1 02-04-2009 10:39 AM
Compress the contents of a directory while copying data into it user1602 Shell Programming and Scripting 2 01-12-2009 08:53 PM
Copying/Routing data from the0 to eth1 and eth2 theVOID IP Networking 0 04-11-2008 05:19 AM
SQL Data with Spaces into Arrays mikem22 Shell Programming and Scripting 0 10-19-2007 06:49 AM
copying data to raw devices using 'dd' Neville UNIX for Advanced & Expert Users 4 03-23-2006 06:01 PM
All times are GMT -4. The time now is 07:00 AM.

Unix & Linux Forums Content Copyright 1993-2018. All Rights Reserved.