Obtaining groups and counts at the same time - mysql

Suppose my table looks like this:
Name City Salary
n1 c1 10
n1 c1 20
n1 c1 30
n2 c2 20
n2 c2 50
n3 c2 70
And I am interested in the following output:
Name Count
n1 3
n2 2
n3 1
I am fairly new to SQL and have been doing similar commands in SAS (e.g. in this case I would have used PROC FREQ) - but I am now required to write the same code only in the SQL language (I am using Aginity Netezza).

If its MySQL, or any fairly standard SQL:
SELECT Name, Count(*)
FROM table
GROUP BY Name

Related

Join table A into table B multiple times

I have 2 tables in a mysql db (simplified example data):
Table 1: 3 names (unique)
Table 2: names (only from table 1), 3 dates, info
For each date there are sometimes all 3 names in table 2, sometimes less then 3.
Example for table 2:
d1 n1 info
d1 n2 info
d1 n3 info
d2 n1 info
d2 n3 info
d3 n3 info
Date 1 has got all 3 names, date 2 has got 2 names, date 3 hast got 1 name.
Goal: I need each "date" to have all 3 names. I can already filter 1 date from table 2 and join the tables successfully to have the desired outcome for one date, but how can I "add" all names to each date?
d1 n1 info
d1 n2 info
d1 n3 info
d2 n1 info
d2 n2 (added by the join, "info" is empty)
d2 n3 info
d3 n1 (added by the join, "info" is empty)
d3 n2 (added by the join, "info" is empty)
d3 n3 info
My real data has got much more names and dates, which makes individual joins impractical. It feels like there should be an easy solution to this, but I could not find any.
I also could do this with code ("for each date add missing names") , but I wonder if it can be done with sql
Working on MySQL 10.1.37
Use CROSS JOIN to create all possible combinations of names and dates, followed by a LEFT JOIN:
SELECT name_table.name, datelist.date, info_table.info
FROM name_table
CROSS JOIN (SELECT DISTINCT date FROM info_table) AS datelist
LEFT JOIN info_table ON name_table.name = info_table.name AND datelist.date = info_table.date

mySql - Select but don't show duplicates of specific row

I need to SELECT from a db and don't show rows that are identical in SOME rows (or actually hide the rows that are identical to eachother except 1 item...)
Let's give an example:
ID C1 C2 C3
1 3 3 4
1 5 5 4
1 2 3 4
1 6 5 4
1 2 3 4
After SELECT i want:
ID C1 C2 C3
1 X 3 4
1 X 5 4
where "X" has no importance...i have to show that column but i don't care which one is shown.
Is this possible with a simple SELECT query?
To sum up, if i ask the question regarding this specific example, What can i do to SELECT from that table and show only one of the rows if it has identical duplicates in ID, C2 and C3?
SIDENOTE: this MYSQL: SELECT Method - but don't show duplicates / GROUP or DISTINCT? doesn't help.
Have you tried GROUP BY?
SELECT Id, MAX(C1), C2, C3
FROM SomeTable
GROUP BY Id, C2, C3
Since you don't care about C1, in this query, I get the largest C1 value.

Sql, print only rows with a value that appear at least n times

I have two tables created in sql.
players(code(PK),surname,name)
and
errors(playererrorcode(PK),date,money,code(FK to players))
So two tables looks like:
Players:
A1 Surname1 Name1
A2 Surname2 Name2
And errors for example:
E1 2015/7/10 10$ A1
E2 2015/2/20 50$ A1
E3 2015/1/30 40$ A2
E4 2015/5/20 30$ A1
Well, now, i have to print(select in this language) ONLY players that have at least 3 errors! In this case i want to print only the row that include: surname and name of who have at least 3 errors, then it have to print only surname1 and name1.
How to do that?
In SQL Server you can use the Count aggregation to get the number of errors per player and use Having to specify the number of error required -- like this:
SELECT
p.surname,
p.name,
FROM players as p
INNER JOIN errors as e
on p.code=e.code
GROUP BY
p.surname,
p.name
HAVING
COUNT(e.playererrorcode)>=3

SSRS Adjacent grouping in matrix report

I have a question related to SSRS matrix report. I have data captured in the following format in the table:
Category Name Month Cost
C1 N1 M1 10
C1 N1 M2 20
C1 N1 M3 30
C1 N2 M1 40
C1 N2 M2 50
C1 N2 M3 60
C2 N3 M1 70
C2 N3 M2 80
C2 N3 M3 90
So basically it captures the category, the product name and costs for various months.
What I want to have in my report is something like this:
Category Name M1 M2 M3
C1 N1 10 20 30
C1 N2 40 50 60
C1 N3 70 80 90
So this is similar to pivot on months with Category and Name as static columns. When I try to create two groups, Category and Name, in the matrix report, the Category becomes the parent group and the report groups all the products in common categories. For example I am getting something like this:
Category Name M1 M2 M3
C1 N1 10 20 30
N2 40 50 60
C1 N3 70 80 90
I don’t want above grouping. I want category c1 to be repeated for N1 and N2.
I am kind of novice in matrix report and maybe I missing some obvious group settings. Can someone please help me in this?
EDIT: I am using SSRS 2008 R2
Thanks & Regards
AK
When you create a matrix style Tablix in SSRS it will look like this be default:
The reason C1 is displayed like this is that by default group headers are not added to the Tablix body area and are only shown once per group.
Note that Category and Name are to the left of the dotted lines:
See Understanding the Tablix Data Region.
Delete the two left columns. When prompted, choose Delete columns only:
Now, add two columns like below:
The tablix now looks like this:
Note there is no longer dotted lines between Name and Month, i.e. everything is in the Tablix body region.
Now the end result looks like your requirement:

Make a multiplication of two tables in SQL Server 2008

If you have two SQL tables and you see them like matrices, The product C of two matrices A and B is defined as
c_ik = a_ij*b_jk
where j is summed over for all possible values of i and k and the notation above uses the Einstein summation convention. The implied summation over repeated indices without the presence of an explicit sum sign is called Einstein summation, and is commonly used in both matrix and tensor analysis. Therefore, in order for matrix multiplication to be defined, the dimensions of the matrices must satisfy
so
where
for example
a1 a2 a3
----------
14 9 4
2 11 5
0 12 17
5 2 3
b1 b2
-------
12 25
9 10
8 5
is equals to
c1 c2
---------
273 455
243 235
244 205
102 160
I was thinking in doing a While loop, but I guess there is a better way to do this
maybe using pivot or a CTE with coords of i,j,k?
DECLARE #count int
DECLARE #max int
SET #count int = 1
SET #max = 4
WHILE #count < #max
BEGIN
INSERT INTO C_Table (c1, c2)
--Do not know how to make the other products or how to get them...
Select Sum( (T1.Value * T2.Value) )
From A As T1
Join B As T2
...
SET #count = #count + 1
END
I am a little lost here...