I've looked at other questions, and I am not certain this is the same as previously asked questions.
Basically, I have three queries in succession. Query 1 results are passed to Query 2, and Query 2 results are passed to query 3, and write query 3 results to a file.
'select convert(char(10),max(paydate),101) from DATA.dbo.payment where status like 'PAID%'
'select distinct groupkey from custom.dbo.ediX091Header_abc where CheckIssue = '<results from query 1>' and status = 'Go'
'select max(GroupKey) from dbo.ediFuncGroup_abc' where GroupKey = '<results from query 2>'
Write any results from query 3 to an output file.
Thanks in advance
You don't need to run all three queries one after the other. You can pretty easily join the tables from the second and third queries. Then use the first query as a subquery to filter the results. Something like this.
select MAX(GroupKey)
from dbo.ediFunctionGroup_abc fc
join custom.dbo.ediX091Header_abc h on h.GroupKey = fc.GroupKey
where h.status = 'Go'
and h.CheckIssue = (select CONVERT(char(10), max(paydate)) from DATA.dbo.payment where status like 'PAID%')
Related
My Platform is MySql
I have two queries that I need to combine, using the first query as a type of filter for the second query.
Query 1:
SELECT * FROM INVENTORY
WHERE INV_ID = 1
AND FSCL_YR = 2017
From this query we will get results back that includes a column named STR_NBR.
Which we then want to use in the second query as 'If the store number appears in the first query, give me the results where it shows in the second'. The second query tables use the column name SND_LOC_NBR instead of STR_NBR.
Query 2:
SELECT * FROM Transfer A
LEFT JOIN Transfer_Detail B
ON A.XFER_NBR = B.XFER_NBR
WHERE A.XFER_NBR = B.XFER_NBR
AND A.XFER_STAT_IND IN ('S','C')
AND (where the SND_LOC_NBR needs to match STR_NBRs found from Query 1)
Try this:
SELECT * FROM Transfer A
LEFT JOIN Transfer_Detail B
ON A.XFER_NBR = B.XFER_NBR
WHERE A.XFER_STAT_IND IN ('S','C')
AND SND_LOC_NBR IN
(SELECT STR_NBR FROM INVENTORY
WHERE INV_ID = 1 AND FSCL_YR = 2017 )
I'm trying to write a MYSQL Query that updates a cell in table1 with information gathered from 2 other tables;
The gathering of data from the other 2 tables goes without much issues (it is slow, but that's because one of the 2 tables has 4601537 records in it.. (because all the rows for one report are split in a separate record, meaning that 1 report has more than 200 records)).
The Query that I use to Join the two tables together is:
# First Table, containing Report_ID's: RE
# Table that has to be updated: REGI
# Join Table: JT
SELECT JT.report_id as ReportID, REGI.Serienummer as SerialNo FROM Blancco_Registration.TrialTable as REGI
JOIN (SELECT RE.Value_string, RE.report_id
FROM Blancco_new.mc_report_Entry as RE
WHERE RE.path_id=92) AS JT ON JT.Value_string = REGI.Serienummer
WHERE REGI.HardwareType="PC" AND REGI.BlanccoReport=0 LIMIT 100
This returns 100 records (I limit it because the database is in use during work hours and I don't want to steal all resources).
However, I want to use these results in a Query that updates the REGI table (which it uses to select the 100 records in the first place).
However, I get the error that I cannot select from the table itself while updateing it (logically). So I tried selecting the select statement above into a temp table and than Update it; however, then I get the issue that I get to much results (logically! I only need 1 result and get 100) however, I'm getting stuck in my own thougts.. I ultimately need to fill the ReportID into each record of REGI.
I know it should be possible, but I'm no expert in MySQL.. is there anybody that can point me into the right direction?
Ps. fixing the table containing 400k records is not an option, it's a program from an external developer and I can only read that database.
The errors I'm talking about are as follows:
Error Code: 1093. You can't specify target table 'TrialTable' for update in FROM clause
When I use:
UPDATE TrialTable SET TrialTable.BlanccoReport =
(SELECT JT.report_id as ReportID, REGI.Serienummer as SerialNo FROM Blancco_Registration.TrialTable as REGI
JOIN (SELECT RE.Value_string, RE.report_id
FROM Blancco_new.mc_report_Entry as RE
WHERE RE.path_id=92) AS JT ON JT.Value_string = REGI.Serienummer
WHERE REGI.HardwareType="PC" AND REGI.BlanccoReport=0 LIMIT 100)
WHERE TrialTable.HardwareType="PC" AND TrialTable.BlanccoReport=0)
Then I tried:
UPDATE TrialTable SET TrialTable.BlanccoReport = (SELECT ReportID FROM (<<and the rest of the SQL>>> ) as x WHERE X.SerialNo = TrialTable.Serienummer)
but that gave me the following error:
Error Code: 1242. Subquery returns more than 1 row
Haveing the Query above with a LIMIT 1, gives everything the same result
Firstly, your query seems to be functionally identical to the following:
SELECT RE.report_id ReportID
, REGI.Serienummer SerialNo
FROM Blancco_Registration.TrialTable REGI
JOIN Blancco_new.mc_report_Entry RE
ON RE.Value_string = REGI.Serinummer
WHERE REGI.HardwareType = "PC"
AND REGI.BlanccoReport=0
AND RE.path_id=92
LIMIT 100
So, why not use that?
EDIT:
I still don't get it. I can't see what part of the problem the following fails to solve...
UPDATE TrialTable REGI
JOIN Blancco_new.mc_report_Entry RE
ON RE.Value_string = REGI.Serinummer
SET TrialTable.BlanccoReport = RE.report_id
WHERE REGI.HardwareType = "PC"
AND REGI.BlanccoReport=0
AND RE.path_id=92;
(This is not an answer, but maybe a pointer towards a few points that need further attention)
Your JT sub query looks suspicious to me:
(SELECT RE.Value_string, RE.report_id
FROM Blancco_new.mc_report_Entry as RE
WHERE RE.path_id=92
GROUP BY RE.report_id)
You use group by but don't actually use any aggregate functions. The column RE.Value_string should strictly be something like MAX(RE.Value_string) instead.
I have made a query that looks like this
Query 1.
SELECT zlec_status.nazwa AS Status,
piorytet.nazwa AS Priorytet,
Concat(koord.imie, ' ', koord.nazwisko) AS `Koordynator`,
Concat(zlec_adresy.miasto, ' - ', zlec_adresy.ulica, ' ',
zlec_adresy.oddzial)
AS `adres`,
zlec_z_dnia,zlec_id,
zlec_nr,
zlec_do,
zlec_ogran,
awizacje,
awizacja_na_dzien,
termin_zamkniecia,
tresc,
uwagi
FROM zlec
INNER JOIN koord
ON zlec.koord = koord.id
INNER JOIN zlec_adresy
ON zlec.zlec_addres = zlec_adresy.id
INNER JOIN piorytet
ON zlec.priorytet = piorytet.id
INNER JOIN zlec_status
ON zlec.status_zlecenia = zlec_status.id
And the following one which is a ordinary one
Query 2.
SELECT * FROM zlec;
The thing is the first one returns ( affects by executing ) 48 rows where the second query returns 103 rows. What could be the possible cause of this?
I will also show you my dumb of the sql in case you would like to make a run on your own http://pastebin.com/cMPAtxCU .
Subquestion - quite no point starting of with a new question for that because its also connected with the row count affect.
Besides I was wondering how can I get into the first query a count(*) to get the affected rows - it has to be done in sql I cannot use php code for that, probably it would be good to use a limit 1 for the count.
With INNER JOIN, if one of your other tables, koord, zlec_adresy, piorytet and zlec_status is missing a record corresponding to a record in zlec, that record in zlec will not be in the result set. If you want every record in zlec to appear, you have to use LEFT JOIN. Check out:
http://blog.codinghorror.com/a-visual-explanation-of-sql-joins/
For a pretty good explanation.
With your additional inner joins rows might be eliminated. Have you tried adding the inner joins to your "Select * FROM zlec;"?
The result of this SQL query should return a set of 6 rows.
SELECT baseFeeds.siteURL, feedFinishes.timeTaken, feedFinishes.timeSubmitted
FROM feedFinishes
LEFT OUTER JOIN baseFeeds
ON feedFinishes.GUID = baseFeeds.GUID
WHERE feedFinishes.nowDate = baseFeeds.nowDate
AND baseFeeds.siteURL LIKE '%www.example.com%'
Currently it returns 18 rows, the correct 6 rows are being repeated 3 times. I figure a solution to this is to have another WHERE clause something similar to:
WHERE DISTINCT feedFinishes.ID
After researching this I have tried:
SELECT baseFeeds.siteURL, feedFinishes.timeTaken, feedFinishes.timeSubmitted
FROM feedFinishes
JOIN baseFeeds
ON feedFinishes.GUID = baseFeeds.GUID
WHERE baseFeeds.siteURL LIKE '%www.example.com%' AND feedFinishes.ID in
(SELECT ID FROM feedFinishes GROUP BY ID HAVING COUNT(ID)=1);
After the discussion found here
Alas, this still returns 18 rows. I believe the answer is similar to here
Any advice would be appreciated.
You should apply group by clause.
SELECT
baseFeeds.siteURL, feedFinishes.timeTaken, feedFinishes.timeSubmitted
FROM feedFinishes
JOIN baseFeeds
ON feedFinishes.GUID = baseFeeds.GUID
WHERE baseFeeds.siteURL LIKE '%www.example.com%'
GROUP BY
baseFeeds.siteURL, feedFinishes.timeTaken, feedFinishes.timeSubmitted
How can i run a UNION query in hibernate.
I'm new to learning hibernate and now i have a named query like this,
<query>
SELECT product.defaultSk.name FROM Product product
WHERE product.defaultSk.name LIKE :name
AND product.defaultSk.activeStartDate <= :currentDate
AND (product.defaultSk.activeEndDate > :currentDate OR product.defaultSk.activeEndDate = null)
AND (product.archiveStatus.archived IS NULL OR product.archiveStatus.archived = 'N')
UNION
SELECT category.name FROM Category category
WHERE category.name LIKE :name
AND (archived = 'N')
</query>
When i execute the above query, I'm getting the result for only the first query and my second query is not returning any results..
My console shows only the triggering of the first query and i cant find the second query being fired.Why does it not run the second query...
This my console after running the query,
CONSOLE:
Hibernate: select skimpl1_.NAME as col_0_0_ from PRODUCT productimp0_, BLC_SK skimpl1_ left outer join PRODUCT_SK_XREF skimpl1_1_ on skimpl1_.SK_ID=skimpl1_1_.SK_ID where productimp0_.DEFAULT_SK_ID=skuimpl1_.SK_ID and (skimpl1_.NAME like ?) and skimpl1_.ACTIVE_START_DATE<=? and (skimpl1_.ACTIVE_END_DATE>? or skimpl1_.ACTIVE_END_DATE is null) and (productimp0_.ARCHIVED is null or productimp0_.ARCHIVED='N')
I have implemented the jquery autocomplete to yeild results from a table and now that i want to merge two table results.I m going with the union query as it merges the result from two tables.
Am i doing something wrong..
I have read some SO posts that union is not possible in Hibernate.
If so what will be the workaround for this problem.
Thanks in advance.
HQL doesn't support union. You'll have to execute two different queries, or use SQL.