How can I count how many rows have same data in 2 columns on an access report?
This report is always changing based on selections that are made by the user in a combo box and a list box. I want to do the coding in the report. is it possible?
I'm assuming you are looking for rows that have duplicate data across columns 1 and 2. So if in the first row column1 = a and column2 = b, and in another row column1 = a and column2 = b, then we have a duplicate row regardless of other columns. We have found 1 duplicate row.
Access has a find duplicates wizard under the create- query wizard tabs. Playing around with the wizard I got a count of rows with the same data. we subtract 1 to get the number of duplicate rows. Then I had to go to the sql pane of the designer to quickly wrap that query in a sum to get SumDuplicates. The resulting sql which give the total number of duplicate rows is:
SELECT Sum(Duplicates) as SumDuplicates
FROM
(
SELECT Count([column1])-1 AS Duplicates
FROM Table1
GROUP BY Table1.column1, Table1.column2
HAVING (((Count(Table1.column1))>1) AND ((Count(Table1.column2))>1))
);
Adust column and table names to fit your database structure
Related
I am using MySQL through R. I am working with two tables within the same database and I noticed something strange that I can't explain. To be more specific, when I try to make a connection between the tables using a foreign key the result is not what it should be.
One table is called Genotype_microsatellites, the second table is called Records_morpho. They are connected through the foreign key sample_id.
If I only select records with certain characteristics from the Genotype_microsatellites table using the following command...
Gen_msat <- dbGetQuery(mydb, 'SELECT *
FROM Genotype_microsatellites
WHERE CIDK113a >= 0')
...the query returns 546 observations for 52 variables, exactly what I would expect. Now, I want to do a query that adds a little more info to my results, specifically by including data from the Records_morpho table. I, therefore, use the following code:
Gen_msat <- dbGetQuery(mydb, 'SELECT Genotype_microsatellites.*,
Records_morpho.net_mass_g,
Records_morpho.svl_mm
FROM Genotype_microsatellites
INNER JOIN Records_morpho ON Genotype_microsatellites.sample_id = Records_morpho.sample_id
WHERE CIDK113a >= 0')
The problem is that now the output has 890 observation and 54 variables!! Some sample_id values (i.e., the rows or individuals in the data frame ) are showing up multiple times, which shouldn't be the case. I have tried to fix this using SLECT DISTINCT, but the problem wouldn't go away.
Any help would be much appreciated.
Sounds like it is working as intended, that is how joins work. With A JOIN B ON A.x = B.y you get every row from A combined with every row from B that has a y matching the A row's x. If there are 3 rows in B that match one row in A, you will get three result rows for those. The A row's data will be repeated for each B row match.
To go a little further, if x is not unique and y is not unique. And you have two x with the same value, and three y with that value, they will produce six result rows.
As you mentioned DISTINCT does not make this problem go away because DISTINCT operates across the result row. It will only merge result rows if the values in all selected fields are the same on those result rows. Similarly, if you have a query on a single table that has duplicate rows, DISTINCT will merge those rows despite them being separate rows, as they do not have distinct sets of values.
I have two tables called tc_revenue and tc_rates.
tc_revenue contains :- code, revenue, startDate, endDate
tc_rate contains :- code, tier, payout, startDate, endDate
Now I need to get records where code = 100 and records should be unique..
I have used this query
SELECT *
FROM task_code_rates
LEFT JOIN task_code_revenue ON task_code_revenue.code = task_code_rates.code
WHERE task_code_rates.code = 105;
But I am getting repeated records help me to find the correct solution.
eg:
in this example every record is repeated 2 time
Thanks
Use a group by for whatever field you need unique. For example, if you want one row per code, then:
SELECT * FROM task_code_rates LEFT JOIN task_code_revenue ON task_code_revenue.code = task_code_rates.code
where task_code_rates.code = 105
group by task_code_revenue.code, task_code_revenue.tier
If code admits duplicates in both tables and you perform join only using code, then you will get the cartessian product between all matching rows from one table and all matching rows from the other.
If you have 5 records with code 100 in first table and 2 records with code 100 in second table, you'll get 5 times 2 results, all combinations between matching rows from the left and the right.
Unless you have duplicates inside one (or both) tables, all 10 results will differ in colums coming either from one table, the other or both.
But if you were expecting to get two combined rows and three rows from first table with nulls for second table columns, this will not happen.
This is how joins work, and anyway, how should the database decide which rows to combine if it didn't generate all combinations and let you decide in where clause?
Maybe you need to add more criteria to the ON clause, such as also matching dates?
I am trying to find duplicates from a table based on multiple columns. I have a table which has a columns like Email(Office),Email(Personal1) & Email_Personal2, Mobile_Personal1, Mobile_Personal2, FirstName,MiddleName,LastName,CompanyName,Designation etc. It has millions of records. There are so many duplicates for a specific record.
The folowing image represents the sample Table:
Now, I want to find the record using sql query which has the full values in its columns, want to keep this record and delete remaining all.
select *,count(*) from mytable where first_name!="" group by First_Name,Email_Office,Email_Personal1,Email_Personal2,Personal_Mobile1,Personal_Mobile2 having count(*)>1
But its showing me a specific record with total number of occurrence in last count(*) column only. Guide me in above query how can I see a record which has all the details along with number of occurrences of that specific record? How do I keep that one complete record and delete remaining all from a table?
I've removed "having count(*) >1" to see each record from above query but its taking so much time to show the output,almost feel like its getting hanged.
select t.* from mytable t inner join (select first_name,middle_name,last_name,designation,company_name,email_office,email_personal1,email_personal2,personal_mobile1,personal_mobile2,count(*) as NoDuplicates from mytable group by first_name,middle_name,last_name,designation,company_name,email_office,email_personal1,email_personal2,personal_mobile1,personal_mobile2 having NoDuplicates > 1) tsum on t.first_name=tsum.first_name and t.Middle_Name=tsum.middle_name and t.Last_Name=tsum.last_name and t.Designation=tsum.designation and t.Company_Name=tsum.company_name and t.Email_Office=tsum.email_office and t.Email_Personal1=tsum.email_personal1 and t.Email_Personal2=tsum.email_personal2 and t.Personal_Mobile1=tsum.personal_mobile1 and t.Personal_Mobile2=tsum.personal_mobile2
As of right now, creating a query with all records from both tables I want to display gives me every record for table b for the first record of table a, then every record of table b for the second record of table a, and so on.
SELECT *
FROM tblSales, tblRepair;
I want to be able to format these tables so that records from each table are displayed within a report, but separately (not joined). Both these tables contain sales data that need to be displayed and calculated together on a daily basis, but my problem right now is getting the data out of these tables and together in a format that doesn't join each record together.
Thanks in advance.
You can use a UNION query to combine both tables. I've added a dummy column to distinguish between the two tables:
SELECT *,'Sales' AS TheTable FROM tblSales
UNION ALL SELECT *, 'Repairs' FROM tblRepairs;
This will list all the Sales records first, followed by all the Repairs. You can add an ORDER BY clause to change this.
Alternatively, depending on the type of report you are creating, you could base the main report on one table and add a subreport based on the second.
I'm trying to create an ssrs report. Here's the data i have:
original data
I need to grouping and numbering based on specific column but ignoring the Entire entry row, the final result should be like this.
result
What's the grouping should be on case like this so i can put the number just like in my screenshot ?
Insert a matrix , row groups for Warehouse, Column1, Column 2
and for column groups use Amount, insert amount field with aggregate function of sum.
to add in the No. row, you will need to insert a empty column between column1 and column2 and insert an expression similar to:
=RunningValue(Fields!Test_case.Value, CountDistinct,"Tablix3" )
Whereby Fields!Test_case.Value is equivelant to Column1 and Tablix3 is equivelant to your matrix.
Example of design and report outcome, just need to correct expression for the No. column: