Folks
I have three select statements. I want to display q_text based on
the respective where condition. How do i combine these three and write
as one select statement.
select q_text Questions from question
where new_account_flag = '1'
select q_text Questions from question
where disc_account_flag = '1'
select q_text Questions from question
where disc_account_flag = '0'
Remember that all the queries returns more than 1 value.
I tried to use
select (query1),
(query2),
(query3)
but because it is returning more than one value, there is error.
Can any suggest me any other syntax??
Thanksselect [q_text Questions] from question
where new_account_flag = '1'
UNION
select [q_text Questions] from question
where disc_account_flag = '1'
UNION
select [q_text Questions] from question
where disc_account_flag = '0'|||I think it should be UNION ALL, since either condition may produce duplicate results. UNION will eliminate duplicates.sqlsql
Showing posts with label write. Show all posts
Showing posts with label write. Show all posts
Tuesday, March 27, 2012
Thursday, March 22, 2012
Combine two queries
Hello, i am an SMS guy trying to write some SQL queries and having little
luck. I have two queries. The first returns the dept code which in my case
is the first 2 letters a computer and a count of computers with that dept
code(see below).
Total counts by dept code
SELECT LEFT(Name0, 2) AS [dept Code], COUNT(*) AS [Total Machines]
FROM v_R_System
GROUP BY LEFT(Name0, 2)
Second is a query that returns the dept code and a count of machines that
have the client installed.
Totals counts installed by dept code
SELECT LEFT(Name0, 2) AS [dept Code], COUNT(*) AS [Total Machines]
FROM v_R_System where client0=1
GROUP BY LEFT(Name0, 2)
My question is this... How the heck can I combine the two into one query so
I return Dept code, total machines, and total machines with client?
Thanks for helping out this rookie...
scottYou can use a CASE like:
SELECT LEFT( Name0, 2 ) AS "dept_code",
COUNT( * ) AS "total_machines",
SUM( CASE WHEN client0 = 1 THEN 1 ELSE 0 END ) AS "client0_count"
FROM v_R_System
GROUP BY LEFT( Name0, 2 ) ;
Anith|||Try,
SELECT
LEFT(Name0, 2) AS [dept Code],
client0,
COUNT(*) AS [Total Machines],
(select count(*) from v_R_System as t1 where LEFT(t1.Name0, 2) =
LEFT(v_R_System.Name0, 2)) as total_dept_comp
FROM
v_R_System
GROUP BY
LEFT(Name0, 2),
client0
order by
LEFT(Name0, 2),
client0
AMB
"scott" wrote:
> Hello, i am an SMS guy trying to write some SQL queries and having little
> luck. I have two queries. The first returns the dept code which in my ca
se
> is the first 2 letters a computer and a count of computers with that dept
> code(see below).
> Total counts by dept code
> SELECT LEFT(Name0, 2) AS [dept Code], COUNT(*) AS [Total Machines]
> FROM v_R_System
> GROUP BY LEFT(Name0, 2)
> Second is a query that returns the dept code and a count of machines that
> have the client installed.
> Totals counts installed by dept code
> SELECT LEFT(Name0, 2) AS [dept Code], COUNT(*) AS [Total Machines]
> FROM v_R_System where client0=1
> GROUP BY LEFT(Name0, 2)
> My question is this... How the heck can I combine the two into one query
so
> I return Dept code, total machines, and total machines with client?
> Thanks for helping out this rookie...
> scott
>sqlsql
luck. I have two queries. The first returns the dept code which in my case
is the first 2 letters a computer and a count of computers with that dept
code(see below).
Total counts by dept code
SELECT LEFT(Name0, 2) AS [dept Code], COUNT(*) AS [Total Machines]
FROM v_R_System
GROUP BY LEFT(Name0, 2)
Second is a query that returns the dept code and a count of machines that
have the client installed.
Totals counts installed by dept code
SELECT LEFT(Name0, 2) AS [dept Code], COUNT(*) AS [Total Machines]
FROM v_R_System where client0=1
GROUP BY LEFT(Name0, 2)
My question is this... How the heck can I combine the two into one query so
I return Dept code, total machines, and total machines with client?
Thanks for helping out this rookie...
scottYou can use a CASE like:
SELECT LEFT( Name0, 2 ) AS "dept_code",
COUNT( * ) AS "total_machines",
SUM( CASE WHEN client0 = 1 THEN 1 ELSE 0 END ) AS "client0_count"
FROM v_R_System
GROUP BY LEFT( Name0, 2 ) ;
Anith|||Try,
SELECT
LEFT(Name0, 2) AS [dept Code],
client0,
COUNT(*) AS [Total Machines],
(select count(*) from v_R_System as t1 where LEFT(t1.Name0, 2) =
LEFT(v_R_System.Name0, 2)) as total_dept_comp
FROM
v_R_System
GROUP BY
LEFT(Name0, 2),
client0
order by
LEFT(Name0, 2),
client0
AMB
"scott" wrote:
> Hello, i am an SMS guy trying to write some SQL queries and having little
> luck. I have two queries. The first returns the dept code which in my ca
se
> is the first 2 letters a computer and a count of computers with that dept
> code(see below).
> Total counts by dept code
> SELECT LEFT(Name0, 2) AS [dept Code], COUNT(*) AS [Total Machines]
> FROM v_R_System
> GROUP BY LEFT(Name0, 2)
> Second is a query that returns the dept code and a count of machines that
> have the client installed.
> Totals counts installed by dept code
> SELECT LEFT(Name0, 2) AS [dept Code], COUNT(*) AS [Total Machines]
> FROM v_R_System where client0=1
> GROUP BY LEFT(Name0, 2)
> My question is this... How the heck can I combine the two into one query
so
> I return Dept code, total machines, and total machines with client?
> Thanks for helping out this rookie...
> scott
>sqlsql
Monday, March 19, 2012
Combine Fields
Hi there!
I have a table with three columns: Id, Name and Departament. (ex: 23, "Mary", "Check-out")
I'd like to write a SQL Select clause so that the three columns are combined in just one column (ex: "23 - Mary - Check-out")
Is it possible? Many thanks for any kind of help.
Hi,
assuming ID is int
SELECT cast(ID as char(5)) + ' - ' + Name + ' - ' + Department AS TheNameYouLike
FROM YourTableName
|||try this
SELECT CAST(ID AS varchar(5))+'-'+ltrim(Rtrim(Name))+'-'+ltrim(rtrim(Department) From yourtablename
Madhu
Wednesday, March 7, 2012
Column Summary
HI,
am new to sql and trying to write a query to summarise data in a table.The table contains a list of documents, their type and author
DocName DocType Author
Name1 letter tom
Name2 essay rick
Name3 essay rick
Name4 letter arnold
Name5 letter tom
Name6 poem ed
Name7 letter tom
...etc
and i wish to do a breakdown of the form:
DocType Total Docs Tom Arnold Ed Rick
letter 54 21 14 14 5
essay 78 18 36 15 9
poem 18 3 6 6 3
have tried a few select queries but have come up short...any ideas?Look up Cross-Tab Reports in Books Online or search here. This has been covered several times and you can find some good examples in each location.|||Originally posted by Paul Young
Look up Cross-Tab Reports in Books Online or search here. This has been covered several times and you can find some good examples in each location.
Cheers Paul, much appreciated
am new to sql and trying to write a query to summarise data in a table.The table contains a list of documents, their type and author
DocName DocType Author
Name1 letter tom
Name2 essay rick
Name3 essay rick
Name4 letter arnold
Name5 letter tom
Name6 poem ed
Name7 letter tom
...etc
and i wish to do a breakdown of the form:
DocType Total Docs Tom Arnold Ed Rick
letter 54 21 14 14 5
essay 78 18 36 15 9
poem 18 3 6 6 3
have tried a few select queries but have come up short...any ideas?Look up Cross-Tab Reports in Books Online or search here. This has been covered several times and you can find some good examples in each location.|||Originally posted by Paul Young
Look up Cross-Tab Reports in Books Online or search here. This has been covered several times and you can find some good examples in each location.
Cheers Paul, much appreciated
Subscribe to:
Posts (Atom)