i have two tables,
Table 1
---------------------------
Col1 Col2
--------------------------
A1 D
A2 D
A1 W
A3 D
A4 W
A2 W
A5 D
A1 D
A2 W
A3 W
Table 2
------------------------
Col1 Col2
-------------------------
A1 Name1
A2 Name2
A3 Name3
A4 Name4
A5 Name5
I want to get the count of all rows with D and W values in the following format..
-------------------------------------------------------
Name Count_D Count_W
-------------------------------------------------------
Name1 2 1
Name2 1 2
Name3 1 1
Name4 0 1
Name5 1 0
----------------------------------------------------------------
Is it possible to achieve this through query..Please help to write query for this
5 1480
hi try this...
select * from(
select col1=t2.col2,col2=t1.col2
from table_1 t1
join table_2 t2
on t1.col1 = t2.col1
) y
pivot(count(col2) for [col2] in (d,w))as te
output is:
-------------
col1 D W
name1 2 1
name2 1 2
name3 1 1
name4 0 1
name5 1 0
hi try this...
select * from(
select col1=t2.col2,col2=t1.col2
from table_1 t1
join table_2 t2
on t1.col1 = t2.col1
) y
pivot(count(col2) for [col2] in (d,w))as te
output is:
-------------
col1 D W
name1 2 1
name2 1 2
name3 1 1
name4 0 1
name5 1 0
Hello,
There is something missing in this query..it gave me an error saying "Incorrect syntax near 'pivot'."..Please will u check the query once more..
Hi,
This is an alternate way that should work. -
-
select a.col1 "NAME",isnull(b.cnt ,0) "Count_D",isnull( c.cnt ,0) "Count_W" from table_2 a
-
left outer join
-
(select col1,col2,count(*) cnt from table_1 where col2='D' group by col1,col2) b
-
on a.col2=b.col1 left outer join
-
(select col1,col2,count(*) cnt from table_1 where col2='W' group by col1,col2)c
-
on a.col2=c.col1
-
-
Hope you problem is solved
Cheers
Its working ...i changed the column name.............
try this..
select * from(
select c1=t2.col2,c2=t1.col2
from table_1 t1
join table_2 t2
on t1.col1 = t2.col1
) y
pivot(count(c2) for [c2] in (d,w))as te
Its working ...i changed the column name.............
try this..
select * from(
select x1=t2.col2,x2=t1.col2
from table_1 t1
join table_2 t2
on t1.col1 = t2.col1
) y
pivot(count(x2) for [x2] in (d,w))as te
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Somerset Bob |
last post by:
I've posted my query at the phpbb forum, where I got half an answer and then
no more. I posted it again at another phpbb forum, where I got half an
answer and no more. I posted it in alt.php, where...
|
by: Somerset Bob |
last post by:
I've posted my query at the phpbb forum, where I got half an answer and then
no more. I posted it again at another phpbb forum, where I got half an
answer and no more. I posted it in alt.php, where...
|
by: Orion |
last post by:
Hi, This is kind of last minute, I have a day and a half left to figure
this out. I'm working on a project using ms-sqlserver. We are
creating a ticket sales system, as part of the system, I...
|
by: jimh |
last post by:
I'm not a SQL expert. I want to be able to write a stored procedure
that will return 'people who bought this product also bought this...'.
I have a user table that links to a transaction table...
|
by: jaggee |
last post by:
Hello,
This is regarding log analysis of a web system, I am finding my backend SQL programming has taken so much of time to process the application due to following quires for a log table having...
|
by: Grammie |
last post by:
I need a report that looks like this:
1. Number of Enrollees as of Today: (count of everyone in
query){can count last names)
2. Total Active People (count of everyone with "A" as...
|
by: Jo |
last post by:
Hi there,
I'm Jo and it's the first time I've posted here. I'm in process of creating
a database at work and have come a little unstuck.....I'm a bit of a novice
and wondered if anyone could...
|
by: wb5plj |
last post by:
Hi I am having a problem passing some sql to the db2cmd via ant. This
is very confusing as I am doing this exact thing elseware with no
problem (just differant sql, and I have verified the sql is...
|
by: troy_lee |
last post by:
I have two fields on a form. These two fields' values are based on an
expression and represent a date range. I need to create a SQL
statement that will use the returned values of these two fields...
|
by: troy_lee |
last post by:
I am trying to count the total number of units for a given part number
that have a Priority rating of 1. Based upon some research, this is
what I came up with for my query. Access says that I have...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |