Select range date from and to and also select specific id - mysql

I want to select a date range with specific provider ID and this query should print 2 data only since there's only 2 row with provider id = 1
SQL Query:
SELECT logs_tb.logs_id
,logs_tb.time_in
,logs_tb.status
,logs_tb.provider_id
,users_tb.user_first_name
,users_tb.user_last_name
,users_tb.user_contactno
,users_tb.user_city
,users_tb.user_avatar
,users_tb.user_email
,users_tb.user_contactno
,users_tb.user_uuid
FROM logs_tb
,users_tb
WHERE logs_tb.provider_id = 1
AND logs_tb.log_date BETWEEN '2022-03-25' AND '2022-03-26'
ORDER BY logs_tb.logs_id desc;
Table:
Output:

I used ON condition, thank you M.Hermant
Query:
SELECT logs_tb.logs_id, logs_tb.time_in, logs_tb.status, logs_tb.provider_id, users_tb.user_first_name, users_tb.user_last_name, users_tb.user_contactno, users_tb.user_city, users_tb.user_avatar, users_tb.user_email, users_tb.user_contactno, users_tb.user_uuid
FROM users_tb
INNER JOIN logs_tb
ON users_tb.user_id = logs_tb.user_id
WHERE (logs_tb.log_date BETWEEN '2022-03-25' AND '2022-03-26') AND logs_tb.provider_id = 1
ORDER BY logs_tb.log_date desc;
Output:

Related

UPDATE from SELECT but still return selected data in MySQL

I have read a bunch of ways that has gotten me this far. But I can't get to the finish line.
I have a table of coupon codes. I want to use one transaction to select the next available code, mark it as used and input the order number. I can get the update and nested select to work, but I cannot figure out how to actually return the coupon code from the select. It just returns 1 row updated.
Here's what I've got:
UPDATE `prcoupon` pr
SET
`pr`.`status` = '1',
`pr`.`invoicenumber` = '09990002'
WHERE
`pr`.`couponCode` = (SELECT
`prcoupon`.`couponcode`
FROM
`prcoupon`
WHERE
`status` = 0
LIMIT 1)
Sample data
What I need returned is: couponCode: SL2T-03A0-JVCY-W2XMXG
If I understand correctly, you can try to use UPDATE ... JOIN with ROW_Nunber windwon function.
UPDATE prcoupon pr
JOIN (
SELECT *,ROW_NUMBER() OVER(ORDER BY couponCode) rn
FROM prcoupon
WHERE status = 0
) t2 ON pr.couponcode = t2.couponcode
SET pr.status = 1,
pr.invoicenumber = '09990002'
WHERE rn = 1
sqlfiddle

Get the rows with highest id

So i have this problem where I'm trying to get the row with highest id, the reason why i did a group restresults.id in the first example is because it show two of the same rows for some reason, but thats eliminated with that, but since the TestSuiteCollectionId and TestCaseId has the same TestCaseId but a different TestSuiteCollectionId and I need the row with highest id or as it's used here testresults.id, I thought I could group the Id's and do a ORDER BY testresult.id DESC, to get the highest id first, but for some reason it's not taking the first two rows, from example 1. Here is my pretty long sql code, don't mind all the nameId's. Have in mind that LIMIT 2 won't work, because I have more TestCaseId's this is just for the Id 1841, so it has to be the highest id for each group by TestSuiteCollectionId, TestCaseId with different TestCaseId's
SELECT
testresults.id, nameId, TestSuiteId, TestCaseId, TestSuiteCollectionId, TestCaseName, TestSuiteName, TestSuiteCollectionName, StatusId
FROM
Testreportingdebug.testresults
LEFT JOIN
TestResultRequirementLink ON testresults.id = TestResultRequirementLink.testresultid
LEFT JOIN
TestCaseRequirement ON TestResultRequirementLink.requirementId = TestCaseRequirement.id
LEFT JOIN
testcase ON testresults.TestCaseId = testcase.id
LEFT JOIN
testsuite ON testresults.TestSuiteId = testsuite.id
LEFT JOIN
testsuitecollection ON testresults.TestSuiteCollectionId = testsuitecollection.id
WHERE
TRUE
AND Testcaserequirement.nameId IN ('49971' , '49974',
'49976',
'305652',
'305653',
'305648',
'46486',
'46487',
'46440',
'46432',
'46447',
'46474',
'46441',
'651706',
'651709',
'305655',
'309133',
'46488',
'46317',
'46364',
'46365',
'46336',
'46342',
'49963',
'49956',
'49961',
'46467',
'46468',
'46469',
'171651',
'171652',
'171654',
'171655',
'190800',
'190799',
'190801',
'171232',
'171647',
'171648',
'171650',
'226065',
'226064',
'236257',
'46344',
'46332',
'46345',
'46331',
'46347',
'46330',
'46346',
'46720',
'46723',
'46725',
'46728',
'46737',
'46740',
'46743',
'46527',
'46530',
'46534',
'46535',
'46550',
'46557',
'46528',
'46533',
'46551',
'46559',
'46560',
'46703',
'46704',
'46706',
'46574',
'46708',
'46537',
'46543',
'46563',
'46567',
'46326',
'46328',
'46680',
'46643',
'46627',
'46626',
'46625',
'46622',
'46623',
'46644',
'46645',
'46647',
'46648',
'46714',
'46673',
'46674',
'46677',
'46679',
'46683',
'46689',
'46713',
'46678',
'46684',
'46699',
'46707',
'49994',
'46601',
'46604',
'46579',
'46602',
'46603',
'46605',
'46690',
'46456',
'46437',
'46438',
'46439',
'46475',
'46455',
'83985',
'46472',
'46471',
'1178127',
'1178128',
'1178129',
'1178132',
'1271718',
'46473',
'1178137',
'1301190',
'1308824',
'1273524',
'1273525',
'1273526',
'1273527',
'1273528',
'1273529',
'1308825',
'1308827',
'1308826',
'1336930',
'1308828',
'991788',
'1273496',
'1308819',
'1273497',
'1273500',
'1273503',
'1273508',
'1273510',
'1273512',
'1273518',
'1308822',
'1273515',
'1284164',
'1301195',
'1273516',
'1273517',
'1271722',
'1271723',
'1271724',
'1284162',
'1271720',
'1271728',
'1271731',
'1271727',
'1271730',
'1271733',
'1280758',
'1271736',
'1271738',
'1271740',
'1271743',
'1271747',
'1271746',
'1271744',
'1271745',
'1301193',
'1271726',
'1271729',
'1271732',
'1271735',
'1271737',
'1271739',
'1271741',
'1301632',
'1273520',
'1273522',
'1280762',
'1280765',
'1284168',
'1284167',
'1301196',
'1284165',
'1284166',
'1273509',
'1273511',
'1273514',
'1308820',
'1308821',
'1273498',
'1273499',
'1273501',
'1273502',
'1273504',
'1273505',
'1308829',
'1308830',
'1308831',
'1273530',
'1273531',
'1273532',
'1273533',
'1273534',
'1273535',
'1308832',
'1308833',
'1308834',
'1273537',
'1273542',
'1273543',
'1273539',
'1280768',
'1273540',
'1273544',
'1273541',
'1301197',
'1280766',
'1273538',
'1280767',
'1280769',
'1284169',
'1308835',
'1308836',
'1308837',
'1273545',
'1273546',
'1273547',
'1273548',
'1273549',
'1273550',
'1308838',
'1308839',
'1308840',
'1273552',
'1273557',
'1273558',
'1280770',
'1273553',
'1280771',
'1273559',
'1273556',
'1280773',
'1273554',
'1280772',
'1301198',
'1273555',
'1284170',
'1273521',
'1280761',
'1280763',
'1280764')
AND `TestSuiteCollectionName` LIKE '%GiM10%' AND TestCaseId = 1841
GROUP BY testresults.id
ORDER BY testresults.id DESC;
OUTPUT
Sorry for posting this as a picture but it's too wide
Table output
Then i tried to group by the id's and get them from highest testresult.id to lowest. but it shows lowest no matter that when without DESC or with.
Group by TestCaseId, TestSuiteCollectionId
Sounds like you want the row with the highest TestResults.Id for every TestSuiteCollection, TestCase. There are a couple of ways to go about it like a group by and joining that. For the cleanest, easiest to read approach, I'd recommend a window function for this.
You can do something like this:
SELECT * FROM
(
SELECT
ROW_NUMBER() OVER (PARTITION BY TestSuiteCollectionId, TestCaseId ORDER BY TestResults.Id DESC) AS RowNumber,
testresults.id,
nameId,
TestSuiteId,
TestCaseId,
TestSuiteCollectionId,
TestCaseName,
TestSuiteName,
TestSuiteCollectionName,
StatusId
FROM
Testreportingdebug.testresults
LEFT JOIN
TestResultRequirementLink ON testresults.id = TestResultRequirementLink.testresultid
LEFT JOIN
TestCaseRequirement ON TestResultRequirementLink.requirementId = TestCaseRequirement.id
LEFT JOIN
testcase ON testresults.TestCaseId = testcase.id
LEFT JOIN
testsuite ON testresults.TestSuiteId = testsuite.id
LEFT JOIN
testsuitecollection ON testresults.TestSuiteCollectionId = testsuitecollection.id
--WHERE
--any additional criteria
)foo
WHERE foo.RowNumber = 1
if the id's are integer then you can try using aggregate function max():
max(Testcaserequirement.nameId)
If i understand you correctly, you need max Id for each TestSuiteCollectionId and TestCaseId, thats why i put the max(id) and group by in the FROM statement and then Joined other tables to that max ID.
SQL:
select
testresults.id,
nameId,
TestSuiteId,
TestCaseId,
TestSuiteCollectionId,
TestCaseName,
TestSuiteName,
TestSuiteCollectionName,
StatusId
from(
select TestSuiteCollectionId, TestCaseId, max(testresults.id) id
FROM Testreportingdebug.testresults
LEFT JOIN testsuitecollection ON testresults.TestSuiteCollectionId = testsuitecollection.id
WHERE TRUE AND Testcaserequirement.nameId IN ('49971' , '49974','49976',........,'1280764')
AND `TestSuiteCollectionName` LIKE '%GiM10%' AND TestCaseId = 1841
GROUP BY TestSuiteCollectionId, TestCaseId) testresults
LEFT JOIN
TestResultRequirementLink ON testresults.id = TestResultRequirementLink.testresultid
LEFT JOIN
TestCaseRequirement ON TestResultRequirementLink.requirementId = TestCaseRequirement.id
LEFT JOIN
testcase ON testresults.TestCaseId = testcase.id
LEFT JOIN
testsuite ON testresults.TestSuiteId = testsuite.id
LEFT JOIN
testsuitecollection ON testresults.TestSuiteCollectionId = testsuitecollection.id

Query on MySQL with multiple joins

I use MySQL 5.7 and have the following tables:
documents:
---------------------------------------------------------------
id collection_id numeric_shelfmark shelfmark
---------------------------------------------------------------
1 kbt y024 Y 24
---------------------------------------------------------------
collections:
---------------------------------------------------------------
id name location
---------------------------------------------------------------
kbt kantonb thur Thurgau
gbt lorem ipsum
----------------------------------------------------------------
subprojects_x_documents
-----------------------------------------------------------------
subproject_id document_id
-----------------------------------------------------------------
foo 1
-----------------------------------------------------------------
documents_revisions
-----------------------------------------------------------------
document_id updated_at numeric_shelfmark shelfmark latest
-----------------------------------------------------------------
1 2016-06-22 10:32:01 y024 Y 24 0
1 2017-09-19 09:19:17 z024 Z 24 1
------------------------------------------------------------------
With the following SELECT query I could manage to get a list of the documents associated with the subproject and collections:
SELECT `documents`.`id`, `documents`.`collection_id`,
`documents`.`numeric_shelfmark`, `shelfmark`, `location`, `name`
FROM (`documents`)
JOIN `collections` ON `documents`.`collection_id` = `collections`.`id`
JOIN `subprojects_x_documents` ON `documents`.`id` =
`subprojects_x_documents`.`document_id`WHERE
`subprojects_x_documents`.`subproject_id` = 'stgall_lotterie'
ORDER BY `location`, `name`, `documents`.`sort_order`, `numeric_shelfmark`
Now, I'm trying to get the latest ("documents_revisions.latest" = 1) revision of document with:
SELECT `documents`.`id`, `documents`.`collection_id`,
`documents`.`numeric_shelfmark`, `documents`.`shelfmark`, `location`,
name`FROM (`documents`)JOIN `collections` ON
`documents`.`collection_id` = `collections`.`id`JOIN
`documents_revisions` ON `documents_revisions`.`document_id` =
`documents`.`id`JOIN `subprojects_x_documents` ON `documents`.`id` =
`subprojects_x_documents`.`document_id` WHERE
`subprojects_x_documents`.`subproject_id` = 'stgall_lotterie' AND
`documents_revisions`.`latest` = 1 ORDER BY `location`, `name`,
`documents`.`sort_order`, `numeric_shelfmark`
But this query returns the same list of documents as before and ignores the documents_revisions.latest" = 1 part.
When I update the query to perform the operation on documents_revisons table instead of documents table:
SELECT `documents`.`id`, `documents`.`collection_id`,
documents.numeric_shelfmark, documents.shelfmark, location,
nameFROM (documents_revisions)JOIN collections ON
documents.collection_id = collections.idJOIN
documents_revisions ON documents_revisions.document_id =
documents.idJOIN subprojects_x_documents ON documents.id =
subprojects_x_documents.document_id WHERE
subprojects_x_documents.subproject_id = 'stgall_lotterie' AND
documents_revisions.latest = 1 ORDER BY location, name,
documents.sort_order, numeric_shelfmark
I get the error message: "Not unique table/alias: 'documents_revisions'"
Your table is called documents, not document
Ok, after some tweaking I got my result with:
SELECT documents_revisions.document_id,
documents_revisions.collection_id,
documents_revisions.numeric_shelfmark,
documents_revisions.shelfmark, location, nameFROM
(documents_revisions)JOIN collections ON
documents_revisions.collection_id = collections.id JOIN
subprojects_x_documents ON documents_revisions.document_id =
subprojects_x_documents.document_id WHERE
subprojects_x_documents.subproject_id = 'stgall_lotterie' AND
documents_revisions.latest = 1 ORDER BY location, name,
numeric_shelfmark

Group by changing boolean value

Hi i have the following SQL question:
SELECT station_id, filling_station_status,date_created ,
case when filling_station_status="FREE" then 0
else 1 end as status
FROM efahrung.electric_station_time_status
where station_id=11
In my table have a column filling_station_status.
It can be "FREE" or "IN_USE".
I want to group elements so, that if the filling_station_status is changed (from "FREE" to "IN_USE") it will create a date range in my case, date_created.
In the next change again from ("IN_USE" to "FREE") it creates a new date range.
Thanks for a suggestions.
If you just need SQL query to generate date range in output, then try this:
Select s.station_id,
Coalesce(e.filling_station_status, s.filling_station_status) fillingStationStatus,
case e.filling_station_status
when "FREE" then 0 else 1 end status,
s.date_created startDate,
e.date_created endDate
From efahrung.electric_station_time_status s
Left Join efahrung.electric_station_time_status e
On e.station_id = s.station_id
and s.filling_station_status = 'IN_USE'
and e.filling_station_status = 'FREE'
and e.date_created =
(Select Min(date_created)
From efahrung.electric_station_time_status
Where station_id = s.station_id
and date_created > s.date_created)

Error creating a SP with temporal table inside and subquery

I've created this temporal table in my store procedure, as you can see I have more than 1 records for the same ID:
#tmpTableResults
TmpInstallerID TmpConfirmDate TmpConfirmLocalTime
============== ============== ===================
173 2011-11-08 11:45:50
278 2011-11-04 09:06:26
321 2011-11-08 13:21:35
321 2011-11-08 11:44:54
483 2011-11-08 11:32:00
483 2011-11-08 11:31:59
645 2011-11-04 10:03:15
645 2011-11-04 07:03:15
That is the result of the query to create #tmpTableResults
DECLARE #tmpTableResults TABLE
(
TmpInstallerID int,
TmpConfirmDate date,
TmpConfirmLocalTime time
)
DECLARE #tmpTableQuery VarChar(800)
SET #tmpTableQuery = 'select FxWorkorder.INSTALLERSYSID, FxWorkorder.CONFIRMDATE, FxWorkorder.CONFIRMLOCALTIME from FxWorkorder
join install on FxWorkorder.INSTALLERSYSID = install.sysid
join RouteGroupWorkarea on FxWorkorder.WORKAREAGROUPSYSID = RouteGroupWorkarea.IWORKAREA_ID
join RoutingGroup on RouteGroupWorkarea.IRG_ID = RoutingGroup.IRG_IDENTITY
where FxWorkorder.SCHEDULEDDATE > = #StartDate and FxWorkorder.SCHEDULEDDATE <= #EndDate
and FxWorkorder.Jobstatus <> "Unassign"
and FxWorkorder.Jobstatus <> "Route"
and install.FOXTELCODE <> ""
and FxWorkorder.CONFIRMLOCALTIME is not null
and FxWorkorder.CONFIRMDATE <> ""
group by FxWorkorder.INSTALLERSYSID, FxWorkorder.CONFIRMDATE, FxWorkorder.CONFIRMLOCALTIME
order by FxWorkorder.INSTALLERSYSID, FxWorkorder.CONFIRMDATE, FxWorkorder.CONFIRMLOCALTIME desc '
INSERT INTO #tmpTableResults EXEC(#tmpTableQuery)
I'm creating another query to get data from another table and only the first record from the temporal table for the same INSTALLERSYSID
SELECT RoutingGroup.SDESCRIPTION, FxWorkorder.INSTALLERSYSID, FxWorkOrder.JOBSTATUS, Install.FOXTELCODE,
install.NAME, FxWorkOrder.ScheduledDate,
count(*) as TotalJobs, COUNT(CONFIRMDATE) as ConfirmedJobs,
(select TmpInstallerID, TmpConfirmDate, TmpConfirmLocalTime from #tmpTableResults where TmpInstallerID = FxWorkorder.INSTALLERSYSID)
from FxWorkorder
join install on fxworkorder.INSTALLERSYSID = install.sysid
join RouteGroupWorkarea on FxWorkOrder.WORKAREAGROUPSYSID = RouteGroupWorkarea.IWORKAREA_ID
join RoutingGroup on RouteGroupWorkarea.IRG_ID = RoutingGroup.IRG_IDENTITY
where FxWorkorder.SCHEDULEDDATE > = #StartDate and FxWorkorder.SCHEDULEDDATE <= #EndDate
and FxWorkOrder.Jobstatus <> 'Unassign'
and FxWorkOrder.Jobstatus <> 'Route'
and Install.FOXTELCODE <> ''
group by RoutingGroup.SDESCRIPTION,FxWorkOrder.INSTALLERSYSID, FxWorkOrder.JOBSTATUS, Install.FOXTELCODE,install.NAME, FxWorkOrder.ScheduledDate,FxWorkOrder.WORKAREAGROUPSYSID
When I tried to save the sp I got the error
"Only one expression can be specified in the select list when the subquery is not introduced with EXISTS."
I can't see why I got this error. But if I run the query in sql that works. Can someone see the error?
I don't know how your second query works for you ‘in sql’ (where is that supposed to be? do you mean SSMS = SQL Server Management Studio?), but I'm sure it cannot possibly work in any version of SQL Server that exists at the moment. It's because of this subquery in the SELECT list:
(select TmpInstallerID, TmpConfirmDate, TmpConfirmLocalTime from #tmpTableResults where TmpInstallerID = FxWorkorder.INSTALLERSYSID)
The thing is, every expression in the SELECT clause should be scalar, but this subquery returns a row of more than one value. Even if it's only one row, it is illegal there, because it returns several columns. The subquery in that context should return no more than one value, i.e. it should be one column and the result produced should contain either no rows or just one.
You could try this query instead (although I'm not entirely sure without knowing more details about your schema):
SELECT
RoutingGroup.SDESCRIPTION,
FxWorkorder.INSTALLERSYSID,
FxWorkOrder.JOBSTATUS,
Install.FOXTELCODE,
install.NAME, FxWorkOrder.ScheduledDate,
count(*) as TotalJobs, COUNT(CONFIRMDATE) as ConfirmedJobs,
tmp.TmpInstallerID,
tmp.TmpConfirmDate,
tmp.TmpConfirmLocalTime
from FxWorkorder
join install on fxworkorder.INSTALLERSYSID = install.sysid
join RouteGroupWorkarea on FxWorkOrder.WORKAREAGROUPSYSID = RouteGroupWorkarea.IWORKAREA_ID
join RoutingGroup on RouteGroupWorkarea.IRG_ID = RoutingGroup.IRG_IDENTITY
join #tmpTableResults tmp ON tmp.TmpInstallerID = FxWorkorder.INSTALLERSYSID
where FxWorkorder.SCHEDULEDDATE > = #StartDate
and FxWorkorder.SCHEDULEDDATE <= #EndDate
and FxWorkOrder.Jobstatus <> 'Unassign'
and FxWorkOrder.Jobstatus <> 'Route'
and Install.FOXTELCODE <> ''
group by
RoutingGroup.SDESCRIPTION,
FxWorkOrder.INSTALLERSYSID,
FxWorkOrder.JOBSTATUS,
Install.FOXTELCODE,install.NAME,
FxWorkOrder.ScheduledDate,
FxWorkOrder.WORKAREAGROUPSYSID
tmp.TmpInstallerID,
tmp.TmpConfirmDate,
tmp.TmpConfirmLocalTime
That is, I added one more join, the one to #tmpTableResults, as well as added the columns you were trying to pull to the SELECT clause and to the GROUP BY clause.
Also, if I were you I would consider using short aliases for tables, like this:
SELECT
…
wo.INSTALLERSYSID,
wo.JOBSTATUS,
…
from FxWorkorder wo
join …
That might make your queries more readable.