I'm trying to execute a SQL SELECT query to get all the stores within a city and also have a field to inform if the store is within working hours or not.
This is the SQL I have so far:
$day = date('w');
$hour = date('H:i:s');
SELECT a.id, a.rating, a.name, a.image, b.cityName
(
SELECT 1 FROM tbStore a, workingHour b
WHERE a.id = b.id_store
AND b.weekDay = '$day'
AND b.hourOpen <= '$hour'
AND b.hourClose >= '$hour')
) as 'open'
FROM tbStore a, tbCity b
WHERE b.url = '$url'
AND a.id_city = b.id
ORDER BY open
The way I'm checking this value is as a boolean, so the 'open' field needs to be 0 (closed) or 1 (open).
It's kind of working... But the problem is, if just 1 store is within working hours, all of the others will be 'open' as well, instead of just that specific store.
I also saw some sql statements where people use CASE instead of another select, so any other type of code can be used, as long as the final result is correct.
You're redefining the tbStore a and losing the restriction by $url. You probably just need to change:
SELECT 1 FROM tbStore a, workingHour b
To:
SELECT 1 FROM workingHour b
I have just rewrite you query using explict join way
SELECT
a.id
, a.rating
, a.name
, a.image
, b.cityName
, when( c.id_store is not null then 1 else 0 end) as open
FROM tbStore a
inner join tbCity b on a.id_city = b.id
LEFT join workingHour c on a.id = c.id_store
WHERE b.url = '$url'
AND c.weekDay = '$day'
AND c.hourOpen <= '$hour'
AND c.hourClose >= '$hour'
ORDER BY open
Related
I try to change database from 'MySQL' to 'PostgreSQL' but i have problem some of my query
SELECT
SUM( CASE WHEN b.DISPLAYNAME IS NULL THEN 0 ELSE 1 END ) AS NUMMENUDETAIL,
A.DISPLAYNAME AS GROUPNAME,
b.displayname,
D.NAME,
min( A.ROLEID ) AS ROLEID
FROM
usermenu A
LEFT JOIN usermenu B ON A.ID = B.GROUPID,
userrole C,
position D,
positiondetail E
WHERE
( A.GROUPID IS NULL )
AND D.id = E.position_id
AND A.ROLEID = E.userrole_id
AND A.ROLEID = C.ID
AND B.ROLEID = E.userrole_id
AND B.ROLEID = C.ID
AND b.displayname = 'Transaction Listing Report'
GROUP BY
A.DISPLAYNAME,
b.displayname
LIMIT 10
i use this query into "MySQL" it pass
but when i use in "PostgreSQL" it error, They want my to GROUP BY d.name
then if i GROUP BY it may have different result
How can i fix this ?
This query does not comply with the SQL standard, so it is unsurprising that it does not work on all databases. The standard requires that all columns in the SELECT list that do not appear only inside aggregate functions must be in the GROUP BY clause.
You can have the same effect by using the (again, non-standard) DISTINCT ON clause in PostgreSQL.
so I have this query
SELECT a.*, b.full_name as salesman
from sales a
LEFT JOIN user b ON a.salesman_id = b.id
WHERE a.deleted_at IS NULL AND (a.status = '1' || a.status = '2' )
AND a.balance <= 0
I want to add another column that is not related to any of the column from the first query. I want to add another column (payment_amount) into the generated result
After googled a while, i come into this query
SELECT a.*, b.full_name as salesman from sales a
LEFT JOIN user b ON a.salesman_id = b.id
WHERE a.deleted_at IS NULL AND (a.status = '1' || a.status = '2' )
AND a.balance <= 0
UNION ALL
SELECT '','','','','','','','','','','','','','','','','','','','','','','','','','','',payment_amount from transaction
However, i cant see payment_amount column next to the generated result.
Please be reminded, that I can't edit the database.
the first query returns 28 columns.
What is the problem here? have been dealing with it for hours.
Any help given is really appreciated. Thank you.
union all will just add rows to your results from previous query. What exactly is ur problem?. Also if you are adding extra column in your 2nd query u need to add on dummy column in first.
SELECT a.*, b.full_name as salesman,"" as payment_amount from sales a
LEFT JOIN user b ON a.salesman_id = b.id
WHERE a.deleted_at IS NULL AND (a.status = '1' || a.status = '2' )
AND a.balance <= 0
UNION ALL
SELECT '','','',... till 28 times,payment_amount from transaction
All rows in the result of a SQL query have the same column names. When you use UNION, the column names are taken from the names/aliases from the first subquery in the union. So in your case, the payment_amount will be in the column named salesman, since that's the corresponding column in the first subquery.
If you want it to be in a column of its own, you can add an extra column 0 AS payment_amount to the first subquery, and an extra '' to the second subquery.
SELECT a.*, b.full_name as salesman, 0 AS payment_amount
from sales a
LEFT JOIN user b ON a.salesman_id = b.id
WHERE a.deleted_at IS NULL AND (a.status = '1' || a.status = '2' )
AND a.balance <= 0
UNION ALL
SELECT '', '','','','','','','','','','','','','','','','','','','','','','','','','','','',payment_amount from transaction
I have the below query to find the row prior to MAX row. i feel like i am missing something, can somebody please help with it. I ammlooking forward to get the b.usercode_1 as row prior to a.usercode_1 not the min or any other random row but the ROW prior to the MAX.
Please suggest.
Select distinct
c.ssn
, c.controlled_group_Status CG_status
, c.last_name || ' , '|| c.first_name FULL_NAME
, a.usercode_1 Current_REG
, a.eff_date effective_since1
, b.usercode_1 PRIOR_REG
, b.eff_date effective_since2
, d.term_eff_date
from employee_eff_date c
, emp_cg_data a
, emp_cg_data b
, emp_ben_elects d
where c.control_id = 'XYZ'
and c.controlled_group_Status <> 'D'
and c.eff_date = (select max( c1.eff_date)
from emp_cg_data c1
where c.control_id = c1.control_id
and c.ssn = c1.ssn)
and a.control_id = c.control_id
and a.ssn = c.ssn
and a.eff_date = (select max(a1.eff_date )
from emp_cg_data a1
where a.control_id = a1.control_id
and a.ssn = a1.ssn)
and a.usercode_1 = 'REG26'
and b.control_id = c.control_id
and b.ssn = c.ssn
and b.eff_date = (select max( b1.eff_date)
from emp_cg_data b1
where b.control_id = b1.control_id
and b.ssn = b1.ssn
and b1.eff_date < a.eff_date)
and b.usercode_1 like 'REG%'
and d.control_id = c.control_id
and d.ssn = c.ssn
and d.life_event_date = (select max( d1.life_event_date)
from emp_ben_elects d1
where d.control_id = d1.control_id
and d.ssn = d1.ssn)
and d.le_seq_no= (select max( d1.le_seq_no)
from emp_ben_elects d1
where d.control_id = d1.control_id
and d.ssn = d1.ssn
and d.life_event_date = d1.life_event_date)
and d.term_eff_date is null
;
NOTE: this is not a complete answer... its a helpful suggestion of what you should start with.
you are doing a Cartesian Product of the four tables, filtered by a WHERE... so something like this
Implicit Join -- generally not a good practice as it can be very difficult to keep the where filters apart from the join conditions.
SELECT *
FROM tableA a, TableB b
WHERE b.id = a.id
another way to write a JOIN (the more generally accepted way)
SELECT *
FROM tableA a
JOIN tableB b ON b.id = a.id
Use the ON clause to join two tables together.
You should change your joins to this format so that others can read your query and understand it better.
suggestion to solve your problem
a fairly simple way to get the second to last row is to use a row counter.
so something like
SELECT *, #row_count := #row_count + 1
FROM tableA a
JOIN tableB b on b.id = a.id AND -- any other conditions for the join.
CROSS JOIN (SELECT #row_count := 0) t
then from here you can get the MAX row, whether thats the ID or something else. and then get the #row_num -1. aka the previous row.
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
cI'm seeking some help with my left join with a limit.
What i'm trying to do is to loop through my users and check in another table if there's problems connected to the user.
But currently I'm getting all kinds of weird results and it does not LIMIT the result for each user and it also lists column status_link_missing = 0 even though i have told the sub query to only list status_link_missing = 1
So I'm stuck for now, help is much appreciated!
SELECT
a.user_id AS settings_userid
, a.contact_interval
, b.user_id
, b.notify_user
, b.status_host_down
, b.status_link_missing
, b.status_relnofollow
FROM `link_exchange_settings` a
LEFT JOIN link_exchange_links b
ON b.id
= ( SELECT c.id
FROM link_exchange_links AS c
WHERE
b.user_id = a.user_id
AND c.notify_user = 1
AND c.status_link_missing = 1
LIMIT 1
)
WHERE a.allow_contact = 1
LIMIT 10
Edit
I switched SELECT b.id to c.id now and LIMIT works but now it only works for the first user
Try this change (no reference to b in the subquery):
SELECT
a.user_id AS settings_userid
, a.contact_interval
, b.user_id
, b.notify_user
, b.status_host_down
, b.status_link_missing
, b.status_relnofollow
FROM `link_exchange_settings` a
LEFT JOIN link_exchange_links b
ON b.id
= ( SELECT c.id -- changed
FROM link_exchange_links AS c
WHERE
c.user_id = a.user_id -- changed
AND c.notify_user = 1
AND c.status_link_missing = 1
-- ORDER BY c.something -- optional, recommended addition
LIMIT 1
)
WHERE a.allow_contact = 1
-- ORDER BY a.something_else -- optional, recommended addition
LIMIT 10 ;
It's also good to have ORDER BY when you use LIMIT. Unless you want indeterminate results.
You are using "c.status_link_missing = 1" in sub-query but if you will use it in WHERE clause with "AND" you will get your desired results.
In fact you have to decide and use appropriate condition in WHERE clause of main query instead of LEFT JOIN sub-query.