How do I SUM all users, I have two different users. One is student and the second is instructor.
SELECT
(SELECT COUNT(user_role) FROM tbl_users WHERE user_role = 'student') as student,
(SELECT COUNT(user_role) FROM tbl_users WHERE user_role = 'instructor') as instructor,
(SELECT SUM(user_role) FROM tbl_users) as totaluser
and why it doesn't add result as 1
in totaluser? It's 1 + 0 = 1. There should be at least 1 total user(s).
This is what you need
select student, instructor, student+instructor totaluser FROM (
select
sum( if( user_role = 'student', 1, 0 ) ) AS student,
sum( if( user_role = 'instructor', 1, 0 ) ) AS instructor
from tbl_users ) t_alias
2 options ;
1 - you can use "union all" for sum of the different selects
SELECT name, COUNT(name) AS count
FROM table
GROUP BY name
UNION ALL
SELECT 'SUM' name, COUNT(name)
FROM table
or
2-
SELECT
(select count(*) from table1 where selectedID = '123')
+
(select count(*) from table2 where selectedID = '123')
+
(select count(*) from table3 where selectedID = '123')
SELECT student, instructor, (SUM(student)+SUM(instructor)) AS totaluser
FROM tbl_users
You can write it in this way:
'''select student,instructor,(student+instructor) as sum from(SELECT
(SELECT COUNT(user_role) FROM tbl_users WHERE user_role = 'student') as student,
(SELECT COUNT(user_role) FROM tbl_users WHERE user_role = 'instructor') ) table'''
Related
I have two tables with many-to-many relationship. I am trying to get values from both of the table where UserId is unique (I'm joining these table on this value)
I am rying to use pre aggregated query, but I get error
Column 'clv.ProbabilityAlive' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
I understand that I should add these all values to group by clause, but then I am getting duplicates because peakClv values repeat.
If i am using simple join then it takes forever because of many to many relationship.
this is my query:
SELECT
distinct(s.userid) as userId,
s.ProbabilityAlive AS ProbabilityAlive,
a.PeakClv as PeakClv
FROM (
SELECT [UserId], ([sb].[ProbabilityAlive]) AS ProbabilityAlive
FROM clv as sb
WHERE sb.[CalculationDate] = '20200311'
GROUP BY [UserId]
) s
LEFT JOIN (
SELECT [UserId], PeakClv
FROM [dbo].[AdditionalClvData] where peakClv > 1
GROUP BY [UserId]
) a ON a.[UserId] = s.[UserId]
I am a bit out of ideas could someone lend a hand?
I also tried using distinct like one answer suggested:
SELECT
distinct (s.userid) as userId,
s.ProbabilityAlive AS ProbabilityAlive,
a.PeakClv as PeakClv
FROM (
SELECT DISTINCT ([UserId]), ([sb].[ProbabilityAlive]) AS
ProbabilityAlive
FROM clv as sb
WHERE sb.[CalculationDate] = '10/09/2020 00:00:00' AND sb.
[EstimatedNumberOfTransactionsLong] >= 0 AND sb.
[EstimatedNumberOfTransactionsLong] <= 5680 AND sb.[ClientId] = '16'
AND sb.[Product] = 'Total'
ORDER BY sb.[userId] asc OFFSET (1 - 1) * 10 ROWS FETCH NEXT 10 ROWS
ONLY
) s
LEFT JOIN (
SELECT DISTINCT [UserId], PeakClv
FROM [dbo].[AdditionalClvData]
) a ON a.[UserId] = s.[UserId]
but I still get duplicates:
If you have not aggregation function like SUM(), MAX() .. you can't use GROUP BY
SELECT
distinct s.userid as userId,
s.ProbabilityAlive AS ProbabilityAlive,
a.PeakClv as PeakClv
FROM (
SELECT DISTINCT [UserId], ([sb].[ProbabilityAlive]) AS ProbabilityAlive
FROM clv as sb
WHERE sb.[CalculationDate] = '20200311'
) s
LEFT JOIN (
SELECT DISTINCT [UserId], PeakClv
FROM [dbo].[AdditionalClvData] where peakClv > 1
) a ON a.[UserId] = s.[UserId]
if you need distinct (not repeated rows) use distinct
but looking to you img seems you need an aggregation function on PeakClv eg max() and group by
SELECT
s.userid as userId,
s.ProbabilityAlive AS ProbabilityAlive,
max(a.PeakClv) as PeakClv
FROM (
SELECT DISTINCT [UserId], ([sb].[ProbabilityAlive]) AS ProbabilityAlive
FROM clv as sb
WHERE sb.[CalculationDate] = '20200311'
) s
LEFT JOIN (
SELECT DISTINCT [UserId], PeakClv
FROM [dbo].[AdditionalClvData] where peakClv > 1
) a ON a.[UserId] = s.[UserId]
GROUP BY s.userid,
s.ProbabilityAlive
I have two tables ... accounts & tax. In table tax I would like to updated records (?) depending on column category available in table accounts like this:
table 'accounts'
id|cat2|cat3
1|A|Active
2|A|Active
3|A|Inactive
4|A|Active
5|B|Inactive
6|B|Active
table 'tax'
id|category|count_total|count_active|count_inactive
1|A|?|?|?
2|B|?|?|?
Desired result:
id|category|count_total|count_active|count_inactive
1|A|4|3|1
2|B|2|1|1
For count_total I tried this:
UPDATE tax t2,
( SELECT count(*)
FROM accounts
) t1
SET t2.count_total = t1.count(*)
WHERE t1.cat2 = t2.category;
You can use join to update your tax table
UPDATE tax t2
JOIN (
SELECT
cat2,
COUNT(*) cnt,
SUM(cat3 = 'Active') count_active,
SUM(cat3 = 'Inactive') count_inactive
FROM
accounts
GROUP BY cat2
) t1
ON t1.cat2 = t2.category
SET t2.count_total = t1.cnt ,
t2.count_active = t1.count_active,
t2.count_inactive = t1.count_inactive
Demo
Another version without JOIN:
UPDATE tax
SET
count_total = (
SELECT COUNT(*)
FROM accounts
WHERE accounts.cat2 = tax.category
),
count_active = (
SELECT COUNT(*)
FROM accounts
WHERE accounts.cat2 = tax.category AND accounts.cat3 = 'Active'
),
count_inactive = (
SELECT COUNT(*)
FROM accounts
WHERE accounts.cat2 = tax.category AND accounts.cat3 = 'Inactive'
);
Demo
Here I have a simple table. I would like to end up with a table like the one given. What is the best way to write the sql statement to achieve this?
Table 1
Id Name Approved
1 Australia 3
2 UAE 1
3 India 2
Table 2
Id Status
1 Submit
2 In-Progress
3 Pending
Show result as
Submitted In-Progress Pending
UAE India Australia
Please Try below query:
create table #country
(
ID int identity(1,1),
Name varchar(30),
Approved int
)
create table #status
(
ID int,
Status varchar(30)
)
insert into #country (Name, Approved) values ('Australia',3), ('UAE',1), ('India',2)
insert into #status (ID, Status) values (1,'Submit'), (2, 'In-Progress'), (3,'Pending')
select Submit, [In-Progress],[Pending]
from (
select t1.Name, t2.Status
from #country t1
inner join #status t2 on t1.Approved = t2.ID
)dd1
pivot (
max(Name) for Status in ([Submit], [In-Progress],[Pending])
) piv
drop table #country
drop table #status
Output of this query:
Try this:-
select
trim(replace(group_concat(submitted),',',' ')) as submitted,
trim(replace(group_concat(InProgress),',',' ')) as InProgress,
trim(replace(group_concat(Pending),',',' ')) as Pending
from
(
Select
case when status='Submit' then A.Name else ' ' end as submitted,
case when status='In-Progress' then A.Name else ' ' end as InProgress,
case when status='Pending' then A.Name else ' ' end as Pending
FROM
TABLE1 A
INNER JOIN
TABLE2 B
ON A.ID=B.ID
) a;
Here's the answer as of given Data:
select `Submit`,`In-Progress`,`Pending` from
(select `Submit`,`In-Progress`,INP.id from
(select Name as 'Submit',a.id from
(select * from table1)as a
LEFT JOIN
(select * from table2) as b
on a.Approvide = b.`Id`
where b.`STATUS` = 'Submit') as Sub
INNER JOIN
(select Name as 'In-Progress',a.id from
(select * from table1)as a
LEFT JOIN
(select * from table2) as b
on a.Approvide = b.`Id`
where b.`STATUS` = 'In-Progress') as INP
on Sub.id != INP.id) as c
INNER JOIN
(select Name as Pending,a.id from
(select * from table1)as a
LEFT JOIN
(select * from table2) as b
on a.Approvide = b.`Id`
where b.`STATUS` = 'Pending') as Pen
on c.id != Pen.id
select Name,Status from Table1 natural join Table2;
That's simple. The result should look like this now. I think this is usual approach that how we actually retrieve the information from multiple tables.
Name Status
Australia Pending
UAE Submit
India In-Progress
Please consider
I have to tables, job_postings and job_applies. How can I get all the jobs, that user has not applied to?
Below are columns of my job_postings table:
id, user_id, title, description, duties, salary, child_count, benefits, created_at, updated_at
Below are columns of the job_applies table
user_id, posting_id, status, created_at, updated_at
What I tried:
$job_postings = DB::table('job_postings')
->select(
'job_postings.title',
'job_postings.description',
'job_postings.duties',
'job_postings.salary',
'job_postings.child_count',
'job_postings.benefits',
'job_postings.created_at',
'job_postings.id AS posting_id',
'job_postings.user_id')
->join('job_applies', 'job_applies.posting_id', '!=', 'job_postings.id')
->where('job_applies.user_id', "=" , user()->id)
->get();
In Mysql you should write (replace * with your field list). You can adapt to your language
SELECT *
FROM JOB_POSTING A
LEFT JOIN JOB_APPLIES B ON B.POSTING_ID = A.ID AND B.USER_ID = A.USER_ID
WHERE B.POSTING_ID IS NULL
or
SELECT *
FROM JOB_POSTING A
WHERE NOT EXISTS (SELECT 1 FROM JOB_APPLIES B WHERE B.POSTING_ID = A.ID AND B.USER_ID = A.USER_ID)
I want to combine multiple tables result count in one result with column wise using MySql (see the result required) but i am confuse about this if you have any query or optimize way regarding this please answer me or helps are definitely appreciated also i was tried myself this Query with Union but seriously not success (see the Query Example)
Result required
post post_comment_likes post_comments post_likes
2 0 3 0
Query
SELECT COUNT(*) AS `post` FROM post WHERE user_id = "123456" UNION
SELECT COUNT(*) AS `post_comment_likes` FROM post_comment_likes WHERE user_id = "123456" UNION
SELECT COUNT(*) AS `post_comments` FROM post_comments WHERE user_id = "123456" UNION
SELECT COUNT(*) AS `post_likes` FROM post_likes WHERE user_id = "123456"
set #post = 0;
set #post_comment_likes = 0;
set #post_comments = 0;
Set #post_likes = 0;
select count(*) into #post from .......;
select count(*) into #post_comment_likes from ......;
select count(*) into #post_comments from ......;
select count(*) into #post_likes from ......;
select (#post + #post_comment_likes + #post_comments + #post_likes) ;
In the above code #post , #post_comment_likes , #post_comments , #post_likes are session variables , you prefix them with '#' character
Using the above code you have access to count of each table.
SELECT (SELECT COUNT(*)
FROM post
WHERE user_id = "123456") AS `post`
,(SELECT COUNT(*)
FROM post_comment_likes
WHERE user_id = "123456") AS `post_comment_likes`
,(SELECT COUNT(*)
FROM post_comments
WHERE user_id = "123456") AS `post_comments`
,(SELECT COUNT(*)
FROM post_likes
WHERE user_id = "123456") AS `post_likes`
You should replace COUNT(*) with COUNT(<some fieldname>) and could use a variable for the user id.
Please try the mysql query given below
SELECT
(SELECT COUNT(*) AS `post` FROM post WHERE user_id = "123456" ) AS post ,
(SELECT COUNT(*) AS `post_comment_likes` FROM post_comment_likes WHERE user_id ="123456") AS post_comment_likes,
(SELECT COUNT(*) AS `post_comments` FROM post_comments WHERE user_id = "123456") AS post_comments ,
(SELECT COUNT(*) AS `post_likes` FROM post_likes WHERE user_id = "123456" ) AS post_likes
thanks