How to get multiple counts with one SQL query?

You can use a CASE statement with an aggregate function. This is basically the same thing as a PIVOT function in some RDBMS: SELECT distributor_id, count(*) AS total, sum(case when level=”exec” then 1 else 0 end) AS ExecCount, sum(case when level=”personal” then 1 else 0 end) AS PersonalCount FROM yourtable GROUP BY distributor_id

Entity Framework VS LINQ to SQL VS ADO.NET with stored procedures? [closed]

First off, if you’re starting a new project, go with Entity Framework (“EF”) – it now generates much better SQL (more like Linq to SQL does) and is easier to maintain and more powerful than Linq to SQL (“L2S”). As of the release of .NET 4.0, I consider Linq to SQL to be an obsolete … Read more

How do I query for all dates greater than a certain date in SQL Server?

select * from dbo.March2010 A where A.Date >= Convert(datetime, ‘2010-04-01’ ) In your query, 2010-4-01 is treated as a mathematical expression, so in essence it read select * from dbo.March2010 A where A.Date >= 2005; (2010 minus 4 minus 1 is 2005 Converting it to a proper datetime, and using single quotes will fix this … Read more

How to use GROUP BY to concatenate strings in SQL Server?

No CURSOR, WHILE loop, or User-Defined Function needed. Just need to be creative with FOR XML and PATH. [Note: This solution only works on SQL 2005 and later. Original question didn’t specify the version in use.] CREATE TABLE #YourTable ([ID] INT, [Name] CHAR(1), [Value] INT) INSERT INTO #YourTable ([ID],[Name],[Value]) VALUES (1,’A’,4) INSERT INTO #YourTable ([ID],[Name],[Value]) … Read more

updating table rows in postgres using subquery

Postgres allows: UPDATE dummy SET customer=subquery.customer, address=subquery.address, partn=subquery.partn FROM (SELECT address_id, customer, address, partn FROM /* big hairy SQL */ …) AS subquery WHERE dummy.address_id=subquery.address_id; This syntax is not standard SQL, but it is much more convenient for this type of query than standard SQL. I believe Oracle (at least) accepts something similar.

SQL query return data from multiple tables

Part 1 – Joins and Unions This answer covers: Part 1 Joining two or more tables using an inner join (See the wikipedia entry for additional info) How to use a union query Left and Right Outer Joins (this stackOverflow answer is excellent to describe types of joins) Intersect queries (and how to reproduce them … Read more

Efficiently convert rows to columns in sql server

There are several ways that you can transform data from multiple rows into columns. Using PIVOT In SQL Server you can use the PIVOT function to transform the data from rows to columns: select Firstname, Amount, PostalCode, LastName, AccountNumber from ( select value, columnname from yourtable ) d pivot ( max(value) for columnname in (Firstname, … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)