Writing a report and having an issue I was hoping someone could guide me in the right direction
I have 2 tables tbl_RegisterMain_Holding and LyndseyCouncils
In the tbl_RegisterMain_Holding there is 12 columns that have LA_id_Build (1 -12) in them
I want to be able to use the variable (5,12,23) = ‘East Midlands’ as the variable that will bring back any rows that have any of those numbers in.
Here is what the data looks like in the LyndseyCouncils table
LA_ID Listings
5 East Midlands
12 East Midlands
23 East Midlands
15 East of England
21 East of England
25 East of England
79 London
80 London
201 London
3 London and the Home Counties
11 London and the Home Counties
13 London and the Home Counties
352 North East
365 North East
372 North East
Here is my Query I’m working on
SELECT rm.Email,Forname,Surname,lc.listings
FROM dbo.tbl_RegisterMain_Holding AS rm
INNER JOIN [dbo].[LyndseyCouncils] AS lc
ON rm.LA_ID_live = lc.LA_ID
WHERE
(rm.LA_ID_BUILD1 = #COUNCIL or rm.LA_ID_BUILD2 = #COUNCIL or rm.LA_ID_BUILD3 = #COUNCIL
or rm.LA_ID_BUILD4 = #COUNCIL
or rm.LA_ID_BUILD5 = #COUNCIL or rm.LA_ID_BUILD6 = #COUNCIL
or rm.LA_ID_BUILD7 = #COUNCIL or rm.LA_ID_BUILD8 = #COUNCIL or rm.LA_ID_BUILD9 = #COUNCIL
or rm.LA_ID_BUILD10 = #COUNCIL
or rm.LA_ID_BUILD11 = #COUNCIL or rm.LA_ID_BUILD12 = #COUNCIL)
This would work if I was using just one La_ID but because I’m using multiple LA_ID It should be IN ()
But have no idea how I would write that,
Maybe I’m going about this in the wrong way.
Any help would be appreciated.
Thanks
Related
Data on Table:-
wkt Partners Team Opponent Runs Balls
1 S Hope & E Lewis WEST INDIES SOUTH AFRICA 43 66
2 S Hope & S Hetmyer WEST INDIES SOUTH AFRICA 70 79
3 D Bravo & S Hetmyer WEST INDIES SOUTH AFRICA 84 97
1 J Malan & Q Kock SOUTH AFRICA WEST INDIES 3 4
2 J Malan & F Plessis SOUTH AFRICA WEST INDIES 32 44
3 J Malan & R Dussen SOUTH AFRICA WEST INDIES 100 90
1 S Dhawan & R Sharma INDIA IRELAND 3 8
2 V Kohli & R Sharma INDIA IRELAND 102 70
I want to return the pair of partners, team they belong to, opponent they play against only once for each wkt where runs are highest for that particular wkt
For above table I'd like result as follow
wkt Partners Team Opponent Runs Balls
1 S Hope & E Lewis WEST INDIES SOUTH AFRICA 43 66
2 V Kohli & R Sharma INDIA IRELAND 102 70
3 J Malan & R Dussen SOUTH AFRICA WEST INDIES 100 90
Following is the code that I've used
SELECT wkt, Partners, Team, Opponent, max(Runs), Balls
FROM Partnerships
GROUP BY wkt
But I've been stuck with following error
Column 'Partnerships.Partners' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
How about row_number()?
select p.*
from (select p.*, row_number() over (partition by wkt order by runs desc) as seqnum
from Partnerships p
) p
where seqnum = 1;
i want to select a column from a table which can have another column reference many times.
select t1.name
from ccp.ENTITIES t1
Non
Albania
Australia
China
Czech Republic
Egypt
Germany
Greece
Group
Hungary
India
Ireland
Italy
Luxembourg
Malaysia
Malta
Netherlands
Portugal
Romania
Spain
Turkey
UK
US
this will give me a list of names of which i want 1 row from another table
v_networks_by_lm this table holds records with column t1.name and network. i want the column network only once for each item in the list. v_networks_by_lmcan hold many t1.name
entity name
a Spain
b Spain
c Spain
d Spain
e Spain
f Spain
g Spain
h Germany
i Germany
j Germany
k Germany
l Germany
m Germany
n Germany
o Germany
p UK
q Germany
r Spain
s Spain
t Portugal
u Portugal
v Portugal
q Portugal
from the above data which is in v_networks_by_lm i only want name returned once with any value of entity. but i want to pick the name from ENTITIES as it can be dynamic
I think aggregation does what you want:
SELECT MAX(n.network) as network, e.name
FROM ccp.ENTITIES e JOIN
ccp.v_networks_by_lm n
ON n.name = e.name
GROUP BY e.name;
Sounds like you want a subquery to get the single instance of name from the table, and then you do the join against entities.
Select sub.one_of_entity_values, sub.name
from ccp.entities e
inner join (
select max(entity) as one_of_entity_values, name
from v_networks_by_lm
group by name) sub on e.name = sub.name
I have been trying to build a view with multiple tables and everything works fine until that last Join. The reasoning behind creating mytickets and nextStep seperate, was to avoid locking issues as much as possible, as nextStep will be updated far less than the mytickets.
I just want to join the Assignlist to the rest of the tables where I can get 2392 Rows and not 99.330 rows
mytickets has state and country ->
Table Country has 250 Rows
country has Country.ISO and country.ID ->
CIDNo Continent SubContinent CountryName ISO
1 Africa North Africa Algeria DZA
2 Africa Middle Africa Angola AGO
3 Africa West Africa Benin BEN
4 Africa South Africa Botswana BWA
5 Africa West Africa Burkina Faso BFA
6 Africa East Africa Burundi BDI
Table [AssignedList] has 314 Rows
AssignedList has Country.ID, State
ALIDNo CIDNo State
1 1 Algeria
2 2 Angola
3 3 Benin
4 4 Botswana
5 5 Burkina Faso
6 6 Burundi
7 7 Cameroon
Table **MyTickets** has 2392
Table **NextStep** has 2392
When I try the following, I end up with 3000+ rows.
Thanks in advance.
SELECT tx.cSystem + '-' + tx.cTicketNo AS cSysTicket, tx.cTicketStatus,
tx.nBDO, tx.cTicketType,
tx.cCompany, tx.cState, tx.cCountry, tx.cLastStatus, DATEDIFF(MINUTE,
tx.dSpUpDate, GETDATE()) AS tSpTimer,
tx.cAddressSP, nx.nIdNo, tx.cW2Tech, DATEDIFF(MINUTE, tx.dUpLoad,
GETDATE()) AS dUploaded,
nx.cEmCode, DATEDIFF(MINUTE, GETDATE(), nx.dFollowUp) AS tNextStepTimer,
CONVERT(nvarchar(20),
nx.dFollowUp, 0) AS dFollowUp, nx.cComments, nx.nUserId,
--al.WorkBy1, al.WorkBy2, al.WorkBy3, al.WorkBy4, al.WorkBy5, al.WorkBy6
--CASE WHEN nx.nUserId = 0 THEN 'SYSTEM UPDATE' ELSE ag.FullName END AS
Agent, co.CIDNo
INNER JOIN dbo.NexStep AS nx
ON tx.cSystem + tx.cTicketNo = nx.cSystem + nx.cTicketNo
-- Runs with 2392 Rows
LEFT OUTER JOIN dbo.Agents AS ag
ON ag.AIDNo = nx.nUserId
-- Runs with 2392 Rows
INNER JOIN dbo.Country AS co
-- Runs with 2392 Rows
ON tx.cCountry = co.ISO
--FAILS HERE I end up with 99330 Rows
INNER JOIN dbo.AssignedList AS al
ON co.CIDNo = al.CIDNo
I replaced the last join with this and it worked perfectly where 107 = USA and 68 = CAN:
dbo.AssignedList AS al ON co.CIDNo = al.CIDNo AND CASE WHEN co.CIDNo IN (107, 68) THEN tx.cState ELSE '' END = CASE WHEN co.CIDNo IN (107, 68) THEN al.State ELSE '' END
I have this result
ZONE SITE BRAND VALUE
north a a_brand1 10
north a a_brand2 15
north a a_brand3 27
south b b_brand1 17
south b b_brand2 5
south b b_brand3 56
Is there any way to add a column wih the sum grouped by zone, and site? like this: Total site a = 10+15+27 = 52 and total site b = 17+5+56 = 78
ZONE SITE BRAND VALUE TOTAL_IN_SITE
north a a_brand1 10 52
north a a_brand2 15 52
north a a_brand3 27 52
south b b_brand1 17 78
south b b_brand2 5 78
south b b_brand3 56 78
Thanks.
Use sum window function.
select t.*,sum(val) over(partition by zone,site)
from tbl t
I've searched but found nothing that could help.
I have the following table in a SQL Server 2005 database:
Parent Child Value
---- -------- ---------
America Mexico 8
America Canada 1
Asia Japan 5
Asia Korea 7
Europe Spain 0
Europe Italy 2
Africa Zimbabwe 1
Mexico Baja California 0
America USA 3
USA California 1
USA Texas 2
Parent and Child are Primary Key, value is not important (IMO). I would like to create a view that results in something like this:
Parent Child Value
---- -------- ---------
America USA 3
USA California 1
USA Texas 2
I would search for America, and the result will give back every nested child there is, recursively, no matter how many it has, since I could include cities, localities, etc.
What I need is similar to what some call a BOM explosion.
Here is how you can do it:
with cte as (
select parent, child
from t
union all
select cte.parent, t.child
from cte join
t
on cte.child = t.parent
)
select cte.*
from cte
where parent = 'America';
Here is a small SQL Fiddle example.