Access - how to add 2 columns and subtract 1 - ms-access

I've uploaded a screenshot and the SQL query below as reference. What I am trying to achieve is finding out the profit/loss by adding up the field 'SubscriptionFee' and 'UNIFORM_UniformPrice' together and then subtract 'UNIFORM_SUPPLY_UniformPrice's total.
Pretty much [SubscriptionFee] + [UNIFORM_UniformPrice] - [UNIFORM_SUPPLY_UniformPrice]
is what I am trying to do but I can't figure out how to make it work.
When I try to run:
Sum([UNIFORM_UniformPrice]+[SubscriptionFee]-[UNIFORM_SUPPLY_UniformPrice]) AS Total
An error pops up saying:
your query does not include the specified expression 'Subscriptionfee' as part of an
aggregate function
The apparent fix seems to be adding a "group by" but I have no idea how I type it out or if that is the fix for my issue.
SQL Query:
SELECT subscription.subscriptionfee,
uniform.uniformprice AS UNIFORM_UniformPrice,
uniform_supply.uniformprice AS UNIFORM_SUPPLY_UniformPrice,
SUM([uniform_uniformprice] + [subscriptionfee]
- [uniform_supply_uniformprice])
AS Expr1
FROM (((uniform_supply
INNER JOIN staff_order
ON uniform_supply.[uniformsupplyid] =
staff_order.[uniformsupplyid])
INNER JOIN uniform
ON uniform_supply.[uniformsupplyid] =
uniform.[uniformsupplyid])
INNER JOIN (subscription
INNER JOIN student_order
ON subscription.[subscriptionno] =
student_order.[subscriptionid])
ON uniform.[uniformproductid] =
student_order.[uniformproductid])
INNER JOIN invoice
ON student_order.[orderid] = invoice.[orderid];

You should not use SUM in this case. You do not aggregate by for example using a group clause.
You can not reference an alias as UNIFORM_UniformPrice in the same select that it is defined in.
You statement should be like:
SELECT subscription.subscriptionfee,
uniform.uniformprice AS UNIFORM_UniformPrice,
uniform_supply.uniformprice AS UNIFORM_SUPPLY_UniformPrice,
uniform.uniformprice + subscriptionfee
- uniform_supply.uniformprice
AS Expr1
FROM (((uniform_supply
INNER JOIN staff_order
ON uniform_supply.[uniformsupplyid] =
staff_order.[uniformsupplyid])
INNER JOIN uniform
ON uniform_supply.[uniformsupplyid] =
uniform.[uniformsupplyid])
INNER JOIN (subscription
INNER JOIN student_order
ON subscription.[subscriptionno] =
student_order.[subscriptionid])
ON uniform.[uniformproductid] =
student_order.[uniformproductid])
INNER JOIN invoice
ON student_order.[orderid] = invoice.[orderid];

Related

MySql - having issues with double left join

I am having issues with getting this double left join to get the listingspecificsListPrice, but that info exists in the table, cant figure out why it would not include it. This is my sql.
SELECT mls_subject_property.*, mls_images.imagePath, mls_forms_listing_specifics.listingspecificsListPrice
FROM mls_subject_property
LEFT JOIN mls_images ON mls_subject_property.mls_listingID = mls_images.mls_listingID
LEFT JOIN mls_forms_listing_specifics ON mls_forms_listing_specifics.mls_listingID = mls_subject_property.mls_listingID AND mls_images.imgOrder = 0
WHERE userID = 413
GROUP BY mls_subject_property.mls_listingID
The result comes out like this..
All of the other fields come back, but it doesnt seem to want to bring back those two items.
This is a picture of the other table, to show that the data does in fact exist.
The mls_images.imgOrder = 0 condition should be in the join with mls_images, not mls_forms_listing_specifics.
Don't use GROUP BY if you're not using any aggregation functions. Use SELECT DISTINCT to prevent duplicates.
SELECT DISTINCT mls_subject_property.*, mls_images.imagePath, mls_forms_listing_specifics.listingspecificsListPrice
FROM mls_subject_property
LEFT JOIN mls_images ON mls_subject_property.mls_listingID = mls_images.mls_listingID AND mls_images.imgOrder = 0
LEFT JOIN mls_forms_listing_specifics ON mls_forms_listing_specifics.mls_listingID = mls_subject_property.mls_listingID
WHERE userID = 413

MYSQL Join all table and get the record of a field between 2 value?

I have this query,is working well,this join the few table together,this have no problem,working well
"SELECT post_info.post_id,username,profile_image_path,status_body,image_path,post_created_at FROM post_info
LEFT OUTER JOIN users_info ON post_info.user_id = users_info.user_id
LEFT OUTER JOIN status_of_post ON post_info.status_id = status_of_post.status_id
LEFT OUTER JOIN image_of_post ON post_info.image_id =image_of_post.image_id"
I want to get the record BETWEEN (post_id + 1) AND (post_id + 20),so I modified it to become this
SELECT post_info.post_id,username,profile_image_path,status_body,image_path,post_created_at FROM post_info
LEFT OUTER JOIN users_info ON post_info.user_id = users_info.user_id
LEFT OUTER JOIN status_of_post ON post_info.status_id = status_of_post.status_id
LEFT OUTER JOIN image_of_post ON post_info.image_id =image_of_post.image_id
WHERE post_id BETWEEN (post_id + 1) AND (post_id + 20)
It state this result.
1046 - No database selected
I want to get 20 post each time the query run,based on the range of post_id,what is the proper way to do it??
Make sure the database you are running the query for is selected in PhpMyAdmin
OR
You can change your query to include the database name like this:
SELECT ..... FROM DATABASE-Name.post_info

Rails - How to force associations to use alias table name

p = Patient.find(30)
p.patient_problems
The above code generates the following query
SELECT `patient_problem`.* FROM `patient_problem` WHERE `patient_problem`.`patient_id` = 30 AND (`patient_problem`.`record_status_id` = 1)
But is there any way to assign/use alias table_name like
p.patient_problems(:alias=>'p1') # just for Ex.. This code will not work
p.patient_problems(:alias=>'p2') # just for Ex.. This code will not work
So it will generate the following queries
SELECT `p1`.* FROM `patient_problem` AS `p1` WHERE `p1`.`patient_id` = 30 AND (`p1`.`record_status_id` = 1)
SELECT `p2`.* FROM `patient_problem` AS `p2` WHERE `p2`.`patient_id` = 30 AND (`p2`.`record_status_id` = 1)
Additional Info
My problem is when I try to use joins
p.patient_problems(:all,:joins=>joins)
I get this error
ActionView::Template::Error (Mysql2::Error: Not unique table/alias: 'patient_problem': SELECT `patient_problem`.* FROM `patient_problem` LEFT OUTER JOIN party on party.id = patient_problem.patient_id
LEFT OUTER JOIN party_identifier on party.id = party_identifier.party_id
LEFT OUTER JOIN blood_type on blood_type.id = party.blood_type_id
LEFT OUTER JOIN education_level on education_level.id = party.education_level_id
LEFT OUTER JOIN religion on religion.id = party.religion_id
LEFT OUTER JOIN living_arrangement on living_arrangement.id = party.living_arrangement_id
LEFT OUTER JOIN patient_problem patient_problem on patient_problem.patient_id = party.id and patient_problem.record_status_id = 1
left join (select user_type,username,user_id,auditable_id from (select MAX(id) id from audits where audits.auditable_type = 'PatientProblem' and user_type is not null group by auditable_id ) t inner join audits v on v.id=t.id ) entered_by1 on entered_by1.auditable_id = patient_problem.id
left outer join user user1 on entered_by1.user_id = user1.id
left outer join party as party_user1 on party_user1.id = user1.person_id
LEFT OUTER JOIN patient_patient_search patient_patient_search1 on patient_patient_search1.patient_id = party.id
left join search search1 on patient_patient_search1.patient_search_id = search1.id
and patient_patient_search1.patient_search_id = '75' WHERE `patient_problem`.`patient_id` = 45 AND (`patient_problem`.`record_status_id` = 1) AND ( (patient_problem.occurrence_date > '2013-01-01 00:00:00' and patient_problem.occurrence_date < '2013-06-30 23:59:59' and patient_problem.patient_problem_status_id in (5) and patient_problem.code is not null and patient_problem.code in ('10725009') ) and ( patient_patient_search1.patient_search_id in (75.0) ) ))
Ofcourse I could do some string manipulation on the generated joins query and set alias to patient_problem. But I thought setting alias for associations would be more cleaner since the joins query generated are unpredictable(in my scenario)
I am not sure what the variable joins is or how it was constructed. To alias tables in a join build your query like
Rails 3
PatientProblem.joins("as p1 OUTER JOIN patient_problem as p2 on ...")
or
PatientProblem.find(:all, :joins => "as p1 OUTER JOIN patient_problem as p2 ON ...")
you can make singleton methods for that and write the query one time and use may time like
def self.p1
#your active record query here.
end
and call like
PatientProblem.p1
Update
You can simply change the table name in your code:
Patient.table_name="p2"
I'm not sure if this would break anything else though ... so good luck!
Orignal Answer
One solution may be to define a separate model for each type of patient_problem and then do something like this:
class PatientProblem2 < ActiveRecord::Base
self.set_table_name "p2"
...
end
Another solution may be to use the ActiveRecord query interface which will allows for significant query flexibility:
http://guides.rubyonrails.org/active_record_querying.html
Perhaps you can be more specific on the nature problem you are trying to solve.

SQL Query producing wrong count result

I have the following SQL query
SELECT
DISTINCT
count("SiteTree_Live"."ID")
FROM
"SiteTree_Live"
LEFT JOIN "Page_Live" ON "Page_Live"."ID" = "SiteTree_Live"."ID"
LEFT JOIN "TourPage_Live" ON "TourPage_Live"."ID" = "SiteTree_Live"."ID"
LEFT JOIN "DepartureDate" ON "DepartureDate"."TourID" = "SiteTree_Live"."ID"
WHERE
("SiteTree_Live"."Locale" = 'en_AU')
AND ("SiteTree_Live"."ClassName" IN ('TourPage'))
AND ("DepartureDate"."DepartureDate" LIKE '2012-11%')
but it producing a wrong count as the query result. The total intented result this query is suppose to return should not be more than 245 but currently, its returning more than about "4569" results.
Thats is because of the JOIN on the "DepartureDate" table as the query returns the expected result when i remove the join from the "DepartureDate" table.
What modification do i need to make to my query to count the Macthes between "SiteTree_Live"."ID" and "DepartureDate"."TourID" whiles counting only the "SiteTree_Live"."ID" count excluding the Departure dates?
Any suggestions welcomed :)
THE ANSWER
SELECT
COUNT(DISTINCT SiteTree_Live.ID)
FROM
"SiteTree_Live" LEFT JOIN "Page_Live" ON "Page_Live"."ID" = "SiteTree_Live"."ID"
LEFT JOIN "TourPage_Live" ON "TourPage_Live"."ID" = "SiteTree_Live"."ID"
LEFT JOIN "DepartureDate" ON "DepartureDate"."TourID" = "SiteTree_Live"."ID"
WHERE
("SiteTree_Live"."Locale" = 'en_AU')
AND ("SiteTree_Live"."ClassName" IN ('TourPage'))
AND ("DepartureDate"."DepartureDate" LIKE '2013-03%')
Seems to give me the right result. Thanks for the tip #Michael Berkowski
Minor correction: if DepartureDate is a date-type, then the LIKE '2013-03% will force it to be coerced into a character type (this is a mysql feature) As a result, any indexes on DepartureDate will not be used, IIRC. Better use a plain range-query:
SELECT
COUNT(DISTINCT stl.ID)
FROM
SiteTree_Live stl
LEFT JOIN
DepartureDate dd ON dd.TourID = stl.ID
WHERE
stl.Locale = 'en_AU'
AND stl.ClassName = 'TourPage'
AND dd.DepartureDate >= '2013-03-01'
AND dd.DepartureDate < '2013-04-01'
;
Do this (You have a bunch of unneeded joins)
SELECT
COUNT(DISTINCT SiteTree_Live.ID)
FROM
`SiteTree_Live`
LEFT JOIN
`DepartureDate` ON `DepartureDate`.`TourID` = `SiteTree_Live`.`ID`
WHERE
`SiteTree_Live`.`Locale` = 'en_AU'
AND `SiteTree_Live`.`ClassName` = 'TourPage'
AND `DepartureDate`.`DepartureDate` LIKE '2013-03%'
You could also do a GROUP BY:
SELECT
COUNT(SiteTree_Live.ID)
FROM
`SiteTree_Live`
LEFT JOIN
`DepartureDate` ON `DepartureDate`.`TourID` = `SiteTree_Live`.`ID`
WHERE
`SiteTree_Live`.`Locale` = 'en_AU'
AND `SiteTree_Live`.`ClassName` = 'TourPage'
AND `DepartureDate`.`DepartureDate` LIKE '2013-03%'
GROUP BY
SiteTree_Live.ID

Using an IF Statement in MySQL

I have the below SQL statement. What I would like to do is include an IF statement to cover the possibility of one of the columns returning a 0 or blank result, there are no 0 ID's. In this case, I'm trying to cover the possibility of the magez_cfv_nations result possibly returning a zero or blank result.
SELECT c.clan_name, n.nation_name,
CONCAT(r.rarity_shorthand, " - ", r.rarity_name) AS rarity_text,
t.trigger_name, s.skill_name
FROM `magez_cfv_cards` AS cards
JOIN `magez_cfv_clans` c ON cards.clan_id = c.clan_id
JOIN `magez_cfv_nations` n ON cards.nation_id = n.nation_id
JOIN `magez_cfv_rarity` r ON cards.rarity_id = r.rarity_id
JOIN `magez_cfv_trigger` t ON cards.trigger_id = t.trigger_id
JOIN `magez_cfv_skills` s ON cards.skill_id = s.skill_id
You need a LEFT JOIN
The LEFT JOIN keyword returns all rows from the left table
(table_name1), even if there are no matches in the right table
Try this
SELECT c.clan_name, n.nation_name,
CONCAT(r.rarity_shorthand, " - ", r.rarity_name) AS rarity_text,
t.trigger_name, s.skill_name
FROM `magez_cfv_cards` AS cards
JOIN `magez_cfv_clans` c ON cards.clan_id = c.clan_id
LEFT JOIN `magez_cfv_nations` n ON cards.nation_id = n.nation_id
JOIN `magez_cfv_rarity` r ON cards.rarity_id = r.rarity_id
JOIN `magez_cfv_trigger` t ON cards.trigger_id = t.trigger_id
JOIN `magez_cfv_skills` s ON cards.skill_id = s.skill_id
the function you should use is IIF() and its syntax is
IIF([condition],[if true],[if false])