my problem is just to show two rows of Data with same ID.
My Table looks like this:
------------------------------
- FlatDestID - Trefferspalte -
- 444555666 - K -
- 444555666 - 1 -
- 444555666 - 1 -
- 111222333 - K -
- 111222333 - 1 -
- 111222333 - 1 -
------------------------------
And i want to have my Table like this
------------------------------
- FlatDestID - Trefferspalte -
- 444555666 - K -
- 444555666 - 1 -
- 111222333 - K -
- 111222333 - 1 -
------------------------------
Sometimes i have the same ID four to five times. And i just want to show the first two Data for each FlatDestId like in the second example. That means if you have 5 times the same FlatDestId then just show the FlatDestId with "Trefferspalte = K" and then a secon Data with the same FlatDestId and "Trefferspalte = 1"
I hope you can understand my Question and the problem i need Solution for.
Greatings from Germany
---------- EDIT ----------
I have for "Trefferspalte" the Values K and 1-6 and i want to see always K and then another FlatDestId with 1 or 2 or 3 or 4 or 5 or 6!
The Solution DISTINCT show's FlatDestId and Trefferspalte with all the Values that Exist for the FlatDestId and not K and just any Trefferspalte. I need to have two Values.
Access doesnt like much nested SQL queries, so first create a Query with this SQL :
SELECT K.FlatDestID, T.AnyTrefferspalte
FROM
(
SELECT DISTINCT FlatDestID
FROM <yourTable>
WHERE Trefferspalte = 'K'
) K
INNER JOIN
(
SELECT FlatDestID, MIN(Trefferspalte) AS AnyTrefferspalte
FROM <yourTable>
WHERE Trefferspalte <> 'K'
GROUP BY FlatDestID
) T
ON K.FlatDestID = T.FlatDestID
The goal is to know which are the FlatDestID that have both a 'K' Trefferspalte row and any other row. If there are multiple non-K Trefferspalte, here with MIN(Trefferspalte), I have chosen to keep their minimum value. You can change it to MAX() to keep the higher value, or to FIRST() to keep the first one encountered, which in fact means random.
Name your query like you want, I have chosen TempQuery
Then, this query should give you the expected results:
SELECT FlatDestID, 'K' AS Trefferspalte FROM TempQuery
UNION ALL
SELECT FlatDestID, AnyTrefferspalte AS Trefferspalte FROM TempQuery
ORDER BY FlatDestID
On a side note: your table structure is weird and needs a serious redesign that would avoid you a lot of headaches. A Primary Key would be a good start.
Make use of DISTINCT to remove duplicates, this will show your 2 rows. When you have more than 2 different values for Trefferspalte you will have to use some other method.
SELECT DISTINCT FlastDestID, Trefferspalte
FROM <yourTable>
You may want to make both columns your primary keys so that it'll thrown an error if you receive duplicate values (so you won't save it in the table). If that's the existing setup and you can't do anything about it, try DISTINCT. If it won't work, then try grouping it (you just need to add a new column in your query:
SELECT FlatDestID, Trefferspalte, COUNT(*) FROM YourTable GROUP BY FlatDestID, Trefferspalte
Related
I have a database that I'm searching through that is sometimes updated by another person. The way it is updated is terrible, but I can't change it. What happens is the updated numbers contain a "-1" or "-2". For example,
ID
1
2
3
4
Whenever one ID is updated, a new row is created like so:
ID
1
1-1
2
3
4
In this case, 1 was updated. Both 1 and 1-1 show up in the table. If it's updated again, it looks like this:
ID
1
1-1
1-2
2
3
4
It makes me furious but I can't do anything about it. I would like to select the rows in a query such that I get
ID
1-2
2
3
4
Does anybody have any suggestions?
I am assuming your IDs are strings since you can use - in them. You can create a saved query with your entire table and two additional columns:
OriginalID: IIf(InStr([ID],'-')=0,[ID],CInt(Left([ID],InStr([ID],'-')-1)))
and
Version: IIf(InStr([ID],'-')=0,0,CInt(Right([ID],Len([ID])-InStr([ID],'-'))))
This converts the number after the dash to an actual number (and zero for the original version).
Then use
SELECT [OriginalID] & IIF(Max([Version])=0,'','-' & Max([Version])) AS MaxID
FROM [MySavedQuery]
GROUP BY [OriginalID]
I have not had a chance to test this so there may be a parenthesis missing here or there or you may have to add a +1 or -1 to some lengths, but it should get you most of the way there.
First, split off the part of the ID without the dash, and set it to 0 if there is no dash:
SELECT ID,
CLng(IIF(ID Like "*-*", Right(ID, Len(ID) - InStr(1, ID, "-")), 0)) As LastPartID,
CLng(IIF(ID LIKE "*-*", Left(ID, InStr(1, ID, "-") - 1), ID)) As FirstPartID
From MyTable
If you save this as a separate query, the next query is simple:
SELECT FirstPartID & IIF(Max(LastPartID) = 0, "", "-" & Max(LastPartID))
FROM MyQuery
GROUP By FirstPartID
OK - I have a table with the following columns - what I need to do, is in instances where there is a same visit_no, I need to populate a int field with an ordinal number (priority) for each code - e.g. - so the "Priority" field is what I am looking to have populated - a new successive number for each "Code" value within the each similar "visit_no"
Visit_no Code Priority
123456 97110 1
445566 85025 1
445566 71402 2
445566 71020 3
789888 80053 1
789888 97110 2
111111 85025 1
Dense_Rank is a function which increments the rank only when the value your ordering on changes.
http://msdn.microsoft.com/en-ca/library/ms173825.aspx
Select Visit_no, Code, Priority = DENSE_RANK() OVER (Partition by Visit_No order by Code)
From YourTable
this is a sample sql query that i created ms access query. i am trying to get only one row the min(DATE). how ever when i run my query i get multiple lines. any hits? thanks
SELECT tblWarehouseItem.whiItemName,
tblWarehouseItem.whiQty,
tblWarehouseItem.whiPrice,
Min(tblWarehouseItem.whiDateIn) AS MinOfwhiDateIn,
tblWarehouseItem.whiExpiryDate,
tblWarehouseItem.whiwrhID
FROM tblWarehouseItem
GROUP BY tblWarehouseItem.whiDateIn,
tblWarehouseItem.whiItemName,
tblWarehouseItem.whiQty,
tblWarehouseItem.whiPrice,
tblWarehouseItem.whiExpiryDate,
tblWarehouseItem.whiwrhID;
If i have my sql code like that is working as it should:
SELECT MIN(tblWarehouseItem.whiDateIn) FROM tblWarehouseItem;
In the first query, you group by a number of columns. That means the minimum value will be calculated for each group, which in turn means you may have multiple rows. On the other hand, the second query will only get the minimum value for the specified column from all rows, so that there is only one row in the result set.
A simple example is shown below to illustrate the above.
Table:
Key Value
1 1
1 2
2 3
2 4
On Group By Key:
GroupKey MinValue
1 = min(1,2) = 1 -> Row 1
2 = min(3,4) = 3 -> Row 2
On Min (Value)
MinValue
=min(1,2,3,4) = 1 -> Row 1
For a table like above, if you want to select all rows and also show the minimum value from whole table rather than per group, you can do something like this:
select key, (select min(value) from table)
from table
SELECT WI.*
FROM tblWarehouseItem AS WI INNER JOIN (SELECT whiimtID, MIN(tblWarehouseItem.whiDateIn) AS whiDateIn
FROM tblWarehouseItem
GROUP BY whiimtID) AS MinWI ON (WI.whiDateIn = MinWI.whiDateIn) AND (WI.whiimtID = MinWI.whiimtID);
I have data in a table like following
Name indicator
A 1
A 2
A 3
B 1
B 2
C 3
I want to get count of Names, for which both indicator 1,2 exists. In the preeceding example, this number is 2 (A & B both have indicator as 1, and 2).
The data I am dealing with is moderately large, and i need to get the similar information of some other permutations of (pre defined ) indicators (which i can change, once i get base query).
Try this:
SELECT Name
FROM Tablename
WHERE indicator IN(1, 2)
GROUP BY Name
HAVING COUNT(DISTINCT indicator) = 2;
See it in action here:
SQL Fiddle Demo
I am working on a project that keeps a track of repaired cell phones.
In the select statement, I would like to find the duplicate IMEI numbers and check if the AddedDate between the duplicates is less than 30 days. Another words, the select should list all the phones even including the duplicated IMEI numbers if the AddedDate is more than 30 days.
I hope I described it clear enough. Thank you.
Additional notes:
I have tried it by including groupBy under a sub-select which did find the duplicates, but I wasn't able to implement an if condition. Instead, I was going to place all duplicates into a dynamic table and then use a select statement against this table. Before doing so, I thought of posting my question here.
For example DB_Phones has the following rows
ID - AddedDate - IMEI
1 - 01.10.2012 - 123456789012345
2 - 15.10.2012 - 987654321012345
3 - 20.10.2012 - 123456789012345
Based on the table above, I would like to list only the second row (ID# 2) because the last duplicate (ID# 3) wasn't added 30 days after the row with the ID# 1. If rows were as below:
ID - AddedDate - IMEI
1 - 01.10.2012 - 123456789012345
2 - 15.10.2012 - 987654321012345
3 - 20.10.2012 - 123456789012345
4 - 21.11.2012 - 123456789012345
Then the second and fourth row should be returned. I need to return just one of the duplicates (last one) if the 30 day condition is met.
I hope it make more sense now. Thanks again.
A guess at what you're after:
SELECT
r.*,
(SELECT COUNT(*) FROM Repairs r2 WHERE r.IMEI = r2.IMEI
AND r.ID != r2.ID) as NumberOfAllDuplicates,
(SELECT COUNT(*) FROM Repairs r2 WHERE r.IMEI = r2.IMEI
AND ABS(DATEDIFF(day, r.AddedDate, r2.AddedDate)) < 30
AND r.ID != r2.ID) as NumberOfNearDuplicates
FROM
Repairs r
This depends on having an ID field, and everything existing in one table. With the correlated sub queries, it may not be very fast on long data.