Initial:
1_ = country
2_ = gender
3_ = purpose
How to make SQL query show data like this
Try this(this is a mysql approach), and next time please post your table schema with text not pictures if you want to ask a question in SO.
select
u.id,
u.name,
max(case when ud.code like '1_%' then fc.code_name else '' end) as country,
max(case when ud.code like '2_%' then fc.code_name else '' end) as gender,
max(case when ud.code like '3_%' then fc.code_name else '' end) as purpose,
group_concat(distinct vt.visit_to) as visit_to
from t_users u
left join t_user_details ud
on u.id = ud.id_user
left join t_field_code fc
on ud.code = fc.code
left join t_visit_to vt
on u.id = vt.id_user
group by u.id, u.name
This would do the job more or less:
SELECT user.id,
user.name,
country.code_name AS country,
gender.code_name AS gender,
purpose.code_name AS purpose,
visit.to_to
FROM t_users AS user
JOIN t_user_details AS details
ON user.id = details.user_id
LEFT OUTER JOIN t_field_code AS country
ON details.code = country.code
AND country.code LIKE '1_%'
LEFT OUTER JOIN t_field_code AS gender
ON details.code = gender.code
AND gender.code LIKE '2_%'
LEFT OUTER JOIN t_field_code AS purpose
ON details.code = purpose.code
AND purpose.code LIKE '3_%'
LEFT OUTER JOIN t_visit_to AS visit
ON user.id = visit.id_user
ORDER BY user.id;
The only problem is that it gives multiple rows in case the user has more places to visit.
Related
I have sql script that needed further tweaking to produce a simpler report as shown below. I am looking for the results to be grouped on username.
userName, weeknum, and #of wins
select
p.userID,
s.gameID,
p.pickID,
u.userName,
s.weekNum,
s.homeID,
s.homeScore,
s.visitorID,
s.visitorScore
from nflp_picks p
inner join nflp_users u
on p.userID = u.userID
inner join nflp_schedule s
on p.gameID = s.gameID
where s.weekNum = 6
and u.userName <> 'admin'
order by p.userID, s.gameTimeEastern, s.gameID;
Make you current query as derived table, then do like this:
SELECT userName,
weeknum,
SUM(CASE WHEN pickID=homeID
AND homeScore > visitorScore THEN 1
WHEN pickID=visitorID
AND visitorScore > homeScore THEN 1
ELSE 0 END) AS "# of wins"
FROM
(SELECT
p.userID,
s.gameID,
p.pickID,
u.userName,
s.weekNum,
s.homeID,
s.homeScore,
s.visitorID,
s.visitorScore
FROM nflp_picks p
INNER JOIN nflp_users u
ON p.userID = u.userID
INNER JOIN nflp_schedule s
ON p.gameID = s.gameID
WHERE s.weekNum = 6
AND u.userName <> 'admin'
ORDER BY p.userID, s.gameTimeEastern, s.gameID) a
GROUP BY userName, weeknum;
SUM() over CASE expression checking if pickID value is the same as the winning team.
Here's a trimmed down fiddle example
Sorry I did not know how to word the question, it's actually a little more complicated than it sounds. I've got the following in my database; please see screenshot in the link:
http://prntscr.com/utyopv
I am generating a report using BIRT and I want that when priority = 2, it displays a value of 'High'. Similarly when priority = 1, it shows 'Medium' and when it = 1, it shows 'Low'.
But I also want that when estimated_time = 4, it displays '16+hrs', when estimated_time = 3, it shows '< 16 hrs' etc etc.
Now I've managed to do the priority using the code below;
SELECT date_created, CONCAT(first_name, " ", last_name) as user, e_name, cc_name, 'High' as priority, job_description, due_date, estimated_time, impediment
FROM job_planning as jp
INNER JOIN user as u
ON jp.user_id = u.id
INNER JOIN employer as e
ON jp.employer_id = e.id
INNER JOIN client_company as cc
ON jp.client_id = cc.id
WHERE jp.priority = '2'
union
SELECT date_created, CONCAT(first_name, " ", last_name) as user, e_name, cc_name, 'Medium' as priority, job_description, due_date, estimated_time, impediment
FROM job_planning as jp
INNER JOIN user as u
ON jp.user_id = u.id
INNER JOIN employer as e
ON jp.employer_id = e.id
INNER JOIN client_company as cc
ON jp.client_id = cc.id
WHERE jp.priority = '1'
union
SELECT date_created, CONCAT(first_name, " ", last_name) as user, e_name, cc_name, 'Low' as priority, job_description, due_date, estimated_time, impediment
FROM job_planning as jp
INNER JOIN user as u
ON jp.user_id = u.id
INNER JOIN employer as e
ON jp.employer_id = e.id
INNER JOIN client_company as cc
ON jp.client_id = cc.id
WHERE jp.priority = '0';
But how can I also include estimated_time? Can i use OR in mysql? I cannot use AND because then it would separate the two.
Thanks and appreciate any help.
SELECT ... ,
CASE priority WHEN 2 THEN 'High'
WHEN 1 THEN 'Medium',
ELSE 'Low'
END AS verbal_priority,
CASE estimated_time WHEN 4 THEN '16+hrs'
...
I have a query with a CASE statement that determines the variable object_name this variable is derived from either the x_ambitions table or the x_trybes table. The queries below were combined so that I could keep it simple by just executing one SQL query.
I've split the SELECT statement into two so that you have a better understanding. The two queries below. Work and pull the correct object_name from the database.
The problem I'm having when I combine the two queries is that the cases 'new_join_ambition','new_created_ambition','new_liked_ambition' object_name returns NULL in the LEFT JOIN.
In the combined query, If I bring the cases: 'new_join_ambition','new_created_ambition','new_liked_ambition' above the 'new_join_trybe','new_created_trybe','new_liked_trybe' cases. The opposite happens. The trybe rows return NULL.
The two SQL queries:
A: (Retrieve Object A)
SELECT
s.id,
s.object_id,
s.type,
s.postee_id,
s.user_id,
s.text,
s.registered,
CONCAT(u.x_first_name,' ',u.x_last_name) AS postee_name,
ui.image_id AS postee_image_id,
CASE s.type
WHEN 'new_join_ambition'
OR 'new_created_ambition'
OR 'new_liked_ambition'
THEN a.name
ELSE 'a'
END AS object_name
FROM
x_share s
LEFT JOIN
x_user u ON u.id = s.postee_id
LEFT JOIN
x_user_images ui ON ui.user_id = s.postee_id
LEFT JOIN
x_ambitions a ON s.type IN ('new_join_ambition', 'new_created_ambition', 'new_liked_ambition') AND s.object_id = a.id
LEFT JOIN
x_ambition_invites ai ON s.type IN ('new_join_ambition') AND s.object_id = ai.ambition_id AND s.postee_id = ai.to
LEFT JOIN
x_ambition_likes al ON s.type IN ('new_liked_ambition') AND s.object_id = al.ambition_id AND s.postee_id = al.profile_id
LEFT JOIN
x_ambition_owner aoo ON s.type IN ('new_created_ambition') AND s.object_id = aoo.ambition_id
WHERE
s.user_id = '%s'
ORDER BY
s.registered DESC
B: (Retrieve Object B)
SELECT
s.id,
s.object_id,
s.type,
s.postee_id,
s.user_id,
s.text,
s.registered,
CONCAT(u.x_first_name,' ',u.x_last_name) AS postee_name,
ui.image_id AS postee_image_id,
CASE s.type
WHEN 'new_join_trybe'
OR 'new_created_trybe'
OR 'new_liked_trybe'
THEN t.name
ELSE 'a'
END AS object_name
FROM
x_share s
LEFT JOIN
x_user u ON u.id = s.postee_id
LEFT JOIN
x_user_images ui ON ui.user_id = s.postee_id
LEFT JOIN
x_trybes t ON s.type IN ('new_join_trybe', 'new_created_trybe', 'new_liked_trybe') AND s.object_id = t.id
LEFT JOIN
x_trybe_invites ti ON s.type IN ('new_join_trybe') AND s.object_id = ti.trybe_id AND s.postee_id = ti.to
LEFT JOIN
x_trybes_likes tl ON s.type IN ('new_liked_trybe') AND s.object_id = tl.trybe_id AND s.postee_id = tl.profile_id
LEFT JOIN
x_trybe_owner too ON s.type IN ('new_created_trybe') AND s.object_id = too.trybe_id
WHERE
s.user_id = '%s'
ORDER BY
s.registered DESC
I've ran both the queries and have captured images of the results of both queries.
Set A:
Set B:
How can I combine the two without the object_name returning NULL? If you have any questions please use the comments and I'll reply without hesitation.
Thanks in advance
I don't know about the rest of your query, but your case statement is incorrect. You have:
(CASE s.type
WHEN 'new_join_ambition' OR 'new_created_ambition' OR 'new_liked_ambition'
THEN a.name
ELSE 'a'
END) AS object_name
The ORs end up treating the values as numbers, so this is equivalent to WHEN 0 THEN . . ..
What you want is this form of the case:
(CASE WHEN s.type IN ('new_join_ambition', 'new_created_ambition', 'new_liked_ambition')
THEN a.name
ELSE 'a'
END) AS object_name
The original query is
SELECT user_id, user_location, displayname FROM engine4_users WHERE user_location IS NOT NULL
Which prints the user_id, user_location and displayname,
Ex:
1 30,32 demo
2 30,31 demo
3 33,32 demo
but now i need ot get the user picture from another table and i have this,
SELECT s.storage_path as url FROM engine4_storage_files as s
LEFT JOIN engine4_users as u ON s.file_id = u.photo_id
Which prints the URL only
What I need is to add that url to the first query
Try this:
SELECT u.user_id, u.user_location, u.displayname, s.storage_path as url
FROM engine4_users u
LEFT JOIN engine4_storage_files s
ON s.file_id = u.photo_id
WHERE u.user_location IS NOT NULL
Extended by request in comments:
SELECT u.user_id, u.user_location, u.displayname, w.value as about, s.storage_path as url
FROM engine4_users u
LEFT JOIN engine4_user_fields_values w
ON w.item_id = u.user_id
LEFT JOIN engine4_storage_files s
ON s.file_id = u.photo_id
WHERE u.user_location IS NOT NULL
I'm very new to SQL/MySQL and Stackoverflow for that matter, and I'm trying to create a query through iReport (though I don't have to use iReport) for SugarCRM CE. What I need is to create a report that displays the number of "Referrals", "Voicemails", "Emails", and "Call_ins" that are linked to a specific "user" (employee). The query I currently have set up works; however it is running through the data multiple times generating a report that is 200+ pages. This is the code that I am currently using:
SELECT
( SELECT COUNT(*) FROM `leads` INNER JOIN `leads_cstm` ON `leads`.`id` = `leads_cstm`.`id_c` WHERE (leadtype_c = 'Referral' AND users.`id` = leads.`assigned_user_id`) ),
( SELECT COUNT(*) FROM `leads` INNER JOIN `leads_cstm` ON `leads`.`id` = `leads_cstm`.`id_c` WHERE (leadtype_c = 'VM' AND users.`id` = leads.`assigned_user_id`) ),
( SELECT COUNT(*) FROM `leads` INNER JOIN `leads_cstm` ON `leads`.`id` = `leads_cstm`.`id_c` WHERE (leadtype_c = 'Email' AND users.`id` = leads.`assigned_user_id`) ),
users.`first_name`,users.`last_name`
FROM
`users` users,
`leads` leads
I would appreciate any guidance!
You want to use conditional summation. The following uses MySQL syntax:
SELECT sum(leadtype_c = 'Referral') as Referrals,
sum(leadtype_c = 'VM') as VMs,
sum(leadtype_c = 'Email') as Emails,
users.`first_name`, users.`last_name`
FROM users join
`leads`
on users.`id` = leads.`assigned_user_id` INNER JOIN
`leads_cstm`
ON `leads`.`id` = `leads_cstm`.`id_c`
group by users.id;
You can use COUNT with CASE for this:
SELECT u.first_name,
u.last_name,
count(case when leadtype_c = 'Referral' then 1 end),
count(case when leadtype_c = 'VM' then 1 end),
count(case when leadtype_c = 'Email' then 1 end)
FROM users u
JOIN leads l ON u.id = l.assigned_user_id
JOIN leads_cstm lc ON l.id = lc.id_c
GROUP BY u.id
To match your exact results, you should probably use an OUTER JOIN instead, but this gives you the idea.
A Visual Explanation of SQL Joins