This question already has answers here:
inserting multiple rows with one insert command
(5 answers)
Closed 4 years ago.
I make a script that generates insert statements from SQL data. When I insert just one line of the insert it works but when I try to insert more than one row I get ORA-00933: SQL command not properly ended here is the insert statement:
This works by itself:
INSERT INTO CAMPAIGN (CAMPAIGN_ID, SHOP_ID, CAMPAIGN_TYPE, SORT_ORDER, STATUS, VALID_FROM, VALID_TILL, CREATED_AT, MODIFIED_AT, CUSTOM_GRID_LAYOUT_CSS, IMAGE_URL, KEY, SHOW_PRODUCTS_FILTER, MOBILE_APP_IMAGE_URL, LAYOUT_ID, SHOW_OWN_BRAND, VALIDATION_STATUS, CAMPAIGN_USAGE_ID, STORE_END_DATE, PAGING_ALLOWED, CAROUSEL_BUTTON_TEXT_COLOR, CAROUSEL_BUTTON_BG_COLOR, CAROUSEL_BUTTON1_LABEL, CAROUSEL_BUTTON1_URL, CAROUSEL_BUTTON2_LABEL, CAROUSEL_BUTTON2_URL, CAROUSEL_HTML_OVERLAY, MOBILE_APP_TEASER_URL, CAROUSEL_CLAIM_URL, CAROUSEL_HERO_URL, CAROUSEL_BOX_COLOR, AVOID_CAMPAIGN_NAV_TEASER, CAROUSEL_BUTTON1_NEW_TAB, CAROUSEL_BUTTON2_NEW_TAB, SALE_QUALIFICATION_ID, SALE_TARGET_CAMPAIGN) VALUES (CAMPAIGN_SEQ.nextval, 1, 'C', 0, 'A', to_date('2016-11-23', 'YYYY-MM-DD'), to_date('2116-11-23', 'YYYY-MM-DD'), to_date('2016-11-23', 'YYYY-MM-DD'), to_date('2018-07-02', 'YYYY-MM-DD'), null, null, null, 1, null, '5_COL', 0, null, 4, to_date('2116-11-23', 'YYYY-MM-DD'), 0, null, null, 'Artikel ab Mo., 02.12.', 'https://www.lidl.de', null, null, null, null, null, null, null, 0, 0, 0, null, 0);
Now I add another row to it separating it by a , character and ending the whole statement with a ; character.
INSERT INTO CAMPAIGN (CAMPAIGN_ID, SHOP_ID, CAMPAIGN_TYPE, SORT_ORDER, STATUS, VALID_FROM, VALID_TILL, CREATED_AT, MODIFIED_AT, CUSTOM_GRID_LAYOUT_CSS, IMAGE_URL, KEY, SHOW_PRODUCTS_FILTER, MOBILE_APP_IMAGE_URL, LAYOUT_ID, SHOW_OWN_BRAND, VALIDATION_STATUS, CAMPAIGN_USAGE_ID, STORE_END_DATE, PAGING_ALLOWED, CAROUSEL_BUTTON_TEXT_COLOR, CAROUSEL_BUTTON_BG_COLOR, CAROUSEL_BUTTON1_LABEL, CAROUSEL_BUTTON1_URL, CAROUSEL_BUTTON2_LABEL, CAROUSEL_BUTTON2_URL, CAROUSEL_HTML_OVERLAY, MOBILE_APP_TEASER_URL, CAROUSEL_CLAIM_URL, CAROUSEL_HERO_URL, CAROUSEL_BOX_COLOR, AVOID_CAMPAIGN_NAV_TEASER, CAROUSEL_BUTTON1_NEW_TAB, CAROUSEL_BUTTON2_NEW_TAB, SALE_QUALIFICATION_ID, SALE_TARGET_CAMPAIGN) VALUES
(CAMPAIGN_SEQ.nextval, 1, 'C', 0, 'A', to_date('2016-11-23', 'YYYY-MM-DD'), to_date('2116-11-23', 'YYYY-MM-DD'), to_date('2016-11-23', 'YYYY-MM-DD'), to_date('2018-07-02', 'YYYY-MM-DD'), null, null, null, 1, null, '5_COL', 0, null, 4, to_date('2116-11-23', 'YYYY-MM-DD'), 0, null, null, 'Artikel ab Mo., 02.12.', 'https://www.lidl.de', null, null, null, null, null, null, null, 0, 0, 0, null, 0),
(CAMPAIGN_SEQ.nextval, 1, 'P', 0, 'A', to_date('2016-11-09', 'YYYY-MM-DD'), to_date('2116-11-09', 'YYYY-MM-DD'), to_date('2016-11-09', 'YYYY-MM-DD'), to_date('2018-07-02', 'YYYY-MM-DD'), null, null, null, 0, null, '4_COL_LEFT_NAV', 1, null, null, null, 1, null, null, null, null, null, null, null, null, null, null, null, 0, 0, 0, null, 0);
The above one will give me the error. Correct me if I am wrong but inserting multiple rows is allowed like this, correct?
Use insert all.
Below is the example.
INSERT ALL
INTO suppliers (supplier_id, supplier_name) VALUES (20, 'Google')
INTO suppliers (supplier_id, supplier_name) VALUES (21, 'Microsoft')
INTO suppliers (supplier_id, supplier_name) VALUES (22, 'Apple')
If you look at the syntax diagram in the documentation, you'll see that there is no reverse arrow after the closing paren for values.
Two solutions. Two inserts:
INSERT INTO CAMPAIGN (CAMPAIGN_ID, SHOP_ID, CAMPAIGN_TYPE, SORT_ORDER, STATUS, VALID_FROM, VALID_TILL, CREATED_AT, MODIFIED_AT, CUSTOM_GRID_LAYOUT_CSS, IMAGE_URL, KEY, SHOW_PRODUCTS_FILTER, MOBILE_APP_IMAGE_URL, LAYOUT_ID, SHOW_OWN_BRAND, VALIDATION_STATUS, CAMPAIGN_USAGE_ID, STORE_END_DATE, PAGING_ALLOWED, CAROUSEL_BUTTON_TEXT_COLOR, CAROUSEL_BUTTON_BG_COLOR, CAROUSEL_BUTTON1_LABEL, CAROUSEL_BUTTON1_URL, CAROUSEL_BUTTON2_LABEL, CAROUSEL_BUTTON2_URL, CAROUSEL_HTML_OVERLAY, MOBILE_APP_TEASER_URL, CAROUSEL_CLAIM_URL, CAROUSEL_HERO_URL, CAROUSEL_BOX_COLOR, AVOID_CAMPAIGN_NAV_TEASER, CAROUSEL_BUTTON1_NEW_TAB, CAROUSEL_BUTTON2_NEW_TAB, SALE_QUALIFICATION_ID, SALE_TARGET_CAMPAIGN)
VALUES (CAMPAIGN_SEQ.nextval, 1, 'C', 0, 'A', to_date('2016-11-23', 'YYYY-MM-DD'), to_date('2116-11-23', 'YYYY-MM-DD'), to_date('2016-11-23', 'YYYY-MM-DD'), to_date('2018-07-02', 'YYYY-MM-DD'), null, null, null, 1, null, '5_COL', 0, null, 4, to_date('2116-11-23', 'YYYY-MM-DD'), 0, null, null, 'Artikel ab Mo., 02.12.', 'https://www.lidl.de', null, null, null, null, null, null, null, 0, 0, 0, null, 0);
INSERT INTO CAMPAIGN (CAMPAIGN_ID, SHOP_ID, CAMPAIGN_TYPE, SORT_ORDER, STATUS, VALID_FROM, VALID_TILL, CREATED_AT, MODIFIED_AT, CUSTOM_GRID_LAYOUT_CSS, IMAGE_URL, KEY, SHOW_PRODUCTS_FILTER, MOBILE_APP_IMAGE_URL, LAYOUT_ID, SHOW_OWN_BRAND, VALIDATION_STATUS, CAMPAIGN_USAGE_ID, STORE_END_DATE, PAGING_ALLOWED, CAROUSEL_BUTTON_TEXT_COLOR, CAROUSEL_BUTTON_BG_COLOR, CAROUSEL_BUTTON1_LABEL, CAROUSEL_BUTTON1_URL, CAROUSEL_BUTTON2_LABEL, CAROUSEL_BUTTON2_URL, CAROUSEL_HTML_OVERLAY, MOBILE_APP_TEASER_URL, CAROUSEL_CLAIM_URL, CAROUSEL_HERO_URL, CAROUSEL_BOX_COLOR, AVOID_CAMPAIGN_NAV_TEASER, CAROUSEL_BUTTON1_NEW_TAB, CAROUSEL_BUTTON2_NEW_TAB, SALE_QUALIFICATION_ID, SALE_TARGET_CAMPAIGN)
VALUES (CAMPAIGN_SEQ.nextval, 1, 'P', 0, 'A', to_date('2016-11-09', 'YYYY-MM-DD'), to_date('2116-11-09', 'YYYY-MM-DD'), to_date('2016-11-09', 'YYYY-MM-DD'), to_date('2018-07-02', 'YYYY-MM-DD'), null, null, null, 0, null, '4_COL_LEFT_NAV', 1, null, null, null, 1, null, null, null, null, null, null, null, null, null, null, null, 0, 0, 0, null, 0);
Or, use SELECT/UNION ALL:
INSERT INTO CAMPAIGN (CAMPAIGN_ID, SHOP_ID, CAMPAIGN_TYPE, SORT_ORDER, STATUS, VALID_FROM, VALID_TILL, CREATED_AT, MODIFIED_AT, CUSTOM_GRID_LAYOUT_CSS, IMAGE_URL, KEY, SHOW_PRODUCTS_FILTER, MOBILE_APP_IMAGE_URL, LAYOUT_ID, SHOW_OWN_BRAND, VALIDATION_STATUS, CAMPAIGN_USAGE_ID, STORE_END_DATE, PAGING_ALLOWED, CAROUSEL_BUTTON_TEXT_COLOR, CAROUSEL_BUTTON_BG_COLOR, CAROUSEL_BUTTON1_LABEL, CAROUSEL_BUTTON1_URL, CAROUSEL_BUTTON2_LABEL, CAROUSEL_BUTTON2_URL, CAROUSEL_HTML_OVERLAY, MOBILE_APP_TEASER_URL, CAROUSEL_CLAIM_URL, CAROUSEL_HERO_URL, CAROUSEL_BOX_COLOR, AVOID_CAMPAIGN_NAV_TEASER, CAROUSEL_BUTTON1_NEW_TAB, CAROUSEL_BUTTON2_NEW_TAB, SALE_QUALIFICATION_ID, SALE_TARGET_CAMPAIGN)
SELECT CAMPAIGN_SEQ.nextval, 1, 'C', 0, 'A', to_date('2016-11-23', 'YYYY-MM-DD'), to_date('2116-11-23', 'YYYY-MM-DD'), to_date('2016-11-23', 'YYYY-MM-DD'), to_date('2018-07-02', 'YYYY-MM-DD'), null, null, null, 1, null, '5_COL', 0, null, 4, to_date('2116-11-23', 'YYYY-MM-DD'), 0, null, null, 'Artikel ab Mo., 02.12.', 'https://www.lidl.de', null, null, null, null, null, null, null, 0, 0, 0, null, 0
FROM dual
UNION ALL
SELECT CAMPAIGN_SEQ.nextval, 1, 'P', 0, 'A', to_date('2016-11-09', 'YYYY-MM-DD'), to_date('2116-11-09', 'YYYY-MM-DD'), to_date('2016-11-09', 'YYYY-MM-DD'), to_date('2018-07-02', 'YYYY-MM-DD'), null, null, null, 0, null, '4_COL_LEFT_NAV', 1, null, null, null, 1, null, null, null, null, null, null, null, null, null, null, null, 0, 0, 0, null, 0
FROM dual;
Related
I'm calculating distance between people like below:
select (
point(
(select latlng from user_account where name = 'Manel')
)
<#>
point(
(select latlng from user_account where name = 'Ben')
)
) as distance
which will then output like this:
The scenario is that if Manel wants to get all the people within 5 km, how to write the SQL Query?
(We are using name as the input parameter and there is Latlng stored in the table the type is point)
Here is the sample data:
DROP TABLE IF EXISTS "user_account";
DROP SEQUENCE IF EXISTS user_account_id_seq;
CREATE SEQUENCE user_account_id_seq INCREMENT 1 MINVALUE 1 MAXVALUE 2147483647 START 1 CACHE 1;
CREATE TABLE "public"."user_account" (
"id" integer DEFAULT nextval('user_account_id_seq') NOT NULL,
"first_name" character varying(64) NOT NULL,
"last_name" character varying(64) NOT NULL,
"nickname" character varying(64),
"age" character varying(32),
"details" text,
"email" character varying(128),
"last_login_time" timestamp,
"create_time" timestamp,
"popularity" numeric(5,2),
"interested_in_relation" character varying(32),
"interested_in_gender" character varying(32),
"tag" character varying(256),
"point" integer,
"membership_type" character varying(32),
"account_status" character varying(32),
"account_name" character varying(32),
"password" character varying(32),
"latlng" point,
CONSTRAINT "user_account_ak_1" UNIQUE ("email"),
CONSTRAINT "user_account_pk" PRIMARY KEY ("id")
) WITH (oids = false);
INSERT INTO "user_account" ("id", "first_name", "last_name", "nickname", "age", "details", "email", "last_login_time", "create_time", "popularity", "interested_in_relation", "interested_in_gender", "tag", "point", "membership_type", "account_status", "account_name", "password", "latlng") VALUES
(8, 'Addison', 'Davies', 'Addison Davies', NULL, NULL, 'addison.davies#example.com', '2021-04-15 15:18:01.982', '2021-04-15 15:18:01.982', NULL, NULL, NULL, NULL, 0, 'normal', 'active', 'blackswan592', 'ajax', '(-0.1277,51.5073)'),
(10, 'Manel', 'Almeida', 'Manel Almeida', NULL, NULL, 'manel.almeida#example.com', '2021-04-15 15:28:25.85', '2021-04-15 15:28:25.85', NULL, NULL, NULL, NULL, 0, 'normal', 'active', 'orangekoala363', 'clarissa', '(-73.5959,-50.4127)'),
(11, 'Ben', 'Tidemann', 'Ben Tidemann', NULL, NULL, 'ben.tidemann#example.com', '2021-04-15 15:28:37.776', '2021-04-15 15:28:37.776', NULL, NULL, NULL, NULL, 0, 'normal', 'active', 'sadfrog539', 'morpheus', '(51.8397,-147.6034)'),
(12, 'Dieter', 'Campos', 'Dieter Campos', NULL, NULL, 'dieter.campos#example.com', '2021-04-15 15:28:38.939', '2021-04-15 15:28:38.939', NULL, NULL, NULL, NULL, 0, 'normal', 'active', 'silvercat262', 'irish', '(-72.8698,-139.8647)'),
(13, 'Girão', 'Martins', 'Girão Martins', NULL, NULL, 'girao.martins#example.com', '2021-04-15 15:33:58.464', '2021-04-15 15:33:58.464', NULL, NULL, NULL, NULL, 0, 'normal', 'active', 'yellowgoose254', 'shotgun', '(-69.7453,-170.808)'),
(14, 'Tammy', 'Hale', 'Tammy Hale', NULL, NULL, 'tammy.hale#example.com', '2021-04-15 15:33:59.925', '2021-04-15 15:33:59.925', NULL, NULL, NULL, NULL, 0, 'normal', 'active', 'beautifulzebra663', 'james007', '(25.8726,55.214)'),
(15, 'Hudson', 'Miller', 'Hudson Miller', NULL, NULL, 'hudson.miller#example.com', '2021-04-15 15:34:33.501', '2021-04-15 15:34:33.501', NULL, NULL, NULL, NULL, 0, 'normal', 'active', 'angrygorilla353', '414141', '(-59.3342,-90.5217)'),
(16, 'Apolline', 'Renard', 'Apolline Renard', NULL, NULL, 'apolline.renard#example.com', '2021-04-15 15:35:02.523', '2021-04-15 15:35:02.523', NULL, NULL, NULL, NULL, 0, 'normal', 'active', 'smallleopard612', '143143', '(0.6492,18.2443)'),
(17, 'Afet', 'Demirel', 'Afet Demirel', NULL, NULL, 'afet.demirel#example.com', '2021-04-15 15:35:08.072', '2021-04-15 15:35:08.072', NULL, NULL, NULL, NULL, 0, 'normal', 'active', 'happyfrog105', 'niners', '(75.6424,138.3762)'),
(18, 'Necati', 'Numanoğlu', 'Necati Numanoğlu', NULL, NULL, 'necati.numanoglu#example.com', '2021-04-15 15:35:09.242', '2021-04-15 15:35:09.242', NULL, NULL, NULL, NULL, 0, 'normal', 'active', 'bluefish441', 'beavis', '(70.8946,-7.5597)'),
(19, 'Danny', 'Soto', 'Danny Soto', NULL, NULL, 'danny.soto#example.com', '2021-04-15 15:35:10.32', '2021-04-15 15:35:10.32', NULL, NULL, NULL, NULL, 0, 'normal', 'active', 'blackpanda305', 'pentium', '(-79.9498,-171.0615)'),
(20, 'Jesse', 'Aro', 'Jesse Aro', NULL, NULL, 'jesse.aro#example.com', '2021-04-15 15:35:11.457', '2021-04-15 15:35:11.457', NULL, NULL, NULL, NULL, 0, 'normal', 'active', 'beautifulkoala522', 'lonely', '(-7.8552,-1.1386)'),
(21, 'Lucy', 'Zhang', 'Lucy Zhang', NULL, NULL, 'lucy.zhang#example.com', '2021-04-15 15:35:12.406', '2021-04-15 15:35:12.406', NULL, NULL, NULL, NULL, 0, 'normal', 'active', 'brownladybug716', 'beautiful', '(63.4473,52.2642)'),
(22, 'محیا', 'گلشن', 'محیا گلشن', NULL, NULL, 'mhy.glshn#example.com', '2021-04-15 15:35:13.503', '2021-04-15 15:35:13.503', NULL, NULL, NULL, NULL, 0, 'normal', 'active', 'browncat969', 'jorge', '(28.8925,109.2892)'),
(23, 'Jorge', 'Roman', 'Jorge Roman', NULL, NULL, 'jorge.roman#example.com', '2021-04-15 16:20:55.996', '2021-04-15 16:20:55.996', NULL, NULL, NULL, NULL, 0, 'normal', 'active', 'crazyostrich244', 'yyyyyyyy', '(-20.7278,-17.5191)'),
(24, 'Clayton', 'Jacobs', 'Clayton Jacobs', NULL, NULL, 'clayton.jacobs#example.com', '2021-04-15 16:20:57.276', '2021-04-15 16:20:57.276', NULL, NULL, NULL, NULL, 0, 'normal', 'active', 'happyrabbit978', 'killers', '(88.5666,-97.9353)');
Here is a query that returns all the people within a range of 5 km for a given name:
SELECT
ua1.id AS id1,
ua1.first_name AS name1,
ua1.latlng AS latlng1,
ua2.id AS id2,
ua2.first_name AS name2,
ua2.latlng AS latlng2,
(ua1.latlng <#> ua2.latlng) * 1.609344 as distance
FROM user_account ua1
JOIN user_account ua2 ON ua1.id <> ua2.id
WHERE ua1.first_name = 'Manel'
AND (ua1.latlng <#> ua2.latlng) * 1.609344 < 5;
Please note that the <#> operator returns miles instead of kilometers, so additional conversion is necessary, as shown above.
Thanks for Karol's nice answer! I also come up with a solution.
WITH latlng1 AS (
SELECT
latlng
FROM
user_account
WHERE
first_name = 'Manel'
),
result AS (
SELECT
(
point(
(latlng1.latlng)
) <#> point(
(user_account.latlng)
)
) as distance,
user_account.nickname
from
user_account,
latlng1
)
select
*
from
result
where
distance < 5 / 1.609344 AND nickname <> 'Manel Almeida'
I'm trying to Insert a double precision (ilosc_zlec) value into a table (M_ZAMWLASNEPOZ). First I use a select statement to get the value. After I run this part of code, I can see the correct value in the variable ( 39 ). Next I'm trying to insert the selected values in the table and most of them get inserted but field ILOSC with should become the value of ilosc_zlec shows NULL. Both ILOSC, ilosc_zlec are double precision variables.
for select t.strumet_katzw,a.kategoria,a.rok_zam,a.symbol_zam,a.indeks,a.nazwa,a.wyroznik,a.ilosc_zlec,a.lp_zam from m_zlecenia a
left join m_przewod_gl g
on g.kat_zlec= a.kategoria and g.rok_zlec= a.rok_zam and g.symb_zlec= a.symbol_zam and g.lp_zlec= a.lp_zam
left join m_przewod_op p
on p.kat_zlec= a.kategoria and p.rok_zlec= a.rok_zam and p.symb_zlec= a.symbol_zam and p.lp_zlec= a.lp_zam and p.nr_przew=g.nr_poz
left join m_kot t
on t.kod_oper= p.kod_oper
where a.wyroznik=:wyroznik and a.grupa_zl= 'KOP' and a.data_wykon is null and f_mid(p.kod_oper,1,4)='KOOD' and t.strumet_katzw is not null
into: strumet_katzw,:kategoria, :rok_zam, :symbol_zam, :indeks, :nazwa, :wyroznik, :ilosc_zlec,:lp_zam
do begin
select symbol_zam from m_zamwlasne where kategoria=:strumet_katzw and F_MID(symbol_zam,1,8) = (:symbol_zam||f_mid(:indeks,5,3)) into: symb_szukan_nagl ;
-- wypelniasz pozycje naglowka
INSERT INTO M_ZAMWLASNEPOZ (KATEGORIA, ROK_ZAM, SYMBOL_ZAM, LP_ZAM, INDEKS, ILOSC, TERMIN, CENA_ZAM, POBRANO_IL, ZREALIZOWANO, DATA_ZM, OPER, KOD_KONTRO, ZAKUP_DLA, NA_PRZEWOD, IL_PO_KONTROLI, JM, ILOSC_ZAK, REL_JM_ZAK, NR_ZAPOTRZEB, DATA_POTW, OPIS_POZ2, OPIS_POZ, KOOP_WYROB, KOOP_WYROB_ILOSC, POBRANO_KOOP, SKAD_KOPIA, TYP_INDEKSU, SUMA_DOST, DATA_REALIZ, LP_OFERTY, KONTO_KOSZT, NA_NADRZ, PRZEDMIOT, RABAT, PR_RABAT, CENA_JEDN, LP_OPER, ILOSC_REJOP_KOOP, ROZLICZONE)
VALUES (:strumet_katzw, :rok_zam, :symb_szukan_nagl, :lp_zam, :indeks, :ilosc_zlec, 'now', 0, 0, 'T', 'now', user, 'NN', NULL, NULL, 0, 'SZT', 0, 0, NULL, 'NOW', NULL, NULL, NULL, 0, 0, NULL, 'I', 0, 'NOW', 1, NULL, NULL, 'a', 0, 0, 0, NULL, NULL, NULL);
suspend;
end
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]
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
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...