My query keeps returning the last date for ONRENT and CLOSED for the same piece of equipment when I add the column "AVAILABILITY". I only want the last entry for each piece of equipment.
Here is my query:
select max([No_]), [Start Date], [Availability Status]
from [Rental Line]
group by [No_], [Start Date], [Availability Status]
If I'm understanding your question correctly, you want only the last availability status for each piece of equipment defined by No_, where "last" would be defined by the latest start date. If so, this should work:
Try this:
select RL.[No_], RL.[Start Date], RL.[Availability Status]
from [Rental Line] RL
join
(
select
No_,
MAX([Start Date]) AS [LastStartDate]
from [Rental Line] RL1
group by No_
) X on RL.No_ = X.No_ AND RL.[Start Date] = X.[LastStartDate]
select max([No_]), [Start Date], [Availability Status]
from [Rental Line]
group by [No_], [Start Date], [Availability Status]
order by [AVAILABILITY] DESC
Related
First off I know nothing about VB and the post I have seen on here refer to that, I am sure that this is a very simple thing to do so please help
I am trying to hve my TN Route be my primary key/no duplicates and have date driver 1 driver 2 and driver 3 all in diff cells and all side by side, and even though the tbl that it is pulling from the TN route is primary, it keeps adding additional rows for each driver.
See attached screenshot:
Thank you in advance.
Stan
data sheet view of query
This query will do it for you. Again, it's not preferable because in Access SQL tends to be a little messy on account of it not having features that more robust SQL platforms do - but it will work. You'll need to change the name of your Date column to Route Date in order for this to work, and replace my tblTest with whatever your table name is:
SELECT DISTINCT tblTest.[TN Route], tblTest.[Route Date], tA.[Driver 1], tB.[Driver 2], tC.[Driver 3]
FROM ((tblTest LEFT JOIN (SELECT tblTest.[TN Route], tblTest.[Route Date], [Driver 1] FROM tblTest WHERE [Driver 1] <> '') AS tA ON (tblTest.[Route Date] = tA.[Route Date]) AND (tblTest.[TN Route] = tA.[TN Route]))
LEFT JOIN (SELECT tblTest.[TN Route], tblTest.[Route Date], [Driver 2] FROM tblTest WHERE [Driver 2] <> '') AS tB ON (tblTest.[Route Date] = tB.[Route Date]) AND (tblTest.[TN Route] = tB.[TN Route]))
LEFT JOIN (SELECT tblTest.[TN Route], tblTest.[Route Date], [Driver 3] FROM tblTest WHERE [Driver 3] <> '') AS tC ON (tblTest.[Route Date] = tC.[Route Date]) AND (tblTest.[TN Route] = tC.[TN Route])
In Access 2010, go Create > Query Design > add your table, and then paste the SQL into the SQL view of the query designer. You can even make a standalone table out of the result.
I have a issue with my SQL code.
When I run this code I'm getting such error mesage:
**
Subquery returned more than 1 value. This is not permitted when the
subquery follows =, !=, <, <= , >, >= or when the subquery is used as
an expression.
**
I don't know how to correct this code to run it?
Can somebothy look at it and help me with it?
Thank you for your help,
SELECT dbo.FRA$.[GUID], dbo.FRA$.[Last Name],
dbo.FRA$.[First Name], dbo.FRA$.[HR Manager Last Name],
dbo.FRA$.[HR Manager First Name], (SELECT dbo.FRA$.[Business Email Address] FROM dbo.FRA$ t WHERE t.[HR Manager ID] = t.[Emplid]) AS [HR Manager E-Mail],
dbo.LOC.Descr AS [COMPANY NAME], dbo.FRA$.[Location Code],
dbo.LOC.[ADDRESS], dbo.LOC.Postal, dbo.LOC.City, dbo.FRA$.[National ID], dbo.FRA$.[Current Employee Class Desc] AS [Current Empl Class],
dbo.FRA$.[Contract type description] AS [CONTRACT TYPE], dbo.FRA$.[Business Title],
CASE dbo.FRA$.[Current Employee Class Desc]
WHEN 'Operator' THEN 10
WHEN 'Clerical worker' THEN 22
WHEN 'Technician' THEN 32
WHEN 'Supervisor' THEN 33
WHEN 'Manager&Engineer' THEN 54
END AS [Salary brutto],
FORMAT(dbo.FRA$.[Birthdate],'yyyy-MM-dd') AS [Birth Date], FORMAT(dbo.FRA$.[Last Start Date],'yyyy-MM-dd') AS [Hire Date]
FROM dbo.FRA$
INNER JOIN dbo.LOC
ON dbo.FRA$.[Location Code] = dbo.LOC.[Location]
WHERE [HR Status] IN ('International Assignment','Active','Leave of Absence');
Try to replace
SELECT dbo.FRA$.[Business Email Address] FROM dbo.FRA$ t WHERE t.[HR Manager ID] = t.[Emplid]
with
SELECT TOP 1 t.[Business Email Address] FROM dbo.FRA$ t WHERE [HR Manager ID] = t.[Emplid]
it will take only one row from rows returned by your subquery
So basically my code involves a created a table which has the LAST COLUMN as the SUM of the previous two columns, with the first of the two coming from table X and the second of the two columns coming from the 'INNER JOIN OF table X with table Y'.
HOWEVER, I want to group ALL THREE of these columns by the 'COUNTERPARTY', a variable which is present BOTH in 'table X' AND in 'INNER JOIN OF TABLE X WITH TABLE Y.'
The tricky part is that there will be some COUNTERPARTIES for which we have data set 1 (coming from TABLE X) and some COUNTERPARTIES for which we have data set 2 (coming from INNER JOIN OF table X with table Y) and SOME FOR WHICH WE HAVE BOTH DATASETS! I WANT TO INCLUDE THE INTERSECTION AS WELL AS THE OUTLIERS BUT ONLY AFTER CHECKED AGAINST A MASTER KEY OF COUNTERPARTIES called c.COUNTERPTY.
Please carefully go through the code below to see how the above explanation is relevant to the problem at hand. I apologize for the length of the code.
select
p.Name as ENTITY, t.[Counterparty Code], c.CNTRPTY_DS as COUNTERPARTY, cs.Tier,
... irrelevant code removed
sum((t.[Current value decimal] - t.[Trade price decimal])/100 * case when t.[Buy Sell Code] = 'B'
then 1 else -1 end * t.[Open Amount]) as [OPEN MTM ($)],
sum((t2.[Weighted Average Settled Pair Off Price] - t2.[Trade price decimal])/100 * case when t2.[Buy Sell Code] = 'B'
then 1 else -1 end * ISNULL(PO.[Pairoff Amount],0)) as [Unsettled Pairoffs/ AOTs ($)],
sum((t.[Current value decimal] - t.[Trade price decimal])/100 * case when t.[Buy Sell Code] = 'B'
then 1 else -1 end * t.[Open Amount]) + sum((t2.[Weighted Average Settled Pair Off Price] - t2.[Trade price decimal])/100 * case when t2.[Buy Sell Code] = 'B'
then 1 else -1 end * ISNULL(PO.[Pairoff Amount],0)) as [TOTAL MTM Exposure ($)]
from
[la-w08-qrm-db-1].qrmprod.dbo.vw_QRM_Trades t2
inner join
[la-w08-qrm-db-1].qrmprod.dbo.VW_QRM_TRADE_PAIROFFS PO
ON
PO.[In Ticket Number] = t2.[Ticket number]
and PO.[Portfolio ID] = t2.[Portfolio ID]
and t2.[derivative type] = 'F' -- note repeat below
and t2.[forward type] ='MBS'
and t2.[Counterparty Code] not in ('PLS', 'PNCO')
and t2.[Portfolio ID] in (1,7)
and t2.[Settlement date] > GETDATE(),
prod.dbo.vw_QRM_Trades t,
prod.dbo.portdesc p,
prod.dbo.cptyall c,
prod.dbo.VW_MB_ACTIVE_RUN r,
pulsar.dbo.CntrPrtySetup CS,
pulsar.dbo.CntrPrtyTiers CT
where
r.mrktid = 1
And r.asmpid = 1
And r.cyclid = 1
and r.compid = t.[Company ID]
and r.portid = t.[Portfolio ID]
and p.PORTID = t.[Portfolio ID]
and c.COUNTERPTY = t.[Counterparty Code] --key piece of code
and cs.CNTRPTY_NO = c.CNTRPTY_NO
and cs.PortID = t.[Portfolio ID]
and cs.Tier = ct.Tier
and t.[derivative type] = 'F' -- note repeat above
and t.[forward type] ='MBS'
and t.[Counterparty Code] not in ('PLS', 'PNCO')
and t.[Portfolio ID] in (1,7)
and t.[Open Amount] > 0
group by
p.Name, c.CNTRPTY_DS , t.[Counterparty Code], cs.Tier -- yes this
order by
p.Name, c.CNTRPTY_DS , t.[Counterparty Code], cs.Tier -- and this
The way to do is was to actually use UNION in order to join three tables together in the order and given the properties I wanted from each of them. Eventually I did manage to finish the whole stored procedure!
I'm fairly new to SQL and having trouble finding the proper commands for what I'm trying to accomplish.
We have a ticketing system that allows us to place tickets on hold. I'm trying to produce a report that will show only the time the ticket was in a non-hold state.
I'm running the query in MySQL workbench.
Here is my query to gather the information I need to achieve my goal:
select ID, HD_TICKET_ID, TIMESTAMP,
case when DESCRIPTION LIKE '%opened" to "hold%' then 'hold' else 'active' end as state,
DESCRIPTION
from H
D_TICKET_CHANGE
where HD_TICKET_ID = 7715
order by TIMESTAMP
Here are my results:
ID HD_TICKET_ID TIMESTAMP state DESCRIPTION
25040 7715 "2014-08-06 16:39:29" active "Ticket Created
25042 7715 "2014-08-06 17:13:15" active "Changed ticket Status from ""New"" to ""Opened"".
25078 7715 "2014-08-07 10:38:28" hold "Changed ticket Status from ""Opened"" to ""Hold"".
25081 7715 "2014-08-07 10:54:55" active "Changed assest name"
25201 7715 "2014-08-11 08:24:56" active "Changed ticket Title. Changed ticket Status from ""Hold"" to ""Opened"".
25202 7715 "2014-08-11 08:25:08" hold "Changed ticket Status from ""Opened"" to ""Hold"".
25341 7715 "2014-08-13 10:56:00" active "Changed ticket Status from ""Hold"" to ""Opened"".
25373 7715 "2014-08-13 13:41:01" hold "Changed ticket Status from ""Opened"" to ""Hold"".
25551 7715 "2014-08-15 13:54:21" active "Added resolution text. Changed ticket Status from""Hold"" to ""Closed"".
At this point I'm stuck. How would I add the time from 1 through 3 because the ticket is active and that time needs to be reported, but subtract 3 through 4 because the ticket is on hold, and continue this addition or subtraction to the end of these result based on active vs hold?
You need to get the next time stamp value. Then you can do what you want with aggregation. I like to use correlated subqueries for this purpose in MySQL:
select ID, HD_TICKET_ID, TIMESTAMP,
(case when DESCRIPTION LIKE '%opened" to "hold%' then 'hold'
else 'active'
end) as state, DESCRIPTION,
(select tc2.timestamp
from HD_TICKET_CHANGE tc2
where tc2.timestamp > tc.timestamp
order by tc2.timestamp
limit 1
) as next_timestamp
from HD_TICKET_CHANGE tc
where HD_TICKET_ID = 7715
order by TIMESTAMP;
Then, you can add up the results as:
select sum((case when state = 'active' then 1 else -1 end) *
timestampdiff(second, next_timestamp, timestamp)
) as TimeInSeconds
from (select ID, HD_TICKET_ID, TIMESTAMP,
(case when DESCRIPTION LIKE '%opened" to "hold%' then 'hold'
else 'active'
end) as state, DESCRIPTION,
(select tc2.timestamp
from HD_TICKET_CHANGE tc2
where tc2.timestamp > tc.timestamp and tc2.HD_TICKET_ID = tc.HD_TICKET_ID
order by tc2.timestamp
limit 1
) as next_timestamp
from HD_TICKET_CHANGE tc
where HD_TICKET_ID = 7715
) t;
This disregards the final time stamp because there is no "next" value and your question doesn't specify what to do in that case.
I have a query as below :
select column_date, field1, field2, sum(field3) from table1
where field1 like '*xyz' and
column_date between [please enter start date] and [please enter end date]
group by column_date, field1, field2
When I leave blank both the parameter boxes, the output is blank. But I want them in output as below
I want to display all records when I leave blank both parameter box
if I put date in any one of the parameter boxes, it should display only records for that date
If i put dates in both the parameter boxes, it should display all records between those dates.
This is my asp code to display the output. It works when I insert values in both the text boxes but if I left blanks any of them or both, it shows error.
<html>
<body>
<%
dim startdate, enddate
startdate = Request.Form ("startdate")
enddate = Request.Form("enddate")
set conn = Server.CreateObject ("ADODB.Connection")
conn.open "connectionname"
set rs = Server.CreateObject ("ADODB.Recordset")
Sqlquery = "queryname '" & startdate & "', '" & enddate &'" "
rs.open sql, conn %>
<table>
<tr>
<%
For each x in rs.fields
response.write ("<th>" & x.name & "</th>")
next %> </tr>
<tr><% Do Until rs.EOF %>
<% For each x in rs.Fields %>
<td>Response.write (x.value)</td>
<%next
rs.movenext %>
</tr>
<% loop
rs.close
conn.close %>
</table>
</body>
</html>
I would start with a plain SELECT query to figure out the WHERE clause. Once you get that working correctly, convert it to a GROUP BY query.
So see if this one targets the correct records:
PARAMETERS [Start Date] DateTime, [End Date] DateTime;
SELECT t.field1, t.field2, t.field3, t.column_date
FROM table1 AS t
WHERE
t.field1 Like '*xyz'
AND (
(t.column_date Between [Start Date] And [End Date])
OR ([Start Date] Is Null And t.column_date = [End Date])
OR (t.column_date = [Start Date] And [End Date] Is Null)
OR ([Start Date] Is Null And [End Date] Is Null)
);
Assuming that first query returns the correct rows, I think this GROUP BY query may give you what you want.
PARAMETERS [Start Date] DateTime, [End Date] DateTime;
SELECT t.column_date, t.field1, t.field2, sum(t.field3)
FROM table1 AS t
WHERE
t.field1 Like '*xyz'
AND (
(t.column_date Between [Start Date] And [End Date])
OR ([Start Date] Is Null And t.column_date = [End Date])
OR (t.column_date = [Start Date] And [End Date] Is Null)
OR ([Start Date] Is Null And [End Date] Is Null)
)
GROUP BY t.column_date, t.field1, t.field2;
If running this query from classic ASP, you will need to substitute the ANSI wild card character % for the Access-style * wild card.
t.field1 Like '%xyz'
Or consider using ALike and the ANSI wild card instead of Like. That way the query will always operate the same without needing to switch wild cards.
t.field1 ALike '%xyz'
Also, with classic ASP, run the query from an ADO Command object, and supply values for the parameters.