I am relatively new to SQL and I am having a really hard time getting this query figured out. I need to show which shipments (shipment_no) were delivered by multiple trucks drivers.
Here are the only two columns in the table (named Package) that I believe I need as well as the entire other table (truck) I am joining it with. As you can see, shipment_no 1775 is the only one that has been delivered by more than one truck/driver.
Package table = Shipment_No - 1770,1771,1772,1773,1774,1774,1774,1775,1775,1775,1776,1777
and Truck_no = 100,103,105,102,108,108,108,101,109,109,100,100 (Respectively)
Truck table = Truck_NO 100,101,102,103,104,105,106,107,108,109
and drivername = JONES,DAVIS,GOMEZ,THOMPSON,HERSHEY,FERRIS,SHAVER,LEE,TOPI,ACKERMAN (Respectively)
This is what I've got so far
select shipment_no, drivername
from package, truck
where package.truck_no=truck.truck_no
group by shipment_no, drivername
My results look like this
- Shipment_no =
1770
1771
1772
1773
1774
1775
1775
1776
1777
- Drivername =
JONES
THOMPSON
FERRIS
GOMEZ
TOPI
ACKERMAN
DAVIS
JONES
JONES
All I need to display is the shipping number in the end so it would look like this.
-Shipment_no
-1775
I've been trying for hours and any help is appreciated.
Thanks a lot!
Select shipment_no
From Package
Group BY shipment_no
Having Count(Distinct Truck_No) > 1
Try this:
SELECT Shipment_no
FROM package
GROUP BY Shipment_no
HAVING COUNT(DISTINCT Truck_no) > 1
Related
This question already has answers here:
Get top 1 row of each group
(19 answers)
Closed 10 months ago.
Let's say I had two tables that looked like this:
Prod_SerialNo
Prod_TestOnAt
Prod_AccountNo
SN0001
2021-04-08
045678
SN0001
2021-01-14
067891
SN0001
2021-11-29
091234
SN0002
2022-01-19
045678
SN0002
2020-07-30
045678
SN0002
2022-03-30
012345
SN0003
2022-04-01
078912
SN0003
2022-02-19
089123
SN0003
2022-03-18
023456
S_AccountNo
S_AccountType
S_AccountName
012345
Homeowner
Adam Smith
023456
Homeowner
Jeremy Chan
034567
Manufacturer
Anne Hudson
045678
Distributor
Barney Jones
056789
Distributor
Jasmine Coleman
067891
Homeowner
Christian Lewis
078912
Distributor
Heather Ogden
089123
Homeowner
Stephen Gray
091234
Distributor
Antony Newman
The Prod Table tabulates specific product tests by what serial number was used, when the product was tested, and who tested it. (There are other things in the table, including a primary key not shown here)
The S Table is a list of subscribers with a variety of information about them. S_AccountNo is the parent to Prod_AccountNo.
I want to query when the last test was performed for each Serial Number and what account name it was that performed the test, but I don't want multiple results (duplicates) for the same serial number. I have tried the following code:
SELECT
Prod_SerialNo,
MAX(Prod_TestOnAt) AS "Last Time Tested",
S_AccountName
FROM Prod
INNER JOIN S ON S.S_AccountNo = Prod.Prod_AccountNo
GROUP BY Prod_SerialNo, S_AccountName
ORDER BY Prod_SerialNo
However, the query ends up outputting the same serial number on multiple rows even though I ask for the max TestOnAt date and I group by serial number. What am I getting wrong?
I think there is no need to use Group by you can get result with Row_Number like this:
SELECT
t.Prod_SerialNo,
t.Prod_TestOnAt AS "Last Time Tested",
t.S_AccountName
FROM (
SELECT
Prod_SerialNo,
Prod_TestOnAt,
S_AccountName,
ROW_NUMBER() OVER (PARTITION BY Prod_SerialNo ORDER BY Prod_TestOnAt DESC) rw
FROM Prod
INNER JOIN S ON S.S_AccountNo = Prod.Prod_AccountNo
) t
WHERE t.rw=1
ORDER BY t.Prod_SerialNo
You are grouping by Prod_SerialNo, S_AccountName so you will get duplicate Prod_SerialNo if multiple rows exist with that Prod_SerialNo and different S_AccountNames. You could do a MAX on Prod_TestOnAt and get that value with it's Prod_SerialNo, then join the result on the table to get your desired info using a subquery like so:
SELECT
p.[Prod_SerialNo],
max.[LastTimeTested],
s.[S_AccountName]
FROM PROD as p
INNER JOIN
(
SELECT
Prod_SerialNo,
MAX(Prod_TestOnAt) as [LastTimeTested]
FROM Prod
GROUP BY [Prod_SerialNo]
) as max
on max.[Prod_SerialNo] = p.[Prod_SerialNo] and max.[LastTimeTested] = p.[Prod_TestOnAt]
INNER JOIN S as s
ON s.[S_AccountNo] = p.[Prod_AccountNo]
ORDER BY p.[Prod_SerialNo]
If you don't like the solution using ROW_NUMBER an alternative is to use CROSS APPLY to identify the last Prod_TestOnAt and the associated Prod_AccountNo.
SELECT DISTINCT p.Prod_SerialNo, ca.Prod_TestOnAt, s.S_AccountName
FROM Prod p
CROSS APPLY (SELECT TOP 1 Prod_TestOnAt, Prod_AccountNo
FROM Prod
WHERE Prod_SerialNo = p.Prod_SerialNo
ORDER BY Prod_TestOnAt DESC) ca
INNER JOIN S ON S.S_AccountNo = ca.Prod_AccountNo
I want to get the subscriber that has maximum value of Bill (Total Bill).
I tried using the following script but SQL did not execute successflly.
Please help me on what I did wrong on this.
I have 2 tables:
Subscriber
FirstName
MIN
Ben
258999542
Reed
458524896
Steve
586692155
Clint
1007772121
Frank
1287548752
Jane
2345824215
Total Bill
Total
MIN
131.5
258999542
139.4
458524896
164
586692155
101
1007772121
224.12
1287548752
97.52
2345824215
And here's the code I tried:
SELECT MAX(B.Total), S.FirstName
FROM Subscriber AS S
JOIN Bill AS B ON S.MIN = B.MIN
It seems you just need TOP + ORDER BY:
SELECT TOP 1 B.Total, S.FirstName
FROM Subscriber AS S
JOIN Bill AS B ON S.MIN = B.MIN
ORDER BY B.Total DESC;
That's based on the fact that your sample data isn't showing multiple Bill records per Subscriber therefore you don't need a sum.
There seems to be a lot of threads on this topic but few that work with Excel.
I have a simple table from which i want to select:
ideally all columns i.e. using * if possible so if a user adds new columns they do not need to edit SQL. (is this a pipe dream?) if so a solution specifying all the returned columns is OK.
only return rows where [name]&[date] (concatenated) is distinct
all other columns i don't care about which row is returned. first, last, limit 1... anything. they are a mix of all types.
this must not create a new table or delete rows, just selecting and joining
name date sales
andy 01/01/2010 100
andy 01/01/2010 900
andy 05/01/2010 100
alex 02/02/2010 200
alex 02/02/2010 200
alex 05/01/2010 200
dave 09/09/2010 300
dave 09/09/2010 300
dave 01/09/2010 300
Also code simplicity is prefered over speed. This is going to be left to run over night so nice looking but slow is fine... and excel doesn't have millions of rows!
Many thanks to everyone in advance.
UPDATE
I would expect the table to look like this:
name date sales
andy 01/01/2010 100
andy 05/01/2010 100
alex 02/02/2010 200
alex 05/01/2010 200
dave 09/09/2010 300
dave 01/09/2010 300
or
andy 01/01/2010 900
andy 05/01/2010 100
alex 02/....
I can select all the 'unique things with this:
SELECT MAX(joined)
FROM
(SELECT [Single$].[date] AS [date],
[Single$].[name] AS [name],
name & date AS [joined]
FROM [Single$]
)
GROUP BY joined
HAVING MAX(joined) IS NOT NULL
But i don't know how to somehow join this back to the original table keeping any single row where the join matches. And i don't know if a join is the right way about this? Thanks
Simply run an aggregate query grouped by [Name] and [Date]. For all other columns run an aggregate like MAX() or MIN() which should work on numeric and string values.
SELECT [Single$].[name] AS [name], [Single$].[date] AS [date],
MAX([Single$].[sales]) As [sales],
MAX(...)
FROM [Single$]
GROUP BY [Single$].[name] AS [name], [Single$].[date] AS [date]
I work on a help-desk and am doing some analysis of PC repair tickets.
I am needing to dump data from our call log system that returns history of tickets for issues on computers where they were recently repaired by another team. We are simply trying to improve QA on deployed machines and this data will help.
I have the query for the analysis of tickets, but I am wanting to return the ticket number of the last PC repair case.
My current query is as follows:
SELECT
CallLog.CallID,
CallLog.CustID,
Subset.Rep_num,
Subset.FirstName,
Subset.LastName,
CallLog.OpndetailCat,
CallLog.Tracker_Full,
CallLog.RecvdDate,
FROM
heatPrd.dbo.CallLog CallLog,
heatPrd.dbo.Subset Subset
WHERE
CallLog.CallID = Subset.CallID AND
CallLog.RecvdDate>='2015-10-01' AND
CallLog.OpnAreaCat='back from repair'
ORDER BY
CallLog.CallID DESC
This returns
CallID CustID Rep_num FirstName LastName OpndetailCat Tracker_Full
2182375 1234 Sarah Doe Missing Email Folde
2181831 1235 JENNIFER Doe ZOTHER
2180815 1236 123 Jason Smith ZOTHER
2180790 1237 124 DARCY Doe Wrong Proxy Config
2180787 1239 125 Jason Smith ZOTHER
I want to add a column to the query that would return something to the effect of
select max(callid)
from calllog
where calltype = 'in_for_service_pc' and custid = '1234'
where calltype = 'in_for_service_pc' resides on the CallLog table and custID would pull from the query result.
This is a lot of info so i hope my request is clear.
Disclaimer: Data resides in SQL Server 2000 so some of the newer commands may not work.
Something like this should be pretty close.
SELECT
cl.CallID,
cl.CustID,
s.Rep_num,
s.FirstName,
s.LastName,
cl.OpndetailCat,
cl.Tracker_Full,
cl.RecvdDate,
x.MaxCallID
FROM heatPrd.dbo.CallLog cl
JOIN heatPrd.dbo.Subset s ON cl.CallID = s.CallID
left join
(
select max(cl2.callid) as MaxCallID
, cl2.custid
from calllog cl2
where cl2.calltype = 'in_for_service_pc'
group by cl2.custid
) x on x.custid = cl.custid
WHERE cl.RecvdDate >= '2015-10-01' AND
cl.OpnAreaCat = 'back from repair'
ORDER BY cl.CallID DESC
I need a setup in Access where some rows in a table are converted to columns...for example, lets say I have this table:
Team Employee DaysWorked
Sales John 23
Sales Mark 3
Sales James 5
And then through the use of a query/form/something else, I would like the following display:
Team John Mark James
Sales 23 3 5
This conversion of rows to columns would have to be dynamic as a team could have any number of Employees and the Employees could change etc. Could anyone please guide me on the best way to achieve this?
You want to create a CrossTab query. Here's the SQL that you can use.
TRANSFORM SUM(YourTable.DaysWorked) AS DaysWorked
SELECT YourTable.Team
FROM YourTable
GROUP BY YourTable.Team
PIVOT YourTable.Employee
Of course the output is slightly different in that the columns are in alphabetical order.
Team James John Mark
Sales 5 23 3
For more detail see Make summary data easier to read by using a crosstab query at office.microsoft.com