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);
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
This is my first time asking a SQL related question. I am having a difficult time getting this query to work.
Basically, I have a database that has 3 tables Course, Faculty and Adjunct. This query is supposed to create a view named Top3Enrollment that returns FirstName, LastName, TotalStudents, and MaxEnrollment of the 3 faculty members with the largest total enrollment for their courses, along with the highest enrollment among the classes they teach.
When attempting to write the query I get an error with selecting the column FirstName
My query:
CREATE VIEW Top3Enrollment
AS
SELECT TOP 3 PERCENT
FirstName, LastName, SUM(Enrollment), MAX(Enrollment)
FROM
Faculty
JOIN
Course ON Faculty.Faculty_ID = Course.Faculty_ID
ORDER BY
MAX(Enrollment);
The error I get is:
Msg 8120, Level 16, State 1, Procedure Top3Enrollment, Line 3 [Batch Start Line 0]
Column 'Faculty.FirstName' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
My database structures:
CREATE TABLE Faculty
(
Faculty_ID VARCHAR(2),
LastName VARCHAR(20),
FirstName VARCHAR(20),
Department VARCHAR(10),
Campus VARCHAR(10)
);
INSERT INTO Faculty
VALUES ('1', 'Brown', 'Joe', 'Business', 'Kent'),
('2', 'Smith', 'John', 'Economics', 'Kent'),
('3', 'Jones', 'Sally', 'English', 'South'),
('4', 'Black', 'Bill', 'Economics', 'Deerwood'),
('5', 'Green', 'Gene', 'Business', 'South');
CREATE TABLE Course
(
Course_ID CHAR(2),
Ref_Number CHAR(5),
Faculty_ID VARCHAR(2),
Term Char(1),
BegDate SMALLDATETIME,
Enrollment INTEGER,
TotRev FLOAT
);
INSERT INTO Course
VALUES ('1', '12345', 'a', 'A', '2016-01-08 00:00:00', 24, 12345.32 ),
('2', '54321', '3', 'B', '2016-02-04 00:00:00', 18, 21435.51 ),
('3', '13524', '1', 'B', '2016-02-04 00:00:00', 7, 1256.67 ),
('4', '24653', '1', 'C', '2016-03-04 00:00:00', 29, 54421.11 ),
('5', '98765', '5', 'A', '2016-01-08 00:00:00', 35, 246753.23),
('6', '14862', '2', 'B', '2016-02-04 00:00:00', 14, 9876.87),
('7', '96032', '1', 'C', '2016-03-04 00:00:00', 8, 863159.31),
('8', '81256', '5', 'A', '2016-01-08 00:00:00', 5, 98762.14),
('9', '64321', '2', 'C', '2016-03-04 00:00:00', 23, 2965.98),
('10','90908', 'a', 'A', '2016-01-08 00:00:00', 45, 91724.02),
('11','90908', '3', 'A', '2016-01-08 00:00:00', 23, 73725.77),
('12','90908', '3', 'A', '2016-01-08 00:00:00', 16, 84224.29),
('13','90908', 'b', 'A', '2016-01-08 00:00:00', 13, 42719.82);
CREATE TABLE Adjuncts
(
Faculty_ID Char(2),
LastName VARCHAR(20),
FirstName VARCHAR(20),
Department VARCHAR(10),
Campus VARCHAR(10)
);
INSERT INTO Adjuncts
VALUES ('a', 'Rogers', 'Aaron', 'Business', 'Kent'),
('b', 'Manning', 'Peyton', 'Economics', 'North'),
('c', 'Drew', 'Maurice', 'English', 'Cecil'),
('d', 'Griffin', 'Robert', 'Music', 'Deerwood'),
('e', 'Goodell', 'Roger', 'Economics', 'South'),
('f', 'Vilma', 'Jonathan', 'Business', 'Kent');
Note:
I Understand I cannot have Order By but I wouldn't know what else to use
Added the database code
When you use aggregate functions like sum and max together with other columns you need to group your aggregations by those other columns.
Add GROUP BY like this
SELECT TOP 3 PERCENT FirstName, LastName, SUM(Enrollment), MAX(Enrollment)
FROM Faculty
JOIN Course ON Faculty.Faculty_ID = Course.Faculty_ID
GROUP BY FirstName, LastName
ORDER BY MAX(Enrollment);
Table sales:
create table sales (
Date date,
customer_id integer,
product_id integer,
units_sold integer,
paid_amount integer
);
Insert into sales (Date, customer_id, product_id, units_sold, paid_amount)
VALUES
('2016-01-01', '1', '1', '5', '45'),
('2016-01-01', '2', '1', '2', '18'),
('2016-01-01', '3', '2', '7', '35'),
('2016-01-07', '1', '3', '3', '45'),
('2016-01-07', '2', '2', '5', '25'),
('2016-01-07', '4', '2', '5', '25'),
('2016-01-10', '1', '4', '5', '30'),
('2016-01-10', '2', '4', '5', '30'),
('2016-01-10', '4', '5', '6', '60'),
('2016-01-10', '4', '3', '9', '135'),
('2016-01-14', '3', '1', '4', '60'),
('2016-01-14', '2', '3', '6', '90'),
('2016-01-14', '2', '3', '6', '90');
How many customers bought more than one different product on every visit (i.e. day)?
You need to group by and get the count() like
select customer_id, count(distinct product_id) as item_purchased
from sales
group by "date", customer_id
having count(distinct product_id) > 1;
Respected Techie,
May any one please help me with this critical scenario.
I am trying to compare Sales value of year 2016 with sales value of (2015,2014)
and the difference stored in alias column as Sales_growth for year 2016 ,
for year (2015,2014) the alias column be as '1'
Based on the key column month, sales_manager_code and sales_group.
but when there is no key match (month, sales_manager_code ,sales_group.)
then the alias column sales_growth 1
Formula for calculation
SGR = (sales(2015 or 2014)-sales (2016)) / Sales_growth(2015)
Table Structure and Data
DECLARE #T TABLE
(
YEAR VARCHAR (50),
MONTH VARCHAR (50),
SALES_MANAGER_CODE VARCHAR (50),
SALES_GROUP VARCHAR (50),
NetProductSales VARCHAR (50)
)
INSERT #T
SELECT '2015', '10', '10', 'ARS', '126431.16' UNION ALL
SELECT '2015', '10', '4', '4', '1247439.2' UNION ALL
SELECT '2014', '11', '4', '2', '1399367.53' UNION ALL
SELECT '2016', '10', '10', 'ARS', '126431.16' UNION ALL
SELECT '2016', '10', '4', '4', '1247439.2' UNION ALL
SELECT '2016', '11', '4', '2', '1399367.53' UNION ALL
SELECT '2015', '8', '11', '0', '44518.18'
Expected Output Here sales_margin is alias column to store result
/*
YEAR MONTH SALES_MANAGER_CODE SALES_GROUP NetProductSales SALES_MARGIN
2015, 10, 10, ARS,126431.16, 1
2015, 10, 4, 4, 1247439.2, 1
2014, 11, 4, 2, 1399367.53, 1
2016, 10, 10, ARS, 3565898.25, -27.20426744
2016, 10, 4, 4, 5469856.25, 3.384868016
2016, 11, 4, 2, 45268912.65, 31.34955198
2015, 8, 11, 0, 44518.18, 1
*/
Thanks,
I'm not sure where you get SalesGrowth but using a LEFT OUTER join will give you a NULL where there is no corresponding join row for which you can then use COALESCE to convert the NULL to 1.
DECLARE #T TABLE
(
YEAR VARCHAR (50),
MONTH VARCHAR (50),
SALES_MANAGER_CODE VARCHAR (50),
SALES_GROUP VARCHAR (50),
NetProductSales decimal( 28, 16 )
)
INSERT #T
SELECT '2015', '10', '10', 'ARS', 126431.16 UNION ALL
SELECT '2015', '10', '4', '4', 1247439.2 UNION ALL
SELECT '2014', '11', '4', '2', 1399367.53 UNION ALL
SELECT '2016', '10', '10', 'ARS', 126431.16 UNION ALL
SELECT '2016', '10', '4', '4', 1247439.2 UNION ALL
SELECT '2016', '11', '4', '2', 1399367.53 UNION ALL
SELECT '2015', '8', '11', '0', 44518.18
select *
, COALESCE( ( tprv.NetProductSales - t16.NetProductSales ) / ???? ), 1 ) as SALES_MARGIN
from #T t16
left outer join #T tprv on tprv.YEAR = t16.YEAR - 1
and tprv.MONTH = t16.MONTH
and tprv.SALES_MANAGER_CODE = t16.SALES_MANAGER_CODE
and tprv.SALES_GROUP = t16.SALES_GROUP
where t16.YEAR = 2016
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