Field NULL in SQL

I have a table in DB: REGULAR(num(bigint) not allow null, title(nvarchar(10)), content(nvarchar(500)), path(nvarchar(50)) allow null) with some data: 1.REGULAR1(1|title1|content1|path1) 2.REGULAR2(2|title2|content2|)–> path is not inputed (null) I excute some query in SQL DB server 2008: 1. select PATH from REGULAR where num=’2′; -> result is PATH:NULL But 2. select count(*) from REGULAR where PATH = NULL; […]

Select only two rows with condition

I have a table which is something like this: id title 11 test 22 test 33 dummy 37 dummy 35 dummy 44 abc 45 abc after select I need only like this one: id title 11 test 22 test 44 abc 45 abc I wrote like this: select * from content having count(*)=2 group by […]

Cannot use WITH statement two times

I wan’t to create a view that is constructed like this: (simplified) Create VIEW viewAll AS With TempLevel1 AS ( SELECT statement ) With TempLevel2 AS (SELECT * from TempLevel1) SELECT * from TempLevel2 The problem is that I cannot use With statement like this because of the following error: Incorrect syntax near the keyword […]

Insert into a table a part of another table

I have two tables, the structure of the first partially recapitulates, iterates the structure of the second: table1 (id, i, j, k, a, b, c, x, y, z) — requests table2 (id, a, b, c, d) — essential elements / bank subjects I need to insert into table1 a record from table2 with given ID. […]

Sql AVG included but getting Column is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause

I am very confused as to why I am getting this error: “Column ‘City.CityID’ is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.” I have two tables: one called City with city info, such as id, name, etc and another table containing the […]

SQL Server loosely coupled tables – performance considerations?

I am building a database schema for a new application from scratch, and my two goals are loose coupling (scalability) and performance (but performance is the MOST important). I am not sure if it would be a good idea to include foreign key columns in central tables. My question would probably be best understood using […]

What is a clean way of passing a list of ints between stored procedures?

I have a stored procedure which results in a temporary table populated with INTs. I also have another stored procedure which I want to take in a list of INTs to perform processing on them. As far as I know there isn’t a clean way to pass a list of values between stored procedures, so […]

SQL Group by Data conditional filtering

I have a table data as follows: Customer Level Dept AAA 1 Parent AAA 2 Corporate AAA 3 SmallBusiness BBB 1 Parent BBB 2 StateLevel BBB 3 Education BBB 4 RegionLevel CCC 1 Parent CCC 2 Sales CCC 3 Healthcare CCC 4 Online CCC 5 Web Conditions: If Level=2 and Dept=Corporate User Department is Corporate […]

Grouping by year and month on date stored as a decimal

I have a data set with the following sample information: ID DTE CNTR 1 20110102.0 2 1 20110204.0 3 1 20110103.0 5 2 20110205.0 6 2 20110301.0 7 2 20110302.0 3 If I want to group the information by month and sum the counter, the code I’m guessing would be this: SELECT t.ID, ,SUM CASE(WHEN […]

SQL – Incorrect syntax near “;”

I have an written a recursive SQL query which returns some int values. The SQL query looks like below: ;WITH GroupHIERARCHY(ID) AS ( SELECT ID FROM tFirstTable te WHERE te.LevelID <> 0 AND GroupID =-1 UNION ALL SELECT t.ElementID FROM tFirstTable AS t, tSecondTable,GroupHIERARCHY WHERE t.TypeID=tSecondTable.TypeID AND GroupHIERARCHY.ID= t.GroupID) SELECT ID FROM GroupHIERARCHY This would […]

MS SQL Server is a Microsoft SQL Database product, include sql server standard, sql server management studio, sql server express and so on.