I am in fight with an sql query i cant seem to figure out, and hope somebody might help me with this.
I got two tables i want to have connected together. My first table is the 'achievement' table, which has 3 fields: achievementId, AchievementName and ZoneId
My second table is a table that is in between my User and this achivement table, which stores basically also 3 items. The UserId, AchievementId and IsChecked. It refers here to a checkbox.
So I am working on a page that shows all the achievements in the form of a checkbox list, so I want to have those who are already achieved to be shown in the list, however, the query i am trying to get fails completely and I have no idea how to fix it. I tried the below item, but off course I failed miserable. So I was hoping if you guys could help me out here and adjust my query.
My current query is
Select * from Achievement
Where Zone = '2'
LEFT JOIN Achievement_User
Where Achievement_User.UserId='2'
And Achievement_User.AchievementId = Achievement.AchievementId
but it fails off course. I probably have the syntax wrong, but I cant figure it out. If somebody could help me out?
EDIT I think i explained this wrong. But the left join shows only the ones that match, however, I need the full list of the achievement, which is like 40 rows, it only shows now the rows where there is data from the Achievement_User with the query supplied by #Aquillo. I prefer ot have all 40 rows supplied by the Achivements with zone 2
First you have two WHERE-clauses, secondly a JOIN is a JOIN ON. Try this:
Select *
from Achievement
LEFT JOIN Achievement_User ON Achievement_User.AchievementId = Achievement.AchievementId
Where Zone = '2'
Update in response to OP's update
In that case you shouldn't use a restriction on the UserID, try the above query.
Related
Ok - I'm rewording my question in hopes of getting as response. I (with help from a co-worker) have created the following SQL query that pulls the EXACT results that I need to appear in an SSRS chart:
select
(SELECT pfsp.SavingsGoal
FROM Projects AS p INNER JOIN
Projects_PerformanceServicesProject AS pfsp ON p.Id = pfsp.Id INNER JOIN
ProjectSavingsGoalTypes AS gt ON pfsp.ProjectSavingsGoalType_Id = gt.Id
WHERE (p.Id = #Project_ID)) as SavingsGoal,
(SELECT
Sum(identifiedSum)
FROM #Yaks where UPPER(name) = 'DECLINED'
GROUP BY name)as IdentifiedDeclined,
(SELECT
Sum(identifiedSum)
FROM #Yaks) as identifiedTotal,
(SELECT
Sum(implementableSum)
FROM #Yaks where upper(name) = 'APPROVED'
GROUP BY name) as implementableSavingsApproved,
(SELECT
Sum(implementedSum)
FROM #Yaks
) as implementedSavingsTotal
What the chart should ultimately look like (generally speaking):
http://i1365.photobucket.com/albums/r745/twarden11/chart_mockup_zps22cfdbf3.png
Telling you everything I've tried would take all my characters, and would be good for a laugh, and that's about it. It was also be futile, as I am an extreme novice (this is my first time to build a chart - ever, please be clear and speak in non-technical terms when possible), and my efforts I can assure had nothing to do with what I need to be trying.
So what I need are plain instructions on how to turn this query into the table graphic that I've included. I can't express how desperate I am at this point. My co-worker said it would be easier to simply pull the exact data that I need in the query, but never told me how to convert the query to a chart.
Thanks so much.
I would redesign the SQL query to return 2 columns and 5 rows. The 1st column would describe the category e.g. Goal, Identified etc. The 2nd column would present the $ values.
This would probably require a series of SELECT ... UNION ALL ... clauses, one for each of the 5 rows required.
Then I would add the 1st column to the chart as the Category Group, and the 2nd column as the Values (series).
I have 5 different tables T_DONOR, T_RECIPIENT_1, T_RECIPIENT_2, T_RECIPIENT_3, and T_RECIPIENT_4. All 5 tables have the same CONTACT_ID.
This is the T_DONOR table:
T_RECIPIENT_1:
T_RECIPIENT_2:
This is what I want the final table to look like with more recipients and their information to the right.
T_RECIPIENT_3 and T_RECIPIENT_4 are the same as T_RECIPIENT_1 and T_RECIPIENT_2 except that they have different RECIPIENT ID and different names. I want to combine all 5 of these tables so on one line I can have the DONOR_CONTACT_ID which his information, and then all of the Recipient's information.
The problem is that when I try to run a query, it does not work because not all of the Donors have all of the recipient fields filled, so the query will run and give a blank table. Some instances I have a Donor with 4 Recipients and other times I have a Donor with only 1 Recipient so this causes a problem. I've tried running queries where I connect them with the DONOR_CONTACT_ID but this will only work if all of the RECIPIENT fields are filled. Any suggestions on what to do? Is there a way I could manipulate this in VBA? I only know some VBA, I'm not an expert.
First I think you want all rows from T_DONOR. And then you want to pull in information from the recipient tables when they include DONOR_CONTACT_ID matches. If that is correct, LEFT JOIN T_DONOR to the other tables.
Start with a simpler set of fields; you can add in the "name" fields after you get the joins set to correctly return the rest of the data you need.
SELECT
d.DONOR_CONTACT_ID,
r1.RECIPIENT_1,
r2.RECIPIENT_1
FROM
(T_DONOR AS d
LEFT JOIN T_RECIPIENT_1 AS r1
ON d.ORDER_NUMBER = r1.ORDER_NUMBER)
LEFT JOIN T_RECIPIENT_2 AS r2
ON d.ORDER_NUMBER = r2.ORDER_NUMBER;
Notice the parentheses in the FROM clause. The db engine requires them for any query which includes more than one join. If possible, set up your joins in Design View of the query designer. The query designer knows how to add parentheses to keep the db engine happy.
Here is a version without aliased table names in case it's easier to understand and set up in the query designer ...
SELECT
T_DONOR.DONOR_CONTACT_ID,
T_RECIPIENT_1.RECIPIENT_1,
T_RECIPIENT_2.RECIPIENT_1
FROM
(T_DONOR
LEFT JOIN T_RECIPIENT_1
ON T_DONOR.ORDER_NUMBER = T_RECIPIENT_1.ORDER_NUMBER)
LEFT JOIN T_RECIPIENT_2
ON T_DONOR.ORDER_NUMBER = T_RECIPIENT_2.ORDER_NUMBER;
SELECT T_DONOR.ORDER_NUMBER, T_DONOR.DONOR_CONTACT_ID, T_DONOR.FIRST_NAME, T_DONOR.LAST_NAME, T_RECIPIENT_1.RECIPIENT_1, T_RECIPIENT_1.FIRST_NAME, T_RECIPIENT_1.LASTNAME
FROM T_DONOR
JOIN T_RECIPIENT_1
ON T_DONOR.DONOR_CONTACT_ID = T_RECIPIENT_1.DONOR_CONTACT_ID
This shows you how to JOIN the first recipient table, you should be able to follow the same structure for the other three...
I'm having an issue getting this SQL query to work properly.
I have the following query
SELECT apps.*,
SUM(IF(adtracking.appId = apps.id AND adtracking.id = transactions.adTrackingId, transactions.payoutAmount, 0)) AS 'revenue',
SUM(IF(adtracking.appId = apps.id AND adtracking.type = 'impression', 1, 0)) AS 'impressions'
FROM apps, adtracking, transactions
WHERE apps.userId = '$userId'
GROUP BY apps.id
Everything is working, HOWEVER for the 'impressions' column I am generating in the query, I am getting a WAY larger number than there should be. For example, one matching app for this query should only have 72 for 'Impressions' yet it is coming up with a value of over 3,000 when there aren't even that many rows in the adtracking table. Why is this? What is wrong here?
Your problem is you have no join conditions, so you are getting every row of every table being joined in your query result - called a cartesian product.
To fix, change your FROM clause to this:
FROM apps a
LEFT JOIN adtracking ad ON ad.appId = a.id
LEFT JOIN transactions t ON t.adTrackingId = ad.id
You haven't provided the schema for your tables, so I guessed the names of the relevant columns - you may have to adjust them. Also, your transaction table may join to adtracking - it's impossible to know from your question, so agin you have have to alter things slightly. Hopefully you get the idea.
Edit:
Note: your group-by clause is incorrect. You either need to list every column of apps (not recommended), or change your select to only select the id column from apps (recommended). Change your select to this:
SELECT apps.id,
-- rest of query the same
Otherwise you'll get weird, incorrect, results.
I have a point system and I'm trying to add them together. They are on two different tables and I'm using a subquery to get both totals and add them together. Both subqueries on their own work fine, but adding them together gives me a far greater number than it's supposed to.
Here's my query:
SELECT (SUM(tbl_achieve.achieve_points)+SUM(tbl_assign.assign_points))
FROM
(SELECT DISTINCT(tbl_achievements.achieve_id), tbl_achievements.achieve_points FROM tbl_achievements INNER JOIN tbl_studentachieve ON tbl_studentachieve.achieve_id = tbl_achievements.achieve_id AND tbl_studentachieve.student_ID = 8 AND tbl_achievements.achieve_cat = "main" ) as tbl_achieve,
(SELECT DISTINCT(tbl_assignments.assign_id), assign_points FROM tbl_assignments INNER JOIN tbl_studentassign ON tbl_studentassign.assign_id = tbl_studentassign.assign_id WHERE tbl_assignments.assign_cat = "main" AND tbl_studentassign.student_id = 8 AND tbl_studentassign.assign_status = "submitted") as tbl_assign
I think what the problem is, is that it puts both row counts together. So instead of having 2 rows of 10 points, I have 10 rows of 10 points because of the other table's number.
Any idea what I could be missing?
This one is going to be tough without knowing anything about your database. I ran the following query:
select SUM(feed_id) + SUM(user_id) FROM events where 1
on my own db and it returns the correct addition of those complete rows together as only one row. It sounds like that is not what you want, but that is the expected behavior of mySQL. perhaps a more detailed explanation of what you're looking for would help to get you where you need to be. Cheers...
Well this will be hard to explain but ill do my best
The thing is i have 4 tables all with a specific column to relate to eachother.
1 table with users(agent_users) , 1 with working hours(agent_pers), 1 with sold items(agent_stat),1 with project(agent_pro)
the user and the project table is irrelevant in the issue at hand but to give you a better understanding why certain tables is included in my query i decided to still mention them =)
The thing is that I use 2 pages to insert data to the working hour and the sold items during that time tables, then i have a third page to summarize everything for current month, the query for that is as following:
SELECT *, SUM(sv_p_kom),SUM(sv_p_gick),SUM(sv_p_lunch) FROM ((
agent_users
LEFT JOIN agent_pers ON agent_users.sv_aid = agent_pers.sv_p_uid)
LEFT JOIN
agent_stat ON agent_pers.sv_p_uid = agent_stat.sv_s_uid)
LEFT JOIN
agent_pro ON agent_pers.sv_p_pid=agent_pro.p_id
WHERE MONTH(agent_pers.sv_p_datum) =7 GROUP BY sv_aname
so the problem is now that i dont want sold items from previous months to get included in the data received, i know i could solve that by simple adding in the WHERE part MONTH(agent_stat.sv_s_datum) =7 but then if no items been sold that month no data at all will show up not the time or anything. Any aid on how i could solve this is greatly appreciated. if there's something that's not so clear dont hesitate to ask and ill try my best to answer. after all my english isn't the best out there :P
regards
breezer
Fair enough :) -- put the condition as a second condition in your JOIN clause. ON (agent_pers.sv_p_uid = agent_stat.sv_s_uid AND agent_stat.sv_s_datum = 7)
Is your data such that you could add (MONTH(agent_stat.sv_s_datum) =7 OR agent_stat.sv_s_datum IS NULL) to the WHERE clause?