In SQL Server 2008 but column name does not appear. I need to put in an empty string as the rows are populated manually in the report.
(SELECT '' As 'Total No of people')
It seems to show up as (No column name)
You can have
SELECT ID as 'ID',
(SELECT <....> FROM table WHERE <...> ) AS 'Total No of people'
FROM somewhere
You have to put the column name after the ) for the inner select
I will say it works correctly! http://sqlfiddle.com/#!3/d41d8/18149
But perhaps your problem is that you do (technically using a subquery)
SELECT ID, (SELECT '' As 'Total No of people') FROM SomeWhere
and that is wrong...
SELECT ID, '' As 'Total No of people' FROM SomeWhere
or
SELECT ID, (SELECT '') As 'Total No of people' FROM SomeWhere
but there is no reason for the inner SELECT
make sure you put in tight order : SELECT '' As 'Total No of people' from PEOPLE
Related
In Pervasive SQL 11 I could use a IF statement in the ORDER BY:
SELECT *
FROM (
SELECT
D1001 as 'part_number',
'' as 'required_date',
'' as 'confirmed_date'
FROM PULAGER
WHERE
D1001 LIKE '1121%'
UNION
SELECT
D5410 as 'part_number',
D5511 as 'required_date',
D5513 as 'confirmed_date'
FROM PUIKOKRO
WHERE
D5410 LIKE '1121%'
) as t1
ORDER BY part_number, IF (confirmed_date = '', required_date, confirmed_date)
But after an upgrade version 15.10.031, I get the error "Reference to column name not allowed in ORDER BY with UNION". No error if I remove the IF statement. Any suggestions?
First order by part_number and then order by required_date or confirmed_date depending on the state of confirmed_date.
I solved it by moving the IF statement to the SELECT to create a new column 'sort_date' and wrapping it all with another SELECT. Doesn't feel like the most beautiful solution, but it works.
SELECT * FROM (
SELECT t1.*, IF (confirmed_date = '', required_date, confirmed_date) as 'sort_date' FROM t1
) ORDER BY part_number, sort_date
Let's say
SELECT 'Max' AS Foo
results into a result with one field. But there are also ways to give multiple values into your SQL application like
SELECT * FROM Customers WHERE Name IN ('Max','Tim')
question:
Is there also a way to display multiple rows like
SELECT ('Max','Tim') AS Foo
you can try like below
SELECT Stuff(
(SELECT N', ' + Name FROM table_name where Name IN ('Max','Tim')
FOR XML PATH(''),TYPE)
.value('text()[1]','nvarchar(max)'),1,2,N'') as name
You can use union statement
select 'max' as foo
union
select 'tim' as foo
ORA-01790: expression must have same datatype as corresponding expression error message. Below is the sql I'm using. I cant really find whats causing the error please help.
select distinct *
from
( select
h.billingaddress,
h.sold_to,
'' as trx_number,
'' as bs_number,
apd.transaction_number
-- apd.transaction_number
from
amti_so_headers2 h
inner join
amti_print_document apd
on
h.header_id = apd.header_id
where
apd.transaction_number in ('9535','')
and
(
report_type = 'Billing Statement'
or
report_type = 'Sales Invoice'
)
UNION
select
billingaddress,
bill_to sold_to,
amti_trx_header.trx_number,
amti_trx_header.bs_number,
'' transaction_number
from
amti_trx_header
where
trx_number in ('','')
or
bs_number in ('9535','')
);
One of your columns had a different data type between your UNION queries. If I had to guess, I imagine the '' transaction_number is causing the issue, because the name implies it's a number, but you've made it text.
I'm trying to find all records that do not match my criteria.
I have a one-to-many table where for each key combo (wbs1, wbs2, wbs3) there may be many records. I'm trying to query this table for each combo, and return the combo (wbs1, wbs2, wbs3) only when the table has no records that match my "in" criteria.
But at the moment, my query seems to be returning all or nothing.
select distinct wbs1, wbs2, wbs3
from dbo.Registry
where not exists ( select wbs1, wbs2, wbs3, InspType
from Registry
where ISNULL(InspType,'') IN ('Test 1','Test 2')
)
What I'd like in the return is a list of wbs1,wbs2,wbs3 where in the table "Registry" there are no entries that match the Test (InspType) criteria.
Thanks!
It looks like you're pretty close, and the subquery likely isn't even necessary, but this modification should get you the correct results. But as-written, you need a way to refer to the outer query, thus the dbo.Registry r alias.
select distinct wbs1, wbs2, wbs3
from dbo.Registry r
where not exists ( select 1
from Registry
where ISNULL(InspType,'') IN ('Test 1','Test 2')
and (wbs1 = r.wbs1 or wbs2 = r.wbs2 or wbs3 = r.wbs3)
)
This is assuming Microsoft Sql Server of some flavor, so I don't know if it'd work in MySql or Oracle or whatever.
But couldn't you also just do
select distinct wbs1, wbs2, wbs3
from dbo.Registry
where ISNULL(InspType, '') != 'Test 1'
and ISNULL(InspType, '') != 'Test 2'
and avoid the subquery entirely?
This sounds like a "set-within-sets" subquery, which I like solving with aggregation and logic in the having clause. The following will return all combinations that are missing one or both tests:
select wbs1, wbs2, wbs3
from dbo.Registry r
group by wbs1, wbs2, wbs3
having sum(case when InspType = 'Test 1' then 1 else 0 end) = 0 or
sum(case when InspType = 'Test 2' then 1 else 0 end) = 0;
i have a table which has many fields but i want to get count of every word in any three fields of that table
find all title in a table that exist more than once...so for that i can issue this statement
SELECT title, COUNT(title) AS NumOccurrences FROM users
GROUP BY titleHAVING ( COUNT(title) > 1 )
suppose my table has three fields called title,url,description.
basically i do not know which word has been stored in which 3 fields in that table maximum time.
i want to issue a sql statement which can show me which word found maximum time...like
word-name occurance
--------- -------
sqlserver 300
jquery 120
ajax 110
please guide me with sample sql for sql server 2000/2005 thanks
Here is my updated full code.....please have look
IF OBJECT_ID('tempdb..#tempSearch') IS NOT NULL
BEGIN
DROP TABLE #tempSearch
END
CREATE TABLE #tempSearch(
ID INT,
Title nvarchar(4000),
Description ntext,
Url nvarchar(4000),
Type char(1))
INSERT INTO #tempSearch
SELECT * from vwProductSearch
INSERT INTO #tempSearch
SELECT * from vwContentSearch
SELECT Word,
COUNT(Word) AS TotalOccurrences,
COUNT(CASE WHEN Field = 'Title' THEN Word END) AS OccurancesInTitle,
COUNT(CASE WHEN Field = 'URL' THEN Word END) AS OccurancesInURL,
COUNT(CASE WHEN Field = 'Description' THEN Word END) AS OccurancesInDescription
FROM ( SELECT CONVERT(NTEXT, Title) AS Word, 'Title' AS Field
FROM #tempSearch
UNION ALL
SELECT CONVERT(NTEXT, URL), 'URL' AS Field
FROM #tempSearch
UNION ALL
SELECT CONVERT(NTEXT, Description), 'Description' AS Field
FROM #tempSearch
) As Fields
GROUP BY Word
HAVING COUNT(Word) > 1
DROP TABLE #tempSearch
You need to use UNION to combine your 3 fields into a single column so you can use this to group by. I've also added a few more counts in case you need to drill down as to where the word occurs the most.
SELECT Word,
COUNT(Word) AS TotalOccurrences,
COUNT(CASE WHEN Field = 'Title' THEN Word END) AS OccurancesInTitle,
COUNT(CASE WHEN Field = 'URL' THEN Word END) AS OccurancesInURL,
COUNT(CASE WHEN Field = 'Description' THEN Word END) AS OccurancesInDescription
FROM ( SELECT Title AS Word, 'Title' AS Field
FROM Users
UNION ALL
SELECT URL, 'URL' AS Field
FROM Users
UNION ALL
SELECT Description, 'Description' AS Field
FROM Users
) As Fields
GROUP BY Word
HAVING COUNT(Word) > 1
EDIT
I know you have asked about SQL_Server 2005 and 2000, but if you were ever to upgrade to 2008 or later there is a much cleaner solution:
SELECT Word,
COUNT(Word) AS TotalOccurrences,
COUNT(CASE WHEN Field = 'Title' THEN Word END) AS OccurancesInTitle,
COUNT(CASE WHEN Field = 'URL' THEN Word END) AS OccurancesInURL,
COUNT(CASE WHEN Field = 'Description' THEN Word END) AS OccurancesInDescription
FROM Users
CROSS APPLY
( VALUES
(Title, 'Title'),
(URL, 'URL'),
(Description, 'Description')
) AS T (Word, Field)
GROUP BY Word
HAVING COUNT(Word) > 1
EDIT 2
If all your columns are different datatypes you will need to explicitly convert them:
SELECT Word,
COUNT(Word) AS TotalOccurrences,
COUNT(CASE WHEN Field = 'Title' THEN Word END) AS OccurancesInTitle,
COUNT(CASE WHEN Field = 'URL' THEN Word END) AS OccurancesInURL,
COUNT(CASE WHEN Field = 'Description' THEN Word END) AS OccurancesInDescription
FROM ( SELECT CONVERT(NTEXT, Title) AS Word, 'Title' AS Field
FROM Users
UNION ALL
SELECT CONVERT(NTEXT, URL), 'URL' AS Field
FROM Users
UNION ALL
SELECT CONVERT(NTEXT, Description), 'Description' AS Field
FROM Users
) As Fields
GROUP BY Word
HAVING COUNT(Word) > 1
EDIT 3
There is no way around the error you are getting, you cannot group by NTEXT. The best solution I can come up with feels very dirty, and I'm not particularly happy with it...
SELECT COALESCE(Title, URL, Description) AS Word,
COALESCE(Title.Occurances, 0) + COALESCE(URL.Occurances, 0) + COALESCE(Description.Occurances, 0) AS TotalOccurances,
COALESCE(Title.Occurances, 0) AS TitleOccurances,
COALESCE(URL.Occurances, 0) AS URLOccurances,
COALESCE(Description.Occurances, 0) AS DescriptionOccurances
FROM ( SELECT CONVERT(NTEXT, Title) AS Title, COUNT(*) AS Occurances
FROM #tempSearch
GROUP BY Title
) AS Title
FULL JOIN
( SELECT CONVERT(NTEXT, URL) AS URL, COUNT(*) AS Occurances
FROM #tempSearch
GROUP BY URL
) AS URL
ON URL LIKE Title
FULL JOIN
( SELECT Description, 1 AS Occurances
FROM #tempSearch
) AS Description
ON Description LIKE Title
This works, but like I said, it isn't perfect and probably won't perform very well. Strongly consider upgrading to a later version of SQL-Server!
You can count the number of occurrences a word has across three fields using the difference in LEN() to account for the word appearing multiple times in the same field:
SELECT
a.wordname,
SUM(
((LEN(b.title) - LEN(REPLACE(b.title, a.wordname, ''))) / LEN(a.wordname)) +
((LEN(b.url) - LEN(REPLACE(b.url, a.wordname, ''))) / LEN(a.wordname)) +
((LEN(b.description) - LEN(REPLACE(b.description, a.wordname, ''))) / LEN(a.wordname))
) AS occurrence
FROM
(
SELECT 'sqlserver' AS wordname UNION ALL
SELECT 'jquery' AS wordname UNION ALL
SELECT 'ajax' AS wordname
) a
CROSS JOIN
users b
GROUP BY
a.wordname
ORDER BY
occurrence DESC
If you have a table of wordnames, just put that table name in place of the many SELECT ... UNION ALL statements I have put for the sake of example.