Right now i am using 3 CSV files ( appointment, Person and Referral ) to calculate what i have called as KPI4, KPI5 and KPI6.
I import them into an Access Database that run some SQL queries. I want to automate this proces a bit and create a measure for these KPI's and just upload/update the 3 csv files and the POWER BI report just get updated.
For example for KPI4 I have the following SQL's queries
First and the main one but which refers to z_KPI8_part1 :
SELECT
Person.LPTID, Person.ccgName, Person.Caseload, t1.FTDate,
t1.SERVICEID, Person.LTCondition, Referral.Caseload, *
FROM
Person
INNER JOIN ((SELECT SERVICEID,FTDate
FROM z_KPI8_part1) AS t1
INNER JOIN Referral
ON t1.SERVICEID = Referral.SERVICEID)
ON Person.LPTID = Referral.LPTID;
z_KPI8_part1 which referrs to z_FirstTreatmentDate :
SELECT
z_FirstTreatmentDate.SERVICEID, Min(z_FirstTreatmentDate.FTDate) AS FTDate,
Max(Appointment.ATTENDANCE) AS ATTENDANCE
FROM
Appointment
INNER JOIN z_FirstTreatmentDate
ON (Appointment.SERVICEID = z_FirstTreatmentDate.SERVICEID) AND
(Appointment.Appointment = z_FirstTreatmentDate.FTDate)
WHERE
(((z_FirstTreatmentDate.FTDate) Between [forms]![frmMain]![txtFTDateFrom] And
[forms]![frmMain]![txtFTDateTo]))
GROUP BY
z_FirstTreatmentDate.SERVICEID;
z_FirstTreatmentDate :
SELECT
Appointment.SERVICEID, Min(Appointment.APPOINTMENT) AS FTDate,
Min(Appointment.APPTYPE) AS APPTYPE
FROM
(SELECT *
FROM Appointment
WHERE
(Appointment.APPTYPE=02 OR Appointment.APPTYPE=03 OR Appointment.APPTYPE=05) AND
(Appointment.ATTENDANCE='5' OR Appointment.ATTENDANCE='6')
) AS [%$###_Alias]
GROUP BY
Appointment.SERVICEID;
In theory KPI4 is First Appointment Date when client's Session Type ( APPTYPE )is one of the following: 2,3,5
And
Attendance Status is one of the following: 5 and 6.
From my method i am not getting anywhere near the numbers i get from Access database.
Please help me or guide me
Related
I have 2 tables in the first I define the equipment possible like fire hydrant or fire alarm system, all of theese are to check within a certain time, in this table I have the name of theese equipments and the time within they are to chec, in the second table I have the date when the last chec was I want create a View List but that contains the information, when the next chec is to realize,
Like this
Create View listafacilitymanagemen AS
SELECT DISTINCT ID, tipo, posicion, checeo, checeo + checeointerval,
JOIN ID, tipo, checeointerval
FROM hotel.facilitymanagemen_equipo
ORDER BY checeo + checeointerval;
My database is very big and I don't want this in another column in the the table "facilitymanagement".
I trust very much in w3school, there I found this https://www.w3schools.com/sql/sql_join_inner.asp.
CREATE VIEW TABLE facilitymanagement
SELECT m.ID,
m.tipo,
e.tipo,
e.ID,
m.posicio,
m.checeo,
m.checeo + e.checeointerval,
m.checeocontrollado,
m.checeooficial,
m.checeooficial + e.checeooficialinterval,
m.checeooficialcontrollado,
m.controllero,
e.checeointerval,
e.checeooficialinterval
FROM hotel.facilitymanagemen (m)
INNER JOIN hotel.facilitymanagement_equipment (e)
ON m.tipo = e.ID
ORDER BY m.checeooficial + e.checeooficialinterval;
I'm a german and by law we are to chec the equipments in a building that has to do with fire to chec by a specialist and a responsable authority, moreover we have for everything an insurance and if the interval are exceeded they don't pay, so this View list is very important, I added some columne aboveall "checeooficial" and this columne with the interval give the ORDER BY criteria.
If my code works I cannot try.
Because I have 2 data bases I precise from whitch one I want the data in the FROM and the INNER JOIN clause it is the data base "hotel".
I added a WHERE claus to filter the lines, so the VIEW TABLE contains only the data after the actual system-date and I ORDERed DESC; so the most urget equipment apears first.
CREATE VIEW TABLE facilitymanagement AS
SELECT m.ID,
m.tipo,
e.tipo,
e.ID,
m.posicio,
m.checeo,
m.checeo + e.checeointerval,
m.checeocontrollado,
m.checeooficial,
m.checeooficial + e.checeooficialinterval,
m.checeooficialcontrollado,
m.controllero,
e.checeointerval,
e.checeooficialinterval
FROM hotel.facilitymanagemen (m)
INNER JOIN hotel.facilitymanagement_equipment (e)
ON m.tipo = e.ID
WHERE m.checeooficial + e.checeooficialinterval > NOW()
ORDER BY m.checeooficial + e.checeooficialinterval DESC;
Instead of NOW() better would be to call the CURDATE()-function.
Hi there I do have one table called user with the following :
cust_num|trans_num|date_of_transaction|price_of_product|id_of_product
and another table called customer with the following:
cont_id|transaction_id|transaction_date|prod_price_net|prod_id
basically I want to map trough it, in the end I would like to have another table with the mapped values,so joind...
and having this :
Sum of distinct “cust_num”
Sum of distinct “trans_num”
Average basket of transactions “avg_transactions”
in another table or as query so that I can ask the database that sum and average #.#
Thanks for the help guys much appreciated.
following the suggestion to give example here we go :
user tb
cust_num|trans_num|date_of_transaction|price_of_product|id_of_product
100504335|100503936|10/11/2019|67023|100059589
100406909|100402572|3/30/2017|1101|101101914
100006079|100007232|5/21/2013|8530|100055307
customer tb
cont_id|transaction_id|transaction_date|prod_price_net|prod_id
100504335|100503936|10/11/2019|67023|100059589
100406909|100402572|3/30/2017|1101|101101914
100006079|100007232|5/21/2013|8530|100055307
100202212|100202600|4/28/2018|470|101101914
100003470|100019059|3/19/2019|20844|790199694
100003470|100003687|9/8/2010|20000|790005573 0
mapped tb
c_id| t_id| t_d| p_pe_n| p_id
100504335|100503936|10/11/2019|67023|100059589
100406909|100402572|3/30/2017|1101|101101914
100006079|100007232|5/21/2013|8530|100055307
100202212|100202600|4/28/2018|470|101101914
100003470|100019059|3/19/2019|20844|790199694
100003470|100003687|9/8/2010|20000|790005573
result
sum_price_cust| sum_of_t| avg_t
67023|1|1
1101|1| 1
8530|1| 1
470|1| 1
20844+20000|2| 2
Should be something like this:
SELECT SUM(m.price_of_product) as sum_price_cust,
COUNT(m.cust_num) as sum_of_t,
SUM(m.price_of_product) / COUNT(m.cust_num) as avg_t
FROM (SELECT u.cust_num, u.trans_num, u.date_of_transaction, u.price_of_product, u.id_of_product
FROM user u
UNION ALL
SELECT c.*
FROM customer c
) m
GROUP BY m.cust_num
Not quite sure if average number is what you want though.
Okay I have look through several posts about SQL running slow and I didn't see anything similar to this, so my apologies if I missed one. I was asked about a month ago to create a view that would allow the user to report what hasn't been billed yet, and the SQL is joining 4 tables together. One is 1.2 million records ish. the rest are between 80K - 250K. (Please note that this should only return around 100 records after the where statements).
SELECT C.Cltnum + '.' + C.CltEng AS [ClientNum],
C.CPPLname,
w.WSCDesc,
MIN(w.Wdate) AS [FirstTDate],
w.WCodeCat,
w.WCodeSub,
SUM(w.Wbilled) AS [Billed],
SUM(w.Whours * w.Wrate) AS [Billable Wip],
sum(ar.[ARProgress]) AS [Progress],
w.Winvnum,
-- dbo.GetInvoiceAmount(w.Winvnum) AS [InvoiceAmount],
SUM(cb.cinvar) AS [AR Balance]
FROM dbo.WIP AS w
--Never join on a select statement
--join BillingAuditCatagoriesT bac on w.WCodeCat = bac.Catagory and w.WCodeSub = bac.Subcatagory
INNER JOIN dbo.Clients AS C ON w.WCltID = C.ID
JOIN dbo.ClientBuckets AS cb on c.cltnum = cb.cltnum
JOIN dbo.AcctsRec AS ar on ar.arapplyto = w.[Winvnum]
-- WHERE w.wcodecat = '1AUDT'
GROUP BY C.Cltnum, C.CltEng, C.CPPLname, w.WCodeCat, w.Wdate, w.Winvnum, w.WCodeSub, w.WSCDesc
so, where I think there may be a problem is that Category is a varchar it is xat, ACT, BID and there are about 15 different Category. this is the same as SubCat. you will notice that there are 3 functions on this and they are GetJamesProgress Which is = (SELECT sum(Amount) From Progress Where inv = w.invnum) and the same with GetInvoiceAmount and GetJamesARBalance. I know that this is bad to do but when I join by invoice number it takes even longer than with out them.
Please help thanks so much!
I need to generate a related of all "Tickets" that is open and with status "Analysis",i need to do a join in two tables.
The first one:
SELECT TOP 1000
[nsu_sugestao]
,[num_sugestao_papel]
,[edv_promotor]
FROM [NovoCLIC].[dbo].[Sugestao]
Exemplo of this select:
nsu_sugestao num_sugestao_papel edv_promotor
1 372759 92602045
In this one, num_sugestao_papel is the number of the ticket, edv_promotor is the person the owns it.
The second one:
SELECT TOP 1000
[iClic]
,[iStatus]
,[dtDateCreated]
FROM [NovoCLIC].[dbo].[T_STATUS_CLIC]
Exemple:
iClic iStatus dtDateCreated
1 1 1999-01-25 13:33:00.000
The iClic is the ticket number, the iStatus is the status currently and dtDateCreated.
I need to show all Tickets the the last status is analysis, the status analysis is the number 2 in iStatus column, i don't need the tickets that already are in different numbers than 2.
SELECT TOP 1000 [nsu_sugestao]
-- ,n.num_sugestao_papel
-- ,n.edv_promotor
-- ,s.iStatus
-- ,s.dtDateCreated
-- FROM [NovoCLIC].[dbo].[Sugestao]
-- with
-- select s.dtDateCreated,MAX(s.EventDate)latestDate
-- from [NovoCLIC].[dbo].[T_STATUS_CLIC] s
-- right join [NovoCLIC].[dbo].[T_STATUS_CLIC] s on [NovoCLIC].[dbo].[Sugestao] =
I'm trying something like this, but I'm getting a lot of errors.
Try the code given below and provide us feedback
SELECT
S.[nsu_sugestao]
,S.[num_sugestao_papel]
,S.[edv_promotor]
,T.[iClic]
,T.[iStatus]
,T.[dtDateCreated]
FROM [NovoCLIC].[dbo].[Sugestao] S
INNER JOIN [NovoCLIC].[dbo].[T_STATUS_CLIC] T on S.nsu_sugestao = T.iClic
INNER JOIN (
SELECT
[iClic]
,Max([dtDateCreated]) As LatestDateCreated
FROM
[NovoCLIC].[dbo].[T_STATUS_CLIC]
GROUP BY
[iClic]
) TM ON T.iClic = TM.iClic AND T.dtDateCreated = TM.LatestDateCreated
WHERE
T.iStatus = 2
I got a quite complex query (at least for me).
I want to create a list of users that are ready to be paid. There are 2 conditions that need to be met: order status should be 3 and the total should be more then 50. Currently I got this query (generated with Codeingiter active record):
SELECT `services_payments`.`consultant_id`
, `consultant_userdata`.`iban`
, `consultant_userdata`.`kvk`, `consultant_userdata`.`bic`
, `consultant_userdata`.`bankname`
, SUM(`services_payments`.`amount`) AS amount
FROM (`services_payments`)
JOIN `consultant_userdata`
ON `consultant_userdata`.`user_id` = `services_payments`.`consultant_id`
JOIN `services`
ON `services`.`id` = `services_payments`.`service_id`
WHERE `services`.`status` = 3
AND `services_payments`.`paid` = 0
HAVING `amount` > 50
The services_payments table contains the commissions, consultant_userdata contains the userdata and services keeps the order data. The current query only gives me 1 result while I'm expecting 4 results.
Could someone please tell me what I'm doing wrong and what would be the solution?
For ActiveRecord, rsanchez' answer would be more of
$this->db->group_by('services_payments.consultant_id, consultant_userdata.iban, consultant_userdata.kvk, consultant_userdata.bic, consultant_userdata.bankname');