Skip to content

Tag: sql

SSMS SQL Identify purchases who only buy one specific item

I want to find customers who have only bought fruit in their purchase. My data looks like this: I want this: Thinking I need a where clause, grouped by ID and Purchase_date? Answer You can try to use Having with condition aggregate function to write the logic. Buy fruit (COUNT with fruit count will be greater…

Dynamic SQL – Use declared VARCHAR in SET SQL string

How to use the declared variable @CodeID inside the SQL string? When I run following statement I get the “Invalid object name (..)” error. Answer It looks to me that you need two levels of dynamic SQL, with the first level inserting the database name (from #folders), and the second level inserting…

SQL Query group by for a beginner

I’m quite new to SQL Server and was wondering if it was possible to run a query like this. I have a table with the following fields: USER, WORKSTATION, DURATION (seconds) Each USER can use multiple workstations and each workstation can be used by multiple USERs. In the table I have records like: I would…

How do I clear specific columns in DataGrid?

i am using WPF, connected to SQL DataBase via LinqToSql. I have filled DataGrid using ItemsSource. I would like to clear everything in my DataGrid, except first two rows(ID,first and second name), and Headers ofcourse, by pressing a button. All items i would like to(either set to null or 0) remove, are floats…

Find Multiple Substring from a String Using SQL

I have a column log. I need to extract values from that log column and make 4 new columns My table: Output I need: Answer If the string really always follows exactly that format, you can use something funky like this: Which gives: This abuses the PARSENAME function, which is meant to parse 4 part named object…

SQL Query to merge several views into one

I was trying to create a new view out of 3 or 4 other views/tables. TableA: title_id homeTeam 1234 WSV 5678 SSV 7890 NULL 4321 SCC TableB: title_id awayTeam 1234 SSV 5678 SFV 7890 NULL 4321 KFC TableC: title_id homeTeam 1234 SSV 5678 NULL 7890 AAB 4711 BFG I would like to generate a new view out of those thre…