SQL - Count with distinct on ID - sql

Here is an example of my table structure:
Key - Country - Store
1 - Germany- YYY
1 - Germany- YYY
2 - France- XXX
2 - France- XXX
2 - France- XXX
3 - United Kingdom- YYY
3 - United Kingdom- YYY
4 - Germany- YYY
5 - France- YYY
5 - France- YYY
I would like to start a query on this table to get the following result:
Country- XXX - YYY
Germany - 0 - 2
France - 1 - 1
United Kingdom - 0 - 1
The problem for me ist the ID/Key which does not get used once but several times, thats why I cant just use a count query.
What should my query look like?

You should be able to use the TRANSFORM function to get this result:
TRANSFORM Count(store)
SELECT Country
FROM
(
select distinct key, country, store
from yourtable
) d
GROUP BY Country
PIVOT Store

Related

how to check and match data in column1 inside table 1 with column2 inside table 2 and get the updated values in side table 3

how to check and match data in column1 inside table 1 with column2 inside table 2 and get the updated values in side table 3
table 1
ID name: status : age
1 john F 28
2 peter G 20
3 Roger K 67
Table 2:
ID name: status : age
1 john Y 28
2 peter J 20
3 Roger K 67
4 trump U 120
5 Donald F 450
Table 3 should contain the updated values
1 john Y 28
2 peter J 20
3 Roger K 67
I need to get the updated status of IDs present in table 1 in table 3 how can I do that.
Note: I am using exacttarget SQL activity and update and many more functionalities does not work so I need some work around> I have tried this but this does not work.
UPDATE
1C-C1-MatchStatus_72hoursSubscribers
SET
1C-C1-MatchStatus_72hoursSubscribers.current_status = B.current_status
FROM
1C-C1-MatchStatus_72hoursSubscribers A
INNER JOIN
a_query B
ON
A.current_status = B.current_status

DB Query matching ids and sum data on columns

Here is the info i have on my tables, what i need is to create a report based on certain dates and make a sum of every stock movement of the same id
Table one Table Two
Items Stocks
---------- ---------------------------
ID - NAME items_id - altas - bajas - created_at
1 White 4 5 0 8/10/2016
2 Black 2 1 5 8/10/2016
3 Red 3 3 2 8/11/2016
4 Blue 4 1 4 8/11/2016
2 10 2 8/12/2016
So based on a customer choice of dates (on this case lets say it selects all the data available on the table), i need to group them by items_id and then SUM all altas, and all bajas for that items_id, having the following at the end:
items_id altas bajas
1 0 0
2 11 7
3 3 2
4 6 4
Any help solving this?
Hope this will help:
Stock.select("sum(altas) as altas, sum(bajas) as bajas").group("item_id")

SQLQuery COUNT number of games per team

I have 4 tables:
Teams
codTeam: 1
year: 1995
codYears: 1
codType: 1
name: FCP
points: 3
codTeam: 2
year: 1990
codYears: 1
codType: 1
name: SLB
points: 3
codTeam: 3
year: 1995
codYears: 3
codType: 2
name: BCP
points: 0
Trainers (People who train a team)
codTrainer: 1
name: Peter
street: Ghost street
cellphone: 252666337
birthdayDate: 1995-02-01
BI: 11111111
number: 121212121
codTrainer: 1
name: Pan
street: Ghost street Remade
cellphone: 253999666
birthdayDate: 1995-01-01
BI: 22222222
number: 212121212
TeamsTrainers (In which team is someone training)
codTeamTrainer: 1
codTeam: 1
codTrainer: 2
dataInicio: 1998-05-05
codTeamTrainer: 2
codTeam: 2
codTrainer: 2
dataInicio: 1998-06-07
codTeamTrainer: 3
codTeam: 2
codTrainer: 1
dataInicio: 1999-09-09
Games
codGame: 1
date: 2015-02-12 13:00:00
codTeamHome: 1
codTeamAgainst: 2
goalsHome: 3
goalsAgainst: 2
codTypeGame: 1
codGame: 2
date: 2015-02-12 15:00:00
codTeamHome: 2
codTeamAgainst: 1
goalsHome: 1
goalsAgainst: 2
codTypeGame: 3
So basically I want to:
Get the table Games and show:
Team Name | Trainer Name | Goals Home | Goals Against | Points | Ammout of Games from the Home Team
I have the following code for that in SQLQuery:
SELECT Teams.name, Trainers.name, Games.goalsHome,
Games.goalsAgainst, Teams.points, COUNT(*)
FROM Teams, Trainers, Games, TeamsTrainers
WHERE Games.codTeamHome = Teams.codTeam AND
TeamsTrainers.codTeam = Teams.codTeam AND
TeamsTrainers.codTrainer = Trainers.codTrainer
GROUP BY Teams.name, Trainers.name, Games.goalsHome,
Games.goalsAgainst, Teams.points
(May have some errors as I translated)
Yet, the COUNT only shows 1 (Probably because on the WHERE it has "teamHome" so it only counts 1), yet, if it's because of that, how do I fix it?
Result:
FCP | Pan | 3 | 2 | 3 | 1 (Count)
SLB | Peter | 1 | 2 | 3 | 1 (Count)
SLB | Pan | 1 | 2 | 3 | 1 (Count)
It should be 2 for each one on the Count
Any idea?
The reason you get wrong result is of wrong joing data type. You should use repsectivelly: left, right or inner join instead of joing data via using where clause. Your data model provides 1 to N relationship, so you should use specific type of join.
See: http://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins
EDIT
SELECT Te.name, Tr.name, Ga.goalsHome, Ga.goalsAgainst, Te.points,
(SELECT COUNT(*)
FROM Games
WHERE codTeamHome = Te.codTeam OR codTeamAgainst = Te.codTeam)
AS CountOfGames
FROM TeamsTrainers AS Tt
LEFT JOIN Teams AS Te ON Tt.codTeam = Te.codTeam
LEFT JOIN Trainers AS Tr ON Tt.codTrainer = Tr.codTrainer
LEFT JOIN Games AS Ga ON Ga.codTeamHome = Te.codTeam
SQL Fiddle
You can change your WHERE clause by saying
[what you have] OR (Games.codTeamAgainst = Teams.codTeam AND ...)
However, this probably causes other problems because you probably care about whether a particular team scores the goals, not whether the home team scores the goals in games that team plays on either side.
You might not notice the other problems for a while because your GROUP BY clause is probably pretty far from what you want, and you might want to be selecting aggregate functions for a much simpler grouping.

Updation in tables involving multiple conditions (one to many relationship)

I have created three tables like Product, Brands and ProductBrands in ms sql server
These tables contains data as the following
Products (PId - PName - PDescription)
1 - Mobiles - blah blah
2 - T.V - blah blah
3 - A.C - blah blah
Brands (BId - BName)
1 - Samsung
2 - Sony
3 - L.G
4 - Apple
ProductsBrands (PBId - PId - BId)
1 - 1 - 1
2 - 1 - 2
3 - 1 - 3
4 - 2 - 1
5 - 2 - 3
...
...
Now I have to perform update operation on Product tables on the following conditions
If user will select product "let's say Mobile", and he will select Samsung and L.G only
so for this requirement, the updation in ProductsBrands should be like this
ProductsBrands (PBId - PId - BId)
1 - 1 - 1
{2 - 1 - 2} should be deleted
3 - 1 - 3
...
...
If user will select product "let's say Mobile", and he will select Apple
so for this requirement, the updation in ProductsBrands should be like this
ProductsBrands (PBId - PId - BId)
1 - 1 - 1
2 - 1 - 2
3 - 1 - 3
4 - 2 - 1
5 - 2 - 3
6 - 1 - 4 {new entry is made}
If user will select product "let's say Mobile", and he will select Samsung, L.G and Apple
so for this requirement, the updation in ProductsBrands should be like this
ProductsBrands (PBId - PId - BId)
1 - 1 - 1
{2 - 1 - 2} should be deleted
3 - 1 - 3
4 - 2 - 1
5 - 2 - 3
6 - 1 - 4 {new entry is made}
Now I am very very confused how to write stored procedure for all above conditions
Please help me!!!
Your scenario was a little ambiguous for me. So lets simplify it as follow, and check if I got it correct or not:
You want to Insert or Delete or even Update a record or a set of records in a table(let say tbl1), whenever a condition(let say con1) is correct(let say if con1 is correct then Insert should me made, otherwise Delete or Update should be made).
If this is what you are looking for, then you could make a use of MERGE statement(Read More).
If this is what you are looking for, and you have problem with MERGE statement, please tell us to clarify it.
Other links:Here and Here

How to select faulty records?

I'm investigating an error in one of our tables of a geographical database. Given the table below, the DistrictName and DisId should always have the same combination (i.e. Bronx = 11, Manhatten = 14), but some records have a different DisId (while still sharing the the same DistrictName).
Id DistrictName DisId Section
------------------------------------------------
1 Bronx 11 1
2 Bronx 11 2
3 Brooklyn 12 1
4 Brooklyn 13 2 //wrong
5 Manhatten 14 1
6 Manhatten 14 2
7 Queens 15 1
8 Queens 16 2 //wrong
9 Queens 17 3 //wrong
How can I select all faulty records in a query?
There is always a Section 1, so records with a section > 1 containing the same DistrictName but having a deviating DisId are the ones I'm looking for.
I've tried using a group by (districtname) but I'm having difficulties comparing with the section1 record. I'm kind of lost when it comes to putting the logic in the having or where clause. Any help appreciated!
select * from your_table
where section > 1
and districtname in
(
select districtname
from your_table
group by districtname
having count(distinct disid) > 1
)