MYSQL sum() with exclusion of records - mysql

I need to calculat fin_sum for visit_numbers and industry_code, but if id in other table then i need to exclude records which have id in other table from calculation but, if id is not esixst in table themn it should be included.
I have table with next structure
create temporary table client_transactions_final
(
id int,
fin_amount decimal (6,2),
ind_code varchar(10),
visit_number int
);
insert into client_transactions_final values
(1, 100, 'Ind 1', 2),
(1, 300, 'Ind 2', 3),
(2, 300, 'Ind 3', 4),
(2, 100, 'Ind 1', 2),
(3, 300, 'Ind 2', 3),
(4, 300, 'Ind 3', 5),
(5, 100, 'Ind 1', 2),
(6, 300, 'Ind 2', 5),
(6, 300, 'Ind 3', 4)
create temporary table term_map
(
id int
);
insert into term_map values
(2),
(4);
from this table i am runing select which do sum groyp by visit_numbers and industry code
SELECT visit_number,
case when id in (SELECT id FROM term_map) then
--sum(fin_amount) do not include ids from SELECT id FROM term_map into
else
SUM(fin_amount) end revenue
FROM
client_transactions_final
GROUP BY visit_number , ind_code
Howewer i need to calculat fin_sum for visit_numbers and industry_code, but if id in other table then i need to exclude records which have id in other table from calculation but, if id is not esixst in table themn it should be included. I alreade try difrentaproaches on how to do it but non of it is working any ideas how to do it ?

Try this:-
SELECT visit_number,
SUM(fin_amount) revenue
FROM
client_transactions_final
where id not in (SELECT id FROM term_map)
GROUP BY visit_number , ind_code

Related

Using mysql: column count doesn't match value count at row 1

I'm new to mysql, and can not figure out why this error keeps coming up. It's a simple table and I want id to be 1, 2, 3, 4 etc. alongside two other columns. Why does it keep reading, column count doesn't match value count at row 1?
CREATE DATABASE thedatabase;
USE thedatabase;
CREATE TABLE cars (
id INTEGER AUTO_INCREMENT,
model INTEGER NOT NULL,
mileage INTEGER NOT NULL,
PRIMARY KEY (id)
);
INSERT INTO thedatabase.cars (
model,
mileage
) VALUES (
(45, 34598),
(22, 23847),
(10, 3847),
(487, 93229),
(237, 238975),
(23, 23987),
(34, 3498),
(57, 34984),
(56, 34983),
(20, 9845);
You have got an extra opening bracked in INSERT statement, after VALUES below should work fine:
INSERT INTO thedatabase.cars ( model,
mileage ) VALUES (45, 34598), (22, 23847), (10, 3847), (487, 93229), (237, 238975), (23, 23987), (34, 3498), (57, 34984), (56, 34983), (20, 9845);

single table reccuring relatives

Can't name it less confusing, sorry...
Imagine the DB table with 3 columns:
object_id - some entity,
relation_key - some property of the object,
bundle_id - we must generalize different objects with this id.
Table has unique key for [object_id, relation_key]:
single object can't have duplicated relation_key,
but different objects can have equal relation_key
Some oxygen understanding with the picture:
Plenty objects can have deep relations by relation_key, all this objects will be related with bundle_id
How can I update bundle_id column with correct values using just single query?
I can write procedure but this way is unsuitable for me.
I look for statement like:
"UPDATE example [join example ON ...] SET bundle_id = ... WHERE ..."
there is "before" schema for mysql:
CREATE TABLE `example` (
`bundle_id` INT(11) DEFAULT NULL,
`object_id` INT(11) NOT NULL,
`relation_key` INT(11) NOT NULL,
PRIMARY KEY (`object_id`,`relation_key`)
);
INSERT INTO `example`(`object_id`, `relation_key`)
VALUES (1, 4), (1, 5), (1, 6), (2, 6), (2, 7), (2, 8), (3, 4),
(3, 9), (3, 10), (4, 11), (4, 12), (4, 13), (5, 14), (5, 15), (5, 16), (6, 17), (6, 11), (6, 18);
Here is the example "before": fiddle example (sqlfiddle stuck for this moment)
And "after" will look like like if you do the queries :
UPDATE `example` SET `bundle_id` = 1 WHERE `object_id` IN (1, 2, 3);
UPDATE `example` SET `bundle_id` = 2 WHERE `object_id` IN (4, 6);
UPDATE `example` SET `bundle_id` = 3 WHERE `object_id` IN (5);
object1 related to object2 by key=6,
object3 related TO object1 by key=4,
so ... objs 1, 2, 3 are related together.
here must be first bundle_id=1.
there is no other keys linking another objects to 1, 2, 3
object_id=4 related to object_id=6 by key=11
so ... obj [4, 6] are related together.
here must be second bundle_id=2,
there is no other keys linking another objects to 4, 6
object_id=5 has no relations to other objects
all object's key belong to itself.
here must be second bundle_id=3,
there is no other keys linking another objects to 5

Is my SQL wrong or is it sqlfiddle?

I'm trying to better wrap my head around PIVOT in MySQL. I made a test table and query in SQLfiddle but I'm not getting it to work. I get the error:
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near 'PIVOT ( COUNT p.[stat] FOR p.[title] IN (p.[title], p.[weight],
p.[length]' at line 3
Table and data:
CREATE TABLE pivoter (
amount int,
title varchar(20),
weight int,
length int,
stat varchar(10)
);
INSERT INTO pivoter VALUES (5, 'Blip', 13, 5, 'hold');
INSERT INTO pivoter VALUES (2, 'Crog', 10, 5, 'full');
INSERT INTO pivoter VALUES (2, 'Gump', 22, 1, 'hold');
INSERT INTO pivoter VALUES (10, 'Yark', 7, 12, 'fun');
INSERT INTO pivoter VALUES (0, 'Blim', 6, 1, 'full');
INSERT INTO pivoter VALUES (3, 'Fron', 5, 8, 'hold');
INSERT INTO pivoter VALUES (8, 'Xand', 2, 7, 'fun');
INSERT INTO pivoter VALUES (2, 'Xnor', 7, 9, 'hold');
INSERT INTO pivoter VALUES (4, 'Rugg', 7, 8, 'fun');
INSERT INTO pivoter VALUES (9, 'Lint', 4, 10, 'fun');
INSERT INTO pivoter VALUES (9, 'Pawn', 7, 2, 'hold');
INSERT INTO pivoter VALUES (0, 'Undr', 12, 11, 'hold');
INSERT INTO pivoter VALUES (14, 'Call', 1, 6, 'full');
Pivot Query:
select stat, title, weight, length, amount from pivoter p
PIVOT
(
COUNT p.[stat]
FOR p.[title]
IN (p.[title], p.[weight], p.[length], p.[amount])
) AS testedPivot;
In case you were wondering, no, the data isn't supposed to make sense. I just made garbage to try this out.
Is the problem with my query or sqlfiddle?
If it's my query what can I do to make this work?
If the problem is sqlfiddle, do you know of an alternative?
The query in the question looks like T-SQL syntax for Microsoft SQL Server.
This syntax is not valid in MySQL, for a couple of reasons.
PIVOT is not a valid keyword in MySQL.
And MySQL uses backticks to escape identifiers, not square brackets.

MySQL query comparing values to previous rows' values

I've been searching but have been unable to find a solution to this--I know it's do-able but I just don't have the ninja SQL skills I need (yet)....
I'm looking for a solution to this issue: I have a 2 tables related to stock market data. The first is a simple list of stock symbols with an ID and stock ticker symbol (ID,SYMBOL). The second table contains historical price data for each of the stocks. (ID, DATE, OPEN, HIGH, LOW, CLOSE, VOLUME).
I'm trying to figure out how to query for stocks that have the most recent CLOSE price that is greater than their CLOSE price 5 trading-days ago. I can't just do date math because the stocks don't trade every day (no trading on weekends & holidays, as well as some stocks may not trade on a normal trading day). Thus, I just need to compare the CLOSE price from most recent row and the 5th row proceeding it for each symbol.
I have sample tables and data here:
http://sqlfiddle.com/#!2/5fe76/2
CREATE TABLE `STOCKS` (
`ID` int,
`SYMBOL` varchar(10)
);
INSERT INTO `STOCKS` (`ID`,`SYMBOL`)
VALUES
(1, 'AA'),
(2, 'ADT'),
(3, 'AEO'),
(4, 'AFA');
CREATE TABLE `PRICES` (
`ID` int,
`DATE` date,
`OPEN` decimal(6,2),
`HIGH` decimal(6,2),
`LOW` decimal(6,2),
`CLOSE` decimal(6,2),
`VOLUME` bigint
);
INSERT INTO `PRICES` (`ID`,`DATE`,`OPEN`,`HIGH`,`LOW`,`CLOSE`,`VOLUME`) VALUES
(1, '2014-11-06', 16.37, 16.42, 16.15, 16.37, 14200400),
(1, '2014-11-05', 16.68, 16.69, 16.17, 16.26, 18198200),
(1, '2014-11-04', 16.85, 16.87, 16.43, 16.56, 13182800),
(1, '2014-11-03', 16.78, 17.03, 16.65, 16.93, 15938500),
(1, '2014-10-31', 16.43, 16.76, 16.24, 16.76, 18618300),
(1, '2014-10-30', 16.17, 16.36, 15.83, 16.22, 17854400),
(1, '2014-10-29', 16.58, 16.70, 16.05, 16.27, 31173000),
(1, '2014-10-28', 16.5, 16.65, 16.41, 16.60, 12305900),
(1, '2014-10-27', 16.56, 16.57, 16.31, 16.38, 15452900),
(1, '2014-10-24', 16.33, 16.57, 16.22, 16.55, 12840200),
(2, '2014-11-06', 35.9, 36.12, 35.75, 36.07, 1018100),
(2, '2014-11-05', 35.68, 35.99, 35.37, 35.96, 1101500),
(2, '2014-11-04', 35.13, 35.69, 35.02, 35.49, 819100),
(2, '2014-11-03', 35.81, 35.99, 35.27, 35.32, 1304500),
(2, '2014-10-31', 35.79, 35.86, 35.46, 35.84, 1319400),
(2, '2014-10-30', 34.7, 35.34, 34.66, 35.19, 1201800),
(2, '2014-10-29', 35.06, 35.56, 34.5, 34.92, 1359000),
(2, '2014-10-28', 34.32, 35.17, 34.15, 35.07, 1301800),
(2, '2014-10-27', 34.2, 34.2, 33.66, 34.1, 662600),
(2, '2014-10-24', 34.02, 34.54, 33.95, 34.5, 750600),
(3, '2014-11-06', 13.27, 13.92, 13.25, 13.82, 6518000),
(3, '2014-11-05', 12.95, 13.27, 12.74, 13.22, 8716700),
(3, '2014-11-04', 12.85, 12.94, 12.65, 12.89, 4541200),
(3, '2014-11-03', 12.91, 13.12, 12.73, 12.89, 4299100),
(3, '2014-10-31', 13.2, 13.23, 12.83, 12.87, 7274700),
(3, '2014-10-30', 12.83, 12.91, 12.68, 12.86, 4444300),
(3, '2014-10-29', 13.02, 13.20, 12.79, 12.91, 2974900),
(3, '2014-10-28', 12.87, 13.10, 12.52, 13.04, 7365600),
(3, '2014-10-27', 12.84, 13.00, 12.67, 12.92, 6647900),
(3, '2014-10-24', 13.26, 13.29, 12.60, 12.92, 12803300),
(4, '2014-11-06', 24.59, 24.59, 24.49, 24.55, 20400),
(4, '2014-11-05', 24.81, 24.9, 24.81, 24.88, 11800),
(4, '2014-11-04', 24.87, 24.88, 24.76, 24.88, 10600),
(4, '2014-11-03', 24.85, 24.88, 24.76, 24.81, 18100),
(4, '2014-10-31', 24.82, 24.85, 24.77, 24.78, 8100),
(4, '2014-10-30', 24.83, 24.87, 24.74, 24.79, 13900),
(4, '2014-10-29', 24.86, 24.86, 24.78, 24.81, 5500),
(4, '2014-10-28', 24.85, 24.85, 24.80, 24.84, 10600),
(4, '2014-10-27', 24.68, 24.85, 24.68, 24.85, 7700),
(4, '2014-10-24', 24.67, 24.82, 24.59, 24.82, 9300);
Pseudo code for the query would be something like this:
"Find symbols whos most recent closing prices is greater than the closing price 5 trading-days earlier"
The query I'd like to create should result in the following:
Date Symbol Close Close(-5)
2014-11-06 AA 16.37 16.22
2014-11-06 ADT 36.07 35.19
2014-11-06 AEO 13.82 12.86
(the symbol 'AFA' would not match as it's recent close is 24.55 and 5 rows prior it was 24.75)
You can get the price 5 days ago using a correlated subquery. In fact, you can get the most recent price the same way. So, this might be the right path:
select s.*,
(select p.close
from prices p
where p.id = s.id
order by date desc
limit 1
) as Close,
(select p.close
from prices p
where p.id = s.id and p.date <= date(now()) - interval 5 day
order by date desc
limit 1
) as Close_5
from stocks s
having Close > Close_5;

(SQL) Is it possible to update a field value with the value of the field in the previous row?

I have a table from which I would like the update the value of a certain colum of fields.
Basicly moving one value down and those under it should inherit the previous value of the one about them.
I wonder if this action is possible using a single SQL query.
My table:
CREATE TABLE `menu` (
`slot_index` int(2) NOT NULL,
`language_ID` int(2) NOT NULL,
`menuItem` text NOT NULL,
PRIMARY KEY (`slot_index`,`language_ID`),
KEY `language_ID` (`language_ID`)
)
The content in it:
INSERT INTO `menu` (`slot_index`, `language_ID`, `menuItem`) VALUES
(1, 1, 'Home'),
(2, 1, 'Wie zijn wij'),
(21, 1, 'Missie'),
(22, 1, 'Doelen'),
(23, 1, 'Visie'),
(24, 1, 'Test'),
(3, 1, 'Wat doen wij'),
(31, 1, 'Medische kaart voor op reis'),
(32, 1, 'Huisartsenkaart'),
(33, 1, 'Huisartsenkaart anderstaligen'),
(4, 1, 'Perskamer'),
(5, 1, 'Beheer'),
(6, 1, 'FAQ'),
(7, 1, 'Ervaringen'),
(8, 1, 'Contact'),
(81, 1, 'Disclaimer'),
(9, 1, 'Links'),
(91, 1, 'Adresgegevens')
I would like to move slot_index 5 to 9, and make the fields under it move up inheriting the value from the field above.
Is this possible with a single query at all, or should I just write a script for this?
Thanks in advance.
Wolfert
Maybe using auto increment will help for future uses.
As for the current one I doubt that this can go with single query, because you cant query the table you are trying to update.