I am trying to do a query to find people living at the same address. But my SQL appears to fail and not sure why.
mysql> SELECT * FROM polished1 WHERE LName = 'BENDICH'; +------+--------------+-------+--------+-------+---------+------------+-----------+--------+----------+-----------+-------------------+-----------+-----------+--------------------+-------------+------------+---------+----------+------------+------------+--------------+---------------------+-----------------------+-------+-------+-------+-------+----------+---------+-------------+------------------+--------------+-----------+------------+
| id | StateVoterID | Title | FName | MName | LName | NameSuffix | Birthdate | Gender | RegStNum | RegStFrac | RegStPreDirection | RegStName | RegStType | RegStPostDirection | RegUnitType | RegUnitNum | RegCity | RegState | RegZipCode | CountyCode | PrecinctPart | LegislativeDistrict | CongressionalDistrict | Mail1 | Mail2 | Mail3 | Mail4 | MailCity | MailZip | MailCountry | Registrationdate | AbsenteeType | LastVoted | StatusCode |
+------+--------------+-------+--------+-------+---------+------------+-----------+--------+----------+-----------+-------------------+-----------+-----------+--------------------+-------------+------------+---------+----------+------------+------------+--------------+---------------------+-----------------------+-------+-------+-------+-------+----------+---------+-------------+------------------+--------------+-----------+------------+
| 2790 | 72253 | | ARNOLD | J | BENDICH | | 5 | M | 1754 | | NE | 2ND | ST | | | 0 | SEATTLE | WA | 98115 | 2071 | 43 | 7 | | | | | | | | 10/1/1965 | P | 2/12/2013 | A | NULL |
| 2791 | 72253 | | JUDITH | E | BENDICH | | 1 | F | 1754 | | NE | 2ND | ST | | | 0 | SEATTLE | WA | 98115 | 2071 | 43 | 7 | | | | | | | | 10/1/1965 | P | 2/12/2013 | A | NULL |
+------+--------------+-------+--------+-------+---------+------------+-----------+--------+----------+-----------+-------------------+-----------+-----------+--------------------+-------------+------------+---------+----------+------------+------------+--------------+---------------------+-----------------------+-------+-------+-------+-------+----------+---------+-------------+------------------+--------------+-----------+------------+
2 rows in set (0.00 sec)
mysql> SELECT *, COUNT(*) c FROM polished1 WHERE LName = 'Bendich' GROUP BY RegStNum, RegStName, RegStType, RegUnitType, RegStPreDirection, RegStPostDirection, RegUnitNum, RegCity, RegState, RegZipCode HAVING c > 1;
+------+--------------+-------+--------+-------+---------+------------+-----------+--------+----------+-----------+-------------------+-----------+-----------+--------------------+-------------+------------+---------+----------+------------+------------+--------------+---------------------+-----------------------+-------+-------+-------+-------+----------+---------+-------------+------------------+--------------+-----------+------------+---+
| id | StateVoterID | Title | FName | MName | LName | NameSuffix | Birthdate | Gender | RegStNum | RegStFrac | RegStPreDirection | RegStName | RegStType | RegStPostDirection | RegUnitType | RegUnitNum | RegCity | RegState | RegZipCode | CountyCode | PrecinctPart | LegislativeDistrict | CongressionalDistrict | Mail1 | Mail2 | Mail3 | Mail4 | MailCity | MailZip | MailCountry | Registrationdate | AbsenteeType | LastVoted | StatusCode | c |
+------+--------------+-------+--------+-------+---------+------------+-----------+--------+----------+-----------+-------------------+-----------+-----------+--------------------+-------------+------------+---------+----------+------------+------------+--------------+---------------------+-----------------------+-------+-------+-------+-------+----------+---------+-------------+------------------+--------------+-----------+------------+---+
| 2790 | 72253 | | ARNOLD | J | BENDICH | | 5 | M | 1754 | | NE | 2ND | ST | | | 0 | SEATTLE | WA | 98115 | 2071 | 43 | 7 | | | | | | | | 10/1/1965 | P | 2/12/2013 | A | NULL | 2 |
+------+--------------+-------+--------+-------+---------+------------+-----------+--------+----------+-----------+-------------------+-----------+-----------+--------------------+-------------+------------+---------+----------+------------+------------+--------------+---------------------+-----------------------+-------+-------+-------+-------+----------+---------+-------------+------------------+--------------+-----------+------------+---+
1 row in set (0.01 sec)
http://bpaste.net/show/UWC8wdOWMbFEQcT7eV6f/
select p.*
from polished1 p JOIN
(
SELECT *, COUNT(*) c
FROM polished1
WHERE LName = 'Bendich'
GROUP BY RegStNum, RegStName, RegStType, RegUnitType, RegStPreDirection, RegStPostDirection, RegUnitNum, RegCity, RegState, RegZipCode
HAVING c > 1) filtered ON p.RegStNum=filtered.RegStNum
and p.RegStName=filtered.RegStName
and p.RegStType=filtered.RegStType
and p.RegUnitType=filtered.RegUnitType
and p.RegStPreDirection=filtered.RegStPreDirection
and p.RegStPostDirection=filtered.RegStPostDirection
and p.RegUnitNum=filtered.RegUnitNum
and p.RegCity=filtered.RegCity
and p.RegState=filtered.RegState
and p.RegZipCode=filtered.RegZipCode
You should retrieve the addresses where count(*)>1 first and then get all joined addresses
Related
Is there anyway I could display all the duplicate values in mysql using group by and having
+---------+--------------+------------+--------+----------+----------+---------+
| ENumber | EmpName | Birthdate | Gender | Address | Salary | DNumber |
+---------+--------------+------------+--------+----------+----------+---------+
| E001 | GSInocencio | 1988-01-15 | F | Munoz | 18000.00 | D005 |
| E002 | EAVillanueva | 1988-04-20 | F | Munoz | 23000.00 | D003 |
| E003 | ALedesma | 1988-05-25 | M | CLSU | 21000.00 | D002 |
| E004 | APGamilla | 1991-10-15 | F | Maligaya | 25000.00 | D001 |
| E005 | ACTolentino | 1989-02-20 | F | Maligaya | 30000.00 | D002 |
| E006 | ANVillasoto | 1999-01-05 | M | CLSU | 15000.00 | D004 |
| E007 | JPPalada | 1997-01-10 | M | Munoz | 21000.00 | D001 |
| E008 | NTNicodemus | 1995-04-15 | F | Maligaya | 22000.00 | D003 |
+---------+--------------+------------+--------+----------+----------+---------+
I want to display all the duplicate value in DNumber
+---------+--------------+------------+--------+----------+----------+---------+
| ENumber | EmpName | Birthdate | Gender | Address | Salary | DNumber |
+---------+--------------+------------+--------+----------+----------+---------+
| E004 | APGamilla | 1991-10-15 | F | Maligaya | 25000.00 | D001 |
| E007 | JPPalada | 1997-01-10 | M | Munoz | 21000.00 | D001 |
| E003 | ALedesma | 1988-05-25 | M | CLSU | 21000.00 | D002 |
| E005 | ACTolentino | 1989-02-20 | F | Maligaya | 30000.00 | D002 |
| E002 | EAVillanueva | 1988-04-20 | F | Munoz | 23000.00 | D003 |
| E008 | NTNicodemus | 1995-04-15 | F | Maligaya | 22000.00 | D003 |
display all the duplicate values in mysql using group by and having
There is more than one way to do it, but if you want to use group by and having, then you can join the table with an aggregate subquery that identifies the duplicates, as follows:
select t.*
from mytable t
inner join (
select dnumber
from mytable
group by dnumber
having count(*) > 1
) x on x.dnumber = t.dnumber
order by t.dnumber, t.enumber
I have this file:
1.nothing
2.o,s,f,d
3.f,d
4.o,s
5.s,f,d
6.s
7.nothing
8.s,f,d
9.o,d
10.s,f
And a table:
describe delete_me;
+------------+----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+----------------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| privileges | set('o','s','f','d') | YES | | NULL | |
+------------+----------------------+------+-----+---------+-------+
When I try to:
LOAD DATA INFILE 'privileges.txt' INTO TABLE delete_me FIELDS TERMINATED BY '.';
I get this:
+------+------------+
| id | privileges |
+------+------------+
| 0 | |
| 2 | f,s,o |
| 3 | f |
| 4 | o |
| 5 | f,s |
| 6 | |
| 7 | |
| 8 | f,s |
| 9 | o |
| 10 | s |
| 11 | o |
| 12 | |
| 13 | o |
| 14 | o |
| 15 | s |
| 16 | |
| 17 | o |
| 18 | o |
| 19 | s,o |
| 20 | f |
+------+------------+
The letter d just disappears. Why?
I have a query in which I want to filter and merge data together. As is the SQL for the query looks as such:
SELECT UCase$(DLookUp("Abbreviation", "tblRankStructure", "tblRankStructure!ID = " & [tblRoster].[Rank])) AS Rank
,UCase$([tblRoster].[LastName] & ", " & [tblRoster].[FirstName] & " " & IIf(IsNull([tblRoster].[MiddleName]), "", Left$([tblRoster].[MiddleName], 1))) AS [Full Name]
,tblRoster.EDIPI
,tblBasicIndividualRecords.PriMilOccSpec AS MOS
,tblBasicIndividualRecords.Gender
,tblBasicIndividualRecords.BloodType
,tblUnitInformationCodes.PltCode
,IIf(IsNull([tblBasicIndividualRecords] ! [MealCardNumber]), "COMRATS", [tblBasicIndividualRecords] ! [MealCardNumber]) AS [Meal Card]
,tblBasicIndividualRecords.SecurityClearance AS Clearance
,tblBasicWeaponRecords.WeaponType
,tblBasicWeaponRecords.WeaponSerial
,tblBasicWeaponRecords.OpticSerial
FROM (
(
(
tblRankStructure INNER JOIN tblRoster ON tblRankStructure.ID = tblRoster.Rank
) INNER JOIN tblBasicIndividualRecords ON tblRoster.EDIPI = tblBasicIndividualRecords.EDIPI
) LEFT JOIN tblBasicWeaponRecords ON tblRoster.EDIPI = tblBasicWeaponRecords.EDIPI
)
INNER JOIN tblUnitInformationCodes ON tblRoster.EDIPI = tblUnitInformationCodes.EDIPI;
With the query as such it will return as rows for the same person if they have multiple weapons. How can I merge to return same data but only one row from the query and still display all required data?
Current Output:
What I would like is to have the row with the M9A1 serial under the pistol serial column and only have one row for SSgt Pilewski
This sample is created by added another field to the tblBasicWeaponRecords for the PistolSerial. I do not want this field there and would like to make my query work with out having this field in the tblBasicWeaponRecords.
All data is pulled from the following tables:
tblRoster
+-------+------------------+----------+-----------+------------+
| EDIPI | Rank | LastName | FirstName | MiddleName |
+-------+------------------+----------+-----------+------------+
| | First Lieutenant | Mozzetta | Jennifer | L |
| | Staff Sergeant | Pilewski | Troy | Andrew |
| | Sergeant | Irizarry | Jonathon | |
+-------+------------------+----------+-----------+------------+
tblBasicIndividualRecords
+----+-------+-------------+------------+-------------------------------+--------------------+---------------+---------------+--------+------------+--------+---------+--------+-----------+-------------------+----------------+-----------------+
| ID | EDIPI | DateOfBirth | DateOfRank | ArmedForcesActiveDutyBaseDate | EndOfActiveService | PriMilOccSpec | AddMilOccSpec | Billet | Deployable | OnHand | Remarks | Gender | BloodType | SecurityClearance | MealCardNumber | MartialArtsBelt |
+----+-------+-------------+------------+-------------------------------+--------------------+---------------+---------------+--------+------------+--------+---------+--------+-----------+-------------------+----------------+-----------------+
| 1 | | | | | 12-Aug-2015 | 0602 | | | TRUE | TRUE | | | | | | |
| 2 | | | | | 23-Aug-2014 | 0659 | | | TRUE | TRUE | | | | | | |
| 3 | | | | | 30-Aug-2013 | 0651 | | | TRUE | TRUE | | | | | | |
+----+-------+-------------+------------+-------------------------------+--------------------+---------------+---------------+--------+------------+--------+---------+--------+-----------+-------------------+----------------+-----------------+
tblBasicWeaponRecords
+----+-------+------------+--------------+-------------+
| ID | EDIPI | WeaponType | WeaponSerial | OpticSerial |
+----+-------+------------+--------------+-------------+
| 1 | | M4 | W151644 | 224604 |
| 3 | | M4 | W142663 | 228541 |
| 4 | | M16A4 | 164522845 | 225487 |
| 6 | | M9A1 | 1476541 | |
+----+-------+------------+--------------+-------------+
tblUnitInformationCodes
+----+-------+---------+
| ID | EDIPI | PltCode |
+----+-------+---------+
| 1 | | CYB |
| 2 | | CYB |
| 3 | | CYB |
+----+-------+---------+
How can I have for each grp_id 2 rows:
before and after row of current unix timestamp,
+---------+--------------------+------------+-------+
| id | grp_id | utimes | value |
+---------+--------------------+------------+-------+
| 4156187 | 5282 | 1455663600 | 15897 |
| 4159888 | 5282 | 1455630000 | 26998 |*
| 4156190 | 5282 | 1455676200 | 28497 |
| 4156186 | 5282 | 1455661800 | 14097 |
| 4156183 | 5282 | 1455652800 | 5097 |
| 4156184 | 5282 | 1455656400 | 8697 |
| 4156185 | 5282 | 1455660000 | 12297 |
| 4156182 | 5282 | 1455651000 | 3297 |*
| 4163311 | 7216 | 1455693000 | 45297 |
| 4163275 | 7203 | 1455681600 | 33897 |
| 4163309 | 7214 | 1455697800 | 50097 |
| 4163308 | 7214 | 1455696000 | 48297 |
| 4163307 | 7214 | 1455694200 | 46497 |
| 4163306 | 7214 | 1455692400 | 44697 |
| 4163305 | 7214 | 1455690600 | 42897 |
| 4163304 | 7214 | 1455688800 | 41097 |
| 4151121 | 4356 | 1455703200 | 55497 |
| 4163271 | 7205 | 1455685500 | 37797 |
| 4163272 | 7205 | 1455687000 | 39297 |
| 4163269 | 7205 | 1455684900 | 37197 |
| 4163273 | 7205 | 1455687300 | 39597 |
| 4163264 | 7206 | 1455674400 | 26697 |
| 4163270 | 7205 | 1455685200 | 37497 |
+---------+--------------------+------------+-------+
Example:
unix-timestamp : 1455647703
+---------+--------------------+------------+-------+
| id | grp_id | utimes | value |
+---------+--------------------+------------+-------+
| 4159888 | 5282 | 1455630000 | 26998 |
| 4156190 | 5282 | 1455651000 | 28497 |
| 4159889 | XYZ | 1455630000 | 26998 |
| 4156191 | XYZ | 1455651000 | 28497 |
| 4159883 | ABC | 1455630000 | 26998 |
| 4156195 | ABC | 1455651000 | 28497 |
+---------+--------------------+------------+-------+
Thank you !
You can do it with a LEFT JOIN operation. The ON clause contains the 'business logic':
SELECT t1.*
FROM mytable AS t1
LEFT JOIN mytable AS t2
ON t1.grp_id = t2.grp_id
AND
((t1.utimes < 1455647703 AND t2.utimes < 1455647703 AND t2.utimes > t1.utimes)
OR
(t1.utimes > 1455647703 AND t2.utimes > 1455647703 AND t2.utimes < t1.utimes))
WHERE t1.grp_id = 5282 AND t2.id IS NULL
Demo here
T1 table
+-----------+------------------+
| cookie_id | impression |
+-----------+------------------+
| 123 | 6/17/15 |
| 123 | 6/18/15 |
| 123 | 6/18/15 |
| 234 | 6/20/15 |
| 234 | 6/22/15 |
+-----------+------------------+
T2 table
+-----------+---------+---------+---------+
| cookie_id | HP | search | book |
+-----------+---------+---------+---------+
| 123 | 6/17/15 | | |
| 123 | 6/18/15 | | |
| 123 | | 6/18/15 | |
| 123 | | | 6/19/15 |
| 234 | 6/23/15 | | |
| 234 | | 6/25/15 | |
| 234 | | | 6/29/15 |
+-----------+---------+---------+---------+
we'd like to join T1 and T2 as the expected result as below T3 table :
+-----------+------------+---------+---------+---------+
| cookie_id | impression | HP | search | book |
+-----------+------------+---------+---------+---------+
| 123 | 6/17/15 | | | |
| 123 | 6/18/15 | | | |
| 123 | 6/18/15 | | | |
| 123 | | 6/17/15 | | |
| 123 | | 6/18/15 | | |
| 123 | | | 6/18/15 | |
| 123 | | | | 6/19/15 |
| 234 | 6/20/15 | | | |
| 234 | 6/22/15 | | | |
| 234 | | 6/23/15 | | |
| 234 | | | 6/25/15 | |
| 234 | | | | 6/29/15 |
+-----------+------------+---------+---------+---------+
http://sqlfiddle.com/#!9/375e6/2
SELECT cookie_id, impression, null,null, null
FROM t1
UNION ALL
SELECT cookie_id, null as impression, hp, search,book
FROM t2
As mentioned in the comment, you seems to want to UNION the tables: This will do it, but it may not be what you really really need...
SELECT * FROM (
(SELECT cookie_id,
"" AS impression,
HP,
search,
book
FROM T1)
UNION
(SELECT cookie_id,
impression,
"" AS HP,
"" AS search,
"" AS book
FROM T2)
) a
ORDER BY cookie_id;
This is out of my head, maybe I made a typo or something.