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.
Related
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
I have 2 tables (cycles and merged_cycles). "cycles" has 2 fields I need to target (userid and cycleid) and "merged_cycles" also has 2 targeted fields (cycleid1 and cycleid2). I need to know all cycles.userid that have more than one record in "cycles", so long as the corresponding cycles.cycleid for any matching record does not appear in any record in "merged_cycles" in either merged-cycles.cycleid1 OR merged_cycles.cycleid2. I currently have it working using 2 different queries, but i was curious if it could be done in one. Here's what i have tried so far:
SELECT cycles.cycleid, cycles.userid, cycles.COUNT(*),
merged_cycles.cycleid1, merged_cycles.cycleid2
FROM cycles,merged_cycles
WHERE merged_cycles.cycleid1 != cycles.cycleid && merged_cycles.cycleid2 != cycles.cycleid
GROUP BY cycles.userid
HAVING cycles.count(*) > 1
Thanks for any suggestions!
I think this does what you want:
SELECT c.cycleid
FROM cycles c
WHERE NOT EXISTS (SELECT 1
FROM merged_cycles mc
WHERE c.cycleid IN (mc.cycleid1, mc.cycleid2)
)
GROUP BY c.userid
HAVING count(*) > 1;
I'm having difficulties creating the result i want on a specific situation.
I have a two tables:
1: sales by warehouse:
+-------------+---------------+--------------+
warehouse | type -------- | value
+-------------+---------------+--------------+
A--------------XX-------------234234----------
A-------------- YY------------ 234343--------------
A-------------- ZZ------------ 534534--------------
B-------------- XX------------ 234432--------------
B-------------- YY------------ 767563--------------
B-------------- ZZ------------ 312332--------------
c-------------- XX------------ 234234--------------
....
2: users by warehouse:
user--------|---warehouse
john--------|-- A
john--------|-- B
john--------|-- C
peter-------|-- A
Daniel------|-- C
Kim---------|-- B
Kim---------|-- C
....
So i created this query:
select a.warehouse, type, value from sales_by_warehouse A
left join users_warehouse B
ON A.warehouse=B.warehouse
where b.user = 'user_logged_on_software'
This works perfectly by giving me the warehouse that the user has access to and its values but, now, i wanted to invert the result such as in a pivot table so that if the user was Peter, this would be the result:
type--|-----A----
XX----|-----123123
YY----|-----3423423
ZZ----|-----3423345
And if the user was kim:
type--|-----B---------|-----C
XX----|-----123123-|---234324
YY----|-----423423-|---245435
ZZ----|-----423345-|---456233
Is there a way to do this with only select statements, without views or PS?
I was given a task to show the CPU usage trend as part of a building process which also do regression test.
Each individual test case run has a record in the table RegrCaseResult. The RegrCaseResult table looks something like this:
id projectName ProjectType returnCode startTime endTime totalMetrics
1 'first' 'someType' 16 'someTime' 'someOtherTime' 222
The RegrCaseResult.totalMetrics is a special key which links to another table called ThreadMetrics through ThreadMetrics.id.
Here is how ThreadMetrics will look like:
id componentType componentName cpuTime linkId
1 'Job Totals' 'Job Totals' 'totalTime' 34223
2 'parser1' 'parser1' 'time1' null
3 'parser2' 'generator1' 'time2' null
4 'generator1' 'generator1' 'time3' null
------------------------------------------------------
5 'Job Totals' 'Jot Totals' 'totalTime' 9899
...
The rows with the compnentName 'Job Totals' is what the totalMetrics from RegrCaseResult table will link to and the 'totalTime' is what I am really want to get given a certain projectType. The 'Job Totals' is actually a summation of the other records - in the above example, the summation of time1 through time3. The linkId at the end of table ThreadMetrics can link back to RegrCaseResult.id.
The requirements also states I should have a way to enforce the condition which only includes those projects which have a consistent return code during certain period. That's where my initial question comes from as follows:
I created the following simple table to show what I am trying to achieve:
id projectName returnCode
1 'first' 16
2 'second' 16
3 'third' 8
4 'first' 16
5 'second' 8
6 'first' 16
Basically I want to get all the projects which have a consistent returnCode no matter what the returnCode values are. In the above sample, I should only get one project which is "first". I think this would be simple but I am bad when it comes to database. Any help would be great.
I tried my best to make it clear. Hope I have achieved my goal.
Here is an easy way:
select projectname
from table t
group by projectname
having min(returncode) = max(returncode);
If the min() and max() values are the same, then all the values are the same (unless you have NULL values).
EDIT:
To keep 'third' out, you need some other rule, such as having more than one return code. So, you can do this:
select projectname
from table t
group by projectname
having min(returncode) = max(returncode) and count(*) > 1;
select projectName from projects
group by projectName having count(distinct(returnCode)) = 1)
This would also return projects which has only one entry.
How do you want to handle them?
Working example: http://www.sqlfiddle.com/#!2/e7338/8
This should do it:
SELECT COUNT(ProjectName) AS numCount, ProjectName FROM (
SELECT ProjectName FROM Foo
GROUP BY ProjectName, ReturnCode
) AS Inside
GROUP BY Inside.ProjectName
HAVING numCount = 1
This groups all the ProjectNames by their names and return codes, then selects those that only have a single return code listed.
SQLFiddle Link: http://sqlfiddle.com/#!2/c52b6/11/0
You can try something like this with Not Exists:
Select Distinct ProjectName
From Table A
Where Not Exists
(
Select 1
From Table B
Where B.ProjectName = A.ProjectName
And B.ReturnCode <> A.ReturnCode
)
I'm not sure exactly what you're selecting, so you can change the Select statement to what you need.
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');