SQL - Syntax Error (Problems with PK and FK) - sql

I a want to fill a DB with SQL, but I get syntax errors. I think the problem is in PK and FK. Can you please take a look, what am I doing wrong in my approach.
Thank you very much!
SQL Code
CREATE TABLE TheOrder
(
ordereditem VARCHAR(50) NOT NULL PRIMARY KEY,
productid INTEGER NOT NULL,
productwholesaler VARCHAR (50) NOT NULL
);
INSERT INTO TheOrder VALUES
('overwatch', 1, 'blizzard'),
('deathadder', 2, 'razer'),
('xboxcontroller', 3, 'microsoft'),
('kraken', 4, 'razer'),
('diablo3', 5, 'blizzard'),
('warcraft3remastered', 6, 'blizzard'),
('fallout3', 7, 'bethesda'),
('pixel', 8, 'google'),
('aspire5', 9, 'acer'),
('destiny2', 10 ,'activision');
CREATE TABLE TheStock
(
counteditems INTEGER NOT NULL,
isInStock BOOLEAN NOT NULL,
stockID INTEGER NOT NULL PRIMARY KEY,
fkordereditem VARCHAR,
CONSTRAINT fkordereditem FOREIGN KEY (fkordereditem) REFERENCES TheOrder(ordereditem)
);
INSERT INTO TheStock VALUES
('overwatch', 5, true, 1),
('deathadder', 2, true, 2),
('xboxcontroller', 0, false, 3),
('kraken', 0, false, 4),
('diablo3', 5, true, 5),
('warcraft3remastered', 0, false, 6),
('fallout3', 7, true, 7),
('pixel', 8, true, 8),
('aspire5', 0, false, 9),
('destiny2', 10 , true, 10);
CREATE TABLE TheReorder
(
fkproductid VARCHAR,
fkproductwholesaler VARCHAR,
howmanydays INTEGER NOT NULL,
mobilenumber INTEGER PRIMARY KEY,
postcode INTEGER
CONSTRAINT fkproductid FOREIGN KEY (fkproductid) REFERENCES TheOrder(productid),
CONSTRAINT fkproductwholesaler FOREIGN KEY (fkproductwholesaler) REFERENCES TheOrder (productwholesaler)
);
INSERT INTO TheReorder VALUES
('overwatch', 'blizzard', 0, 649494, 1020),
('deathadder', 'razer', 0, 498494, 1150),
('xboxcontroller', 'microsoft', 3, 948849, 71000),
('kraken', 'razer', 5, 249489, 32009),
('diablo3', 'blizzard', 0, 194984, 29000),
('warcraft3remastered', 'blizzard', 12, 398484, 11000),
('fallout3', 'bethesda', 0, 694895, 42132),
('pixel', 'google', 0, 873243, 9201),
('aspire5', 'acer', 15, 988564, 10020),
('destiny2', 'activision', 0, 745637, 10090);
CREATE TABLE TheCourier
(
couriername VARCHAR(50) NOT NULL PRIMARY KEY,
couriercompany VARCHAR (50) NOT NULL,
courieravailable BOOLEAN NOT NULL
);
INSERT INTO TheCourier VALUES
('jon snow', 'the wall', false),
('ned stark', 'winterfell', false),
('elon musk', 'space x', true),
('yusaku maezawa', 'zozo', false),
('hokusai katsushika', 'the great wave', false),
('ragnar lothbrok', 'vikings', false),
('jax teller', 'sons of anarchy', true),
('harvey specter', 'post', true),
('michael corleone', 'ups', false),
('tommy shelby', 'fedex', false);
CREATE TABLE ThePenaltyInfo
(
fkhowmanydays INTEGER,
fkproductidpen INTEGER,
feetopay INTEGER NOT NULL,
currencie VARCHAR(50) NOT NULL,
penaltyid INTEGER NOT NULL PRIMARY KEY,
CONSTRAINT fkhowmanydays FOREIGN KEY (fkhowmanydays) REFERENCES TheReorder(howmanydays),
CONSTRAINT fkproductidpen FOREIGN KEY (fkproductidpen) REFERENCES TheOrder(productid)
);
INSERT INTO ThePenaltyInfo VALUES
(0, 1, 0, 'euro', 1),
(0, 2, 0, 'euro', 2),
(3, 3, 50, 'usd', 3),
(5, 4, 90, 'usd', 4),
(0, 5, 0, 'ron', 5),
(12, 6, 30, 'ron', 6),
(0, 7, 0, 'euro', 7),
(0, 8, 0, 'euro', 8),
(15, 9, 800, 'euro', 9),
(0, 10, 0, 'euro', 10);
CREATE TABLE TheShipementList
(
fkcouriercompany VARCHAR,
fkproductidship VARCHAR,
shipementid INTEGER NOT NULL PRIMARY KEY,
shipementaddress VARCHAR(50) NOT NULL,
customernumber INTEGER NOT NULL,
CONSTRAINT fkcouriercompany FOREIGN KEY (fkcouriercompany) REFERENCES TheCourier(couriercompany),
CONSTRAINT fkproductidship FOREIGN KEY (fkproductidship) REFERENCES TheOrder(productid)
);
INSERT INTO TheShipementList VALUES
('space x', 1, 1, 'Palma de Mallorca', 1994007),
('space x', 2, 1, 'Palma de Mallorca', 1994007),
('space x', 3, 1, 'Palma de Mallorca', 1994007),
('space x', 4, 1, 'Palma de Mallorca', 1994007),
('space x', 5, 1, 'Palma de Mallorca', 1994007),
('space x', 6, 1, 'Palma de Mallorca', 1994007),
('space x', 7, 1, 'Palma de Mallorca', 1994007),
('space x', 8, 1, 'Palma de Mallorca', 1994007),
('space x', 9, 1, 'Palma de Mallorca', 1994007),
('space x', 10, 1, 'Palma de Mallorca',1994007);
ERROR MESSAGE
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 '
CONSTRAINT FK FOREIGN KEY (fkordereditem) REFERENCES TheOrder(ordereditem) ' at line 6

You have multiple problems :
you forgot a comma after postcode INTEGER
You forgot to specify the length for some VARCHAR column definitions (this is mandatory, if you don't want to specify a length use VARCHAR(MAX).
column order in some INSERT statements does not match the table definition (you need to respect the order, or specify the column names in the INSERT statement)
you are trying to set foreign keys that references non primary key, if you want to do this you need to define referenced columns as UNIQUE
it seems you are trying to insert duplicates for some primary key values
you are referencing foreign keys with different data types then your column

I found the error. The assigned types have not been the same for the FK and for the referenced attribute.
CREATE TABLE TheOrder
(
productid INTEGER NOT NULL PRIMARY KEY,
ordereditem VARCHAR(50) NOT NULL,
productwholesaler VARCHAR (50) NOT NULL
);
CREATE TABLE TheStock
(
counteditems INTEGER NOT NULL,
isInStock BOOLEAN NOT NULL,
stockID INTEGER NOT NULL PRIMARY KEY,
testid INTEGER,
CONSTRAINT testid FOREIGN KEY (testid) REFERENCES TheOrder(productid)
);

Related

How Can i fix error Title cannot contain "Error: near "PRIMARY": syntax error" [duplicate]

This question already has answers here:
SQLite add Primary Key
(10 answers)
Closed 1 year ago.
I made export database from phpmyadmin to file. I want to import database from file sql in DB browser for SQlite. I have
error: near "PRIMARY": syntax error. I am beginner.
CREATE TABLE "uzytkownicy" (
`id` int(11) NOT NULL,
`user` text COLLATE utf8_polish_ci NOT NULL,
`pass` text COLLATE utf8_polish_ci NOT NULL,
`email` text COLLATE utf8_polish_ci NOT NULL,
`drewno` int(11) NOT NULL,
`kamien` int(11) NOT NULL,
`zboze` int(11) NOT NULL,
`dnipremium` int(11) NOT NULL
);
INSERT INTO `uzytkownicy` (`id`, `user`, `pass`, `email`, `drewno`, `kamien`, `zboze`, `dnipremium`) VALUES
(11, 'jonasz', '$2y$10$bS33in2FUkc.vsUpP0ZQl.UyX.cEfhPhGL8h41plJ2awGAhruidXO', 'jonasz#gmail.com', 100, 100, 100, 14),
(12, 'jon', '$2y$10$Mrt6deeNqDWeVXfu4b2myuyqHJZ4iBBBvG9Gp6bT2LMNqK1Y4ubWC', 'jon#gmail.com', 100, 100, 100, 14),
(13, 'jonaszekek', '$2y$10$8cfbUoRdJudHG85WAWvph.fh1sCChrzBLv01tRXBFKxW7kpjT3kVy', 'jonaszekek#gmail.com', 100, 100, 100, 14),
(14, 'jonaszafa', '$2y$10$8PiG8XM/mrs5JEARozVKMu7NXtePoLbAUJ5jnuJjQ1ebVVfsLvoO6', 'jonasz131#gmail.com', 100, 100, 100, 14),
(15, 'joners', '$2y$10$mRn8GrtRXwPu/HtAsqLdN.dvA.W3ZrfkemYbDiwwzHXF8ks4IoO8q', 'jonasz1131#gmail.com', 100, 100, 100, 14),
(16, 'jjaafa', '$2y$10$U/oRS5SJpFKxmp8Yv93moue6nuEV1ZOJYFGKYeaQBHTNkwew4.huu', 'fafafs1131#gmail.com', 100, 100, 100, 14),
(17, 'dadasd', '$2y$10$6Ka7oda8BCxhMUuHzJBaeOcNtLPTZ7Xagu12Qp2n2OqjzdTso48G.', 's1131#gmail.com', 100, 100, 100, 14),
(18, 'agas', '$2y$10$u5bCb1YnRJ5fRETceaeYeO.KT3FnT2.QLyIhPQuXScd7pe74c/SUq', 'dadas1#gmail.com', 100, 100, 100, 14),
(19, 'jdada', '$2y$10$aUCoZ5TVElpUxnJZdOCiXuJPR687pPy8./nMWejtMy9Td743sKde.', 'faf3121#gmail.com', 100, 100, 100, 14),
(20, 'dad', '$2y$10$9P9L.eiVmdIfZ5.iNSuoaeutSqPwmFJF/NYG7UFj.HN45sDu9gWoa', 'fa131#gmail.com', 100, 100, 100, 14),
(21, 'dafas', '$2y$10$0LisKP3Fir5BBRuBXYEoA./mOazARccFg.31QLfHdZbwCqmzJTnei', 'f12131#gmail.com', 100, 100, 100, 14),
(22, 'adsdgs', '$2y$10$P2HvhD.UYP0jhOiIv4UXeOTmYCmWyXZav.6r6p0IAnCQbHRP5BPIi', '876595h#gmail.com', 100, 100, 100, 14);
ALTER TABLE `uzytkownicy`
ADD PRIMARY KEY (`id`),
ADD UNIQUE KEY `id` (`id`);
ALTER TABLE `uzytkownicy`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=23;
COMMIT;
In sqlite you can NOT add constraint (FK or PK) after creation of table
you have to make a copy of data into another data , drop your table and recreate it with all constraint needed and insert back the data.
BEGIN;
CREATE TABLE uzytkownicy_new (
[...],
primary Key (id)
);
INSERT INTO uzytkownicy_new SELECT * FROM uzytkownicy;
DROP TABLE uzytkownicy;
ALTER TABLE uzytkownicy_new RENAME TO uzytkownicy;
COMMIT;

Get the right data for a specific id

I want to get the prices for each product for a specific place. Here's what I have done so far.
SELECT pl.id AS place_id,
pl.data_name AS place_name,
pp.data_price AS product_price,
pp.date_updated AS price_updated
FROM places AS pl
JOIN products AS pr
ON pl.id = pr.id_place
JOIN products_prices AS pp
WHERE pp.id_product = '30'
GROUP BY pl.id, pp.data_price, pp.date_updated
ORDER BY pp.data_price DESC, pp.date_updated DESC
As you can see from the image above, product_price and price_updated are all the same. place_name also shows 3 places that does not have the product with id 30.
Here's how it should looks like (cheapest, most recent product purchase on top):
place_id place_name product_price price_updated
3 ICA Maxi 4.95 2018-05-16
1 ICA Supermarket 5.90 2018-05-27
26 ICA Skutan 6.50 2018-05-29
Here's the database structure:
CREATE TABLE IF NOT EXISTS `places` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`data_name` tinytext NOT NULL,
`data_address` text,
`data_address_city` tinytext NOT NULL,
`data_coordinates` varchar(50) NOT NULL,
UNIQUE KEY `id` (`id`)
);
INSERT INTO `places` (`id`, `data_name`, `data_address`, `data_address_city`, `data_coordinates`) VALUES
(1, 'ICA Supermarket', 'Björkhagsgatan 9', 'Skoghall', '59.324971,13.467291'),
(2, 'ICA Maxi', 'Bergviks Köpcentrum', 'Karlstad', '59.376563,13.428787'),
(3, 'ICA Kvantum', 'Bivägen 11', 'Hammarö', '59.343388,13.504583'),
(4, 'IKEA', 'Bergviksvägen 43', 'Karlstad', '59.379032,13.420646'),
(5, 'Karlstad Naprapatklinik', 'Västra Torggatan 15', 'Karlstad', '59.381379,13.501683'),
(9, 'Besök i Borgvik AB', '', 'Borgvik', '59.348261,12.954707'),
(23, 'Mariebergsskogen', '', 'Karlstad', '59.369403,13.486485'),
(24, 'Happy Price', 'Brehogsvägen 20', 'Tanumshede', '58.723730,11.344768'),
(25, 'Trekanten Kök & Bar', 'Parkvägen 2', 'Hamburgsund', '58.552733,11.270998'),
(26, 'ICA Skutan', 'Strandvägen', 'Hamburgsund', '58.552122,11.270898');
CREATE TABLE IF NOT EXISTS `products` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`id_place` int(11) NOT NULL,
`data_barcode` text NOT NULL,
`data_name` text NOT NULL,
`data_weight` text NOT NULL,
`data_weight_type` text NOT NULL,
UNIQUE KEY `id` (`id`)
);
INSERT INTO `products` (`id`, `id_place`, `data_barcode`, `data_name`, `data_weight`, `data_weight_type`) VALUES
(7, 3, '7311070008494', 'Pågen Lantbröd', '650', 'g'),
(8, 3, '7310618084808', 'Grumme citronsåpa', '750', 'ml'),
(9, 3, '7318690079835', 'ICA Basic toalettpapper', '', ''),
(12, 1, '7318690134640', 'ICA Basic Milk & Caramel', '100', 'g'),
(18, 3, '7310380512103', 'ICA Home dishmatic refillsvamp', '', ''),
(19, 3, '7340109200684', 'Plastkasse', '', ''),
(20, 1, '7310751163903', 'ICA tonfisk filébitar i vatten', '185', 'g'),
(24, 1, '7310865001818', 'Arla mellanmjölk', '1.5', 'kg'),
(25, 1, '7318690079712', 'ICA kattsand (klumpbildande, ej parfym)', '6', 'kg'),
(29, 26, '3068320055008', 'Evian mineralvatten', '500', 'mL'),
(30, 26, '7318690134640', 'ICA Basic Milk & Caramel', '100', 'g'),
(33, 2, '0', 'ICA Basic Milk & Caramel', '100', 'g');
CREATE TABLE IF NOT EXISTS `products_prices` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`id_product` int(11) NOT NULL,
`id_transaction` int(11) NOT NULL,
`data_price` decimal(10,2) NOT NULL,
`data_amount` tinytext NOT NULL,
`data_discount` tinytext,
`data_discount_amount` tinytext,
`data_discount_sum` tinytext,
`data_pant` tinytext,
`date_updated` date NOT NULL,
UNIQUE KEY `id` (`id`)
);
INSERT INTO `products_prices` (`id`, `id_product`, `id_transaction`, `data_price`, `data_amount`, `data_discount`, `data_discount_amount`, `data_discount_sum`, `data_pant`, `date_updated`) VALUES
(1, 1, 907, 18.90, '4', '', '2', '30', NULL, '2018-05-18'),
(2, 12, 907, 4.90, '4', '', '', '', NULL, '2018-05-18'),
(5, 4, 904, 18.90, '1', '', '', '', NULL, '2018-05-18'),
(6, 14, 904, 14.90, '1', '', '', '', NULL, '2018-05-18'),
(17, 19, 936, 2.00, '1', '', '', '', NULL, '2018-05-21'),
(18, 21, 947, 23.90, '1', '', '', '', NULL, '2018-05-23'),
(19, 22, 947, 10.50, '1', '', '', '', NULL, '2018-05-23'),
(20, 20, 947, 15.90, '1', '', '', '', NULL, '2018-05-23'),
(21, 13, 947, 49.90, '1', '', '', '', NULL, '2018-05-23'),
(22, 23, 948, 14.90, '1', '', '', '', NULL, '2018-05-24'),
(23, 24, 961, 14.90, '1', NULL, NULL, NULL, NULL, '2018-05-27'),
(24, 12, 961, 5.90, '4', NULL, NULL, NULL, NULL, '2018-05-27'),
(32, 28, 967, 8.50, '2', NULL, NULL, NULL, NULL, '2018-05-28'),
(33, 29, 972, 12.90, '1', NULL, NULL, NULL, '1', '2018-05-29'),
(34, 30, 973, 6.50, '2', NULL, NULL, NULL, NULL, '2018-05-29'),
(35, 31, 976, 10.00, '1', NULL, NULL, NULL, NULL, '2018-05-30'),
(36, 32, 976, 10.00, '1', NULL, NULL, NULL, NULL, '2018-05-30');
As mentioned in my comment to your request: You forgot the ON clause that links the products_prices table to the other two.
MariaDB nastily converts your inner join into a cross join instead of raising an error as it should. What this gives you is: select all prices for product 30 combined with all places multiplied with the number of products therein, instead of just combined with the product's place.
By the way: I'd put table products first in the FROM clause, because this is the base (you want product 30). Then join the product's prices and the product's place.
There is no reason to group your data, as you only want to list the prices. You are not aggregating anything. So you get:
SELECT
pl.id AS place_id,
pl.data_name AS place_name,
pp.data_price AS product_price,
pp.date_updated AS price_updated
FROM products AS pr
JOIN products_prices AS pp ON pp.id_product = pr.id
JOIN places pl ON pl.id = pr.id_place
WHERE pr.id = 30
ORDER BY pp.data_price DESC, pp.date_updated DESC;
Try this:
SELECT PL.id AS place_id,
PL.data_name AS place_name,
PP.data_price AS product_price,
PP.date_updated AS price_updated
FROM products_prices PP
JOIN products PR
ON PR.id = PP.id_product
JOIN places PL
ON PL.id = PR.id_place
WHERE PR.id = '30'

How can I prevent SQL Server from squaring the number of rows scanned?

I'm running a query over a table variable that holds 22 227 rows. The query used to take 2-3 seconds to complete (which I still think is too slow) but since I added another field to the ORDER BY clause in DENSE_RANK() it now completes in 4.5 minutes!
If I include [t2].[aisdt] with or without [t2].[aiID], the execution plan shows that it's scanning 494 039 529 rows, which is 22 227 squared. The following query generates the correct results, just much too slowly to be useful.
SELECT MAX([t].[SetNum]) OVER (PARTITION BY NULL) AS [MaxSet]
,*
FROM (
SELECT DENSE_RANK() OVER (ORDER BY [t2].[aisdt], [t2].[aiID]) AS [SetNum]
,[t2].*
FROM (
SELECT [aiID]
,COUNT(DISTINCT [acID]) AS [noac]
FROM #Temp
GROUP BY [aiID]
) [t1]
JOIN #Temp [t2]
ON [t2].[aiID] = [t1].[aiID]
WHERE [t1].[noac] < [t2].[asm]
) [t]
Just to be clear, the culprit is the bold section in "DENSE_RANK() OVER (ORDER BY [t2].[aisdt], [t2].[aiID])". Removing this field (which needs to remain) drops the execution time back down to 2-3 seconds. I think it might have something to do with JOINing the table to itself on [aiID] but not [aisdt].
How can I speed this query up to complete in the same time as before, or less?
EDIT
Table definition:
DECLARE #Temp TABLE (
[aiID] INT NOT NULL INDEX [IX_Temp_aiID] -- not unique
,[aisdt] DATETIME NOT NULL INDEX [IX_Temp_aisdt] -- not unique
,[asm] INT NOT NULL
,[cpcID] INT NULL
,[cpce] VARCHAR(10) NULL
,[acID] INT NULL
,[ctvID] INT NULL
,[ct] VARCHAR(100) NULL
,[_36_other_non_matched_fields_] VARCHAR(MAX)
,UNIQUE ([aiID], [cpcID], [cpce], [acID], [ctvID], [ct])
)
[aisdt] is unique per [aiID], but there can be multiple [aiID]s with the same [aisdt].
INSERT INTO #TEMP
VALUES (64, '2017-03-23 10:00:00', 1, 17, '', NULL, NULL, NULL, 'blah')
,(64, '2017-03-23 10:00:00', 1, 34, '', NULL, NULL, NULL, 'blah')
,(99, '2017-04-08 09:00:00', 1, 25, 'Y', NULL, NULL, NULL, 'blah')
,(99, '2017-04-08 09:00:00', 1, 16, 'Y', NULL, NULL, NULL, 'blah')
,(99, '2017-04-08 09:00:00', 1, 76, 'Y', NULL, NULL, NULL, 'blah')
,(99, '2017-04-08 09:00:00', 1, 82, 'Y', NULL, NULL, NULL, 'blah')
,(42, '2017-04-14 16:00:00', 2, 32, '', 32, NULL, NULL, 'blah')
,(42, '2017-04-14 16:00:00', 2, 32, '', 47, NULL, NULL, 'blah')
,(42, '2017-04-14 16:00:00', 2, 47, '', 32, NULL, NULL, 'blah')
,(42, '2017-04-14 16:00:00', 2, 47, '', 47, NULL, NULL, 'blah')
,(54, '2017-03-23 10:00:00', 1, 17, '', NULL, NULL, NULL, 'blah')
,(54, '2017-03-23 10:00:00', 1, 34, '', NULL, NULL, NULL, 'blah')
,(89, '2017-04-08 09:00:00', 1, 25, 'Y', NULL, NULL, NULL, 'blah')
,(89, '2017-04-08 09:00:00', 1, 16, 'Y', NULL, NULL, NULL, 'blah')
,(89, '2017-04-08 09:00:00', 1, 76, 'Y', NULL, NULL, NULL, 'blah')
,(89, '2017-04-08 09:00:00', 1, 82, 'Y', NULL, NULL, NULL, 'blah')
,(32, '2017-04-14 16:00:00', 3, 32, '', 32, NULL, NULL, 'blah')
,(32, '2017-04-14 16:00:00', 3, 32, '', 47, NULL, NULL, 'blah')
,(32, '2017-04-14 16:00:00', 3, 47, '', 32, NULL, NULL, 'blah')
,(32, '2017-04-14 16:00:00', 3, 47, '', 47, NULL, NULL, 'blah')
It must be sorted by [aisdt] (datetime) first, then [aiID], then numbered into sets based on [aiID].
I want to see:
5, 1, 54, '2017-03-23 10:00:00', 1, 17, '', NULL, NULL, NULL, 'blah'
5, 1, 54, '2017-03-23 10:00:00', 1, 34, '', NULL, NULL, NULL, 'blah'
5, 2, 64, '2017-03-23 10:00:00', 1, 17, '', NULL, NULL, NULL, 'blah'
5, 2, 64, '2017-03-23 10:00:00', 1, 34, '', NULL, NULL, NULL, 'blah'
5, 3, 89, '2017-04-08 09:00:00', 1, 25, 'Y', NULL, NULL, NULL, 'blah'
5, 3, 89, '2017-04-08 09:00:00', 1, 16, 'Y', NULL, NULL, NULL, 'blah'
5, 3, 89, '2017-04-08 09:00:00', 1, 76, 'Y', NULL, NULL, NULL, 'blah'
5, 3, 89, '2017-04-08 09:00:00', 1, 82, 'Y', NULL, NULL, NULL, 'blah'
5, 4, 99, '2017-04-08 09:00:00', 1, 25, 'Y', NULL, NULL, NULL, 'blah'
5, 4, 99, '2017-04-08 09:00:00', 1, 16, 'Y', NULL, NULL, NULL, 'blah'
5, 4, 99, '2017-04-08 09:00:00', 1, 76, 'Y', NULL, NULL, NULL, 'blah'
5, 4, 99, '2017-04-08 09:00:00', 1, 82, 'Y', NULL, NULL, NULL, 'blah'
5, 5, 32, '2017-04-14 16:00:00', 3, 32, '', 32, NULL, NULL, 'blah'
5, 5, 32, '2017-04-14 16:00:00', 3, 32, '', 47, NULL, NULL, 'blah'
5, 5, 32, '2017-04-14 16:00:00', 3, 47, '', 32, NULL, NULL, 'blah'
5, 5, 32, '2017-04-14 16:00:00', 3, 47, '', 47, NULL, NULL, 'blah'
The main idea is taken from Partition Function COUNT() OVER possible using DISTINCT that #Jayvee pointed out with a small addition that would make it work when acID has NULL values.
Most likely you can remove all indexes from your #Temp table, the server will have to sort it in several different ways for different window functions anyway, but there is no self-join, so it should be faster.
The plan will have many sorts and they also can be slow, especially when engine underestimates the number of rows in a table. And table variable is exactly this case. Optimiser thinks that table variable has only 1 row. So, I'd recommend to use a classic #Temp table here, even without indexes.
An index on (aiID, acID) should help, but there will be other sorts any way.
WITH
CTE_Counts
AS
(
SELECT
*
-- use DENSE_RANK() to calculate COUNT(DISTINCT)
, DENSE_RANK() OVER (PARTITION BY [aiID] ORDER BY [acID])
+ DENSE_RANK() OVER (PARTITION BY [aiID] ORDER BY [acID] DESC)
-- subtract extra 1 if acID has NULL values within the partition
- MAX(CASE WHEN [acID] IS NULL THEN 1 ELSE 0 END) OVER (PARTITION BY [aiID])
- 1 AS [noac]
FROM #Temp
)
,CTE_SetNum
AS
(
SELECT
*
, DENSE_RANK() OVER (ORDER BY [aisdt], [aiID]) AS [SetNum]
FROM CTE_Counts
WHERE [noac] < [asm]
)
SELECT
*
, MAX([SetNum]) OVER () AS [MaxSet]
FROM CTE_SetNum
ORDER BY
[aisdt]
,[aiID]
,[SetNum]
;
Index as suggested in the comments would definitely play a major part but also I think you can re-write the query without self join in this way:
SELECT MAX([t].[SetNum]) OVER (PARTITION BY NULL) AS [MaxSet]
,*
FROM (
SELECT DENSE_RANK() OVER (ORDER BY [t1].[aisdt], [t1].[aiID]) AS [SetNum]
,[t1].*
FROM (
SELECT * ,dense_rank() over(partition by aiID order by [acID]) -
dense_rank() over(partition by aiID order by [acID]) - 1 AS [noac]
FROM #Temp
) [t1]
WHERE [t1].[noac] < [t1].[asm]
) [t]

insert data to contacts2.db

Why error in raw_contacts ?
ERROR :
SQLiteManager:
INSERT INTO "raw_contacts" VALUES(1,1,NULL,0,2,1,0,1,0,0,NULL,0,0,NULL,0,2147483647,'09999999999','09999999999',20,NULL,'0','09999999999','#',208,'09999999999','#',208,0,NULL,NULL,NULL,NULL); [ no such collation sequence: PHONEBOOK ]
Exception Name: NS_ERROR_FAILURE
Exception Message: Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [mozIStorageConnection.createStatement]
SQL statement for INSERT :
INSERT INTO "contacts"
VALUES(1, 1, NULL, NULL, NULL, 0, 0, 0, 0, 2147483647, 1, '0r1-1224142414221E16121C1C', NULL, 1448202599930);
INSERT INTO "data"
VALUES(1, NULL, 5, 1, 0, 0, 0, 0, '09999999999', '2', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
INSERT INTO "default_directory"
VALUES(1);
INSERT INTO "phone_lookup"
VALUES(1, 1, '09999999999', '5502681');
INSERT INTO "raw_contacts"
VALUES(1, 1, NULL, 0, 2, 1, 0, 1, 0, 0, NULL, 0, 0, NULL, 0, 2147483647, '09999999999', '09999999999', 20, NULL, '0', '09999999999', '#', 208, '09999999999', '#', 208, 0, NULL, NULL, NULL, NULL);
INSERT INTO "search_index"
VALUES(1, NULL, NULL, '09999999999');
INSERT INTO "visible_contacts"
VALUES(1);
Help me please :((
Sorry.My English is not good.
structure raw_contacts :
CREATE TABLE raw_contacts
(
_id INTEGER PRIMARY KEY AUTOINCREMENT,
account_id INTEGER REFERENCES accounts(_id),
sourceid TEXT,
raw_contact_is_read_only INTEGER NOT NULL DEFAULT 0,
version INTEGER NOT NULL DEFAULT 1,
dirty INTEGER NOT NULL DEFAULT 0,
deleted INTEGER NOT NULL DEFAULT 0,
contact_id INTEGER REFERENCES contacts(_id),
aggregation_mode INTEGER NOT NULL DEFAULT 0,
aggregation_needed INTEGER NOT NULL DEFAULT 1,
custom_ringtone TEXT,
send_to_voicemail INTEGER NOT NULL DEFAULT 0,
times_contacted INTEGER NOT NULL DEFAULT 0,
last_time_contacted INTEGER,
starred INTEGER NOT NULL DEFAULT 0,
pinned INTEGER NOT NULL DEFAULT 2147483647,
display_name TEXT, display_name_alt TEXT,
display_name_source INTEGER NOT NULL DEFAULT 0,
phonetic_name TEXT, phonetic_name_style TEXT,
sort_key TEXT COLLATE PHONEBOOK,
phonebook_label TEXT,
phonebook_bucket INTEGER,
sort_key_alt TEXT COLLATE PHONEBOOK,
phonebook_label_alt TEXT,
phonebook_bucket_alt INTEGER,
name_verified INTEGER NOT NULL DEFAULT 0,
sync1 TEXT, sync2 TEXT, sync3 TEXT, sync4 TEXT
)
This table requires that the application using it has defined a collation named "PHONEBOOK".
You have to either define this collation, or remove the COLLATE PHONEBOOK from the table definition and live with the different sorting order.
Change the single quotes on the VALUES section to double quotes
See this link
NS_ERROR_FAILURE in SQLite insert

How do I write this MySQL query to get the correct information? (Subquery, multiple subqueries)

Here is the query that I am working on:
SELECT `unitid`, `name` FROM apartmentunits
WHERE aptid = (
SELECT `aptid` FROM rentconditionsmap WHERE rentcondid = 1 AND condnum = 1
)
What I am having trouble figuring out is how to write this to add more rentcondition limiters to filter this list down more.
SELECT `aptid` FROM rentconditionsmap WHERE rentcondid = 1 AND condnum = 1
Data:
CREATE TABLE IF NOT EXISTS `rentconditionsmap` (
`rcid` bigint(10) unsigned NOT NULL AUTO_INCREMENT,
`rentcondid` int(3) unsigned NOT NULL,
`condnum` tinyint(3) unsigned NOT NULL,
`aptid` bigint(10) unsigned DEFAULT NULL,
PRIMARY KEY (`rcid`), KEY `aptid` (`aptid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=18 ;
INSERT INTO `rentconditionsmap`
(`rcid`, `rentcondid`, `condnum`, `aptid`)
VALUES
(1, 1, 1, 1),
(2, 2, 1, 1),
(3, 3, 0, 1),
(4, 4, 1, 1),
(5, 8, 0, 1);
CREATE TABLE IF NOT EXISTS `apartmentunits` (
`unitid` bigint(10) NOT NULL AUTO_INCREMENT,
`aptid` bigint(10) NOT NULL,
`name` varchar(6) NOT NULL,
`verified` tinyint(1) NOT NULL DEFAULT '0',
`rentcost` int(4) unsigned DEFAULT NULL,
`forrent` tinyint(1) NOT NULL DEFAULT '0',
`unittypekey` varchar(2) DEFAULT NULL,
`sqft` smallint(6) DEFAULT NULL,
PRIMARY KEY (`unitid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=121 ;
INSERT INTO `apartmentunits`
(`unitid`, `aptid`, `name`, `verified`, `rentcost`, `forrent`, `unittypekey`, `sqft`)
VALUES
(1, 1, '3', 1, 540, 0, '2B', NULL),
(2, 1, '5', 1, NULL, 0, '2B', NULL),
(3, 1, '7', 1, NULL, 0, '2B', NULL),
(53, 1, '1', 1, NULL, 0, '2B', NULL),
(54, 1, '2', 1, NULL, 0, '2B', NULL),
(55, 1, '4', 1, 570, 0, '2B', NULL),
(56, 1, '6', 1, NULL, 0, '2B', NULL),
(57, 1, '8', 1, NULL, 0, '2B', NULL),
(58, 1, '9', 1, NULL, 0, '2B', NULL),
(59, 1, '10', 1, NULL, 0, '2B', NULL),
(60, 1, '11', 1, NULL, 0, '2B', NULL);
As Eric J said as a comment:
Try changing = to IN
SELECT `unitid`, `name` FROM apartmentunits
WHERE `aptid` IN (
SELECT `aptid` FROM rentconditionsmap WHERE rentcondid = 1 AND condnum = 1
)
why not:
SELECT unitid, name
FROM apartmentunits a
INNER JOIN rentconditionsmap r on a.aptid = r.aptid
WHERE (rentcondid = 1 and condnum = 1) OR (rentcondid = 2 and condnum = 2)
Using ANSI-92 join syntax:
SELECT au.unitid,
au.name
FROM APARTMENTUNITS au
JOIN RENTCONDITIONSMAP rcm ON rcm.aptid = au.aptid
AND rcm.rentcondid = 1
AND rcm.condnum = 1
Use a JOIN (below is TSQL sintax for a join, or you can use the explicit INNER JOIN).
SELECT apartmentunits.unitid, apartmentunits.name
FROM apartmentunits, rentconditionsmap
WHERE apartmentunits.aptid = rentconditionsmap.aptid
AND rentconditionsmap.rentcondid = 1
AND rentconditionsmap.condnum = 1
-- AND whatever else...