Here's my SELECT query:
SELECT col1, col3, col5, col8
FROM Table1
In my SELECT query, I want to perform a COUNT(*) exclusively for the current row.
I want something like this, but have no idea how I can get this:
SELECT col1, col3,
(
SELECT COUNT(*)
FROM table1 WHERE col3 = col3 value for current row
),
col5, col8
FROM Table1
What is the correct way to perform a COUNT(*) for the current row of a SELECT query resultset?
try this:
set #num := 0;
SELECT #num := #num+1 , col1, col3, col5, col8 FROM Table1
or other way:
SET #num := ( SELECT COUNT( * ) FROM Table1) ;
SELECT #num := #num -1 , col1, col3, col5, col8
FROM Table1
SELECT col1, col3,
(
SELECT COUNT(*)
FROM table1 WHERE id = A.Id
) Count,
col5, col8
FROM Table1 A
Found it.
If I write my query like this:
SELECT col1, col3,
COUNT(*) AS count,
col5, col8
FROM Table1
then I get COUNT of all the items in Table1 for all rows.
I actually wanted COUNT of all items for selective rows.
Like I may have col3 value appearing more than once. I actually want the count of all items for each col3.
So, GROUP BY clause is the solution.
SELECT col1, col3,
COUNT(*) AS Count,
col5, col8
FROM Table1
GROUP BY col3
Sorry, I think I wasn't able to explain my problem properly, so it confused a lot of people.. A very poor question. My bad.
Related
I want to delete all the rows which are returned by this query.
SELECT col1, col2, col3 FROM myTable GROUP BY col1, col2, col3 HAVING count(*) > 1;
I tried this, but it gives me a syntax error.
DELETE FROM myTable WHERE col1, col2, col3 IN (
SELECT col1, col2, col3 FROM (
SELECT col1, col2, col3 FROM myTable
GROUP BY col1, col2, col3 HAVING count(*) > 1 )
t );
Use an INNER JOIN with your table
DELETE t1 FROM myTable t1
INNER JOIN (
SELECT col1, col2, col3 FROM (
SELECT col1, col2, col3 FROM myTable
GROUP BY col1, col2, col3 HAVING count(*) > 1 )
t )
t2 ON t2.rcol1 = t1.rcol1 AND t2.col2 = t1.col AND t2.col3 = t1.col3;
But you you should test it on a test database, because i don't think that your select identifies the right rows, better would be to have a UNIQUE column, that would identify the correct rows, because this would delete all rows
In my MySql database, I want to create select query which should give output like this:
in my select query i want a column output as 1, if the column value present in a list returned by a select query else 0 .
Select col1,col2,
,IF col3 IN
((select col from tabl2 ),1,0)AS col5
from tbl1.
Thanks in Advance
SELECT col1, col2,
IF col3 IN ((select col from tabl2 ),1,0) AS col5
FROM tbl1
Using IF and subquery in MySQL
SELECT table1.column1, table1.column2,
(
SELECT IF (
(SELECT column3 FROM table1 WHERE column3 IN (SELECT column FROM table2)), 1, 0
)
) AS column_output
FROM table1
In general:
SELECT col1,
col2,
CASE WHEN col3 IN (select col from tabl2)
THEN 1
ELSE 0
END AS col5
FROM tbl1
Specific for MySQL:
SELECT col1,
col2,
col3 IN (select col from tabl2) AS col5
FROM tbl1
I am trying to get the row with the largest number col5 based on a sum as below:
SELECT
Col1,
Col2,
col3,
col4,
SUM(col5) AS TOTAL_col5
FROM table1
WHERE (((Col1)=variable1) AND ((col3)="variable2"))
GROUP BY
Col1,
Col2,
col3,
col4
ORDER BY
col4,
SUM(col5) AS TOTAL_col5 DESC
I have tried using Top 1 in the select though this only brings back one row, I have also tried doing a MAX(SUM(col5) AS TOTAL_col5) but I get errors because it seems you can't combine aggregates. I think the answer is a subquery, but I can't wrap my head around how to write it.
More detail as per HarveyFrench: I am looking for the outer query that would then return a table which has the LARGEST sum in col5 for every combination of cols 1 to 4. (The inner query returns ALL of the sums for each combination). Based on his input I tried the following, but it still returns everyting
SELECT
T1.Col1,
T1.Col2,
T1.Col3,
T1.Col4,
MAX(T1.TOTAL_col5)
FROM
(
SELECT
Col1,
Col2,
col3,
col4,
SUM(col5) AS TOTAL_col5
FROM table1
WHERE (((Col1)=variable1) AND ((col3)="variable2"))
GROUP BY
Col1,
Col2,
col3,
col4
ORDER BY
col4,
SUM(col5) AS TOTAL_col5 DESC
) AS T1
GROUP BY
T1.Col1,
T1.Col2,
T1.Col3,
T1.Col14
I don't quite understand what you are asking "how to get the largest row in each group".
In the SQL below, the inner query returns a table T1 which sums col5 for every combination of cols 1 to 4.
The outer query find the highest SUMof col5 for each col4.
I think this is what you are asking for, but until you question is clearer, I can't help any further.
SELECT col4
, MAX(TOTAL_col5)
FROM
(
SELECT
Col1,
Col2,
col3,
col4,
SUM(col5) AS TOTAL_col5
FROM table1
WHERE (((Col1)=variable1) AND ((col3)="variable2"))
GROUP BY
Col1,
Col2,
col3,
col4
) AS T1
GROUP BY col4
)
I have few columns in a table
Col1, Col2, Col3, Col4
Now I want to select like this
SELECT DISTINCT (Col1, Col2, Col3), Col4
i.e. get the distinct based on only these three colunms.
Just GROUP BY Col1, Col2, Col3 with an aggregate function with the col4 like MAX, MIN, etc .. like so:
SELECT Col1, Col2, Col3, MAX(Col4)
FROM TableName
GROUP BY Col1, Col2, Col3
From a comment to another answer:
Can I get like this Col1, Col2 , Col3 and (Col4 in delimiter form)
Yes, you can use the for xml path.
select Col1,
Col2,
Col3,
(
select ',' + T2.Col4
from YourTable T2
where T1.Col1 = T2.Col1 and
T1.Col2 = T2.Col2 and
T1.Col3 = T2.Col3
for xml path(''), type
).value('substring((./text())[1], 2)', 'varchar(max)') as Col4
from YourTable as T1
group by T1.Col1, T1.Col2, T1.Col3
SQL Fiddle
The group by and distinct perform almost similar functionality in SQL:
SO both queries below are almost equivalent:
SELECT DISTINCT Col1, Col2, Col3 FROM tbl
SELECT Col1, Col2, Col3 FROM tbl GROUP BY Col1, Col2, Col3
select distinct ( Convert(varchar(255),Col1) +' '+
Convert(varchar(255),Col2)+' '+Convert(varchar(255),Col3)),
Col4 from clients
I simply want to return duplicate records from a table. In my case, a record is duplicate if more than one record has the same value in col1, col2, col3, and col4.
SELECT col1, col2, col3, col4
, COUNT(*) AS cnt
FROM yourTable
GROUP BY col1, col2, col3, col4
HAVING COUNT(*) > 1
If there are additional columns that you want to be shown, you can JOIN the above to the table:
SELECT t.*
, dup.cnt
FROM yourTable t
JOIN
( SELECT col1, col2, col3, col4
, COUNT(*) AS cnt
FROM yourTable
GROUP BY col1, col2, col3, col4
HAVING COUNT(*) > 1
) AS dup
ON t.col1 = dup.col1
AND t.col2 = dup.col2
AND t.col3 = dup.col3
AND t.col4 = dup.col4
SELECT m.*
FROM (
SELECT col1, col2, col3, col4, COUNT(*)
FROM mytable
GROUP BY
col1, col2, col3, col4
HAVING COUNT(*) > 1
) md
JOIN mytable m
ON m.col1 = md.col1
AND m.col2 = md.col2
AND m.col3 = md.col3
AND m.col4 = md.col4