Select from my tabel where date = specific date - mysql

My date in table is TIMESTAMP something like this 2016-05-28 18:39:00
Now I am trying to select from table where date = 2016-05-28
here is my query
$getReport = $db->prepare('SELECT a.proId, a.userId, DATE(a.date), a.status, a.canceledBy, b.id, b.pName, b.pPrice FROM purchaseshistory AS a INNER JOIN products AS b ON(a.proId=b.id) WHERE a.date=?');
$getReport->bind_param('i', $dateOfBirth);
$getReport->execute();
$getReport->bind_result($proId, $userId, $date, $status, $canceledBy, $id, $pName, $pPrice);
$getReport->store_result();
while ($getReport->fetch()) {
Date is coming from here
$dateOfBirth = $_POST['dateOfBirth'];
results are empty

could be you need a correct date format
SELECT a.proId, a.userId,
DATE(a.date), a.status, a.canceledBy, b.id, b.pName, b.pPrice
FROM purchaseshistory AS a
INNER JOIN products AS b ON(a.proId=b.id)
WHERE date_format(a.date,'%Y-%m-%d') = ?

You've converted a.date to a DATE in the SELECT, but not in the WHERE clause, I would recommend modifying your query as below:
SELECT
ph.proId, ph.userId, DATE(ph.date), ph.status, ph.canceledBy,
p.id, p.pName, p.pPrice
FROM purchaseshistory AS ph
INNER JOIN products AS p ON ph.proId = p.id
WHERE DATE(ph.date) = ?

Related

How can I display the values excluded by the Where clause?

I'm trying to figure out how to to display all those values that are excluded from this WHERE clause instead of those that are chosen by:
$pdo = $service->pdo;
$sql = "SELECT a.id, a.name, a.surname, a.job, b.start_date, b.end_date
FROM table1 a
INNER JOIN table2 b on b.id = a.id
WHERE a.job = '{$job}'
AND
STR_TO_DATE(b.`start_date`, '%d-%m-%Y') = '{$date}'
GROUP BY a.id ";
return $pdo->query($sql)->fetchAll(PDO::FETCH_OBJ);
What this query returns are values that match "work title" first and then values that exist in that date in the table 2. What I'm trying to show are all the other values based on the "work title" excluding those values that exist for that specific date. Basically I need to figure out for example how to know who is free in a specific date based on this query.
Thank you for your help
You can modify your query and set the inverse where clause like this, you get the rows that don't match 'work title' OR (this is important) the rows that don't match the date requeriment in table2:
$pdo = $service->pdo;
$sql = "SELECT a.id, a.name, a.surname, a.job, b.start_date, b.end_date
FROM table1 a
INNER JOIN table2 b on b.id = a.id
WHERE a.job <> '{$job}'
OR
STR_TO_DATE(b.`start_date`, '%d-%m-%Y') <> '{$date}'
GROUP BY a.id ";
return $pdo->query($sql)->fetchAll(PDO::FETCH_OBJ);
EDIT
After reading your comments, i see that you donĀ“t need the roes that your query has dircarded, you need the rows that match the first requeriment and don't match the second one. So you would need it this way:
$pdo = $service->pdo;
$sql = "SELECT a.id, a.name, a.surname, a.job, b.start_date, b.end_date
FROM table1 a
INNER JOIN table2 b on b.id = a.id
WHERE a.job = '{$job}'
AND
STR_TO_DATE(b.`start_date`, '%d-%m-%Y') <> '{$date}'
GROUP BY a.id ";
return $pdo->query($sql)->fetchAll(PDO::FETCH_OBJ);

Count invitation response against events and response codes

I have this table for Response Codes:
And this table for invitations:
My query so far gives this:
While I want to achieve this:
MY QUERY:
SELECT
i.eventId
,code.responseCode
,COUNT(i.attendeeResponse) responseCount
FROM invitations i
LEFT JOIN response_codes code
ON code.responseCode = i.attendeeResponse
GROUP BY i.eventId, code.responseCode, i.attendeeResponse;
SQLFiddle
You need to construct a cartesian product of all eventIds and responseCodes at first (you can achieve it with join without condition):
select c.eventId
, c.responseCode
, count( i.attendeeResponse ) as responseCount
from ( select distinct t1.responseCode
, t2.eventId
from `response_codes` t1
join `invitations` t2 ) c
left join `invitations` i on c.responseCode = i.attendeeResponse and c.eventId = i.eventId
group by c.eventId, c.responseCode;
SQLFiddle
You need to cross join the responsecode table to get the all combinations of eventid and responsecode.
SQL Fiddle
SELECT distinct
i.eventId
,code.responseCode
,case when t.responseCount is null then 0
else t.responsecount end rcount
FROM invitations i
cross JOIN response_codes code
left join
(SELECT i.eventId
,code.responseCode
,COUNT(i.attendeeResponse) responseCount
FROM invitations i
JOIN response_codes code
ON code.responseCode = i.attendeeResponse
group by i.eventid, code.responsecode) t
on t.responsecode =code.responsecode and t.eventid = i.eventid
order by i.eventid, code.responsecode desc
Another lazy way could be:
SELECT B.EVENTID,A.RESPONSECODE,
IFNULL((SELECT COUNT(*) FROM INVITATIONS C WHERE C.EVENTID = B.EVENTID AND C.ATTENDEERESPONSE = A.RESPONSECODE),0) AS 'responseCount'
FROM
RESPONSE_CODES A,
INVITATIONS B
GROUP BY A.RESPONSECODE,B.EVENTID
ORDER BY EVENTID ASC,RESPONSECODE DESC
SQL Fiddle

Dissolve second select statement

I got following query which is as well working.
SELECT date, duration, educationDepartment, yearOfTraining, completedtasks
FROM programm_completedtask
WHERE date BETWEEN '2014-08-05' AND '2014-08-08'
AND trainee_id =
(SELECT id FROM programm_trainee WHERE username = 'Markus');
Is it possible to do anything else instead of the second select statement ? I would love to just select once.
Use an INNER JOIN ...
SELECT p.date, p.duration, p.educationDepartment, p.yearOfTraining, p.completedtasks
FROM programm_completedtask as p
INNER JOIN programm_trainee as pt on p.trainee_id = pt.id AND pt.username = 'Markus'
WHERE p.date BETWEEN '2014-08-05' AND '2014-08-08'
or
SELECT p.date, p.duration, p.educationDepartment, p.yearOfTraining, p.completedtasks
FROM programm_completedtask as p
INNER JOIN programm_trainee as pt on p.trainee_id = pt.id
WHERE p.date BETWEEN '2014-08-05' AND '2014-08-08'
AND pt.username = 'Markus'

How to deduct in group by query in sum in access

I have query like this ::
SELECT account.AccountNumber, account.NAME, Sum(agro.price * agro.qty) AS Expr1
FROM ((account
INNER JOIN data ON account.AccountNumber = data.acno)
INNER JOIN agro ON agro.BillNo = data.BillNo)
WHERE data.db='true'
GROUP BY account.AccountNumber, account.NAME;
I want to deduct another groupby query output in to Sum(agro.price * agro.qty) this
the another group by query is SELECT Sum(rs),acno
FROM jma group by acno;
i want to deduct Sum(agro.price * agro.qty)-Sum(rs) how its work please help me solve this
If I am understanding you correctly the following query may work for you:
SELECT subQ.AccountNumber, subQ.NAME, (subQ.subSum - jmaSum.jSum) AS FinalSum
FROM
(
SELECT a.AccountNumber, a.NAME, Sum(ag.price * ag.qty) AS subSum
FROM (account AS a
INNER JOIN data AS d ON a.AccountNumber = d.acno)
INNER JOIN agro AS ag ON ag.BillNo = d.BillNo
WHERE d.db = 'true'
GROUP BY a.AccountNumber, a.NAME
) AS subQ
LEFT JOIN
(
SELECT Sum(j.rs) AS jSum, j.acno
FROM jma AS j
GROUP BY j.acno
) AS jmaSum ON subQ.AccountNumber = jmaSum.acno

Trying to add one last SUM() column to my query in SQL Server 2008

I have the first query which is producing correct results. What I need is I need to add the sum of values as a last column grouped by surveyid. I can't insert Sum(c.value) into the first query because it is an aggregate function. I have the correct query as my second query below. I know there's pivot functionality but not sure if it can be used here. I do realize that there will be repetition but that's okay.
'first query
SELECT
A.PATIENTID, B.STUDENTNUMBER, c.surveyid,
convert(varchar, A.CreatedDate, 107),
C.QuestionID, C.Value, D.Question
FROM
dbo.Survey A, dbo.Patient B, [dbo].[SurveyQuestionAnswer] C, [dbo].[LookupQuestions] D
WHERE
A.PATIENTID = B.ID
and c.SurveyID = A.ID
and c.QuestionID = d.ID
and c.questionid <> 10
ORDER BY
A.PATIENTID
'second query
select
c.surveyid,SUM(c.value) as scores
from
dbo.SurveyQuestionAnswer c
group by
c.SurveyID
order by
SurveyID '---not important
You can use SUM if you add the OVER clause. In this case:
SELECT
A.PATIENTID, B.STUDENTNUMBER, c.surveyid,
convert(varchar, A.CreatedDate, 107),
C.QuestionID, C.Value, D.Question,
SUM(c.Value) OVER(PARTITION BY c.surveyid) scores
FROM
dbo.Survey A
INNER JOIN dbo.Patient B
ON A.PATIENTID = B.ID
INNER JOIN [dbo].[SurveyQuestionAnswer] C
ON c.SurveyID = A.ID
INNER JOIN [dbo].[LookupQuestions] D
ON c.QuestionID = d.ID
WHERE
c.questionid <> 10
ORDER BY
A.PATIENTID
You could use something like this:
SELECT
s.PATIENTID, p.STUDENTNUMBER, sqa.surveyid,
CONVERT(varchar, s.CreatedDate, 107),
sqa.QuestionID, sqa.Value, lq.Question,
Scores = (SELECT SUM(Value) FROM dbo.SurveyQuestionAnswer s2 WHERE s2.SurveyID = s.ID)
FROM
dbo.Survey s
INNER JOIN
dbo.Patient p ON s.PatientID = p.ID
INNER JOIN
[dbo].[SurveyQuestionAnswer] sqa ON sqa.SurveyID = s.ID
INNER JOIN
[dbo].[LookupQuestions] lq ON sqa.QuestionID = lq.ID
WHERE
sqa.questionid <> 10
ORDER BY
s.PATIENTID
By having a subquery with the SUM(...) you should be able to get that sum as a single value and you don't need to use any grouping function