SQLQuery with Paste in R & RODBC - mysql

I have a problem with RODBC;this is the error :
chargerExp("C:\\test.csv",NE=1,NC=1,s=1)
Exeperience: 1 Execution: 1 Sujet: 1
> ajouter(new ("BDD"),new("Exp"))
[1] "42000 1064 [MySQL][ODBC 5.2(a) Driver][mysqld-5.6.17]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 ' , ' NA/NA/NA/ ' , ' NA/NA/NA/ ' , ' NA/NA/NA/ ' , ' NA/NA/NA/ ' , ' NA/NA/NA/' at line 1"
[2] "[RODBC] ERROR: Could not SQLExecDirect 'INSERT INTO `test` (`NE`, `NC`, `E`, `X`, `Y`,
`Z`, `T`, `A`, `S`) VALUES ( , , ' NA/NA/NA/ ' , ' NA/NA/NA/ ' , ' NA/NA/NA/ ' , ' NA/NA/NA/ ' , ' NA/NA/NA/ ' , , );'"
I'm using Mysql, RODBC and Rstudio with a method ajouter to insert in the data base
setMethod( f ="ajouter",signature =c(x="BDD",obj="Exp"),
def = function(x, obj)
{
channel <- odbcConnect(dsn="RSQL",uid="root",pwd="toor")
ne <- obj["ne"]
nc <- obj["nc"]
s <- obj["S"]
e<- encoder((obj["E"]))
x <- encoder((obj["X"]))
y <- encoder((obj["Y"]))
z <- encoder((obj["Z"]))
t<- encoder((obj["T"]))
a <- (obj["A"])
requeteSql.valeur <- paste("'",e,"'",",",
"'",x,"'",",",
"'",y,"'",",",
"'",z,"'",",",
"'",t,"'",",")
requetesql <- paste("INSERT INTO `test` (`NE`, `NC`, `E`, `X`, `Y`, `Z`, `T`,
`A`, `S`) VALUES (",ne,",",nc,", ",requeteSql.valeur, a,", ",s,");")
sqlQuery(channel, requetesql)
}
)
This is encoder method. It's main purpose is to convert my object to a text with some concatenation.
setMethod( f ="encoder", signature ="Para",
def =function(x, i, j, value)
{
s <- as.character(x["val"][1])
for(i in 2:length(x["val"]))
{
s <- paste(s,x["val"][i],sep="/")
}
return(s)
}
)
The call of my methods :
ajouter(new ("BDD"), new("Exp"))
This is my table in data base :
CREATE TABLE `test` (
`idTest` int(11) NOT NULL AUTO_INCREMENT,
`NE` int(11) DEFAULT NULL,
`NU` int(11) DEFAULT NULL,
`E` text,
`X` text,
`Y` text,
`Z` text,
T` text,
`A` float DEFAULT NULL,
`S` int(11) DEFAULT NULL,
PRIMARY KEY (`idTest`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
My problem is from sqlquery, it said that my syntax are not correct, I don't know why.
This is the the object I want to load into the database (from dput so copy/pasteable):
new("Para"
, ne= 1
, nc = 1
, E = new("E"
, val = c(-13, -11, -11, -11, -11, -9, -10, -12, -12, -12, -11, -10,
-8, -8, -8, -9, -9, -7, -9, -10, -9, -9, -9, -9, -11, -9, -7,
-7, -7, -7, -7, -8, -7, -7, -5, -5, -7, -8, -7, -5, -5, -6, -6,
-9, -35, -76, -96, -62, 38, 167, 251, 251, 248, 157, 94, 56,
)
)
, X = new("Para"
, val = c(115, 116, 114, 113, 113, 114, 115, 114, 114, 113, 112, 111,
113, 114, 114, 115, 115, 116, 115, 115, 114, 116, 114, 115, 114,
113, 114, 114, 114, 114, 114, 113, 113, 114, 114, 114, 114, 115,
114, 115, 115, 115, 114, 115, 116, 114, 114, 114, 116, 115, 113,
)
)
, Y = new("Para"
, val = c(10, 11, 9, 9, 10, 9, 10, 9, 11, 10, 11, 10, 11, 11, 10, 11,
10, 10, 11, 10, 9, 10, 11, 12, 11, 10, 11, 11, 11, 12, 11, 11,
11, 10, 11, 11, 10, 12, 10, 11, 11, 11, 11, 11, 11, 10, 11, 11,
10, 11, 11, 11, 10, 11, 11, 11, 10, 11, 10, 12, 11, 10, 10, 10,
)
)
, Z = new("Para"
, val = c(-42, -42, -44, -43, -42, -41, -42, -42, -42, -42, -42, -43,
-40, -41, -41, -40, -41, -43, -41, -41, -41, -41, -41, -40, -40,
-41, -40, -40, -41, -40, -42, -41, -41, -41, -41, -41, -43, -42,
-43, -42, -41, -42, -42, -40, -41, -42, -40, -41, -41, -41, -42,
)
, T = new("Para"
, val = c(25.2, 25.2, 25.2, 25.2, 25.2, 25.2, 25.2, 25.2, 25.2, 25.2,
25.2, 25.2, 25.2, 25.2, 25.2, 25.2, 25.2, 25.2, 25.2, 25.2, 25.2,
25.2, 25.2, 25.2, 25.2, 25.2, 25.2, 25.2, 25.2, 25.2, 25.2, 25.2,
25.2, 25.2, 25.2, 25.2, 25.2, 25.2, 25.2, 25.2, 25.2, 25.2, 25.2,
)
)
, A = 1L
, S = 1
)

I downloaded your code and there seem to be many problems, the lest of which is actually running the query you tried. The error tells you it tried to run the SQL command
INSERT INTO `test` (`NE`, `NC`, `E`, `X`, `Y`, `Z`, `T`, `A`, `S`)
VALUES ( , , ' NA/NA/NA/ ' , ' NA/NA/NA/ ' , ' NA/NA/NA/ ' ,
' NA/NA/NA/ ' , 'NA/NA/NA/ ' , , );'
And it should be clear that looks invalid. I don't think you can have emply values in an INSERT statement like that.
The blank values are from the lines
ne <- obj["ne"]
nc <- obj["nc"]
s <- obj["S"]
In your function definition, obj is your Exp (or Experimentation) and you simply haven't set any values for these slots yet. Since they are empty vectors, they do not render as anything in the paste statement.
The batman values (aka NA/NA/NA) are from the
e<- encoder((obj["E"]))
x <- encoder((obj["X"]))
y <- encoder((obj["Y"]))
lines. When the Signal class is empty (as it is when you create a new, empty object) the val slot is just an empty numeric vector. So when you go to encode it, you've set up a loop that's basically going
s <- NA
for(i in 2:1)
{
s <- paste(s,NA,sep="/")
}
which is resulting in that NA/NA/NA/ value. And really that loop in unnecessary, a simple
s <- paste(as.character(x["val"]), collapse="/")
would give you the string you want without the counting-backwards problem.
So really, running ajouter(new ("BDD"),new("Exp")) with empty objects like that doesn't make much sense. You really should test each of your classes first by themselves before putting them together like that. I can't believe you didn't get other errors or warnings before that. You should work on isolating your errors and testing each part individually.

Related

how do i rewrite the correct statement for COUNT under this condition

Bring back the count of all the batsmen who have played a certain number of innings only if there are more than one batsman who have played that number of innings. 4 innings – 2 batsmen, 5 innings – 3 batsmen etc.
SELECT COUNT(Innings) AS TotalInnings, COUNT(player) AS Totalplayer
FROM Batting
GROUP BY Player
Player is the field name for the batsmens name. I am kinda stuck on how to write the last lines statement
4 innings – 2 batsmen, 5 innings – 3 batsmen etc
This is the table design
CREATE TABLE Batting (
Player CHAR(25),
CarrerSpan VARCHAR(20),
Matches INT,
Innings INT,
Playing INT,
Runs INT,
HighestScore CHAR(10),
AverageScore NUMERIC,
BallsFaced INT,
StrikeRate NUMERIC,
Hundreds INT,
Fifties INT,
Zeros INT,
Fours INT,
Sixs INT
);
INSERT INTO Batting
VALUES ('JP Duminy', '2007-2018', 77, 71, 22, 1825, '96*', 37.24, 1468, 124.31, 0, 11, 6, 130, 65);
INSERT INTO Batting
VALUES ('AB de Villiers', '2006-2017', 78, 75, 11, 1672, '79*', 26.12, 1237, 135.16, 0, 10, 5, 140, 60);
INSERT INTO Batting
VALUES ('HM Amla', '2009-2018', 41, 41, 5, 1158, '97*', 32.16, 883, 131.14, 0, 7, 2, 133, 23);
INSERT INTO Batting
VALUES ('F du Plessis', '2012-2017', 36, 36, 6, 1129, 119, 37.63, 849, 132.97, 1, 7, 0, 102, 35);
INSERT INTO Batting
VALUES('DA Miller', '2010-2018', 58, 51, 15, 1043, '101*', 28.97, 745, 140.00, 1, 1, 0, 71, 46);
INSERT INTO Batting
VALUES('GC Smith', '2005-2011', 33, 33, 2, 982, '89*', 31.67, 770, 127.53, 0, 5, 1, 123, 26);
INSERT INTO Batting
VALUES('Q de Kock','2012-2018', 32, 32, 4, 821, 59, 29.32, 637, 128.88, 0, 2, 3, 97, 24);
INSERT INTO Batting
VALUES('JH Kallis', '2005-2012', 25, 23, 4, 666, 73, 35.05, 558, 119.35, 0, 5, 0, 56, 20);
INSERT INTO Batting
VALUES('JA Morkel', '2005-2015', 50, 38, 11, 572, 43, 21.18, 402, 142.28, 0, 0, 1, 29, 39);
INSERT INTO Batting
VALUES('F Behardien', '2012-2018', 37, 29, 13, 515, '64*', 32.18, 402, 128.10, 0, 1, 1, 37, 16);
INSERT INTO Batting
VALUES('HH Gibbs', '2005-2010', 23, 23, 1, 400, '90*', 18.18, 318, 125.78, 0, 3, 4, 45, 12);
INSERT INTO Batting
VALUES('RR Rossouw', '2014-2016', 15, 14, 3, 327, 78, 29.72, 237, 137.97, 0, 2, 2, 29, 12);
INSERT INTO Batting
VALUES('LE Bosman', '2006-2010', 14, 14, 1, 323, 94, 24.84, 219, 147.48, 0, 3, 2, 27, 20);
INSERT INTO Batting
VALUES('RR Hendricks', '2014-2018', 13, 13, 0, 294, 70, 22.61, 262, 112.21, 0, 1, 2, 33, 3);
INSERT INTO Batting
VALUES('MV Boucher', '2005-2010', 25, 21, 6, 268, '36*', 17.86, 275, 97.45, 0, 0, 0, 22, 2);
INSERT INTO Batting
VALUES('RE Levi', '2012-2012', 13 ,13, 2, 236, '117*', 21.45, 167, 141.31, 1, 1, 3, 20, 15);
INSERT INTO Batting
VALUES('MN van Wyk', '2007-2015', 8, 7, 1, 225, '114*', 37.50, 157, 143.31, 1, 1, 0, 19, 14);
INSERT INTO Batting
VALUES('CA Ingram', '2010-2012', 9, 9, 1, 210, 78, 26.25, 162, 129.62, 0, 1, 1, 23, 7);
INSERT INTO Batting
VALUES('JM Kemp', '2005-2007', 8, 7, 3, 203, '89*', 50.75, 160, 126.87, 0, 1, 0, 17, 10);
INSERT INTO Batting
VALUES('J Botha', '2006-2012', 40, 0, 9, 201, 34, 18.27, 165, 121.81, 0, 0, 1, 15, 9);
INSERT INTO Batting
VALUES('H Davids', '2012-2013', 9, 9, 0, 161, 68, 17.88, 134, 120.14, 0, 2, 2, 18, 4);
INSERT INTO Batting
VALUES('JL Ontong', '2008-2015', 14, 10, 0, 158, 48, 15.80, 109, 144.95, 0, 0, 1, 6, 11);
INSERT INTO Batting
VALUES('JT Smuts', '2017-2018', 8, 8, 0, 126, 45, 15.75, 114, 110.52, 0, 0, 1, 14, 5);
INSERT INTO Batting
VALUES('RJ Peterson', '2006-2014', 21, 12, 4, 124, 34, 15.50, 113, 109.73, 0, 0, 1, 13, 2);
INSERT INTO Batting
VALUES('WD Parnell', '2009-2017', 40, 13, 9, 114, '29*', 28.50, 96, 118.75, 0, 0, 0, 10, 3);
INSERT INTO Batting
VALUES( 'H Klaasen', '2018-2018', 4, 4, 0, 110, '69', 27.50, 64, 171.87, 0, 1, 0, 5, 9);
INSERT INTO Batting
VALUES( 'M Mosehle', '2017-2017', 7, 6, 1, 105, '36', 21.00, 65, 161.53, 0, 0, 0, 6, 9);
INSERT INTO Batting
VALUES( 'D Wiese', '2013-2016', 20, 11, 4, 92, '28', 13.14, 75, 122.66, 0, 0, 1, 4, 3);
INSERT INTO Batting
VALUES( 'SM Pollock', '2005-2008', 12, 9, 2, 86, '36*', 12.28, 70, 122.85, 0, 0, 3, 4, 4);
INSERT INTO Batting
VALUES( 'CH Morris', '2012-2018', 17, 10, 3, 77, '17*', 11.00, 70, 110.00, 0, 0, 2, 7, 2);
INSERT INTO Batting
VALUES( 'RE van der Merwe', '2009-2010', 13, 6, 3, 57, '48', 19.00, 50, 114.00, 0, 0, 1, 2, 4);
INSERT INTO Batting
VALUES( 'C Jonker', '2018-2018', 1, 1, 0, 49, '49', 49.00, 24, 204.16, 0, 0, 0, 5, 2);
INSERT INTO Batting
VALUES( 'HG Kuhn', '2009-2017', 7, 6, 2, 49, '29', 12.25, 42, 116.66, 0, 0, 0, 3, 2);
INSERT INTO Batting
VALUES( 'JJ van der Wath', '2006-2007', 8, 4, 1, 46, '21', 15.33, 39, 117.94, 0, 0, 0, 3, 1);
INSERT INTO Batting
I think this could be the solution to your problem:
Query1
SELECT Innings,
COUNT(player) AS Totalplayer
FROM Batting
GROUP BY Innings
HAVING COUNT(player) > 1;
OR
Query2
SELECT Innings,
COUNT(player) AS Totalplayer
FROM Batting
GROUP BY Innings
HAVING COUNT(Innings) > 1;
Sample Output :
Innings
Totalplayer
4
2
6
3
7
2
9
3
10
2
13
3
14
2
23
2
View on DB Fiddle
Well you can save the number of rows for which the Innings column has certain value. You can save this number in a temporary table and then query the table. See following SQL:
CREATE TEMPORARY TABLE BatsmenCount (SELECT count(*) as total FROM Batting WHERE Innings=71);
SELECT * FROM BatsmenCount WHERE total > 1;
This query will return the number of batsmen that have played 71 innings and save the result in a temporary table. Next this number is fetched using SELECT query. If the query returns no results, then the number of batsmen that have played 71 innings is 1 or less. If the query returns a result, then this result is the number of batsmen that have played 71 innings.
See the SQL code on dbfiddle.

Whats wrong in my code ? 1064 check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line

I have a problem in my code
I don't know where the error in my code and I think my code is fine (maybe)
plase help me
this is my code :
CREATE TABLE IF NOT EXISTS `dt_training` (
`id_training` int(11) NOT NULL AUTO_INCREMENT,
`tanggal` date NOT NULL,
`pm25` int(1) DEFAULT NULL,
`curahHujan` int(1) DEFAULT NULL,
`kelembaban` int(1) DEFAULT NULL,
`kecepatanAngin` int(1) DEFAULT NULL,
`RESULT` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id_training`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=476 ;
INSERT INTO `dt_training` (`id_training`, `tanggal`, `pm25`, `curahHujan`, `kelembaban`,
`kecepatanAngin`, `RESULT`) VALUES
(1, '2/1/2019', 37.69416376, 1, 78, 2, 0),
(2, '2/2/2019', 27.61848739, 2.3, 70, 3, 0),
(3, '2/8/2019', 34.82260521, 0, 79, 2, 0),
(4, '2/9/2019', 40.56758941, 1.3, 79, 2, 0),
(5, '2/10/2019', 24.05520774, 30.5, 84, 2, 0),
.
.
.
(450, '8/3/2020', 43.53877095, 0, 74, 2, 0),
(451, '8/4/2020', 59.05010846, 0,79, 1, 0),
(452, '8/5/2020', 106.6554527, 14.6, 78, 1, 0),
(453, '8/6/2020', 67.41181118, 0, 75, 2, 0),
(454, '8/7/2020', 62.0547619, 0, 76, 2, 0),
(456, '8/8/2020', 87.904, 0, 76, 2, 0),
(457, '8/9/2020', 69.39713576, 0, 74, 2, 0),
(458, '8/10/2020', 77.89735294, 0, 74, 2, 0),
(459, '8/11/2020', 94.91111111, 0, 78, 0, 0),
(460, '8/12/2020', 58.48239865, 20, 83, 1, 0),
(461, '8/13/2020', 16.5097891, 0, 87, 1, 0),
(462, '8/14/2020', 15.48111111, 2.2, 84, 1, 0),
(463, '8/15/2020', 14.62266776, 3.6, 80, 2, 0),
(464, '8/16/2020', 12.87, 0.8, 79, 2, 0),
(465, '8/17/2020', 10.55883162, 0.4, 75, 2, 0),
(467, '8/18/2020', 14.51629568, 0, 78,1 , 0),
(468, '8/19/2020', 7.316003289, 0, 74, 3, 0),
(469, '8/20/2020', 9.824069767, 0, 70, 2, 0),
(470, '8/26/2020', 0, 0, 58, 4, 0),
(471, '8/27/2020', 0, 0, 63, 3, 0),
(472, '8/28/2020', 0, 0, 67, 3, 0),
(473, '8/29/2020', 0, 0, 74, 2, 0),
(474, '8/30/2020', 0, 0, 70, 2, 0),
(475, '8/31/2020', 0, 0, 66 ,2 , 0);
and MYSQL said :
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB
server version for the right syntax to use near '' at line 469
... 1064 ... near ''
That usually means that that is an unclosed parenthesis, quote, or other syntax punctuation. (I don't happen to see such in the snippet you provided; check the rest of the text.)
Other issues. Use a 1-byte TINYINT instead a 4-byte INT for flags.
'2/1/2019' is not compatible with DATE datatype. You could use a str_to_date() to fix it as you insert the data, or you could change to '2019/02/01' (Or is it '2019/01/02'??)

MYSQL- Substract two colums and get the result

I am trying to write a query wherein it should count the number of students and tell me the remaining seats available in a vehicle.
Have managaed to identify which student is associated to which bus but getting stuck to find the seat remaining
Below is data :
vehnum route seats student id
23 2 45 2345
33 3 46 6789
Below is the query :
SELECT deveh.vehicle_reg_no AS vehnum
, veh.route_code AS route
, deveh.seating_capacity AS vehseat
, class.fk_stu_id
FROM tbl_stu_class AS class
JOIN tbl_stu_route AS route
ON route.fk_stu_cls_id = class.pk_stu_cls_id
JOIN list_routes AS veh
ON route.fk_route_id = veh.pk_route_id
JOIN list_vehicles AS deveh
ON deveh.pk_vehicle_id = veh.fk_vehicle_id
WHERE class.fk_year_id = 62
AND class.current_yr = 'Y'
Added sample data :
INSERT INTO `list_vehicles` (`pk_vehicle_id`, `vehicle_reg_no`, `vehicle_type`, `regd_owner_name`, `seating_capacity`, `brand_model`, `type_of_body`, `reg_address`, `fuel_type`, `chasis_no`, `reg_authority`, `engine_no`, `color`, `reg_date`, `reg_valid_date`, `month_yr_mfg`, `fk_user_id`, `timestamp`) VALUES
(46, 'J58987', 'Bus', 'M', 30, 'VOlvo', 'Steel', 'FBD', 'Petrol', '565', 'M1', '5689', 'blue', '2016-10-02', '2016-10-02', '2014-12-31', 1, '2018-07-11 18:01:06'),
(53, 'J1234', 'Bus', 'der', 45, 'Volvo', 'Metal', 'Indirapuram', 'Petrol', '123456', 'det', '2365', 'blue', '2010-12-12', '2020-12-12', '2009-12-11', 1, '2018-07-12 06:54:50'),
(54, 'J1234er', 'Van', 'der', 46, 'Volvo', 'Metal', 'Indirapuram', 'Petrol', '12345634', 'det', '236534', 'blue', '2020-02-03', '2020-02-03', '2008-11-11', 1, '2018-07-12 06:57:59');
INSERT INTO `tbl_stu_class` (`pk_stu_cls_id`, `fk_stu_id`, `fk_year_id`, `fk_class_id`, `fk_section_id`, `current_yr`, `fk_user_id`, `timestamp`) VALUES
(1, 56, 50, 22, 10, 'N', 1, '2018-06-08 06:57:34'),
(3, 123, 50, 24, 7, 'N', 1, '2018-06-12 07:54:46'),
(4, 126, 50, 24, 7, 'N', 56, '2018-06-12 07:54:46'),
(5, 123, 52, 25, 7, 'Y', 1, '2018-06-12 17:30:32'),
(6, 126, 52, 25, 7, 'Y', 1, '2018-06-12 17:30:32'),
(7, 132, 50, 22, 9, 'Y', 1, '2018-06-24 10:27:57'),
(8, 133, 51, 23, NULL, 'Y', 1, '2018-06-24 18:22:33'),
(10, 127, 51, 23, NULL, 'Y', 0, '2018-07-11 17:47:05'),
(11, 134, 62, 22, NULL, 'Y', 0, '2018-07-13 08:11:16'),
(12, 135, 62, 21, 7, 'Y', 1, '2018-07-13 11:12:08'),
(13, 136, 62, 21, 9, 'Y', 1, '2018-07-13 14:59:04');

MySQL database queries between queries

Here is my SQL file:
-- phpMyAdmin SQL Dump
-- version 3.4.7.1
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Jan 23, 2012 at 09:26 AM
-- Server version: 5.1.56
-- PHP Version: 5.2.9
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET #OLD_CHARACTER_SET_CLIENT=##CHARACTER_SET_CLIENT */;
/*!40101 SET #OLD_CHARACTER_SET_RESULTS=##CHARACTER_SET_RESULTS */;
/*!40101 SET #OLD_COLLATION_CONNECTION=##COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
--
-- Database: `danielle_youtube`
--
-- --------------------------------------------------------
--
-- Table structure for table `items`
--
CREATE TABLE IF NOT EXISTS `items` (
`id` int(11) NOT NULL,
`name` varchar(65) COLLATE utf8_unicode_ci NOT NULL,
`cost` float NOT NULL,
`seller_id` int(11) NOT NULL,
`bids` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Dumping data for table `items`
--
INSERT INTO `items` (`id`, `name`, `cost`, `seller_id`, `bids`) VALUES
(1, 'Brand New iMac Computer', 149.99, 32, 3),
(2, 'used diaper from my sister', 2.04, 1, 0),
(3, 'Fresh apple pie', 14.99, 54, 32),
(4, 'New gym socks', 2.34, 90, 566),
(5, 'Weedwacker only slightly used', 4.56, 84, 2),
(6, 'New ipad stolen from best buy', 399, 32, 23),
(7, 'Book about having babies', 21.34, 44, 21),
(8, 'Woman Jeans', 49.5, 56, 123),
(9, 'traditional carpet', 25.45, 14, 75),
(10, '3 boxes of frogs', 30.49, 68, 145),
(11, '48 boxes of frogs', 74.29, 6, 99),
(12, '7 boxes of frogs', 857.75, 18, 88),
(13, 'laptop', 743.3, 89, 158),
(14, 'thumbelina', 228.05, 15, 49),
(15, 'bed', 127.15, 65, 189),
(16, 'shampoing', 12.8, 6, 105),
(17, 'stove', 37.66, 68, 111),
(18, 'cushion', 7.15, 97, 157),
(19, 'refrigerator', 657.49, 61, 129),
(20, 'gold necklace', 853.07, 10, 101),
(21, 'pan', 33.7, 7, 184),
(22, 'awesome alien computer game', 10.75, 18, 29),
(23, 'baby coat', 89.99, 14, 47),
(24, 'baby seat', 145.78, 2, 199),
(25, 'satchel', 44.71, 15, 66),
(26, 'women perfum', 110.9, 48, 84),
(27, 'conveyor belt', 1120.75, 11, 4),
(28, 'used car', 5700.5, 12, 135),
(29, 'supercomputer', 49.75, 50, 176),
(30, 'mirror', 26.8, 19, 56),
(31, 'piano', 1800.4, 13, 147),
(32, 'quitar', 88.4, 25, 164),
(33, 'trumpet', 255.15, 36, 23),
(34, 'machintosh', 3845, 20, 107),
(35, 'earphone', 10.5, 17, 110),
(36, 'computer', 418, 11, 152),
(37, 'night light', 13.87, 97, 198),
(38, 'pc bag', 50.99, 48, 65),
(39, 'babyfoot', 376.7, 2, 121),
(40, 'hairdryer', 88.9, 12, 177),
(41, 'babyliss', 130.75, 68, 79),
(42, 'door', 150.5, 98, 13),
(43, 'baby soap', 12.7, 4, 198),
(44, 'used phone', 43.75, 9, 69),
(45, 'bath', 757.15, 96, 55),
(46, 'flower', 10.75, 16, 89),
(47, 'battery charger', 48.75, 25, 87),
(48, 'air conditioner', 975, 12, 151),
(49, 'casserole', 115.75, 46, 35),
(50, 'used toilet', 180.7, 64, 11),
(51, 'teashirt', 14.98, 65, 114),
(52, 'moto', 920, 22, 174),
(53, 'saxophone', 220.9, 60, 140),
(54, 'bicycle', 180.55, 97, 35),
(55, 'man perfum', 95, 75, 199),
(56, 'table', 157.25, 91, 48),
(57, 'boat', 4890.5, 17, 177),
(58, 'iphone', 547, 8, 28),
(59, 'body milk', 50.5, 16, 90),
(60, 'new curtain for bedroom', 278.4, 92, 11),
(61, 'diamond ring', 1900, 15, 45),
(62, 'swept', 4.5, 9, 99),
(63, 'women hat', 17.55, 39, 60),
(64, 'washing machine', 680.9, 42, 125),
(65, 'baby bottle', 27.98, 91, 117),
(66, 'women sun glasses', 66.7, 18, 174),
(67, 'person weighs', 65.25, 10, 100),
(68, 'photo frame', 18, 85, 170),
(69, 'key board', 16.7, 90, 101),
(70, 'screen', 250, 81, 188),
(71, 'bucket', 2.5, 1, 19),
(72, 'lipstick', 24.75, 3, 44),
(73, 'wardrobe', 120.75, 9, 71),
(74, 'blue dress size 40', 88.9, 7, 113),
(75, 'newspaper', 1.5, 95, 172),
(76, 'scanner', 350, 14, 62),
(77, 'camera', 550.7, 17, 95),
(78, 'camcorder', 788.99, 25, 127),
(79, 'gun', 420.1, 81, 107),
(80, 'domestic dog', 200, 19, 129),
(81, 'horse', 759.5, 30, 115),
(82, 'truck', 7800.5, 32, 123),
(83, 'soccer ball', 95.49, 54, 155),
(84, 'gold earring', 385, 75, 92),
(85, 'basket', 250.45, 46, 142),
(86, 'bikini', 85.2, 12, 57),
(87, 'red skirt', 15.9, 18, 188),
(88, 'copier machine', 800.7, 50, 160),
(89, 'handbag', 35.9, 8, 108),
(90, 'bath towel', 25.1, 11, 186),
(91, 'coffee machine', 210.89, 15, 170),
(92, 'wedding dress', 690, 26, 48),
(93, 'man sun glasses', 80.7, 19, 174),
(94, 'candle', 7.5, 22, 102),
(95, 'scarf', 11.9, 7, 143),
(96, 'microwave', 150.29, 6, 11),
(97, 'electric oven', 645, 62, 171),
(98, 'play station', 256.75, 12, 188),
(99, 'dvd', 126.84, 14, 113),
(100, 'magazine', 3.5, 8, 152);
/*!40101 SET CHARACTER_SET_CLIENT=#OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=#OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=#OLD_COLLATION_CONNECTION */;`
Now, when I run this query:
SELECT name , MIN(cost) FROM items WHERE name LIKE '% boxes of frogs' AND seller_id IN (
SELECT seller_id FROM items WHERE name LIKE '% boxes of frogs'
)
I get
name: 3 boxes of frogs
MIN(cost): 30.489999771118164
But when I use:
SELECT name , MIN(cost) FROM items WHERE seller_id IN(
SELECT seller_id FROM items WHERE name LIKE '% boxes of frogs'
)
I get this result:
name: 3 boxes of frogs
MIN(cost): 10.75
I want to know why the results are different?
The first query returns the name of the item and the cheapest price from items of a box of frogs (see the condition on name in the external query).
The second query returns the name of the item and the cheapest price from items of any item sold by someone who is also selling a box of frogs (the condition on name is only applied in the internal condition).

Using date ranges in SQL

I access SQL and the database through an ODBC connection via Excel. This way I can create refresh-able reports. I use a ? when using the date between condition so that I can use this report often with different date ranges.
The issue is, when I use OR conditions I end up having to put in the date range 4 times, or entering 8 different dates. Is there someway to simplify the below, so that I don't have to enter in the date as many times?
Select CFF2X AS TYPE, Count(*) AS COUNT
FROM ZBP602F.SIH
JOIN ZBP602UF.NRCMCVL1 ON SICUST=CUSTX
WHERE SIINVD BETWEEN ? AND ? AND SICOMP IN (01, 03, 06) and SITOT <> 0 and CFF2X <> (' ') AND IHOCLS IN (004, 800, 007, 100, 008, 102, 104, 140, 105, 110, 111,109)
OR SIINVD BETWEEN ? AND ? AND SICOMP IN (01, 03, 06) and SITOT <> 0 and CFF2X = (' ') AND IHOCLS IN (004, 800, 007, 008, 100, 102, 104, 140, 105, 110, 111)
OR SIINVD BETWEEN ? AND ? AND SICOMP IN (01, 03, 06) and SITOT <> 0 AND IHOCLS IN (114) and SIPAY in ('A', 'E')
OR SIINVD BETWEEN ? AND ? AND SICOMP IN (01, 03, 06) and SITOT <> 0 AND IHOCLS IN (114) and SIPAY = 'B' and CFF2X <> (' ')
Group by CFF2X
WHERE SIINVD BETWEEN ? AND ? AND SICOMP IN (01, 03, 06) AND SITOT <> 0
AND (
(CFF2X <> ' ' AND
IHOCLS IN (004, 800, 007, 100, 008, 102, 104, 140, 105, 110, 111,109)
)
OR (CFF2X = ' ' AND
IHOCLS IN (004, 800, 007, 008, 100, 102, 104, 140, 105, 110, 111)
)
OR (IHOCLS = 114 and SIPAY in ('A', 'E'))
OR (IHOCLS = 114 and SIPAY = 'B' and CFF2X <> ' ')
)
You're simply putting the unchanging items in once and then putting the changing bits in as a couple of OR statements. The process of simplifying boolean logic like this is called Logic Reduction (think Karnaugh maps). There are additional optimization you can perform here as well.
I'd try using some parentheses to prevent the repetitiveness.
If you change the WHERE to something like:
WHERE (SIINVD BETWEEN ? AND ? AND SICOMP IN (01, 03, 06) and SITOT <> 0) AND
(
(CFF2X <> (' ') AND IHOCLS IN (004, 800, 007, 100, 008, 102, 104, 140, 105, 110, 111,109)) OR
(CFF2X = (' ') AND IHOCLS IN (004, 800, 007, 008, 100, 102, 104, 140, 105, 110, 111)) OR
(IHOCLS IN (114) and SIPAY in ('A', 'E')) OR
(IHOCLS IN (114) and SIPAY = 'B' and CFF2X <> (' '))
)
;
This will check the SIINVD, SICOMP, and SITOT for all records first being that it is the same query for all. Then the 4 different cases will be matched. The current query as you have it may not actually do what you think because of the lack of parenthesis.