Related
I have two similar tables that I would like to join. See reproducible example below.
WHAT NEEDS TO BE DONE
See comments in code: concatenating the values '2021-01-01'(column: Date), 'hat'(column: content), 'cat'(column: content) and 'A'(column: Tote) in first_table would lead to a unique key that can be joined with the exact same data in second_table. The result would be the first row of the 4 unique events (see desired_result: '#first tote'). In reality the rows would be a few million.
Reproducible example:
CREATE OR REPLACE TABLE
`first_table` (
`Date` string NOT NULL,
`TotearrivalTimestamp` string NOT NULL,
`Tote` string NOT NULL,
`content` string NOT NULL,
`location` string NOT NULL,
);
INSERT INTO `first_table` (`Date`, `TotearrivalTimestamp`, `Tote`, `content`, `location`) VALUES
('2021-01-01', '13:00','A','hat','1'), #first tote
('2021-01-01', '13:00','A','cat','1'), #first tote
('2021-01-01', '14:00', 'B', 'toy', '1'),
('2021-01-01', '14:00', 'B', 'cat', '1'),
('2021-01-01', '15:00', 'A', 'toy', '1'),
('2021-01-01', '13:00', 'A', 'toy', '1'),
('2021-01-02', '13:00', 'A', 'hat', '1'),
('2021-01-02', '13:00', 'A', 'cat', '1');
CREATE OR REPLACE TABLE
`second_table` (
`Date` string NOT NULL,
`ToteendingTimestamp` string NOT NULL,
`Tote` string NOT NULL,
`content` string NOT NULL,
`location` string NOT NULL,
);
INSERT INTO `second_table` (`Date`, `ToteendingTimestamp`, `Tote`, `content`, `location`) VALUES
('2021-01-01', '20:00', 'B', 'cat', '2'),
('2021-01-01', '19:00', 'A', 'cat', '1'), #first tote
('2021-01-01', '19:00', 'A', 'hat', '1'), #first tote
('2021-01-01', '20:00', 'B', 'toy', '2'),
('2021-01-01', '14:00', 'A', 'toy', '1'),
('2021-01-02', '14:00', 'A', 'hat', '1'),
('2021-01-02', '14:00', 'A', 'cat', '1'),
('2021-01-01', '16:00', 'A', 'toy', '1');
CREATE OR REPLACE TABLE
`desired_result` (
`Date` string NOT NULL,
`Tote` string NOT NULL,
`TotearrivalTimestamp` string NOT NULL,
`ToteendingTimestamp` string NOT NULL,
`location_first_table` string NOT NULL,
`location_second_table` string NOT NULL,
);
INSERT INTO `desired_result` (`Date`, `Tote`, `TotearrivalTimestamp`, `ToteendingTimestamp`, `location_first_table`, `location_second_table`) VALUES
('2021-01-01', 'A', '13:00', '19:00', '1', '1'), #first tote
('2021-01-01', 'B', '14:00', '20:00', '1', '1'),
('2021-01-01', 'A', '15:00', '16:00', '1', '2'),
('2021-01-02', 'A', '13:00', '14:00', '1', '1');
#### this does not give what I want####
select first.date as Date, first.tote, first.totearrivaltimestamp, second.toteendingtimestamp, first.location as location_first_table, second.location as location_second_table
from `first_table` first
inner join `second_table` second
on first.tote = second.tote
and first.content = second.content;
I was able to reproduce the'desired_result' table (mostly) with the SQL below. I believe there exists a few typos with the 'insert into' statements. However, I think this meets the intent.
Query:
select
first_table.date as Date,
first_table.tote,
first_table.totearrivaltimestamp,
second_table.toteendingtimestamp,
first_table.location as location_first_table,
second_table.location as location_second_table
from first_table
inner join `second_table`
on first_table.Date = second_table.Date
and first_table.tote = second_table.tote
group by first_table.Date, first_table.TotearrivalTimestamp, first_table.tote;
result:
2021-01-01|A|13:00|19:00|1|1
2021-01-01|B|14:00|20:00|1|2
2021-01-01|A|15:00|19:00|1|1
2021-01-02|A|13:00|14:00|1|1
This result assumes your first table dates will always match for totes/timestamps. The group by function then merges duplicate results. The second table information matches the date and tote of the first table and is appended to the line item.
This answer should work. I think your issue might be with some of your quoting of tables....
select f.'date'
,f.tote
, f.totearrivaltimestamp
, s.toteendingtimestamp
, f.location as location_first_table
, s.location as location_second_table
from first f
,INNER JOIN "second" s on f.'date' = s.'date'
and f.tote = s.tote
and f.content = s.content
SELECT
COUNT(*)
FROM retailer_master r,
dlymtd d
WHERE r.retle_no = d.sender_mobile
AND r.fos_no = '"+1234567890+"'
AND d.amount IN ('5.00', '9.00', '11.00', '13.00', '14', '15', '15.05', '16', '17', '18', '21', '22', '23', '26', '31', '34', '35.01', '37', '38', '42', '45', '53', '57', '59', '61', '76', '79', '89', '92', '96', '97', '99', '102', '145', '148', '159', '194', '199', '249', '299', '549')
It is taking lots of time to give output.
Please try to create index on retle_no and sender_mobile and check the performance.
Hope this will help you
I was using the following code to query my database in DAO, which worked fine:
SELECT *
FROM (Resources LEFT JOIN [Select * FROM AvailabilityBlocks LEFT JOIN Location ON AvailabilityBlocks.LocationID=Location.LocationID WHERE ((CStr(AvailabilityBlocks.LocationID) IN ('8', '14', '16', '1', '15', '17', '10', '9', '19', '12', '5', '18', '13', '20', '3', '26', '2', '25', '28', '27') AND (AvailabilityBlocks.Type = 3 OR AvailabilityBlocks.Type = 4)) OR AvailabilityBlocks.Type = 2) AND Begin < #15-Jul-2013 12:00:00 AM# And Begin >= #08-Jul-2013 12:00:00 AM#]. AS FilteredTable ON Resources.ResourceID=FilteredTable.ResourceID) LEFT JOIN EmployeeTypes ON EmployeeTypes.TypeID=Resources.EmployeeType ORDER BY RClass, Resources.LastName ASC, Resources.FirstName ASC, Resources.ResourceID ASC, AvailabilityBlocks.Begin ASC, AvailabilityBlocks.End Desc, Location.SubType DESC
I then converted all my code to ADO and the SQL stopped working and now shows an
Syntax error in FROM clause.
error message!
Any help would be appreciated!
Try this.
I think the way your are closing out your derived table ("FilteredTable") is off.
Also. You might want to try a table alias...
SELECT *
FROM
Resources res
LEFT JOIN (
[Select * FROM AvailabilityBlocks avb LEFT JOIN Location loc ON avb.locID=loc.locID
WHERE ((CStr(avb.locID) IN ('8', '14', '16', '1', '15', '17', '10', '9', '19', '12', '5', '18', '13', '20', '3', '26', '2', '25', '28', '27')
AND (avb.Type = 3 OR avb.Type = 4)) OR avb.Type = 2)
AND Begin < #15-Jul-2013 12:00:00 AM# And Begin >= #08-Jul-2013 12:00:00 AM#] ) AS FilteredTable
ON res.ResourceID=FilteredTable.ResourceID)
LEFT JOIN EmployeeTypes ON EmployeeTypes.TypeID=res.EmployeeType
ORDER BY RClass, res.LastName ASC, res.FirstName ASC, res.ResourceID ASC, avb.Begin ASC, avb.End Desc, loc.SubType DESC
Hello I am trying to clean up a query and there is one line that is repeated six times.
Is there a way to set something like a constant in SQL?
Here is an example of the issue:
select Distinct DSF.CityName,
( Select count (Distinct DSF1.IncdtKey)
from dbo.IncidentFile DSF1
Where DSF1.IncidentMostSevere in ('1', '2', '4', '5', '6')
and DSF1.CategoryKey in ('15', '01', '02', '03', '04', '05', '06')<-----
and DSF1.CityName = DSF.CityName) as 'Weapons Possession 11-12',
( Select count (Distinct DSF2.IncdtKey)
from dbo.IncidentFile DSF2
Where DSF2.IncidentMostSevere in ('7', '8', '9', '10', '11', '12')
and DSF2.CategoryKey in ('15', '01', '02', '03', '04', '05', '06') <-----
and DSF2.CityName = DSF.CityName) as 'Drugs Related 11-12',
( Select count (Distinct DSF3.IncdtKey)
from dbo.IncidentFile DSF3
Where DSF3.IncidentMostSevere in ('14', '15', '17', '20', '21', '22', '26')
and DSF3.CategoryKey in ('15', '01', '02', '03', '04', '05', '06') <-----
and DSF3.CityName = DSF.CityName) as 'Incident with Injury 11-12',
( Select count (Distinct DSF4.IncdtKey)
from dbo.IncidentFile DSF4
Where DSF4.IncidentMostSevere in ('16', '18', '19', '23', '24', '25')
and DSF4.CategoryKey in ('15', '01', '02', '03', '04', '05', '06') <-----
and DSF4.CityName = DSF.CityName) as 'Incident no Injury 11-12',
( Select count (Distinct DSF5.IncdtKey)
from dbo.IncidentFile DSF5
Where DSF5.IncidentMostSevere in ('3', '13', '29', '31', '32', '33')
and DSF5.CategoryKey in ('15', '01', '02', '03', '04', '05', '06') <-----
and DSF5.CityName = DSF.CityName) as 'Other reason for 11-12',
( Select count (Distinct DSF6.IncdtKey)
from dbo.IncidentFile DSF6
Where DSF6.CategoryKey in ('15', '01', '02', '03', '04', '05', '06') <-----
and DSF6.CityName = DSF.CityName) as 'Total Incidents'
from dbo.IncidentFile DSF
group by DSF.CityName
Order by DSF.CityName
Thanks
You should be able to use a CTE and then an aggregate function with CASE expression:
;with cte as
(
select distinct CityName,
IncidentMostSevere,
IncdtKey
from dbo.IncidentFile
where CategoryKey in ('15', '01', '02', '03', '04', '05', '06')
)
select CityName,
count(case
when IncidentMostSevere in ('1', '2', '4', '5', '6')
then IncdtKey end) as 'Weapons Possession 11-12',
count(case
when IncidentMostSevere in ('7', '8', '9', '10', '11', '12')
then IncdtKey end) as 'Drugs Related 11-12',
count(case
when IncidentMostSevere in ('14', '15', '17', '20', '21', '22', '26')
then IncdtKey end) as 'Incident with Injury 11-12',
count(case
when IncidentMostSevere in ('16', '18', '19', '23', '24', '25')
then IncdtKey end) as 'Incident no Injury 11-12',
count(case
when IncidentMostSevere in ('3', '13', '29', '31', '32', '33')
then IncdtKey end) as 'Other reason for 11-12',
count(case
when IncidentMostSevere in ('15', '01', '02', '03', '04', '05', '06')
then IncdtKey end) as 'Total Incidents'
from cte
group by CityName
order by CityName
This approach works by moving your sub queries into the from clause and adding the list of categories once in your where clause.
select
DSF.CityName,
Dsf1.cnt as 'Weapons Possession 11-12',
Dsf2.cnt as 'Drugs Related 11-12',
...
Dfsn.cnt as 'Total Incidents'
From
dbo.IncidentFile DSF
Inner join
(
Select
DSF.CategoryKey,
DSF.CityName,
count (Distinct DSF.IncdtKey) as cnt
from
dbo.IncidentFile DSF
Where
DSF.IncidentMostSevere in ('1', '2', '4', '5', '6').
group by
1, 2
) DSF1
On DSF1.CityName = DSF.CityName
Inner join
(
Select
DSF.CategoryKey,
DSF.CityName,
count (Distinct DSF.IncdtKey) as cnt
from
dbo.IncidentFile DSF
Where
DSF.IncidentMostSevere in ('7', '8', '9', '10', '11', '12')
group by
1, 2
) DSF2
On DSF2.CityName = DSF.CityName
...
Inner join
(
Select
DSF.CategoryKey,
DSF.CityName,
count (Distinct DSF.IncdtKey) as cnt
from
dbo.IncidentFile DSF
Where
DSF.IncidentMostSevere in ('7', '8', '9', '10', '11', '12')
group by
1, 2
) DSFn
On dfsn.CityName = DSF.CityName
Where
dfsn.CategoryKey in ('15','01','02,'03','04','05','06')
Order by DSF.CityName
I am asked to add 8 rows into a table.
insert into Rating ( rID, mID, stars, ratingDate )
values ('207', '101', '5', null), ('207', '102', '5', null),
('207', '103', '5', null), ('207', '104', '5', null),
('207', '105', '5', null), ('207', '106', '5', null),
('207', '107', '5', null), ('207', '108', '5', null)
This operation works good with one value added but when adding multiple gives the error
Query failed to execute: near ",": syntax error
What is missing?
A late answer
If your are using SQLITE version 3.7.11 or above, then multiple rows insert is possible by this syntax,
SIMPLEST WAY
INSERT INTO Rating (rID, mID, stars, ratingDate) VALUES ('207', '102', '5', null) , ('207', '102', '5', null) , ('207', '102', '5', null)
The above clause posted in question do work if the new SQLITE version is used.
SELECT CLAUSE
insert into Rating
SELECT '207' AS rID, '101' AS mID, '5' AS stars, null AS ratingDate
UNION SELECT '207', '102', '5', null
UNION SELECT '207', '103', '5', null
UNION SELECT '207', '104', '5', null
UNION SELECT '207', '105', '5', null
UNION SELECT '207', '106', '5', null
UNION SELECT '207', '107', '5', null
UNION SELECT '207', '108', '5', null
or SQL is
insert into Rating (rID, mID, stars, ratingDate)
SELECT '207', '101', '5', null
UNION SELECT '207', '102', '5', null
UNION SELECT '207', '103', '5', null
UNION SELECT '207', '104', '5', null
UNION SELECT '207', '105', '5', null
UNION SELECT '207', '106', '5', null
UNION SELECT '207', '107', '5', null
UNION SELECT '207', '108', '5', null
REMEMBER I you do not want to check for duplicate in above set of inserted values then use UNION ALL in place of UNION as it will be little faster.
I assume your RDBMS don't support such construction.
insert into Rating ( rID, mID, stars, ratingDate )
values ('207', '101', '5', null);
insert into Rating ( rID, mID, stars, ratingDate )
values ('207', '102', '5', null);
.....
I sugest:
insert into Rating ( rID, mID, stars, ratingDate ) values ('207', '101', '5', null);
insert into Rating ( rID, mID, stars, ratingDate ) values ('207', '102', '5', null);
...
insert into Rating ( rID, mID, stars, ratingDate ) values ('207', '108', '5', null);
i created table in sql lite . table creation script is as follows
create table Rating (rID varchar(10),mID varchar(10),stars varchar(10),ratingDate date);
And i used following query to insert into above table and its working fine for me.
insert into Rating ( rID, mID, stars, ratingDate )
values ('207', '101', '5', null), ('207', '102', '5', null),
('207', '103', '5', null), ('207', '104', '5', null),
('207', '105', '5', null), ('207', '106', '5', null),
('207', '107', '5', null), ('207', '108', '5', null);