I am working on converting a legacy MS access system to a spring-boot application and I came across a big query. I am mostly done with converting the functions from access to mysql but not able to understand how to convert the following DLookUp sub-query as a mySql subquery
DLookUp("[price]","tbl_so","[so_id] = " & tbl_trade.so_id & " AND [product_id] = " & tbl_po.product_id
What I understood is following won't work as I don't have the Inner Joins set between the 3 tables, tbl_so, tbl_po, tbl_trade
SELECT tbl_so.price FROM tbl_so WHERE tbl_so.so_id = tbl_trade.so_id AND tbl_so.product_id = tbl_po.product_id
My question is how do I know how the tables will join with each other in this case and also when this DLookUp query is seldom used. And also the solution for this query.
Well, as a general rule, dlookup() can be replaced with a left join. However, you can also use a sub-query and they tend to be "less" change to the query.
So, if we had this:
SELECT id, partNum, dlookup("PartDescrt","tblParts","PartID = " & partNum)
as Description from tblOrders.
You would replace the above dlookup() with a sub-query like this:
SELECT id, partNum,
(select PartDescrt from tblParts where tblParts.PartID = tblOrders.PartNum)
AS Description
from tblOrders
The above is SQL or access sql syntax, but quite sure the same should work for MySQL.
If there is more then one partNumber that matches the above, then add a TOP 1, and a order by with some unique row (PK ID column is best). So the query becomes:
(select TOP 1 PartDescrt from tblParts where tblParts.PartID = tblOrders.PartNum
ORDER BY PartDescrt, ID DESC)
AS Description
Related
How can i get values from mysql database usin while loop?
Sry for my bad english and i'm new in vb.net so please do not insult me because I do not know vb.net, I'm trying to learn, i'll try to explain what i need. :)
Lets go:
We know that EmailAddress is real email address, now first query must be something like this
Query1 = "select ID from database.table_users where email='" & EmailAddress & "'"
Now we know that Query1 will return value id from table_users.
Now we need create new query like this
Query2 = "select friend_id from database.table_friends where user_id='" & query1 & "'"
Okay, now Query2 return friend_id from table_friends.
One more query like this
Query3 = "select displayname from database.table_users where id='" & Query2 & "'"
Query3 must return displaynames of all users which we got in query2.
Example:
First i need query ID from table_users where email=EmailAddres
Then i need query friend_id from table_friends where user_id=ID
After that i nedd query displayname from table_users where id=friend_id
*these are just examples and they are not correct as you can see :D
I need 3 querys in while loop, and only last query (displayname) will go to the listbox. While must return all mysql values from table_friends where ID= id of EmailAddress user.
Join your tables into a single query. Something like this:
SELECT
Friends.displayname
FROM
table_users AS Users
INNER JOIN table_friends AS Connections
ON Users.user_id = Connections.user_id
INNER JOIN table_users AS Friends
ON Connections.friend_id = Friends.user_id
WHERE
Users.email = #EmailAddress
This creates two references to the table_users table, one for the initial user and one for the friends, connected by your many-to-many linking table. (Note also the use of a query parameter, which I highly recommend instead of directly concatenating input into the query.)
SQL is very powerful for querying structured data (hence its name), you'll be much better off building your query logic into SQL rather than keeping the SQL queries overly simple and making multiple trips to the database.
why loop and why can't you make it single query like below using JOIN
select tf.displayname from table_users tu
join table_friends tf on tu.id = tf.friend_id
and tu.email = '" & EmailAddress & "';
I am trying to do an INNER JOIN on two tables that have similar values, but not quite the same. One table has a fully qualified host name for its primary key, and the other the hosts short name, as well as the subdomain. It it safe to assume that the short name and the subdomain together are unique.
So I've tried:
SELECT table1.nisinfo.* FROM table1.nisinfo INNER JOIN table2.hosts ON (table1.nisinfo.shortname + '.' + table1.nisinfo.subdomainname + '.domain.com') = table2.hosts.fqhn WHERE table2.hosts.package = 'somepkg';
This doesn't return the results I expect, it returns the first result hundreds of times. I'd like to return distinct rows. It takes a long time to run as well.
What am I doing wrong? I was thinking of running a subquery to get the hostnames, but I don't know what the right path from here is.
Thank you!
You can use group by in your query so you can achieve the desired results you want
please see this two links
Group by with 2 distinct columns in SQL Server
http://www.sqlteam.com/article/how-to-use-group-by-with-distinct-aggregates-and-derived-tables
Try putting your results into a temp table and then view the table to make sure that the columns are as expected.
SELECT table1.nisinfo.*, table1.nisinfo.shortname + '.' + table1.nisinfo.subdomainname + '.domain.com' AS ColID
INTO #temp
FROM table1.nisinfo;
Select *
from #temp INNER JOIN table2.hosts ON ##temp.ColID = table2.hosts.fqhn
WHERE table2.hosts.package = 'somepkg'
;
Put a Group By clause at the end of the second statement
So in this case, I used a subquery to get the initial results, and then used a join.
SELECT table1.nisinfo.* FROM table1.nisinfo JOIN (SELECT distinct(fqhn) FROM table2.hosts WHERE package = 'bash') AS FQ ON ((SUBSTRING_INDEX(FQ.fqhn, '.', 1)) = table1.nisinfo.shortname);
Last time I was able to combined 3 different SELECT queries since it was from the same table. Now, I'm trying to execute a query where in the info was from 2 different tables.
Here's my query string
SELECT applicantinfo.FirstName,
applicantinfo.MiddleName,
applicantinfo.LastName,
applicantaccess.ApplicantExamPassword
FROM applicantinfo
LEFT JOIN applicantaccess WHERE applicantaccess.ApplicantID = '" & lblID.Text & "'"
using myphpadmin to test this query and replacing the lblID.text with a value, Instead of showing up a singe result, it shows 2 rows.
Here's what it looks like
I think I miss used using the INNER JOIN keyword here.
My expected output should only be the first row.
*note
Jaranilla's ID should be '201458971' and password is 6zo93ie82m
lopez's ID should be '201437095' and password is 4ew93fo86t
You have to specify which properties constitute the join, assuming that "id" is the common attribute, e.g.
SELECT applicantinfo.FirstName, applicantaccess.ApplicantExamPassword FROM applicantinfo LEFT JOIN applicantaccess ON applicantinfo.id = applicantaccess.id;
In join put condition on which you want to join both tables, try following appID is you primary key and fk_appID is foregin key.
SELECT applicantinfo.FirstName, applicantinfo.MiddleName, applicantinfo.LastName, applicantaccess.ApplicantExamPassword
FROM applicantinfo as ainfo
LEFT JOIN applicantaccess as aa on ainfo.appID = aa.fk_appID
WHERE applicantaccess.ApplicantID = '" & lblID.Text & "'"
Trying to convert below query into SQL, query works fine on MySQL. Problem seems to be the GROUP BY area. Even when I use just 1 GROUP BY field I get same error. Using query in InformaticaCloud.
ERROR
"the FROM Config_21Cent WHERE resp_ind = 'Insurance' GROUP BY
resp_Ind;;] is empty in JDBC connection:
[jdbc:informatica:sqlserver://cbo-aps-inrpt03:1433;DatabaseName=SalesForce]."
SELECT sum(Cast(Resp_Ins_Open_dol AS decimal(10,2))) as baltotal,
carrier_code,
carrier_name,
carrier_grouping,
collector_name,
dataset_loaded,
docnum,
envoy_payer_id,
loc,
market,
master_payor_grouping,
plan_class,
plan_name,
resp_ins,
resp_ind,
resp_payor_grouping,
Resp_Plan_Type,
rspphone,
state
FROM Config_21Cent
WHERE resp_ind = 'Insurance'
GROUP BY
(resp_ins + resp_payor_grouping +
carrier_code + state + Collector_Name);
Your entire query isn't going to work. The group by statement contains a single expression, the summation of a bunch of fields. The select statement contains zillions of columns without aggregates. Perhaps you intend for something like this:
select resp_ins, resp_payor_grouping, carrier_code, state, Collector_Name,
sum(Cast(Resp_Ins_Open_dol AS decimal(10,2))) as baltotal
from Config_21Cent
WHERE resp_ind = 'Insurance'
GROUP BY resp_ins, resp_payor_grouping, carrier_code, state, Collector_Name;
THis will work in both databases.
The columns in SELECT statement must be a subset (not proper subset but subset) of columns in 'GROUP BY' statement. There is no such restriction on aggregates in SELECT statement though. There could be any number of aggregates; aggregates even on columns not in GROUP BY statement can be included.
How do I ensure that I pick up the right number of records when filtering for dates within an Access Query:
SELECT ID, REF, SalesDate, DCount("ID","tblRecords"," Ref='" & [Ref] & "' AND [SalesDate]=#" & format([SalesDate],"yyyy/mm/dd") & "#") as EXPR1 from tblCurrent
It picks up the date ok if it cannot be misconstrued such as 28-04-12, but if it is 04-06-12 it doesn't pick it up as it's assuming it's the wrong way around.
Note that this query is not created on the fly or generated from a form etc...
I either use yyyy/mm/dd for dates in VBA:
#" & Format([SalesDate],"yyyy/mm/dd") & "#"
Or parameters, for building queries.
EDIT re additional information
Seeing you are using SQL server, I suggest you use a derived table, which you may find faster, for example:
SELECT dbo_Table_1.ADate, ACount FROM dbo_Table_1
LEFT JOIN (SELECT a.ADate,Count(*) As ACount
FROM dbo_Table_1 As a GROUP BY a.ADate) b
ON dbo_Table_1.Adate=b.ADate
EDIT re discussion
SELECT * FROM dbo_vwRecordsCurrent As t
LEFT JOIN (
SELECT a.OpptyIncentiveModifiedDate, a.DataSetID, Count(*) AS ACount
FROM dbo_vwRecordsHistorical AS a
WHERE a.OpportunityIgnored = True
GROUP BY a.OpptyIncentiveModifiedDate, a.DataSetID) AS h
ON t.OpptyIncentiveModifiedDate = h.OpptyIncentiveModifiedDate
AND t.DataSetID = h.DataSetID
I have aliased your tables as the names are very long, so to me, it is more readable to use aliases on the outer sql. They are essential in the inner sql. It is not a good idea to alias a derived table with the name of an existing table.