I've written the following query:
SELECT [User], count(*) as Records
FROM dbo.PBBuilds
GROUP BY [User]
ORDER BY [Records] DESC
It selects a USERID and counts the amount or rows associated as 'records'.
--------------------------------------------------
| Users | records |
------------------------------------------------- |
| 0ca3f535-96f2-4926-9b4d-25fb2aa713eb | 3 |
| a32d1784-d636-4caf-bb9d-60bfbfc1aadf | 2 |
| 5fabb0aa-f61f-4180-90f6-5dd1650fd0db | 1 |
---------------------------------------------------
The Users column relates to an ID column in another table where the users name is stored. How can I retrieve and match the data by this link?
Assuming your other table is called other_table and also has the ID in column User, then:
SELECT UserName, COUNT(*) Records
FROM dbo.PBBuilds b INNER JOIN other_table o
on b.User = o.User
GROUP BY UserName
ORDER BY COUNT(*) DESC
Related
I tried searching this kind of use case on google but did found exact what i was looking so, Need help to reach to specific point by useful answer, docs or any reference
I have this situation: table A has to be joined with table B and table C; B and C have similar columns, so there will be duplicate column names in select part, however give preferences to all the data of tables B if available otherwise show data from table c
For example:
SELECT
ae.*, ml.name as name, ml.contact AS contact,
ve.name AS name, ve.contact AS contact
FROM
TABLE ae
LEFT JOIN
TABLE ml ON ae.eid = ml.eid
LEFT JOIN
TABLE ve ON ae.eid = ve.eid
WHERE
ae.eid = 1
ml data
eid | name | contact
----+------+--------
1 | xyz | null
ve data
eid | name | contact
----+------+--------
1 | xyz | 1
ae data
eid | gender
----+--------
1 | male
I want this result:
eid | gender | name | contact
----+--------+------+--------
1 | male | xyz | null
But I am getting this for now:
eid | gender | name | contact | contact
----+--------+------+---------+--------
1 | male | xyz | 1 | null
I'm using node-mssql driver for querying SQL Server data.
Thanks,
You must join ve only if there is no matching row in ml and you do it if you add in the ON clause the condition ... AND ml.eid IS NULL.
Also use COALESCE() to select the columns from ml first and if they don't exist from ve:
SELECT ae.*,
COALESCE(ml.name, ve.name) AS name,
COALESCE(ml.contact, ve.contact) AS contact
FROM ae
LEFT JOIN ml ON ae.eid = ml.eid
LEFT JOIN ve ON ae.eid = ve.eid AND ml.eid IS NULL
WHERE ae.eid = 1
See the demo.
Results:
eid
gender
name
contact
1
male
xyz
null
My database has a table of Products, like so:
PRODUCTS
----------------------------
id | name | suppliers
----------------------------
1 | widget | {1,2}
2 | gizmo | {1}
3 | geegaw | {3}
4 | tchotchke | null
The suppliers column contains an array (numeric[]) of IDs belonging to a table of suppliers:
SUPPLIERS
------------
id | name
------------
1 | alpha
2 | beta
3 | gamma
How can I write a query that will return the contents of PRODUCTS except with an array of supplier names instead of supplier ID numbers? Result should look like this:
-----------------------------------
id | name | suppliers
-----------------------------------
1 | widget | {'alpha','beta'}
2 | gizmo | {'alpha'}
3 | geegaw | {'gamma'}
4 | tchotchke | null
Succinct and efficient methods would be preferred, but readability/understandability are also nice.
Edit: This isn't a duplicate of the linked question, although that question does involve the unnest operation, it doesn't re-aggregate the result. The answer to this question makes a new contribution to the site.
select t1.id, t1.name, array_agg(s.Name) as Suppliers
from Products t1
left join lateral (
select unnest(suppliers) as supplierId from myProducts t2
where t1.id = t2.id) as t on true
left join Suppliers s on s.Id = t.supplierID
group by t1.id, t1.name;
What was I thinking. Here is a better version:
select p.id, p.name, array_agg(s.Name) as Suppliers
from (select *,unnest(suppliers) as supplierId from Products) p
left join Suppliers s on s.Id = p.supplierID
group by p.id, p.name;
I have table with Employees (tblEmployee):
| ID | Name |
| 1 | Smith |
| 2 | Black |
| 3 | Thompson |
And a table with Roles (tblRoles):
| ID | Name |
| 1 | Submitter |
| 2 | Receiver |
| 3 | Analyzer |
I have also a table with relations of Employees to their Roles with many to many relation type (tblEmployeeRoleRel):
| EmployeeID | RoleID |
| 1 | 1 |
| 1 | 2 |
| 2 | 1 |
| 2 | 2 |
| 2 | 3 |
| 3 | 3 |
I need to select ID, Name from tblEmployee that have exaclty the same set of roles from tblEmployeeRoleRel as has the Employee with ID = 1. How can I do it?
Use a where clause to limit the roles you're looking at to those of employeeID of 1 and use a having clause to make sure that the employee's role count matches that of employee1.
SELECT A.EmployeeID
FROM tblEmployeeRoleRel A
WHERE Exists (SELECT 1
FROM tblEmployeeRoleRel B
WHERE B.EmployeeID = 1
and B.RoleID = A.RoleID)
GROUP BY A.EmployeeID
HAVING count(A.RoleID) = (SELECT count(C.RoleID)
FROM tblEmployeeRoleRel C
WHERE EmployeeID = 1)
This assumes that employeeID and roleID are unique in tblEmployeeRoleRel otherwise we may have to distinct the roleID fields above.
Declare #EmployeeID int = 1 -- change this to whatever employee ID you like, or perhaps you'd pass an Employee ID to it in a stored procedure.
Select Distinct e.EmployeeID -- normally distinct would incur extra overhead, but in this case you only want the employee IDs. not using Distinct when an employee has multiple roles will give you multiple employee IDs.
from tblEmployeeRoleRel as E
where E.EmployeeID not in
(Select EmployeeID from tblEmployeeRoleRel where RoleID not in (Select RoleID from tblEmployeeRoleRel where Employee_ID = #EmployeeID))
and exists (Select EmployeeID from tblEmployeeRoleRel where EmployeeID = e.EmployeeID) -- removes any "null" matches.
and E.Employee_ID <> #Employee_ID -- this keeps the employee ID itself from matching.
This might be a simple question and answer but I am just stuck. =(
Lets say we have a Access Database with items ordered by users saved
ID | Product Number | Product Name
-----------------------------------
1 | 00001 | Some Name 1
-----------------------------------
2 | 00001 | Some Name 1
-----------------------------------
3 | 00002 | Some Name 2
-----------------------------------
4 | 00003 | Some Name 3
-----------------------------------
5 | 00003 | Some Name 3
-----------------------------------
What I'd like is the code to
Open the db <-- Got that part down.
Read the table with the said data listed above. <--Got this down
Need Help : Report the most common Product number ordered.
I have messed with a few idea's but with no success. The data will be reported to a listview.
Thanks..
try this
Command.CommandText = "SELECT COUNT([Product Number]) AS CNT, [Product Number] FROM TableXXX GROUP BY [Product Number] ORDER BY COUNT([Product Number]) DESC"
You could just include most of this in your query to the database, like this (where t1 is your table name):
SELECT [Product Number], count(*) as TotalOrders
FROM t1
GROUP BY [product number]
ORDER BY count(*) desc
That will automatically count and order the Product Numbers.
I have a postgres database with duplicated entries on one of the table. I would like to show the created_by columns
Table1
id | number
1 | 123
2 | 124
3 | 125
4 | 126
Table2
id | number | created_on
1 | 123 | 3/29
2 | 123 | 4/3
3 | 124 | 3/31
4 | 124 | 4/1
On table 2 number are duplicated. I would like to form a single query to list the following:
id | number | created_on
1 | 123 | 4/3
2 | 124 | 4/1
For duplicated entries only the latest entry will be included. How could I form that SQL query?
SELECT DISTINCT ON (Table1.number) Table1.id, Table2.number, Table2.create_on FROM Table1
JOIN Table2 ON Table1.number=Table2.number
ORDER BY Table2.create_on;
Actually I tried putting 'DISTINCT ON' and 'ORDER BY' in a single query (with JOIN) it gives me the following error:
SELECT DISTINCT ON expressions must match initial ORDER BY expressions
The columns in DISTINCT ON() have to be the first ones in the ORDER BY query, also if you want the latest created_on date you should order by created_on DESC
SELECT DISTINCT ON (Table1.number) Table1.id, Table2.number, Table2.created_on
FROM Table1
JOIN Table2
ON Table1.number=Table2.number
ORDER BY Table1.number,Table2.created_on DESC;
http://sqlfiddle.com/#!12/5538a/2
As you said in the comment: created_on=date_trunc('day', now()), so the data type of the field created_on is timestamp. Here is what you can do:
SELECT table_1.id, table_1.number, max(created_on) as created_on
FROM table_1
inner join table_2 using(number)
group by table_1.id, table_1.number