count(distinct col1) not giving correct count - sql-server-2008

I have a below table
id date1
1 1973-07-06
1 null
1 1994-01-20
1 1973-07-06
1 1980-03-12
1 1994-01-20
2 1960-10-17
2 1996-12-12
2 1996-12-12
2 2000-02-18
2 null
2 null
2 1960-10-17
2 1960-10-17
2 null
I need to find unique date1 value count for each id
Query i am using is as follows
select id,count(distinct date1) as col1
from iop
group by id
this query is giving me the wrong result as
id col1
1 4
2 4
However I should get
id col1
1 3
2 3

the query is counting the null value, try this:
select id,count(distinct date1) as col1
from iop
WHERE date1 NOT LIKE 'NULL'
group by id

Related

Union columns in same table and show sum of two columns based on the ID

I'm trying to find a query that can help me doing the following, I have one table like bellow
ID1 VALUE1 ID2 VALUE2
=========================
1 1 2 2
1 2 1 2
2 3 2 4
2 4 3 5
3 6 1 5
4 2 1 1
I'm looking for
ID VALUE1 VALUE2
=========================
1 3 8
2 7 6
3 6 5
4 2 0
Basically, search in ID1 and shows sum in Value1 and search same ID in ID2 and shows sum of Value2
I tried
SELECT
ID1 as ID,
SUM(VALUE1) AS VALUE1,
SUM(VALUE2) AS VALUE2
FROM TABLE
GROUP BY ID1;
but the query above does not show what I want.
I made this where it union both IDs
SELECT ID1 as ID from TABLE
UNION
SELECT ID2 from TABLE
but then I do't know how to use the created columns to search on Value1 and Value2
Use union all to combine the four columns into a more reasonable format. Then aggregate with group by:
select id, sum(value1), sum(value2)
from ((select id1 as id, value1 as val1, 0 as val2 from t
) union all
(select id2, 0, value2 from t
)
) tt
group by id;

Count distinct values where count of another value = 1

Given
------------------------------
|id | val1 | val2 | date |
______________________________
1 10 2 1990-10-02
2 10 3 1990-10-02
3 1 1 1990-10-02
4 21 1 1990-10-02
5 30 3 1990-10-02
6 30 1 1990-10-02
I want to have in results the row with id 3 and 4 because they have only one val1 if we group by val1.
How to achieve this?
SELECT DISTINCT COUNT(*) WHERE
You can use group by and having for this:
select t.*
from t
group by val1
having count(*) = 1;
In general, I am opposed to having an aggregation query with unaggregated columns in the select. However, it is fine in this case because the count(*) guarantees that there is only one matching row.
Note: this will not work in other databases.
SELECT DISTINCT x.*
FROM my_table x
LEFT
JOIN my_table y
ON y.id <> x.id AND y.val1 = x.val2
WHERE y.id IS NULL;

need group by data in mysql

in mysql:
Have data in data:
ColA | ColB | Rank
1 2 0
1 3 1
2 1 0
3 1 0
3 2 1
Keeping Col A as key field, need to get data on base of highest rank i.e.,
output:
ColA | ColB | Rank
1 3 1
2 1 0
3 2 1
any ideas..
You can do it this way:
SELECT T1.ColA, T2.ColB, T1.Rank
FROM TableName T2 JOIN
(SELECT ColA,MAX(Rank) as Rank
FROM TableName
GROUP BY ColA) T1 ON T1.ColA=T2.ColA AND T1.Rank=T2.Rank
Explanation:
Inner query (T1) selects records with highest rank for each values of ColA.
Outer query (T2) is used to select ColB with respect to the values of ColA and Rank from T2.
Result:
ColA ColB Rank
--------------------
1 3 1
2 1 0
3 2 1
See result in SQL Fiddle

Select WHERE IN and LIMIT by number of rows

I have a table which looks like this
userid | value | time
1 0 1
1 1 2
1 2 3
3 5 4
4 6 5
1 9 6
3 10 7
Using a select where in query I would want to select userd, value, and time but limit the total number of rows pulled for each userid
My SELECT query,
select userid, value, time from table where userid in (1,3) order by time desc;
This query outputs all the values like so
userid | value | time
1 0 1
1 1 2
1 2 3
3 5 4
3 10 7
I would hover want to limit the number of rows for each userid to two to get an output like so
userid | value | time
1 0 1
1 1 2
3 5 4
3 10 7
I tried using limit but that limits the number of rows for the entire output.
You can use a rank query to limit rows per user
select userid,
value,
`time`
from (
select *,
#r:= case when #g = userid then #r + 1 else 1 end row_num,
#g:=userid
from test t
cross join (select #g:= null,#r:=0) t1
where userid in (1,3)
order by userid,value,`time` desc
) t2
where row_num <= 2
Above query will give rank to each record for same user like user 1 has 3 records the they will assigned rank as 1,2,3 and if user 2 has 2 records then rank will be 1,2 for user 2 and in parent query i am just filtering the records according to the rank that return only result where rank is less than equal to 2 for for each user only 2 rows with rank 1 and 2 will be returned
Demo

MySQL 5.5 select MAX from table2 for every unique id in table1

I have two tables:
table1 table2
column id column id column value
1 1 count_20
2 1 count_30
3 2 count_20
4 2 count_50
... 2 count_200
3 count_30
3 count_50
3 count_200
4 count_130
... ...
I want to build a query that will select every row from table1, column id and the MAX value from the corresponding column value from table2 and make a DESC short as int in value column.
So the output should be:
table1.id value
2 count_200
3 count_200
4 count_130
1 count_30
... ...
I have tried JOIN but then, for every value (count_%) in table2 I get the corresponding id from table1.
SELECT table1.id, table2.id, table2.value FROM table1
LEFT JOIN table2
ON table2.id=table1.id
WHERE table1.id<'100'
ORDER BY
CASE value
WHEN 'count_20' THEN '20'
END DESC,
CASE value
WHEN 'count_30' THEN '30'
END DESC,
CASE value
WHEN 'count_50' THEN '50'
END DESC,
CASE value
WHEN 'count_130' THEN '130'
END DESC,
CASE value
WHEN 'count_200' THEN '200'
END DESC;
Output:
table1.id table2.id value
2 2 count_200
3 3 count_200
4 4 count_130
2 2 count_50
3 3 count_50
1 1 count_30
3 3 count_30
1 1 count_20
2 2 count_20
... ...
Any help would be appreciated.
Give this a try:
select t2.id,
'count_' + cast(max(cast(replace(t2.value, 'count_', '') as int)) varchar) intValue
from table1 t1
join table2 t2 on t1.id = t2.id
group by t2.id
order by max(cast(replace(t2.value, 'count_', '') as int)) desc
This results in:
+----+-----------+
| ID | VALUE |
+----+-----------+
| 2 | count_200 |
| 3 | count_200 |
| 4 | count_130 |
| 1 | count_30 |
+----+-----------+
Fiddle here.
http://www.w3schools.com/sql/sql_distinct.asp
check this out. this will only show the values onces.