I'm learning SQLite3 and having trouble with this particular output.
Let's say I have a column like:
user0
user1
user1
user2
user2
user3
user3
user3
user4
user4
user4
user4
I would like to count how many times a user appears in the column, and having an output like:
1 | 1
2 | 2
1 | 3
1 | 4
Meaning: There is 1 user appearing 1 time, 2 users appearing 2 times, 1 user appearing 3 times, 1 user appearing 4 times.
I don't need to know anything else, only how many users are with how many accounts.
You group by once to get the counters of the 1st column you need and then again on this result:
select count(*) total, counter
from (
select count(*) counter
from tablename
group by col
)
group by counter
See the demo
Results:
| total | counter |
| ----- | ------- |
| 1 | 1 |
| 2 | 2 |
| 1 | 3 |
| 1 | 4 |
Here is a small example using GROUP BY and the COUNT()-function.
Im using MSSQL, but it should be nearly the same in SQLITE3.
This is the table Im using:
select count([name]), [name] from Test
group by [name]
This is the result:
Related
My situation
I'm making a site where people can reserve meeting rooms. On the reservation form for a meeting room, I've some optional field sets like below:
ID | Name
-- | ------------
1 | Catering
2 | Coffee break
3 | Drinks
Here are some example meeting rooms:
ID | Name | Location
-- | ------- | ---------
1 | Dog | Brussel
2 | Cat | Antwerpen
3 | Chicken | Brugge
4 | Cow | Gent
I'm using Microsoft SQL server 2016.
Database structure
The fieldsets from the first code block stands inside my database in the table reservationFieldsets.
The meeting rooms stands in the meetingRooms table.
There is an intermediate table named meetingRoomsReservationFieldsets.
Question
Now I'll fill meetingRoomsReservationFieldsets with all rooms and all the fieldsets like below:
RoomID | FieldsetID
------ | ----------
1 | 1
2 | 1
3 | 1
4 | 1
1 | 2
2 | 2
3 | 2
4 | 2
1 | 3
2 | 3
3 | 3
4 | 3
I've tried
I've tried to do it manually but there are a lot of rooms and too much to do that manual.
I've found it by the comments on the question. I use this code:
INSERT INTO meetingRoomsReservationFieldsets
SELECT meetingRooms.id, reservationFieldsets.id
FROM reservationFieldsets
CROSS JOIN meetingRooms
Users owns licenses, and a plan is a combination of licenses.
Sometimes a user owns an individual license, which is not part of a plan.
I want to count the number of users per plan. In the exemple below, it should return :
PlanName | Number of Users
P1 | 1
P2 | 2
Tables :
Users Licenses Plans
----------------- --------------------- ----------------
Username | UserID LicenseName|LicenseID PlanName|PlanID
user1 | 1 L1 | 1 P1 | 1
user2 | 2 L2 | 2 P2 | 2
user3 | 3 L3 | 3
user4 | 4 L4 | 4
L5 | 5
UsersAndLicenses PlansAndLicenses
---------------- ----------------
UserID | LicenseID PlanID | LicenseID
1 | 1 P1 | 1
1 | 2 P1 | 2
1 | 3 P1 | 3
2 | 4 P2 | 4
2 | 5 P2 | 5
3 | 1
4 | 4
4 | 5
I started with a select to get the list of users and plans (I will apply a count on this select) and I get an issue : user3 who has only L1 (he hasn't a plan) is listed in P1 (L1 is part of P1). My select statement is :
SELECT Plans.PlanName, Users.UserName FROM Users
INNER JOIN (((Licenses INNER JOIN LicensesPlans ON Licenses.LicenseID = LicensesPlans.LicenseID)
INNER JOIN Plans ON LicensesPlans.PlanID = Plans.PlanID)
INNER JOIN UsersLicenses ON Licenses.LicenseID = UsersLicenses.LicenseID) ON Users.UserID = UsersLicenses.UserID
GROUP BY Plans.PlanName, Users.UserName;
What is wrong with my select ?
As you are only making counts of how many users for each plan you could probably get away with joining these two tables
UsersAndLicenses
PlansAndLicenses
and aggregating the number of users for each plan id.
What have you tried so far?
I wasn't able to solve my issue with just a query.
I created a stored procedure which is called for each user.
This stored procedure use a cursor to scroll through all the plans, retrieve the licenses of each plan and compare them (using an EXCEPT statement) with the licences owned by the user.
Hi I have doubt in sql server
Trantable:
empid | deptid | Projectname | Transactionid
1 |10 | test | 1
2 |11 | test1 | 2
2 |10 | jai | 3
2nd table: dimemp ....> here dimemp is scdtype2 dimension.its all ready done
empkey | empid | ename | flag
1 | 1 | a | 1
2 | 2 | b | 1
3 | -1 | na | 1
3rd table: dimdept------>here dimdept is scdtype2 dimension.implementaion allready done.
deptkey | deptid | deptname | flag
1 | 10 | hr | 1
2 | 11 | ceo | 1
3 | -1 | NA | 1
Here I want load trantable data into facttran table with corresponding keys. here transactionid is unique column
to identiy unique record.
Facttran table structure look like below and factran we need to maintain scd type1 data.
empkey | deptkey | projectname |transactionid
I tried like below query
merge into facttran target
using (select ISNULL(a.empkey, (select empkey from Dimemp where empid = -1)) empkey,ISNULL(b.deptkey, (select deptkey from dimdept where deptid = -1)) deptkey, c.projectname, c.transactionid
from trantable c
left join dimemp a on a.empid=c.empid and a.flag=1
left join dimdept b on b.deptid=c.deptid and b.flag=1)source
on target.transactionid=source.transactionid
when not matched
then insert ([deptkey],[empkey],[projectname],[transactionid])
values(source.deptkey,source.empkey,source.projectname,source.transactionid)
when matched
then update set target.empkey=source.empkey ,
target.deptkey=source.deptkey,
target.projectname=source.projectname,
target.transactionid=source.transactionid ;
then I got output like below
Table :facttran
empkey | deptkey | projectname |transactionid
1 | 1 | test | 1
2 | 2 | test1 | 2
2 | 1 | Jai | 3
upto now its working fine.
2nd day in my trantable few records updated and few records insert in sourc trantable.based on below table data I want update in facttable with corresponding key.
2nd table: dimemp ....> here dimemp is scdtype2 dimension
empkey | empid | ename | flag
1 | 10 | a | 0
2 | 11 | b | 1
3 | -1 | na | 1
4 | 10 | aaa | 1
3rd table: dimdept------>here dimdept is scdtype2 dimension.implementaion allready done.
deptkey | deptid | deptname | flag
1 | 10 | hr | 0
2 | 20 | ceo | 1
3 | -1 | NA | 1
4 | 10 |hrdept | 1
Trantable:
empid | deptid | Projectname | Transactionid
1 |11 | test | 1 ------record updated in source side here deptid changed from 10 to 11
1 |11 | test123 | 2 -------Here empid changed from empid 2 to 1 and projectname changed test1 to test123
2 |10 | jai | 3 ------here no records are not changed
1 |10 | cod | 4 ----------new rocrd is came
based on above trantable.I want facttran table data look like below.
Table :facttran
empkey | deptkey | projectname |transactionid
1 | 2 | test | 1
1 | 2 | test123 | 2
2 | 1 | Jai | 3
4 | 10 | cod | 4
when I ran 2nd time with same query.I am not able get to expected result.
here mainily source trantable related transactioni id is exist or not in facttran table .if not exist then we need to insert correspondig dimensionkeys with lates flaf=1
values.if we found transactionid exist in fact table then we need to updated existing dimension corresonding key.
suppose if we take transactionid=1 records here only chnaged deptid not empid that time we donot need update empid corresponding lates flag=1 corresondingkey
we need check exisig transaction id is updated each dimension need to check exist or not if not exist latest flag=1 related corresponding key.
if exist we donot need to updated that one.if new reocrds came then we need to insert with latest flag=1 corresponding keys in factran table.
please tell me how to write query to achive this task in sql server.
i am trying to find the total marks for each student based on StudentId and MarksTypeId row count.
i have 3 tables
MarksType
----------------------------------
MarksTypeId | MarkType | Marks
----------------------------------
1 | Writing | 10
2 | Drawing | 30
3 | Singing | 20
----------------------------------
Students
--------------------------------
StudentId | Name | Address
--------------------------------
1 | John | USA
2 | Raja | India
3 | Paul | AUS
--------------------------------
MarksDetails -- Has two foreign keys
-------------------------------------------------------
MarksDetailsId | MarksTypeId | StudentId | Date
-------------------------------------------------------
1 | 3 | 1 | 18 jan
2 | 3 | 1 | 18 jan
3 | 1 | 3 | 19 jan
-------------------------------------------------------
This is my Desired Result :
------------------------------------
StudentId | Name | Total Marks
------------------------------------
1 | John | 40
2 | Raja | 0
3 | Paul | 10
------------------------------------
i mean if John sang two times a day, so using StudentId and MarksTypeId, i need his total Marks as result.
so far i did the following:
select Sum(MarksType.Marks) from MarksType inner join MarksDetails on MarksType.MarksTypeId=1
but the sum returns wrong total,
UPDATED OTHER ATTEMPTS:
this results total row count for each studentid
select MarksDetails.StudentId , COUNT(MarksDetails.StudentId ) as count from MarksDetails
group by MarksDetails.StudentId
this results Marks, with studentid and markstypeid
select MarksType.Marks, MarksType.MarksTypeId , MarksDetails.StudentId from MarksType inner join MarksDetails on MarksType.MarksTypeId = MarksDetails.StudentId
may i know what i am doing wrong.
Any help would be Great.
finally with kind guidance from sql developers and after making some research, here is the query with desired result, maybe useful for someone, enjoy !
SELECT Student.StudentId, Student.Name, sum(MarksType.Marks) as TotalMarks
FROM
MarksType
INNER JOIN
MarksDetails on MarksType.MarksTypeId = MarksDetails.MarksTypeId
INNER JOIN
Student on Student.StudentId = MarksDetails.StudentId
group by Student.Name,Student.StudentId
we are facing a strange behaviour in SQLite (Version 3).
We have a table for Vehicles with two columns referencing an engine and a gear.
Of course there could be more than one vehicle with the same engine gear combination.
I now want to find the distinct combination of engines and gears of the vehicles (and use it for an insert => thats why randomblob(36)).
Example:
Vehicle | EngineId | GearId
-----------------------------
1 | 1 | 1
1 | 1 | 2
1 | 2 | 1
1 | 2 | 2
1 | 1 | 2
1 | 1 | 2
The following select statement results in too many rows:
Select randomblob(36), tmp.EngineId, tmp.GearId from (Select distinct EngineId, GearId from tblVehicle order by EngineId, GearId) as tmp;
RandomId| EngineId | GearId
-----------------------------
1 | 1 | 1
2 | 1 | 2
3 | 2 | 1
4 | 2 | 2
5 | 1 | 2
6 | 1 | 2
But the expected result would just be:
RandomId| EngineId | GearId
-----------------------------
1 | 1 | 1
2 | 1 | 2
3 | 2 | 1
4 | 2 | 2
If I replace the randomblob(36) with a constant, the result is as expected (of course without a random Id).
Select 2, tmp.EngineId, tmp.GearId from (Select distinct EngineId, GearId from tblVehicle order by EngineId, GearId) as tmp;
Can someone explain me this behaviour of SQLite? Is this the expected behaviour?
This is a bug.
I can reproduce this with SQLite 3.6.23.1 but not with 3.7.15, so it has been fixed already.