How to use Left function with MS Access 2007 - ms-access

Alright, I am attempting to use a basic left() function with a MS Access query, in order to create an inner join between two tables. The two columns that will be joined contain the first 3 character of a given street address.
I have attempted to use the function as shown:
I simply want to take the first 3 characters (from the left obviously) of this column, and join it to the "Left 3 of adress" column in my Branch Management sheet. Every time I attempt to run this query however, I get a syntax error. The left function is pretty straightforward in almost every language/RDBMS, what am I doing wrong?
EDIT: here is a screen shot of the error:
The error is wonderfully vague.

You left out a close parenthesis. Change this ...
ON ([Branch Mgmt].[Left 3 of address] = left([SalesPage Offices w/CRD].ADDRESS_LINE_1,3)
to this ...
ON ([Branch Mgmt].[Left 3 of address] = left([SalesPage Offices w/CRD].ADDRESS_LINE_1,3))

I don't think you can use the LEFT() function in your join statement. Instead run a sub query to get the field you want and then link them together. Something like the following:
SELECT M.TRADE_FIRM, M.POSTAL_CODE_1, M.ADDRESS_LINE_1,
M.OFFICE_ID, M.STATE_PROVINCE, M.CITY, B.*
FROM
(
SELECT S.*, LEFT(S.ADDRESS_LINE_1, 3) AS Left3Addr
FROM [SalesPage Offices w/CRD] AS S
) AS M
INNER JOIN [Branch Mgmt] AS B ON B.[Left 3 of address] = M.Left3Addr
AND B.State = M.State
AND B.City = M.City
WHERE M.TRADE_FIRM = 'WHATEVER'

Related

MySQL DISTINCT returning not so distinct results

Good day,
I have a small issue with MySQL Distinct.
Trying the following query in my system :
SELECT DISTINCT `booking_id`, `booking_ticket`, `booking_price`, `bookingcomment_id`, `bookingcomment_message` FROM `mysystem_booking`
LEFT JOIN `mysystem_bookingcomment` ON `mysystem_booking`.`booking_id` = `mysystem_bookingcomment`.`bookingcomment_link`
WHERE `booking_id` = 29791
The point is that there are bookings like 29791 that have many comments added.
Let's say 10. Then when running the above query I see 10 results instead of one.
And that's not the way DISTINCT supposes to work.
I simply want to know if there are any comments. If the comment ID is not 0 then there is a comment. Of course I can add COUNT(blabla) as comment_number but that's a whole different story. For me now I'd like just to have this syntax right.
You may try aggregating here, to find which bookings have at least a single comment associated with them:
SELECT
b.booking_id,
b.booking_ticket,
b.booking_price
FROM mysystem_booking b
LEFT JOIN mysystem_bookingcomment bc
ON b.booking_id = bc.bookingcomment_link
WHERE
b.booking_id = 29791
GROUP BY
b.booking_id
HAVING
COUNT(bc.bookingcomment_link) > 0;
Note that depending on your MySQL server mode, you might have to also add the booking_ticket and booking_price columns to the GROUP BY clause to get the above query to run.
You can try below - using a case when expression
SELECT DISTINCT `booking_id`, `booking_ticket`, `booking_price`, `bookingcomment_id`,
case when `bookingcomment_message`<>'0' then 'No' else 'Yes' end as comments
FROM `mysystem_booking`
LEFT JOIN `mysystem_bookingcomment` ON `mysystem_booking`.`booking_id` = `mysystem_bookingcomment`.`bookingcomment_link`
WHERE `booking_id` = 29791

SQL Joining Diffrent Size Tables Together With Null Value Replacement

I am working on a query for a datatable and I can't seem to get it to display how I want, I don't know if this is even possible in SQL What I am looking to do is get a query to respond with ideally an extra column of Boolean type.
Currently I can run two queries and they both work perfectly but I can't work out how to join them together bellow is the code from my first query what this does is return beers a user has tried this works fine and as expected and returns as expected.
SELECT *
FROM keg.beer
JOIN keg.userbeer
ON beer.id = userbeer.beer_id
WHERE userbeer.username_id = 1;
The second query is even simpler and is just a select getting the list of beers.
SELECT * FROM keg.beer
What I want to do is run a query and have it return a list of beers with a Boolean value if the user has tried it or not.
You're not going to run into too many scenarios for "Desired Results" that can't be produced with plain 'ol SQL. In this case you'll use a CASE statement to determine if the person has tried a beer. You'll also want a LEFT OUTER JOIN so you don't drop records coming from your beer table when your filtered userid doesn't have a userbeer record for that beer:
SELECT
beer.name,
beer.id,
beer.country,
CASE WHEN userbeer.username_id IS NULL THEN 0 ELSE 1 END AS user_tried_beer_boolean
FROM keg.beer
LEFT OUTER JOIN keg.userbeer
ON beer.id = userbeer.beer_id
AND userbeer.username_id = 1;
As #SeanLange mentioned in the comments here, the restriction of the WHERE statement for the userid would cause records to be dropped that you want in your result set, so we move the restriction of username_id = 1 to the ON portion of the LEFT OUTER JOIN so that the userbeer table results are restricted to just that user before it's joined to the beer table.
Now I need a drink.
SELECT b.id,
b.name,
CASE WHEN u.username_id IS NOT NULL THEN TRUE ELSE FALSE END AS userdrankbeer
FROM keg.beer b
LEFT JOIN ( SELECT * FROM keg.userbeer WHERE username_id = 1 ) u
ON beer.id = userbeer.beer_id
;

Why can't I exclude this row based on a condition?

http://sqlfiddle.com/#!3/3ec1f/119
Here's my fiddle...I want the result to look like this but the query I'm using doesn't do that:
My problem with the query is that I can't seem to exclude "The Kingdom of the Crystal Skull" using the exclusion_flag condition. I also don't know why it seems that Contract 3 (Raiders of the Lost Arc) is not showing up either. I have been toiling with this for hours and have no idea what the problem is. I tried looking into subqueries, but I'm not sure that's the solution...
There's a couple of questions/issues there so I'll try to address them individually.
1) You can't exclude "The Kingdom of the Crystal Skull" using the exclusion_flag because contract_sid 7 and 8 both refer to product_list_sid 3 which includes "The Kingdom of the Crystal Skull" - you would need to create a separate product_list_sid if you wanted a contract which excluded it.
2) "Raiders of the Lost Arc" (contract_sid 3) isn't showing up because it's a "single product" contract, and your query only joins from scope to product_list_join using product_list_id - contract_sid 3 is in the product_sid column so you need a separate join to cater for contracts that use product_sid instead of product_list_sid (I assume that a contract can't use both). This is a pretty dodgy schema design but here's a query that solves that issue. Notice the use of LEFT OUTER JOIN to indicate that the table being joined to might not contain any rows (for example when scope.product_list_sid is NULL but scope.product_sid is not).
SELECT s.contract_sid,
c.contract_description,
ISNULL(p.product_description, p2.product_description) AS product_description
FROM scope s
JOIN contracts c ON (c.contract_sid = s.contract_sid)
LEFT OUTER JOIN
product_list_join plj ON (plj.product_list_sid = s.product_list_sid)
LEFT OUTER JOIN
products p ON (p.product_sid = plj.product_sid)
LEFT OUTER JOIN
products p2 ON (p2.product_sid = s.product_sid)
WHERE s.exclusion_flag = 'N'
ORDER BY s.contract_sid;
Here's the SQLFiddle for my solution: http://sqlfiddle.com/#!3/fc62e/10
Edit: After posting this I realised what you're actually trying to do - the scope table not only provides the details of contracts but also provides specific products to exclude from contracts. Again, this is bad schema design and there should be a separate scope_exclusions table or something, but here's a query that does that and excludes "The Kingdom of the Crystal Skull" as requested:
SELECT inner_query.contract_description,
inner_query.product_description
FROM (
SELECT s.contract_sid,
c.contract_description,
ISNULL(p.product_sid, p2.product_sid) AS product_sid,
ISNULL(p.product_description, p2.product_description) AS product_description
FROM scope s
JOIN contracts c ON (c.contract_sid = s.contract_sid)
LEFT OUTER JOIN
product_list_join plj ON (plj.product_list_sid = s.product_list_sid)
LEFT OUTER JOIN
products p ON (p.product_sid = plj.product_sid)
LEFT OUTER JOIN
products p2 ON (p2.product_sid = s.product_sid)
WHERE s.exclusion_flag = 'N'
) inner_query
WHERE NOT EXISTS ( SELECT 1
FROM scope
WHERE exclusion_flag = 'Y'
AND contract_sid = inner_query.contract_sid
AND product_sid = inner_query.product_sid )
ORDER BY inner_query.contract_description;
SQL Fiddle: http://sqlfiddle.com/#!3/fc62e/14

How do I convert this SQL query into hibernate

Can someone please tell me how to convert this SQL query into hibernate?
SELECT * FROM sys_quote_master AS g1
INNER JOIN
(SELECT order_base_id, order_id FROM sys_quote_master
GROUP BY order_base_id, order_date_last_revised
ORDER BY order_date_last_revised desc) AS g2
ON g2.order_id = g1.order_id;
Basically,
I have tried this and it doesn't work:
DetachedCriteria crit1 = DetachedCriteria.forClass(QuoteMaster.class);
ProjectionList pList = Projections.projectionList();
pList.add(Projections.groupProperty("orderBaseId"));
session = HibernateSessionFactory.currentSession();
Criteria crit = session.createCriteria(QuoteMaster.class);
Criteria c = crit.createCriteria("QuoteMaster", CriteriaSpecification.INNER_JOIN);
c.setProjection(pList);
I get this error:
org.hibernate.QueryException: could not resolve property: this of:
QuoteMaster
And I think it has to do with this line of code were I am trying to create an inner join to the same table:
Criteria c = crit.createCriteria("QuoteMaster", CriteriaSpecification.INNER_JOIN);
So basically, my question is 'How to create a join to the same table using Criteria.createCriteria or Criteria.createAlias?' The doc for Criteria class states the first parameter is a dot-separated property path, but what the heck does that mean? :)
Every example I found so far shows how to do this with 2 or 3 tables but not to the same table and I have no idea what to use for the first argument.
I dont know how your entities are, this should give rough idea.
from SystQuoteMasterEntity sqm join ( select sqm1.orderbaseid, sqm1.orderId from SystQuoteMasterEntity sqm1 group by orderbaseid,orderdatelastrev order by orderdatelast desc) g2

Searching two mysql columns added together for one variable

I currently have a search form which should allow a user to search for a customers full name and it will return the row.
For Example: A user searches for "Mr. N Mallow" and it will return the row which matches that query. Since I am new to MySQL I need some help, I've tried + but that has no effect, probably because it's not standard mysql or something like that.
select *
from mooring
left join customer
on mooring.assignedTo = customer.id
where mooring.Number like \"$var\"
or (customer.TitleName + customer.Surname = '$var')
Any suggestions?
select * from mooring
left join customer on mooring.assignedTo = customer.id
where mooring.Number like \"$var\" OR (customer.TitleName + customer.Surname = '$var')
Try CONCAT_WS or CONCAT, which join strings together (the first version is "with separator"):
CONCAT(customer.TitleName,' ',customer.Surname)
or
CONCAT_WS(' ',customer.TitleName,customer.Surname)