Using GoldMine CRM and wrote this query to try and find out people in our system based on a record Type, Lead Creation Date and Last Contact Date. Now when running the query it brings up multiple results say for John Smith it is showing me all of his information rather than just the last contacted date which I want. I'll paste the query below any help would be much appreciated.
SELECT DISTINCT CONTACT1.CONTACT AS Contact_Name,
Min(CONTACT1.CONTACT),
CONTACT1.KEY1 AS Rec_Type,
CONTACT1.CREATEON AS Lead_Type,
CONTHIST.LASTDATE AS Last_Contact,
CONTHIST.RESULTCODE AS Stamp
FROM CONTACT1
INNER JOIN CONTHIST
ON CONTACT1.ACCOUNTNO = CONTHIST.ACCOUNTNO
WHERE CONTACT1.KEY1 = 'Lead'
GROUP BY CONTACT1.CONTACT,
CONTACT1.KEY1,
CONTACT1.CREATEON,
CONTHIST.LASTDATE,
CONTHIST.RESULTCODE
HAVING Count(CONTACT1.CONTACT) = 1
ORDER BY CONTACT1.CREATEON
Related
Ok, hopefully I can explain this accurately. I work in SQL Server, and I am trying to get one row from a table that will show multiple rows for the same person for various reasons.
There is a column called college_attend which will show either New or Cont for each student.
My issue: my initial query narrows down the rows I'm pulling by Academic Year, which consists of two semesters: Fall of one year, and Spring of the following to create an academic year. This is why there are two rows returned for some students.
Basically, I need to generate an accurate count of those that are "New" and those that are "Cont", but I don't want both records for the same student counted. They will have two records because they will have one for spring and one for fall (usually). So if a student is "New" in fall, they will have a "Cont" record for spring. I want the query to show ONLY the "New" record if they have both a "New' and "Cont" record, and count it (which I will do in Report Builder). The other students will basically have two records that are "Cont": one for fall, and one "Cont" for spring, and so those would be considered the continuing ones or "Cont".
Here is the basic query I have so far:
SELECT DISTINCT
people.people_id,
people.last_name,
people.first_name,
academic.college_attend AS NewORCont,
academic.academic_year,
academic.academic_term,
FROM
academic
INNER JOIN
people ON people.people_id = academic.people_id
INNER JOIN
academiccalendar acc ON acc.academic_year = academic.academic_year
AND acc.academic_term = academic.academic_term
AND acc.true_academic_year = #Academic_year
I'm not sure if this can be done with a CASE statement? I thought of a GROUP BY, but then SQL Server will want me to add all of my columns to the GROUP BY clause, and that ends up negating the purpose of the grouping in the first place.
Just a sample of what I work with for each student:
People ID
Last
First
NeworCont
12345
Soanso
Guy
New
12345
Soanso
Guy
Cont
32345
Person
Nancy
Cont
32345
Person
Nancy
Cont
55555
Smith
John
New
55555
Smith
John
Cont
---------
------
-------
----------
Hopefully this sheds some light on the duplicate record issue I mentioned.
Without sample data its awkward to visualize the problem, and without the expected results specified it's also unclear what you want as the outcome. Perhaps this will assist, it will limit the results to only those who have both 'New' and 'Cont' in a single "true academic year" but the count seems redundant as this (I imagine) will always be 2 (being 1 New term and 1 Cont term)
SELECT
people.people_id
, people.last_name
, people.first_name
, acc.true_academic_year
, count(*) AS count_of
FROM academic
INNER JOIN people ON people.people_id = academic.people_id
INNER JOIN academiccalendar acc ON acc.academic_year = academic.academic_year
AND acc.academic_term = academic.academic_term
AND acc.true_academic_year = #Academic_year
GROUP BY
people.people_id
, people.last_name
, people.first_name
, acc.true_academic_year
HAVING MAX(academic.college_attend) = 'New'
AND MIN(academic.college_attend) = 'Cont'
I want to have a query which shows me records of only those employees that were absent on various dates.
I have two queries in MS Access 2010. One is qAllDatesAllEmp which has all monthly dates for all employees. Second is qDailyWorkEmp which shows date-wise the working of employees. Now, say Emp1 had only one missing date of 5-Jan-2019; emp2 had 15-Jan-2019 missing and so on, the result should show me only these missing dates with employee names. What i have tried so far is that I have joined the date field from qAllDatesAllEmp to the date field in qDailyWorkEmp with join arrow pointing from qAllDatesAllEmp to qDailyWorkEmp. I also added the date field from qDailyWorkEmp to the query grid below but removed the check mark. When I run this query, it gives blank result. I would really welcome the experts' help on how to get the desired output below.
My Sample Data:
Query1: qAllDatesAllEmp contains {WorkDate; empID; empName; CityBased}
Query2: qDailyWorkEmp contains {DailyDate; empID; empName; CityBased}
The SQL for this is as follows:
SELECT qDailyWorkEmp.empID, qDailyWorkEmp.empName, qDailyWorkEmp.CityBased
FROM qAllDatesAllEmp LEFT JOIN
qDailyWorkEmp
ON qAllDatesAllEmp.WorkDate = qDailyWorkEmp.DailyDate
WHERE (((qDailyWorkEmp.DailyDate) Is Null));
My desired output:
empID; EmpName; MissingDate
001; ABC; 5-Jan-2019
007; LMN; 15-Jan-2019
...and so on
From what I can gather from your question, it seems like you are on the right track with your left join query, however, since your qAllDatesAllEmp query contains the master data, you should output the data from this query for records for which the corresponding values are null in your qDailyWorkEmp query.
You also appear to have a typo in your query on this line:
qAllDatesAllEmp.WhatDate = qDailyWorkEmp.DailyDate
As you have stated in your question that the query qAllDatesAllEmp contains the fields:
Query1: qAllDatesAllEmp contains {WorkDate; empID; empName; CityBased}
WhatDate is not one of these fields.
You will also need to join the empID field in both queries, so that you are comparing the date field on a per-employee basis, as opposed to comparing each date with the dates for all employees - hence your join criteria should be:
qAllDatesAllEmp LEFT JOIN qDailyWorkEmp ON
qAllDatesAllEmp.WorkDate = qDailyWorkEmp.DailyDate AND
qAllDatesAllEmp.empID = qDailyWorkEmp.empID
With the information provided, I might suggest the following:
SELECT
qAllDatesAllEmp.empID,
qAllDatesAllEmp.empName,
qAllDatesAllEmp.CityBased
qAllDatesAllEmp.WorkDate as MissingDate
FROM
qAllDatesAllEmp LEFT JOIN qDailyWorkEmp ON
qAllDatesAllEmp.WorkDate = qDailyWorkEmp.DailyDate AND
qAllDatesAllEmp.empID = qDailyWorkEmp.empID
WHERE
qDailyWorkEmp.DailyDate IS NULL
Tables
ARMASTER = Customer Information
ORDERHEAD = Sales order information (Ship to, bill to)
Hey everyone! I'm still quite new to SQL and I do belive I'm picking it up fairly quick. I have been racking my brain on this for a few hours now and have asked around the office and nobody seems to have a solution.
I'm trying to identify how many times a customer account has been used as a SHIP TO location & a BILL TO location.
SELECT ARMASTER.CUSTOMER,
ARMASTER.DIVISION,
ARMASTER.STATUS,
ARMASTER.CUHEAD AS "MASTER",
COUNT (ORDERHEAD.BILLTO) AS "COUNT_BILL",
COUNT (ORDERHEAD.SHIPTO) AS "COUNT_SHIP"
FROM ARMASTER
LEFT OUTER JOIN ORDERHEAD
ON ARMASTER.CUSTOMER = ORDERHEAD.BILLTO
LEFT OUTER JOIN ORDERHEAD
ON ARMASTER.CUSTOMER = ORDERHEAD.SHIPTO
GROUP BY ARMASTER.CUSTOMER,
ARMASTER.DIVISION,
ARMASTER.STATUS,
ARMASTER.CUHEAD
And I'm not even remotley getting what I should be getting. However, when I remove one of my joins the count is exactly what it should be for either 1 or the other of them.
Any guidance would be muchly appreciated! Thank you!
I think it can also work
SELECT CUSTOMER,
DIVISION,
STATUS,
CUHEAD AS "MASTER",
(SELECT COUNT(1) FROM ORDERHEAD WHERE SHIPTO = ARMASTER.CUSTOMER ) AS "COUNT_BILL",
(SELECT COUNT(1) FROM ORDERHEAD WHERE BILLTO = ARMASTER.CUSTOMER ) AS "COUNT_SHIP"
FROM ARMASTER
I have two tables: TaskUser and PaperMaterial
TaskUser - Contains all users One column titled: User1) that could be a user in the PaperMaterial Table
PaperMaterial - Multi-column table where data could be written multiple times for the same user on multiple dates.
What I am trying to accomplish is a query that will show all users for a given date range and their total page count regardless if they have data in the papermaterial table.
IE:
Select Assigned_To,
Case
When PaperMaterial.Assigned_To = TaskUser.User1 then sum(Page_Count)
When PaperMaterial.Assigned_To <> TaskUser.User1 then '0'
End as Count1
From PaperMaterial
Inner Join TaskUser
on TaskUser.User1 = PaperMaterial.Assigned_To
Where Date_Assigned between ('06/09/2014') and ('06/13/2014')
Order By Assigned_To
Now, obviously this code does not work, because I am new to SQL and I am not very good yet. However, you should be able to see the goal here. which is:
Mike 0
Bob 200
Jen 0
Betty 125
so on, so forth
Thank you
I think this will do the trick
SELECT a.User1, ifnull(sum(Page_Count),0) AS Count1
From TaskUser AS a
LEFT JOIN PaperMaterial AS b
ON b.Assigned_To = a.User1
Where Date_Assigned between DATE('2014-06-09') AND DATE('2014-06-13')
GROUP BY a.User1
Order By a.User1
You might need to tweak the Date_Assigned logic, depending on your date formats... but the current clause you're using doesn't really make a lot of sense. I think the one above should do the job, assuming your Date_Assigned variable is formatted as a date or datetime
SELECT NMC.*, Exam.Final_Exam_Level
FROM
(SELECT Technicians.Technician_ID AS Technician_ID,
Technicians.First_Name AS First_Name,
Technicians.Surname AS Surname,
MAX(New_Models.Date_Issued) AS Last_Course_Date,
MAX(New_Models.Issue) AS Last_Issue,
MAX(New_Models.Model_ID) AS Last_Model_ID,
Technicians.Course_Level AS No_Training_Courses
FROM New_Models, New_Models_Allocation, Technicians
WHERE New_Models.Model_ID=New_models_Allocation.Model_ID
And Technicians.Technician_ID=New_Models_Allocation.Technician_ID
GROUP BY Technicians.Technician_ID, Technicians.Course_Level, First_Name, Surname
ORDER BY MAX(New_Models.Model_ID) DESC)
AS NMC
INNER JOIN (SELECT Technicians.Technician_ID, COUNT(*) AS Final_Exam_Level
FROM Technicians, Exams, Exam_Allocation
WHERE (Technicians.Technician_ID)=Exam_Allocation.Technician_ID
And ((Exams.Exam_ID)=Exam_Allocation.Exam_ID)
And (Exams.Date_Taken)<=#12/31/2010#
GROUP BY Technicians.Technician_ID, Technicians.Course_Level
ORDER BY Technicians.Technician_ID)
AS Exam ON Exam.Technician_ID=NMC.Technician_ID;
This query shows each technician, Last Exam, Last New_Model, Last course.
SELECT Technicians.Technician_ID, Jobs.Job_ID, Jobs.Date_Occured, Fix
FROM Technicians, Jobs, Tech_Allocation, Recovery
WHERE Technicians.Technician_ID=Tech_Allocation.Technician_ID
And Jobs.Job_ID=Tech_Allocation.Job_ID
And Jobs.Job_ID=Recovery.Job_ID
And Jobs.Date_Occured>=#1/1/2010#
And Jobs.Date_Occured<=#12/31/2010#
ORDER BY Fix;
This query shows the jobs each technician has done.
However, when creating a report in Ms Access, the jobs are repeated. Hence, instead of a technician having done 3 jobs, it shows 12 for example. Although when running the second query itself, results aren't repeated.
Any Help?
For obvious reasons, I don't usually read other people's SQL queries, but your example was very well formatted. Is this the problem?
INNER JOIN (SELECT Technicians.Technician_ID, COUNT(*) AS Final_Exam_Level
...
GROUP BY Technicians.Technician_ID, Technicians.Course_Level
These 2 lines are from the 2nd subquery of your first query. You have 1 index field (Technician_ID), but 2 grouping fields (Technician_ID and Course_Level). This would produce results like:
Technician_ID Final_Exam_Level
Bob 5
Bob 4
Nadine 5
I recommend either removing Course_Level from the Group By or adding it to the Select row.