SQLite Count Summary Query - sqlite

I'm trying to get a query to summarize each employees work for the week. For example, John Doe did a total of 12 tickets for the week, 4 of which were Break/Fixes, and 4 were Enhancement, and another 4 were uncategorized.
This is what I have so far:
SELECT (users.first_name || ' ' || users.last_name) AS Name,
COUNT(tickets.id) AS 'Number of Tickets Closed',
COUNT(tickets.category = 'Maintenance') AS 'Maintenance Tickets',
COUNT(tickets.category = 'After Hours') AS 'After Hours Tickets',
COUNT(tickets.category = 'Break Fix') AS 'Break Fix Tickets',
COUNT(tickets.category = 'Enhancement') AS 'Enhancement Tickets',
COUNT(tickets.category = '') AS 'Non Categorized Tickets'
FROM tickets, users
ON tickets.assigned_to=users.id
WHERE (tickets.status = 'closed') AND
(tickets.closed_at >= '2011-07-16 00:00:00') AND
(tickets.closed_at <= '2011-07-22 23:59:59')
GROUP BY Name;
Here is a sample result:
John Doe1 10 10 10 10 10 10
John Doe2 2 2 2 2 2 2
John Doe3 25 24 24 24 24 24
John Doe4 2 2 2 2 2 2
John Doe5 12 10 10 10 10 10
John Doe6 7 7 7 7 7 7
This query doesn't quite work as I expected it to as all of the columns have the same total (The total number of tickets closed, the following columns seems to only contain the categorized ones.) Help?
EDIT
Just wanted to post the functional code:
SELECT (users.first_name || ' ' || users.last_name) AS Name,
COUNT(tickets.id) AS 'Number of Tickets Closed',
COUNT(case tickets.category when 'Maintenance' then 1 else null end) AS 'Maintenance Tickets',
COUNT(case tickets.category when 'After Hours' then 1 else null end) AS 'After Hours Tickets',
COUNT(case tickets.category when 'Break Fix' then 1 else null end) AS 'Break Fix Tickets',
COUNT(case tickets.category when 'Enhancement' then 1 else null end) AS 'Enhancement Tickets',
COUNT(case tickets.category when '' then 1 else null end) AS 'Non Categorized Tickets'
FROM tickets, users
ON tickets.assigned_to=users.id
WHERE (tickets.status = 'closed') AND
(tickets.closed_at >= '2011-07-16') AND
(tickets.closed_at <= '2011-07-22')
GROUP BY Name;

you may want to use COUNT like this
...
COUNT(case tickets.category when 'Maintenance' then 1 else null end),
COUNT(case tickets.category when 'After Hours' then 1 else null end),
...

It seems to me you cannot use an alias in the GROUP BY clause. Don't your users have an ID you could use to differenciate them?
And you must use SUM instead of COUNT if you want to count compared with a condition.
SELECT (users.first_name || ' ' || users.last_name) AS Name,
COUNT(tickets.id) AS 'Number of Tickets Closed',
SUM(tickets.category = 'Maintenance') AS 'Maintenance Tickets',
SUM(tickets.category = 'After Hours') AS 'After Hours Tickets',
SUM(tickets.category = 'Break Fix') AS 'Break Fix Tickets',
SUM(tickets.category = 'Enhancement') AS 'Enhancement Tickets',
SUM(tickets.category = '') AS 'Non Categorized Tickets'
FROM tickets, users
ON tickets.assigned_to=users.id
WHERE (tickets.status = 'closed') AND
(tickets.closed_at >= '2011-07-16 00:00:00') AND
(tickets.closed_at <= '2011-07-22 23:59:59')
GROUP BY Name;

Related

How can I get a scalar Balance

To compute the beginning ledger balance, I've this query:
select
case when DrControl = 7 then sum(Amount) end Debit,
case when CrControl = 7 then sum(Amount) end Credit
from Transactions
where 7 in (DrControl, CrControl)
and Date < '2020-05-31'
group BY DrControl --, CrControl
and it returns two row:
Debit Credit
------------------
900000 NULL
NULL 40000
I've to compute balance by subtracting one from the other in my Application code. What I really need in this case is Debit - Credit (900000 - 40000) = 860000.
You don't need group by ..., just conditional aggregation:
select
sum(case when DrControl = 7 then Amount end) -
sum(case when CrControl = 7 then Amount end)
from Transactions
where 7 in (DrControl, CrControl) and Date < '2020-05-31'
Just in case there are no rows with DrControl = 7 or CrControl = 7 use also COALESCE() to return 0 instead of null:
select
coalesce(sum(case when DrControl = 7 then Amount end), 0) -
coalesce(sum(case when CrControl = 7 then Amount end), 0)
from Transactions
where 7 in (DrControl, CrControl) and Date < '2020-05-31'

two counts in the same row

is't possible to set 2 counts in the same row.
my result from query is like this:
enter image description here
and i will that the end result seem like this :
enter image description here
and at the end build the precent count1 to count2
my attempt trough case was not successful : SELECT Date,Shift , CASE description WHEN 'Defects' THEN count ELSE 0 END AS Defect_Count , CASE description WHEN 'Total' THEN count ELSE 0 END AS Total_Count FROM ("Queries union)
Here you go. Hope this helps. Thanks.
MYSQL:
select
t.dates, t.shift,
sum(case when t.description = 'Defects' then t.counts else 0 end) as `Defects`,
sum(case when t.description = 'Total' then t.counts else 0 end) as `Total`
from (
select *
from tbl ) t
group by t.dates, t.shift
order by t.dates, t.shift
ORACLE:
SELECT dates, shift, defects , total
FROM
(
SELECT *
FROM tbl
)
PIVOT
(
sum(counts)
FOR description IN ('Defects' as defects, 'Total' as total)
)
ORDER BY dates
Result:
dates shift Defects Total
2018-01-20 AM 21 56
2018-01-20 PM 19 54
2018-01-23 AM 16 58
2018-01-23 PM 20 45
many Thanks is working for the first Step (counts in the same Row).
i will try now to build the percent (Defects to Total).
Thanks.
to build the percent (defects to Total):
select dates,shift,defects,total,round((100*defects/total),2) Percent2Total from(select t.dates, t.shift,
sum(case when t.description = 'Defects' then t.counts else 0 end) as 'Defects',
sum(case when t.description = 'total' then t.counts else 0 end) as 'Total'
from (
select *
from tbl ) t
group by t.dates, t.shift
)q order by dates,Shift.
may be it's possible to build that only with Pivot or?

load row data based on the first filed in the row

i have to develop new website for CAR Wash company , this company own three cleaning car which receive order from my system , max order per day for every car is 5 orders so i should add 5 green places beside every car in the the same car row . (ex) if car have two order so two fields with background yellow and other three fields in green background
I have two tables
Cars
Orders
Car table
ID CarName
1 Car1
2 Car2
3 Car3
Order table
ID ClientName Phone Date CarID
1 Jack 11111 22-7-2017 10:22:00 AM 2
2 Susan 22222 22-7-2017 01:30:00 PM 2
3 Hany 33333 22-7-2017 10:22:00 AM 3
4 Karim 44444 21-7-2017 08:22:00 AM 1
5 Halaa 55555 22-7-2017 05:22:00 PM 3
i want to select all orders for every car in one row per day based on selection of day from DATETIMEPICKER so i will select orders for 22-07
Cars Orders
car1 Empty Empty Empty Empty Empty
car2 Jack(111111) Susan(22222) Empty Empty Empty
car3 Hany(333333) HalaSusan(55555) Empty Empty Empty
So how can do it if i use GridView and what is the best query
The following should do the trick...
IF OBJECT_ID('tempdb..#Vehicle', 'U') IS NOT NULL
DROP TABLE #Vehicle;
GO
CREATE TABLE #Vehicle (
VehicleID INT NOT NULL PRIMARY KEY CLUSTERED,
VehicleName VARCHAR(10) NOT NULL
);
GO
INSERT #Vehicle (VehicleID, VehicleName) VALUES
(1, 'Car1'), (2, 'Car2'), (3, 'Car3');
GO
IF OBJECT_ID('tempdb..#Orders', 'U') IS NOT NULL
DROP TABLE #Orders;
GO
CREATE TABLE #Orders (
OrderID INT NOT NULL PRIMARY KEY CLUSTERED,
ClientName VARCHAR(15) NOT NULL,
PhoneNumber VARCHAR(10) NOT NULL,
ServiceDate DATETIME NOT NULL,
VehicleID INT NOT NULL -- create FK refference back to Vehicle.VehicleID)
);
GO
-- create an index to support the row_number() function...
-- The sort cost is greater than the difference between a seek & scan...
CREATE NONCLUSTERED INDEX ix_Orders_VehicleID_ServiceDate
ON #Orders (VehicleID, ServiceDate)
INCLUDE (ClientName, PhoneNumber);
GO
INSERT #Orders (OrderID, ClientName, PhoneNumber, ServiceDate, VehicleID) VALUES
(1, ' Jack ', '11111', '07-22-2017 10:22:00 AM', 2),
(2, ' Susan', '22222', '07-22-2017 01:30:00 PM', 2),
(3, ' Hany ', '33333', '07-22-2017 10:22:00 AM', 3),
(4, ' Karim', '44444', '07-21-2017 08:22:00 AM', 1),
(5, ' Halaa', '55555', '07-22-2017 05:22:00 PM', 3);
GO
--SELECT * FROM #Vehicle v;
--SELECT * FROM #Orders o;
--=======================================================================
DECLARE #WorkDate DATE = '2017-07-22';
WITH
cte_AddRN AS (
SELECT
o.OrderID, o.ClientName, o.PhoneNumber, o.ServiceDate, o.VehicleID,
RN = ROW_NUMBER() OVER (PARTITION BY o.VehicleID ORDER BY o.ServiceDate)
FROM
#Orders o
WHERE
CAST(o.ServiceDate AS DATE) = #WorkDate
)
SELECT
VehicleName = MAX(v.VehicleName),
Order1 = MAX(CASE WHEN ar.RN = 1 THEN ar.ClientName + ' (' + ar.PhoneNumber + ')' ELSE '' END),
Order2 = MAX(CASE WHEN ar.RN = 2 THEN ar.ClientName + ' (' + ar.PhoneNumber + ')' ELSE '' END),
Order3 = MAX(CASE WHEN ar.RN = 3 THEN ar.ClientName + ' (' + ar.PhoneNumber + ')' ELSE '' END),
Order4 = MAX(CASE WHEN ar.RN = 4 THEN ar.ClientName + ' (' + ar.PhoneNumber + ')' ELSE '' END),
Order5 = MAX(CASE WHEN ar.RN = 5 THEN ar.ClientName + ' (' + ar.PhoneNumber + ')' ELSE '' END)
FROM
#Vehicle v
LEFT JOIN cte_AddRN ar
ON v.VehicleID = ar.VehicleID
GROUP BY
v.VehicleID;
Results...
VehicleName Order1 Order2 Order3 Order4 Order5
----------- --------------- --------------- --------------- --------------- ---------------
Car1
Car2 Jack (11111) Susan (22222)
Car3 Hany (33333) Halaa (55555)

Using Case statement with multiple when in Teradata

Suppose I have a data table with the following fields:
CUSTOMER: either A or B
DAY: either Monday or Tuesday
PAID: either Y or N
The total number of rows being four, let's say the data table is this:
CUSTOMER DAY PAID
A Monday Y
A Tuesday N
B Monday Y
B Tuesday N
How do I create a SQL query on Teradata SQL Assistant, that will show the number of people who were Y on Monday and N on Tuesday? (or any of these combinations) I tried to use the query below, but cannot seem to figure out the logic. Your help is much appreciated!
SELECT DAY,
COUNT(CASE PAID WHEN 'Y' THEN CUSTOMER ELSE 0 END) AS PAID_CUSTOMERS,
COUNT(CASE WHEN PAID = 'Y' AND DAY = 'Monday' AND DAY = 'Tuesday' AND PAID = 'N' THEN CUSTOMER ELSE 0 END) AS CUSTOMERS_YM_NT
FROM T1
GROUP BY 1
ORDER BY 1
So, break each day down into a separate case statement:
case when DAY = 'Monday' and PAID = 'NO' then 'NO' else 'YES' end as Monday,
case when DAY = 'Tuesday' and PAID = 'NO' then 'NO' else 'YES end as Tuesday,
etc
Then, you can wrap that with another select and apply whatever criteria you want:
select
<whatever columns>
from
(select
case when DAY = 'Monday' and PAID = 'NO' then 'NO' else 'YES' end as Monday,
case when DAY = 'Tuesday' and PAID = 'NO' then 'NO' else 'YES end as Tuesday,
...
) t
where Monday 'YES'
and Tuesday = 'YES'

SQL count all of the column that has a value greater than 10 and sum it up

I want to count all of the column in my table that has value >= 10.
here is my table :
Date ##### || Value1 || Value2 || Value3
23/04/2014 || __ 1,2 || __ 12,3 ||__ 10 ||
23/04/2014 ||__ 11,2 || ____ 3 || __ 10,3 ||
24/04/2014 || __ 10,9 || ____ 3 || __ 1 ||
I want it to display:
Date ##### || Count ||
23/04/2014 || __ 4 ||
24/04/2014 || __ 1 ||
Assume that I have a lot of date, I want it to display only the last 3 rows.
here is my first code :
Dim strCommand As String = "Select Date, count(*) as tcount from tbBooth having count(*) >= 10 group by date"
already changed based on the solution from Collapsar into this:
Dim strCommand As String = "Select t.d, sum(t.valcount) cnt from (select [date] AS d, CASE WHEN t1.ManualAssists1 >= 10 THEN 1 ELSE 0 END + CASE WHEN t1.ManualAssists2 >= 10 THEN 1 ELSE 0 END + CASE WHEN t1.ManualAssists3 >= 10 THEN 1 ELSE 0 END AS valcount from tbBooth t1) t group by t.d"
it's works, but I want to display only the last 3 row based on ASC order.
Is there anyway how to do it?
Thanks in advances....
try
select t.d
, sum(t.valcount) cnt
from (
select [date] AS d
, CASE WHEN t1.value1 >= 10 THEN 1 ELSE 0 END
+ CASE WHEN t1.value2 >= 10 THEN 1 ELSE 0 END
+ CASE WHEN t1.value3 >= 10 THEN 1 ELSE 0 END
AS valcount
from table t1
) t
group by t.d
;
SELECT
D,
SUM(one + two + three) AS tcount
FROM
(
SELECT
[Date] AS D,
CASE WHEN Value1 >= 10 THEN 1 ELSE 0 END AS one,
CASE WHEN Value2 >= 10 THEN 1 ELSE 0 END AS two,
CASE WHEN Value3 >= 10 THEN 1 ELSE 0 END AS three
FROM
tbBooth
)
GROUP BY
D

Resources