Good day again to all SQL guru here :)
I have a problem querying my table using COUNT.
This is the data inside of the table (tblstudentoffense)
And this is the RESULT that I want (Let's say that there is a new column after the offenseType that will count the Stealing and Gaming Offense
Is this possible? If possible kindly add an explanation with your provided code. Thanks :)
Using subquery to get count and concatenate count with main select statement
SELECT DISTINCT OffenseType + ' ' + CAST(B.Cnt AS VARCHAR) ,
Othercolumn1,Othercolumn2
FROM Your_tableName A
JOIN
(
SELECT COUNT(*) Cnt , StudentId
FROM Your_tableName
GROUP BY StudentId
) B ON B.StudentId = A.StudentId
Try using CONCAT_WS().The CONCAT_WS() function is used to join two or more strings with a separator.
Note: offensetypeCount is an new column having expected output.
SELECT *,CONCAT_WS(' ',offensetype,count(offenseType) AS offensetypeCount FROM tblstudentoffense GROUP BY offensetype;
Is this what you are looking for:
WITH SRC AS
(SELECT 'STEALING' OFFENSETYPE,'1' STUDENTID FROM DUAL UNION ALL
SELECT 'STEALING' OFFENSETYPE,'1' STUDENTID FROM DUAL UNION ALL
SELECT 'STEALING' OFFENSETYPE,'1' STUDENTID FROM DUAL UNION ALL
SELECT 'STEALING' OFFENSETYPE,'1' STUDENTID FROM DUAL UNION ALL
SELECT 'GAMING' OFFENSETYPE,'1' STUDENTID FROM DUAL UNION ALL)
SELECT OFFENSETYPE,STUDENTID,COUNT(*) FROM SRC GROUP BY OFFENSETYPE,STUDENTID;
You could try
SELECT CONCAT(offensetype, ' ', COUNT(offensetype)) AS offenseType,
studentid,
offenseln,
offensefn,
offensemn,
offensesuffixname
FROM tblstudentoffense
GROUP BY offensetype,
studentid,
offenseln,
offensefn,
offensemn,
offensesuffixname;
I've created a sample at this link http://rextester.com/WZGW41761.
Related
This is the query i am executing
SELECT email,firstname,lastname FROM `sco_customer`
WHERE id_customer IN (7693,7693,7693,7693,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,7693,3,3,3,3,3,7693,7693,3,3,3,7693,3,3,3)
This gives me only two records as their are same number of id_customer is filtered i.e 7693,3
email firstname lastname
abc#any.com Test Mage
abc2#any.com User Mage
It should give the same number of records as much is the id_customer
Any thoughts how this can be achieved ?
Try below. Instead of WHERE clause you can generate a dummy table and join it with your main table.(WITH works for version 8 or above)
WITH SAMPLE AS
(
SELECT 7693 AS ID FROM DUAL
UNION ALL
SELECT 3 AS ID FROM DUAL
)
SELECT email,firstname,lastname FROM `sco_customer`
INNER JOIN SAMPLE ON SAMPLE.ID=ID_CUSTOMER
Below mysql version 8:
SELECT email,firstname,lastname FROM `sco_customer`
INNER JOIN (
SELECT 7693 AS ID FROM DUAL
UNION ALL
SELECT 3 AS ID FROM DUAL
)SAMPLE ON SAMPLE.ID=ID_CUSTOMER
The following statement should solve you problem:
SELECT email,firstname,lastname FROM `sco_customer`
join (select 7693 as id_customer union all
select 7693 union all
select 7693 union all
select 3 union all
select 3 union all
select 3
) tmp on sco_customer.id_customer = tmp.id_customer
I have a table with two columns, one column named user, one json column named js that looks like this:
{"1":{"partner_id":54,"provider_id":13},
"2":{"partner_id":56,"provider_id":8},
"3":{"partner_id":2719,"provider_id":274}}
I want to select all 'provider_id' in one column/row.So it should look like this:
user| provider_ids
0001| 13,8,274
0002| 21,36,57,12
How can I do this? Thanks in advance!
Your provided json format is not so easy to work with.
Crated table for test purposes:
create table json_test as
select '0001' as usr, '{"1":{"partner_id":54,"provider_id":13},
"2":{"partner_id":56,"provider_id":8},
"3":{"partner_id":2719,"provider_id":274}}'
as json_text
union all
select '0002' as usr, '{"1":{"partner_id":54,"provider_id":21},
"2":{"partner_id":56,"provider_id":36},
"2":{"partner_id":56,"provider_id":57},
"3":{"partner_id":2719,"provider_id":12}}'
as json_text;
Query to return results:
with NS AS (
select 1 as n union all
select 2 union all
select 3 union all
select 4 union all
select 5 union all
select 6 union all
select 7 union all
select 8 union all
select 9 union all
select 10
)
select usr,
listagg(trim(TRIM(split_part(SPLIT_PART(js.json_text, '},', NS.n),'"provider_id":',2)),'}'),',') within group(order by null) AS t
from NS
join json_test js ON true and NS.n <= REGEXP_COUNT(js.json_text, '\\},') + 1
group by usr;
Notes:
1) do not name column "user" as it is reserved keyword
2) add as many dummy rows in NS subquery as there is maximum of json provider records
3) Yes, I know, this isn't very readable SQL :D
I came accross a strange problem with a MySQL Query
SELECT COUNT(id) FROM members
100
SELECT COUNT(id) FROM members WHERE lastname = 'Smith'
20
SELECT COUNT(id) FROM members WHERE lastname <> 'Smith'
0
The problem is, that the last query (Members with lastname != 'Smith') returns 0.
If there are 100 members in total and 20 members named 'Smith', the number of member with other last names should be 80, shouldn't it?
I tried different version using <>, !=, enclosing Smith with ' or ". The result when using LIKE and NOT LIKE instead is the same.
How is this possible? It seems that I am missing something quite obvious, but what...?
because others are null
try this :
SELECT COUNT(id) FROM members WHERE IFNULL(lastname ,'--')<> 'Smith'
Example :
CREATE TABLE my_table
SELECT 'ersin' name FROM dual
union all
SELECT 'ersin' name FROM dual
union all
SELECT 'ersin' name FROM dual
union all
SELECT null name FROM dual
union all
SELECT null name FROM dual
union all
SELECT null name FROM dual;
select script:
select count(*) from my_table where IFNULL(name ,'--') <> 'ersin' ;
output:
count(*)
3
========================================================
this is the sample db
I just want to get user who has both 2 and 14 in skills column. The answer should be "2"
Try this:
SELECT seekerID
FROM mytable
WHERE skillID IN (2, 14)
GROUP BY seekerID
HAVING COUNT(DISTINCT skillID) = 2
DISTINCT keyword is necessary only in case skillID values can occur multiple times for a single seekerID.
The easiest way to do this would be
select seekerID, count(*) as cnt
from table_name
where skillid in (2,14)
group by seekerID
having cnt = 2
use this:
select seekerID from table_name where skillid="2" and seekerID = ( select author from table_name where skillid="14")
I have a table that has the following schema:
DATA | CAUSE_1 | TIME_1 | CAUSE_2 | TIME_2 | CAUSE_3 | TIME_3
The CAUSE.* field (VarChar) can not contain any string, and if so, the field TIME.* is 0.
I'm trying to create a query, but unfortunately without success, where I would have the result display in this form:
CAUSE | TOT_TIME | N_RIPET_CAUSE,
where:
In CAUSE I have a list of what is contained in CAUSE_1 ... CAUSE_3,
In TOT_TIME the sum of the values in TIME_1 ... TIME_3,
In N_RIPET_CAUSE the number of repetitions of each CAUSE.
I hope I explained.
try this
SELECT DATA ,CAUSE , TOT_TIME , N_RIPET_CAUSE
FROM ( select DATA, CONCAT(`CAUSE_1`,' ',`CAUSE_2`, ' ', `CAUSE_3`) as CAUSE ,
sum(`TIME_1` + `TIME_2` +`TIME_3`) as TOT_TIME ,
(count(`CAUSE_1`)+count(`CAUSE_2`)+count(`CAUSE_3`)) as N_RIPET_CAUSE
FROM your_table
group by DATA
) t
SEE SQLFIDDLE DEMO
EDIT.
try this
( select DATA , `CAUSE_1` as CAUSE ,
sum(`TIME_1` + `TIME_2` +`TIME_3`) as TOT_TIME ,
(count(`CAUSE_1`)+count(`CAUSE_2`)+count(`CAUSE_3`)) as N_RIPET_CAUSE
FROM Table1
group by DATA)
union all
(select DATA , `CAUSE_2` as CAUSE ,
sum(`TIME_1` + `TIME_2` +`TIME_3`) as TOT_TIME ,
(count(`CAUSE_1`)+count(`CAUSE_2`)+count(`CAUSE_3`)) as N_RIPET_CAUSE
FROM Table1
group by DATA )
union all
(select DATA , `CAUSE_3` as CAUSE ,
sum(`TIME_1` + `TIME_2` +`TIME_3`) as TOT_TIME ,
(count(`CAUSE_1`)+count(`CAUSE_2`)+count(`CAUSE_3`)) as N_RIPET_CAUSE
FROM Table1
group by DATA )
SQL DEMO HERE
EDIT:
try this due to your need
select cause, sum(time) Tot_time, count(cause) N_Ripet_Cause
from(
select cause_1 as cause, time_1 as time
from Table1
union all
select cause_2 as cause, time_2 as time
from Table1
union all
select cause_3 as cause, time_3 as time
from Table1
) t
group by cause
DEMO SQL FIDDLE
If you cannot change the table structure, then in order to get this result, you are going to need to unpivot the columns into rows.
MySQL does not have an unpivot function but this can be done using a UNION ALL query. You can then apply the aggregate to those values to get the final result:
select cause, sum(time) Tot_time, count(cause) N_Ripet_Cause
from
(
select data, cause_1 as cause, time_1 as time
from yourtable
union all
select data, cause_2 as cause, time_2 as time
from yourtable
union all
select data, cause_3 as cause, time_3 as time
from yourtable
) src
group by cause
You could make a select from union select like that:
select * from
(
select cause_1 as cause, time_1 as time from tableName
union
select cause_2 as cause, time_2 as time from tableName
union
select cause_3 as cause, time_3 as time from tableName
) as joinedValues
Then you could perform any actions from that select.
Like number of each clause:
select cause, count(cause) from
(
...
) as joinedValues
group by cause
Jack is on the mark - you've got too many possibly redundant cells in your table structure. Use relations to eliminate such occurrences.
DataTable
dID | Data
instancesTable
ID | dID | CAUSE | TIME
Then use NATURAL JOIN upon the two tables to extract the information;
SELECT * FROM DataTable NATURAL JOIN instancesTable WHERE dID=? LIMIT 3
This query will return a list of causes and times of whatever occurred on the ID of the 'Data' in the first table.
Edit: *N_RIPET_CAUSE* can be found using a SUM(CAUSE) on the dID.