Select Distinct using JET and no PKEY - Duplicate rows - sql

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]

Related

Merge SQL Rows in Subquery

I am trying to work with two tables on BigQuery. From table1 I want to find the accession ID of all records that are "World", and then from each of those accession numbers I want to create a column with every name in a separate row. Unfortunately, when I run this:
Select name
From `table2`
Where acc IN (Select acc
From `table1`
WHERE source = 'World')
Instead of getting something like this:
Acc1
Acc2
Acc3
Jeff
Jeff
Ted
Chris
Ted
Blake
Rob
Jack
Jack
I get something more like this:
row
name
1
Jeff
2
Chris
3
Rob
4
Jack
5
Jeff
6
Jack
7
Ted
8
Blake
Ultimately, I am hoping to download the data and somehow use python or something to take each name and count the number of times it shows up with each other name at a given accession number, and furthermore measure the degree to which each pairing is also found with third names in any given column, i.e. the degree to which they share a cohort. So I need to preserve the groupings which exist with each accession number, but I am struggling to find info on how one might do this.
Could anybody point me in the right direct for this, or otherwise is the way I am going about this wise if that is my end goal?
Thanks!
This is not a direct answer to the question you asked. In general, it is easier to handle multiple rows rather than multiple columns.
So, I would recommend that you put each acc value in a separate row and then list the names as an array:
select t2.acc, array_agg(t2.name order by t2.name) as names
from `table2` t2
where t2.acc in (Select t1.acc
From `table1` t1
where t1.source = 'World'
)
group by t2.acc;
Otherwise, you are going to have a challenge just naming the columns in your result set.

How to select a record from SQL that does not have a certain value

I have a list of students who have several levels of English to complete. Is there a way to find those students who are doing English I and haven't moved to the next level
ID STUDENTID NAME ENGLISHLEVEL STARTDATE ENDDATE
----------------------------------------------------------------
1 001 Eric English-1 2017-01-01 2018-01-01
2 002 Brian English-1 2017-01-01 2017-01-31
3 002 Brian English-2 2017-02-01 2017-03-01
4 003 David English-1 2017-05-01 2017-06-01
5 003 David English-2 2017-06-02 2017-07-03
I have a list similar to above for thousands of students and want to know how I can query the table to show me those students who did English-1 but never got started with English-2 or English-3
Advice would be appreciated.
You can join the table to itself with an outer join. Have the outer joined table check for English2/3, and then filter out any results where there is a match.
select
eng1.*
from
students_table eng1
left outer join students_table eng2
on (
eng1.STUDENTID=eng2.STUDENTID
and eng2.ENGLISHLEVEL in ('English-2', 'English-3')
)
where
eng2.STUDENTID is null -- Filter out rows where an eng2 row was found
You can use not exists :
select t.*
from table t
where not exists (select 1
from table t1
where t1.STUDENTID = t.STUDENTID and
t1.ENGLISHLEVEL in ('English-2', 'English-3')
);
Your English levels can be sorted alphabetically:
select studentid, name
from t
group by studentid, name
having max(englishlevel) = 'English-1'
You can just use IN in your WHERE statement to get those students who did English-1
SELECT * FROM tblStudents
WHERE ENGLISHLEVEL IN ('English-1')
What does "students who are doing English I" mean in data terms in the table in your post?
What does "and havent moved to the next level" mean in data terms in the table?
Do you have a table that contains all possible values of ENGLISHLEVEL?
How are the STARTDATE and ENDDATE columns populated?
If the ENDDATE was only populated once the person has actually completed the training, you could use the NULL value there to look for people that have not completed a training. That might be the way to go here.
If you have a table that defines all possible values of ENGLISHLEVEL, then you can compare the count of distinct ENGLISHLEVEL values with the count of values in the ENGLISHLEVEL table, and that will tell you which students have completed all the courses. You can also do this comparison for each training that way.

Sql query to add comparison columns dynamically

My table looks like
Fields Jack Mike Bruce ... Tony
Salary 150 300 125 ... 150
CTC 100 100 250 ... 500
Here Jack is the base user and I need to compare the salary and CTC of Mike , Bruce ,Tony upto n columnof users in the table and add comparison rating columns such as the output looks like,
Fields Jack Mike Mike_rating Bruce Bruce_rating ... Tony
Salary 150 300 high 125 low ... 150
CTC 100 100 equal 250 high ... 500
Output Explanation
The user list grows dynamically and corresponding rating column needed to be added.
Jack Salary is 150 and Mike is 300 . So Mike_Rating column should have value as high else low else if two values are equal then equal
Any Help would be appreciated . Thank you
Create a trigger on table user. Trigger is used exactly for things like this. So, for example, if u create a trigger on table Users to be activated every time after insert, then u can get the inserted data, and using it you can make changes to your table. You would also probably want to create triggers for delete and update operations on User table. You can find details on how to create a trigger here

SQL Server 2008 join and count

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

Rows to Dynamic columns in Access

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