SQL Server is there a way around repeated conditional lines - sql

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

Related

Parameterise a Where clause in SPARK SQL

How do I parameterise a query containing an IN clause with a variable number of arguments, like this one?
SELECT * FROM Tags
WHERE ID IN ('01',
'02',
'03',
'04',
'05',
'15',
'16',
'20',
'21',
'22',
'24',
'25',
'27',
'31',
'34',
'43',
'53',
'57',
'60',
'61',
'68',
'70',
'80',
'85',
'A8',
'A9',
'B1',
'B2',
'B3',
'B4')
In SQL (Sql server ) it is to create a Table valued Parameter
how to achieve this in SPark SQL ? OR Is there any way to make the where condition values as a pyspark function output ?
From the manual (Manual):
>>> df[df.name.isin("Bob", "Mike")].collect()
[Row(age=5, name='Bob')]
>>>df[df.age.isin([1, 2, 3])].collect()
[Row(age=2, name='Alice')]
Thus...
tags = ['01', '02', ... 'B4']
df[ df.id.isin(tags) ].collect()

How we can fetch data faster in oracle 9i by using select command between two tables

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

How to find how many customer buy more than one stuff from a table

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;

Switched from DAO to ADO and SQL query now returns an error, any ideas?

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

Adding multiple rows in SQL

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);