SQL Join and getting count in table

SQL Join and getting count in table



Here is my SQL Fiddle



I am joining user and address table for now. Now i need to take the count of the logs table where in_time and out_time is filled


user


address


in_time


out_time



Here is my SQL Query that i have so far


select u.id, u.name, a.address from user u
left join address a on u.id = a.user_id
where u.id = 1



I.e., Output should be like this


id name address total_count proper_count
1 Alpha Chennai 4 3





What does log.user_id refer to? It's a varchar(50) which doesn't match any other column in other tables. Using explicit foreign keys would make it a lot easier to understand...
– Shawn
Aug 28 at 3:15


varchar(50)





Hint: use Group By and Count. Also, use NOT NULL on date fields that have no data in the Where condition.
– NoChance
Aug 28 at 3:16




3 Answers
3



Use below schema design for your requirement. User_id must be INTEGER, and use datetime datatype for in & out time column.


CREATE TABLE log (
id BIGINT,
user_id BIGINT,
in_time datetime,
out_time datetime
);

INSERT INTO log (id,user_id, in_time, out_time) VALUES (1,1,'2018-07-21 06:50:41','2018-07-21 10:50:41');
INSERT INTO log (id,user_id, in_time, out_time) VALUES (2,1,'2018-07-22 06:50:41','2018-07-22 10:50:41');
INSERT INTO log (id,user_id, in_time) VALUES (3,1,'2018-07-23 06:50:41');
INSERT INTO log (id,user_id, in_time, out_time) VALUES (4,1,'2018-07-24 06:50:41','2018-07-22 10:50:41');

select u.id as user_id, u.name, a.address, COUNT(in_time) AS total_count, COUNT(out_time) as proper_count
from log l
INNER JOIN user u on u.id = l.user_id
INNER JOIN address a on a.user_id = u.id
GROUP BY u.id , u.name, a.address



You could you this:


SELECT u.id, u.name, a.address,
COUNT(*) AS total_count,
SUM(CASE WHEN l.in_time = 0 OR l.out_time = 0 THEN 0 ELSE 1 END) AS proper_count
FROM USER u
LEFT JOIN address a
ON u.id = a.user_id
LEFT JOIN log l
ON u.id = l.user_id
WHERE u.id = 1
GROUP BY u.id, u.name, a.address;



You were just one step back from your expected query. just need another join with log table and use aggregate function


select u.id, u.name, a.address,
sum(case when in_time is not null and out_time is not null
then 1 else 0 end ) as total_count ,
SUM(CASE WHEN l.in_time = 0 OR l.out_time = 0 THEN 0 ELSE 1 END) AS proper_count
from user u
left join address a on u.id = a.user_id
left join log l on u.id=l.user_id
where u.id = 1
group by u.id, u.name, a.address



http://sqlfiddle.com/#!9/b2efe0/6


id name address total_count proper_count
1 Alpha Chennai 4 3






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế