In the database there are column values which appear in multiple rows. The example rows are the following (in csv format):
AFAB19476C2CEEEE101FFA45FD207BA8B6185B29,539EE0643AFC3A3BE3D20DC6BE7D5376DC536D34,9800,58,29,24,34,2
A801DA9B2F4116A7A1B14A13532B2177C7436C43,91850E4C50536D45C9CEAFE5FB5B3A87154EB754,9800,15,15,15,15,1
4C1E0B5387FB7FE19FC1ED682D0EB08249779180,9B17AE806C79437945F99C054B59A859D5639D11,9800,51,51,51,51,1
5B83A4BE4161497C62471BF133A4E1AD905D25F8,BFF4CED4F54F221A76714B311623398070847B26,9800,71,71,71,71,1
145145E49302ABBEEFF2797CAA8E122FFD3D5BFD,0C287F08E8E11DB4CF10CEB5801EBD61E7664FE4,9800,55,55,55,55,1
99C1F96461BC870574D002034F001BA3F96A9AB5,2EC4F3158764DC07D981008B3054B97809A0B048,Tujina,34,34,34,34,1
**CCB433630C735A8DA1B7828C10820B8CF91F25B9**,2C9C297BEF9CC1C0CF16A0559DE828FA0E226698,9817,339,169,137,202,2
BF2A7F0A9AD762B46A4423F76BF0479B9A72F163,336FB392EA4EF85EFE2563332CDE7D32FCE711B2,9800,34,34,34,34,1
...
**CCB433630C735A8DA1B7828C10820B8CF91F25B9**,C4015FE337F1EEFA1ECE4143D77F9627BEB9D358,9800,464,464,464,464,1
**CCB433630C735A8DA1B7828C10820B8CF91F25B9**,0EC08D78C637EF0A05E858B2BAC85C3EF05DF959,9800,73,73,73,73,1
In this example the value CCB433630C735A8DA1B7828C10820B8CF91F25B9 appears in three rows in combination with different values.
I am looking for a way to count in how many rows the value from the 1st column appears and then order the values by the number of rows that contain that value.
For example only the value from the first row was checked the query would be the following
SELECT COUNT (*) FROM records WHERE column_1 = 'AFAB19476C2CEEEE101FFA45FD207BA8B6185B29'
but instead of just first row the values from all rows have to be checked.
I would be very thankful if anyone of you could suggest an appropriate SQL query statement or a function to sort all the values from the first column by the number of repeatings.
Thank you!
SELECT column_1, COUNT(*)
FROM records
GROUP BY column_1
ORDER BY COUNT(*) DESC
The following will show you the counts of all the values in column1, ordered in ascending order:
select column_1, count(*)
from records
group by column_1
order by 2 asc
Would something like this work?
SELECT column_1, COUNT(column_2)
FROM records
GROUP BY column_1
ORDER BY COUNT(column_2) DESC
Related
I have two MySql tables which I need to select a column from one, and where the results are used to select from another table. I know how to do it as two different select statements. However, I believe I can do it as a single statement but have no idea how.
Table one has two columns the second column has values which are also found in table two. I need to select all rows in table two which has the same values as those found in table one and where another column value is 0.
Any ideas how to go about doing this?
Use Join On tables to get columns form both table using query as
SELECT column_list
FROM table_1
LEFT JOIN table_2 ON
table_1.column = table_2.column;
Try to use Join query
SELECT columns
FROM table1
INNER JOIN table2
ON table1.column = table2.column;
I’m working on a project which is an online shop,
I want to show in a page the most sold items,
So my sql is
Select (*), Count(Product_ID) as n from Order_Details order by n desc.
But it doesn’t work. Can someone help?
You need to aggregate the data first, this can be done using the GROUP BY clause:
SELECT (*), COUNT(DISTINCT Product_ID)
FROM table
GROUP BY Product_ID
ORDER BY COUNT(DISTINCT Product_ID) DESC
The DESC keyword allows you to show the highest count first, ORDER BY by default orders in ascending order which would show the lowest count first.
SELECT (*), Max(DISTINCT Product_ID)
FROM table
GROUP BY Product_ID
ORDER BY Max(DISTINCT Product_ID) DESC
the most sold item you use max
I want to create a SQL query that will do the counting for me instead of in the razor code, I want to calculate the amount of distinct dates in my database.
I found that SQL Server CE does not support count(distinct column_name) and instead using group by would be the solution, but for some reason I could not make it work, what am I doing wrong?
SELECT COUNT(date) as date
FROM Test
GROUP BY date
This simply counted every date in my database and not the distinct ones when writing it out like the following
var totalcount = 0;
foreach (var c in db.Query(query))
{
var ttotalcount = c.date;
totalcount += ttotalcount;
}
<a>#totalcount<a>
Updated
Your query is asking for the counts of each distinct date. If you want to know how many of those you have you need to sum them up. You can do this be nesting your query with a SUM query. Then use an additional column defulted to "1", to allow to sum up the number of rows (which should be your distinct dates). Also date can be a reserved word. You might want to try and avoid using that as a column name.
SELECT SUM(New_Row) as dateSUM from (
SELECT COUNT(date) as dateCount, 1 as New_Row FROM Test GROUP BY date
) a
Maybe:
SELECT COUNT(date) as [date]
FROM Test
GROUP BY date
Date is a reserved word, need to add []
You are confusing SQL with the three usages of date. I am assuming that the original field is named date. I am also assuming that each record has the date field populated.
SELECT COUNT(*) as numDates FROM Test GROUP BY date
I have a table like this:
Table name is List.
I have to get sum of each column. I cannot give this query:
SELECT SUM(jayesh,murali,jins) from List;
because the columns are added dynamically. Column names are given as user input. Is there any other code to do this..?
If you want the sum in each column:
SELECT SUM(jayesh), SUM(murali), SUM(jns) from List;
Not saying that any of this is or is not a good idea because no context was provided, but...
Sum of all columns combined for all records (one sum for entire table):
SELECT SUM(jayesh+murali+jns)
FROM List
Sum of all columns for each record (one sum for each row):
(This option requires having an id column to group by so that you can determine how to group the rows)
SELECT
ID,
SUM(jayesh+murali+jns)
FROM List
GROUP BY ID
Sum of each column separately for all records (one sum for each column):
SELECT
SUM(jayesh),
SUM(murali),
SUM(jns)
FROM List
I would also recommend reconsidering your design in adding dynamic columns based on user input. This is generally not a good idea, certainly not for this case. Read more here: http://www.sommarskog.se/dynamic_sql.html
I have a datatable and it has 4 columns. My problem is some columns has same data, same date. I have to delete same data, same date. How can I delete dublicate data?
My Datatable:
In this table I have to delete 1 or 3 (Id) In code side with for or foreach loop. Because in the same date there is a same Isban.
Id Name Isban Date
1 A 123 09.09.2010
2 B 123 10.09.2010
3 C 123 09.09.2010
4 A 234 11.09.2010
5 B 342 12.09.2010
Thanks You
john
A standard way to do this is to run a select distinct query to insert the distinct records into a new table, delete the existing table, and then rename the new table to the previous table.
Edit: You can that you have to do this client-side.
One way is addressed here: Distinct in DataTable
Alternatively, loop through the table and store in a hash table each record; use the pair Isban/Date as the key and the record as the value. When you encounter a duplicate record it will already be in the hash table so you pass over it. Then, you can create a new data table from the records in the hash table.
If you DO have to do it in a loop, I would do it something like the following... Pre-query based on the minimum ID based on the given duplicate entitie elements, then delete for NOT being the minimum key
Select
FldDup1,
FldDup2,
min( IDKey ) as KeepThisID,
count(*) as TotalPerDupFields
from
YourTable
group by
FldDup1,
FldDup2
having
TotalPerDupFields > 1
In this case, you'll end up with a sample result of...
FldDup1 FldDup2 KeepThisID TotalPerDupFields
123 09.09.2010 1 2
as I was ignoring the 2nd column of "A", "B" and "C" as it didn't appear to be the indicator in your explanation of duplicates.
Then, I would isse a delete... via parameterized SQL-Delete query
Delete from YourTable
Where FldDup1 = ResultQuery.FldDup1
and FldDup2 = ResultQuery.FldDup2
and NOT IDKey = ResultQuery.KeepThisID