I have three tables: T_O, T_C, & T_D they each have a date (run_date) and count column.
I need to select the data to show a summary by date for each count. I cannot get the nested sql to make it look right.
Needs to be grouped by Run Date. End result should look like this:
Run_Date Total Defects Not Closed Closed
05/29/13 178 100 78
06/04/13 204 103 101
06/11/13 234 114 120
I assume the three tables each have two columns: Run_Date and Total Defects, Not Closed or Closed. If you have a table with all the run dates in it, something like this will work:
SELECT
RunDates.Run_Date,
T_D.[Total Defects],
T_O.[Not Closed],
T_C.[Closed]
FROM ((
RunDates
LEFT JOIN T_C ON RunDates.Run_Date = T_C.Run_Date)
LEFT JOIN T_O ON RunDates.Run_Date = T_O.Run_Date)
LEFT JOIN T_D ON RunDates.Run_Date = T_D.Run_Date
If not, you will need to construct one using a UNION (not UNION ALL):
SELECT
RunDates.Run_Date,
T_D.[Total Defects],
T_O.[Not Closed],
T_C.[Closed]
FROM ((
(SELECT Run_Date FROM T_C
UNION
SELECT Run_Date FROM T_O
UNION SELECT Run_Date FROM T_D) AS RunDates
LEFT JOIN T_C ON RunDates.Run_Date = T_C.Run_Date)
LEFT JOIN T_O ON RunDates.Run_Date = T_O.Run_Date)
LEFT JOIN T_D ON RunDates.Run_Date = T_D.Run_Date
Related
I have a 3 Columns namely:
Payment Channel
Payment Name
Total Transaction
The Total Transaction will be the COUNT of each Payment Channel and Payment Name.
In the Payment Channel, values have PC01-PC09.
I want my result to display all the Payment Channel even if no transactions.
Here is my SQL Script:
SELECT
B2C_BUY_LOG.PG_CHANNEL AS Payment_Channel,
COMM_CODE.CODE_NAME AS Payment_Name,
COUNT(B2C_BUY_LOG.PAY_ID) AS Total_Transaction
FROM B2C_BUY_LOG
INNER JOIN B2C_BUY_HIST ON B2C_BUY_LOG.PAY_ID = B2C_BUY_HIST.PAY_ID
INNER JOIN COMM_CODE ON B2C_BUY_LOG.PG_CHANNEL = COMM_CODE.CODE
WHERE B2C_BUY_LOG.RET_CODE = 1
AND B2C_BUY_LOG.PAY_ID LIKE '190220%'
AND COMM_CODE.CODE IN('PC01', 'PC02', 'PC03', 'PC04', 'PC05', 'PC06', 'PC07', 'PC08', 'PC09')
GROUP BY Payment_Channel, Payment_Name;
Here is the result of my Query:
Payment_Channel Payment_Name Total_Transaction
PC01 Name-1 14
PC02 Name-2 2
PC03 Name-3 7
PC04 Name-4 9
PC06 Name-6 21
PC08 Name-8 18
PC09 Name-9 95
This query returns only the match values between the joined tables because it is INNER JOIN and the PC05 and PC07 is missing on the result because it has no transaction. I also tried different JOINS.
How can I display the PC05 and PC07 with a count of 0 if no transaction?
Thanks!
You can try below, Issue is with column on which filter is applied.
SELECT
B2C_BUY_LOG.PG_CHANNEL AS Payment_Channel,
COMM_CODE.CODE_NAME AS Payment_Name,
COUNT(B2C_BUY_LOG.PAY_ID) AS Total_Transaction
FROM B2C_BUY_LOG
left outer JOIN B2C_BUY_HIST ON B2C_BUY_LOG.PAY_ID = B2C_BUY_HIST.PAY_ID
left outer INNER JOIN COMM_CODE ON B2C_BUY_LOG.PG_CHANNEL = COMM_CODE.CODE
WHERE B2C_BUY_LOG.RET_CODE = 1
AND B2C_BUY_LOG.PAY_ID LIKE '190220%'
AND B2C_BUY_LOG.CODE IN('PC01', 'PC02', 'PC03', 'PC04', 'PC05', 'PC06', 'PC07', 'PC08', 'PC09')
GROUP BY Payment_Channel, Payment_Name;
Use left join and move your other condition in ON Clause like below-
SELECT
B2C_BUY_LOG.PG_CHANNEL AS Payment_Channel,
COMM_CODE.CODE_NAME AS Payment_Name,
COUNT(B2C_BUY_LOG.PAY_ID) AS Total_Transaction
FROM B2C_BUY_LOG
left JOIN B2C_BUY_HIST ON B2C_BUY_LOG.PAY_ID = B2C_BUY_HIST.PAY_ID and B2C_BUY_LOG.RET_CODE = 1 AND B2C_BUY_LOG.PAY_ID LIKE '190220%'
left JOIN COMM_CODE ON B2C_BUY_LOG.PG_CHANNEL = COMM_CODE.CODE
and COMM_CODE.CODE IN('PC01', 'PC02', 'PC03', 'PC04', 'PC05', 'PC06', 'PC07', 'PC08', 'PC09')
GROUP BY Payment_Channel, Payment_Name;
So guys, trying to write a query to get the count of statuses where project_id = ? and statuses in 'New' from a couple of tables so let me break it down.
I have these three tables
Case_Status
id case_status
1 New
2 Failed
3. Accepted
Referral
id case_status_id project_id application_id
1 1 1 20
2 2 1 21
Project
id name
1 project1
2 project2
So this is my query
SELECT COUNT(referrals.id) AS count_all, case_statuses.case_status AS counted
FROM "case_statuses" LEFT OUTER JOIN "referrals" ON "referrals"."case_status_id" = "case_statuses"."id"
WHERE "case_statuses"."deleted_at" IS NULL AND (case_statuses.case_status IN ('New') AND referrals.project_id = 1)
GROUP BY case_statuses.case_status;
This is my result
count_all counted
1 New
1 Failed
But I am expecting this result instead
count_all counted
1 New
1 Failed
0 Accepted
Does anyone know what's wrong with my query that isnt showing count for all the case_statuses?
Thanks
Conditions on the second table (in a left join) should be in the on clause:
SELECT COUNT(r.id) AS count_all, cs.case_status AS counted
FROM case_statuses cs LEFT OUTER JOIN
referrals r
ON r.case_status_id = cs.id AND r.project_id = 1
WHERE cs.deleted_at IS NULL AND cs.case_status NOT IN ('New')
GROUP BY cs.case_status;
Otherwise, the WHERE clause turns the outer join into an inner join.
change your query like this
SELECT COUNT(referrals.id) AS count_all, case_statuses.case_status AS counted
FROM "case_statuses" LEFT JOIN "referrals" ON "referrals"."case_status_id" = "case_statuses"."id" AND referrals.project_id = 1
WHERE "case_statuses"."deleted_at" IS NULL AND case_statuses.case_status NOT IN ('New')
GROUP BY case_statuses.case_status;
Given your data and the expected result you just need to loose the WHERE clause.
SELECT COUNT(referrals.id) AS count_all, case_statuses.case_status AS counted
FROM case_statuses
LEFT OUTER JOIN referrals ON referrals.case_status_id = case_statuses.id
GROUP BY case_statuses.case_status;
See this fiddle for details.
I have a table leave_form which looks like:
type id reporting_id leave_bal from_date_id leave_from to_date_id leave_to number leave_for status applied_dates_id pendays
personal 99 6 10 1023 full day 1313 full day 10 personal yes 1026 null
I have separate table for dates, so that I can refer these dates into leave_form. My leave_date table looks like:
date_id(AI) dates(UK)
1025 2016-02-18
1301 2016-02-20
1218 2016-02-16
This date_id I have inserted into from_date_id, to_date_id, applied_dates_id columns in leave_form table i.e. all dates are inserted into leave_date table and from this table I am only referring the date_id into leave_form table.
There is also a table that keeps the emp_code and emp_name. My personal table is:
id(AI) emp_code(PK) emp_name
99 K0209 Nijo
When I am trying to fetch the date for from_date_id, to_date_id, applied_dates_id column from leave_form table I don't get any values.
My query for fetching the dates is:
select g.type, a.emp_code, h.rm_id, h.rm_name, g.leave_bal, i1.dates as from_date,
g.leave_from, i2.dates as to_date, g.leave_to, g.number, g.leave_for, g.status,
i3.dates as applied_date, g.pendays
from personal a
inner join leave_form g
on a.id = g.id
inner join inform_to_rm h
on h.reporting_id = g.reporting_id
inner join leave_dates i1
on i1.dates = g.from_date_id
inner join leave_dates i2
on i2.dates = g.to_date_id
inner join leave_dates i3
on i3.dates = g.applied_dates_id
where a.emp_code = 'K0209';
It shows me result like:
type, emp_code, rm_id, rm_name, leave_bal, from_date, leave_from, to_date, leave_to, number, leave_for, status, applied_date, pendays
i.e no data gets returned when I am executing this query.
I would agree with one of the comments to the question. I would recommend referencing the date directly in the leave_form table instead of a FK to a table with dates. But back to the question. You haven't described all of your tables completely, so it is possible that there are multiple problems that I can't see, however, there is definitely one problem.
Your query joins on
inner join leave_dates i1
on i1.dates = g.from_date_id
inner join leave_dates i2
on i2.dates = g.to_date_id
inner join leave_dates i3
on i3.dates = g.applied_dates_id
This is incorrect. leave_dates.dates is the actual DATE, while the columns that you are joining on (leave_form.from_date_id, leave_form.to_date_id, leave_form.applied_dates_id) are foreign key references.
For example, 1023 does not equal 2016-02-18 so you get no match. Replacing the above query-snippet with the following would correct this particular problem.
inner join leave_dates i1
on i1.date_id = g.from_date_id
inner join leave_dates i2
on i2.date_id = g.to_date_id
inner join leave_dates i3
on i3.date_id = g.applied_dates_id
So, I have a simple left join:
SELECT
a.SetNumber,
a.SetID,
COUNT(a.QuantityOwned) AS Pwnd,
b.ImageURL,
COUNT(a.Quantity) AS Cmplt
FROM a
LEFT JOIN b ON a.SetNumber = b.Number
GROUP BY a.SetID
That produces this:
SetNumber 11 21 13
SetID 1 2 1
Pwnd 45 33 50
Cmplt 50 36 50
ImgURL a.jpg b.jpg c.jpg
Which is fine, when I use the data, but I want a >> arrow in my pagination and to do that I would like to get the amount of rows, ie the desired result in this case is:
3
I know I can count rows with COUNT(*) in one table, but how do I do it in a left join?
I guess you need to do that
SELECT COUNT(1) FROM (SELECT a.SetNumber, a.SetID, COUNT(a.QuantityOwned) AS Pwnd ,b.ImageURL, COUNT(a.Quantity) AS Cmplt
FROM a
LEFT JOIN b ON a.SetNumber = b.Number
GROUP BY a.SetID) table1
The left join is'nt the problem here, it's the group by
But a lot of "Libraries" Allows to count the result count like
$result->count();
In my opinion if you use JAVA, php etc... you don't need to do another request
SELECT (select count(u.ag_code)
from table1 as u inner join table2 as tc
on u.industry_id=tc.tempcatid
where u.ag_code!=0) as agnt,
(select count(u.ag_code)
from table1 as u inner join table2 as tc
on u.industry_id=tc.tempcatid where u.ag_code=0),as dircus,
tc.catename from table1 as u inner join table2 as tc
where u.industry_id=tc.tempcatid
group by tc.tempcatid
this query have error
i need two count and category name in one query
this is the condition for count
ag_code!=0
ag_code=0
in table1 have column ag_code (this have 0 and nonzero value)
my result need like this
Full Texts
agent customer catename
11 3 Real Estate
15 1 Automobile
3 0 Medical
34 77 Business
1 45 Travel & Hotels
11 3 Construction & Engineering
SELECT tc.catename,
count(case when u.ag_code!=0 then 1 end) agnt,
count(case when u.ag_code =0 then 1 end) dircus
from table1 as u
inner join table2 as tc on u.industry_id=tc.tempcatid
group by tc.tempcatid, tc.catename
Hi There you might be able to use the below example to get back the result you require.
Select SUM(Inactive) Inactive ,SUM(Active) Active FROM
(
Select Count(t1.UserId) Inactive,0 Active
FROM
(select * from users where inactive=1) as t1
UNION
SELECT 0 Inactive,Count(t2.UserId) Active FROM
(select * from users where inactive=0) as t2
) as result